Documentation ¶
Overview ¶
Package pageable is for page query based on GORM package
As a quick start:
func getResultSet (page int,rowsPerPage int)(*pageable.Response,error){ //your empty result set resultSet := make([]*User,0,30) //prepare a handler to query handler := DB. Module(&User{}). Where(&User{Active:true}) //use PageQuery to get data resp,err := pageable.PageQuery(page,rowsPerPage,handler,&resultSet) // handle error f err != nil { panic(err) } // goto Next Page resp,err := resp.GetNextPage() //new *Response if next page }
And then you can print this value to see the page info
fmt.Println(resp.PageNow) //PageNow: current page of query fmt.Println(resp.PageCount) //PageCount: total page of the query fmt.Println(resp.RawCount) //RawCount: total raw of query fmt.Println(resp.RawPerPage) //RawPerPage: rpp fmt.Println(resp.ResultSet) //ResultSet: result data fmt.Println(resp.FirstPage) //FirstPage: if the result is the first page fmt.Println(resp.LastPage) //LastPage: if the result is the last page fmt.Println(resp.Empty) //Empty: if the result is empty fmt.Println(resp.StartRow) //Empty: the first row of the result set, 0 when result set is empty fmt.Println(resp.EndRow) //Empty: the last row of the result set, 0 when result set is empty
And here a clear JSON object of the Response LIKE Spring Pageable
{ "PageNow": 2, "PageCount": 1, "RawCount": 1, "RawPerPage": 25, "ResultSet": [{...your data struct}], "FirstPage": false, "LastPage": false, "Empty": true }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetRecovery ¶
func SetRecovery(handler func())
SetRecovery Set custom recovery
Here are some sample of the custom recovery
package main import ( "fmt" pageable "github.com/BillSJC/gorm-pageable" ) //your recovery func myRecovery(){ if err := recover ; err != nil { fmt.Println("something happened") fmt.Println(err) //then you can do some logs... } } func init(){ //setup your recovery pageable.SetRecovery(myRecovery) }
func Use0AsFirstPage ¶
func Use0AsFirstPage()
Use0AsFirstPage : the default first page is 1. However,if u want to use 0 as the first page, just follow this step:
pageable.Use0AsFirstPage()
Types ¶
type Response ¶
type Response struct { PageNow int //PageNow: current page of query PageCount int //PageCount: total page of the query RawCount int //RawCount: total raw of query RawPerPage int //RawPerPage: rpp ResultSet interface{} //ResultSet: result data FirstPage bool //FirstPage: if the result is the first page LastPage bool //LastPage: if the result is the last page Empty bool //Empty: if the result is empty StartRow int //The number of first record the the resultSet EndRow int //The number of last record the the resultSet // contains filtered or unexported fields }
Response: Base response of query
func PageQuery ¶
func PageQuery(page int, rawPerPage int, queryHandler *gorm.DB, resultPtr interface{}) (*Response, error)
PageQuery: main handler of query
page: 1 for the first page
resultPtr : MUST input a Slice or it will be a error
queryHandler : MUST have DB.Module or it will be a error
Remember: all element of Response should be READ ONLY! once it changed, the logic of the query may broke
func (*Response) GetEndPage ¶ added in v0.0.2
GetNextPage: return end page`s Response
func (*Response) GetFirstPage ¶ added in v0.0.2
GetFirstPage: return first page`s Response
func (*Response) GetLastPage ¶ added in v0.0.2
GetNextPage: return last page`s Response
func (*Response) GetNextPage ¶ added in v0.0.2
GetNextPage: return next page`s Response
func getResultSet (page int,rowsPerPage int)(*pageable.Response,error){ //your empty result set resultSet := make([]*User,0,30) //prepare a handler to query handler := DB. Module(&User{}). Where(&User{Active:true}) //use PageQuery to get data (this page) resp,err := pageable.PageQuery(page,rowsPerPage,handler,&resultSet) // handle error f err != nil { panic(err) } //get next page resp,err := resp.GetNextPage() //Response of next page }
func (*Response) SetHandler ¶ added in v0.0.2
SetHandler: once you want to change the query handler, you can do this to replace it
resp.SetHandler(DB.Model(&User{}).Where(&User{UserName:"john"})) //set the handler