pageable

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 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)
    // handle error
    if err != nil {
        panic(err)
    }
    //Here are the response
	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
}

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)
}

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
}

And here a clear JSON object of the Response LIKE Spring Pageable

{
	"PageNow": 2,
	"PageCount": 1,
	"RawCount": 1,
	"RawPerPage": 25,
	"ResultSet": [],
	"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
}

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

Jump to

Keyboard shortcuts

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