pageable

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2020 License: MIT Imports: 5 Imported by: 0

README

Gorm-Pageable

Go Report Card Build Status Go GoDoc codecov

A page query management of GORM

->中文文档

Installation

We Recommend to use package manager vgo to manage this package

import pageable "github.com/BillSJC/gorm-pageable"

Usage

Just prepare a struct and FOLLOW the code below

package main

import (
    "fmt"
    pageable "github.com/BillSJC/gorm-pageable"
    "github.com/jinzhu/gorm"
)

var DB *gorm.DB //your gorm DB connection

// the struct you want to search
type User struct{
    gorm.Model
    Active bool
    UserName string
    Age uint
}

// a function to get ddata
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)
    // if need, you can turn to next/last page
    resp,err = resp.GetNextPage() //to next page
    resp,err = resp.GetLastPage() //to last page
    resp,err = resp.GetFirstPage() //to first page
    resp,err = resp.GetEndPage() //to end page
    // you can use both resp.ResultSet or the resultSet you input to access the result
    // handle error
    if err != nil {
        //print the err or do sth else
        fmt.Println("something happened...")
        fmt.Println(err)
        return nil,err
    }
    // Here are the response
    // NOTICE:  all element of Response should be READ ONLY! once it changed, the logic of the query may broke
	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(resultSet)          //the same as resp.ResultSet and have the raw type
	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)  //StartRow: the first row of the result set, 0 when result set is empty
	fmt.Println(resp.EndRow)  //EndRow: the last row of the result set, 0 when result set is empty
}

Notice: when access to the result, you can use both resp.ResultSet in Response or the param resultSet you input into the function, both of then have same pointer and same data, but the type of resp.ResultSet is interface{} and you mat need to convert to the raw type if you need to do any operation of the result set

Guidance

Use 0 as the first page

the default first page is 1. However,if u want to use 0 as the first page, just follow this step:

    pageable.Use0AsFirstPage()
Set Default Result Per Page(rpp)

Sometimes you just want to use same rpp in every query, then u just need do this:

    pageable.SetDefaultRPP(25) //set 25 rows per page in every query

And next time, you can use rpp=0 to use Default rpp

    pageable.PageQuery(page:1, rpp:0, queryHandler: ..., resultPtr: ...)
Use custom recovery

The default recovery will only print stack trace. If you want to use your custom Recovery handler, just follow the step:

package main
import (
    "fmt"
    pageable "github.com/BillSJC/gorm-pageable"
)

//your recovery
func myRecovery(){
    if err := recover ; err != nil {
        fmt.Println("something happend")
        fmt.Println(err)
        //then you can do some logs...
    } 
}

func init(){
    //setup your recovery
    pageable.SetRecovery(myRecovery)
}

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 SetDefaultRPP

func SetDefaultRPP(rpp int) error

SetDefaultRPP Set default rpp

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

func (r *Response) GetEndPage() (*Response, error)

GetNextPage: return end page`s Response

func (*Response) GetFirstPage added in v0.0.2

func (r *Response) GetFirstPage() (*Response, error)

GetFirstPage: return first page`s Response

func (*Response) GetLastPage added in v0.0.2

func (r *Response) GetLastPage() (*Response, error)

GetNextPage: return last page`s Response

func (*Response) GetNextPage added in v0.0.2

func (r *Response) GetNextPage() (*Response, error)

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

func (r *Response) SetHandler(handler *gorm.DB)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL