paging

package
v0.0.0-...-d31700d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 4 Imported by: 0

README

paging

paging util for golang.

Usage

package paging_test

import (
	"fmt"

	"gitee.com/go-better/dev/db/pg"
	"gitee.com/go-better/dev/db/paging"
	"gitee.com/go-better/dev/debug/errs"
)

var db *bsql.DB

type Result struct {
	Rows []Row `json:"rows"`
	*paging.Paging
}

type Row struct {
	Id   int64
	Name string
}

func List(page, size string) (*Result, error) {
	result := Result{Paging: paging.New(page, size)}

	var mainSql = fmt.Sprintf("FROM users WHERE type = %d", 1)
	if err := db.Query(&result.Rows, fmt.Sprintf(
		"SELECT id, name %s %s", mainSql, result.Paging.SQL(),
	)); err != nil {
		return nil, errs.Trace(err)
	}
	if err := result.SetupTotalSize(len(result.Rows), db, "SELECT count(*) "+mainSql); err != nil {
		return nil, errs.Trace(err)
	}

	return &result, nil
}

func List2(page, size string) (*Result, error) {
	result := Result{
		Paging: paging.New(page, size, paging.Option{DefaultPageSize: 10, MaxPageSize: 100}),
	}

	var mainSql = fmt.Sprintf("FROM users WHERE type = %d", 1)
	if err := db.Query(&result.Rows, fmt.Sprintf(
		"SELECT id, name %s %s", mainSql, result.Paging.SQL(),
	)); err != nil {
		return nil, errs.Trace(err)
	}

	if result.CurrentPage == 1 && len(result.Rows) < int(result.PageSize) {
		result.SetTotalSize(len(result.Rows))
	} else {
		if err := db.Query(result, "SELECT count(*) "+mainSql); err != nil {
			return nil, errs.Trace(err)
		}
	}

	return &result, nil
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultPageSize, MaxPageSize int64 = 10, 1000

Functions

This section is empty.

Types

type Option

type Option struct {
	DefaultPageSize int64
	MaxPageSize     int64
}

type Paging

type Paging struct {
	TotalSize   int64 `json:"totalSize"`
	TotalPage   int64 `json:"totalPage"`
	CurrentPage int64 `json:"-"`
	PageSize    int64 `json:"-"`
}

func New

func New(page, size string, options ...Option) *Paging

New returns a Paging from page, size in string type

Example
paging := New("3", "20")
fmt.Println(paging.SQL())
// we implemented sql.Scanner
paging.Scan(int64(82))
fmt.Printf("%+v\n", paging)
Output:

LIMIT 20 OFFSET 40
&{TotalSize:82 TotalPage:5 CurrentPage:3 PageSize:20}

func NewFromInt64

func NewFromInt64(page, size int64, options ...Option) *Paging

func NewFromQuery

func NewFromQuery(query url.Values, options ...Option) *Paging

NewFromQuery returns a paging from url.Values

Example
fmt.Printf("%+v\n", NewFromQuery(nil))
Output:

&{TotalSize:0 TotalPage:0 CurrentPage:1 PageSize:10}

func (*Paging) CalcTotalPage

func (p *Paging) CalcTotalPage()

func (*Paging) Offset

func (p *Paging) Offset() int64

func (*Paging) SQL

func (p *Paging) SQL() string

func (*Paging) Scan

func (p *Paging) Scan(src interface{}) error

Scan implemented sql.Scanner, so just use rows.Scan(paging).

Example
p := NewFromInt64(1, 10)
fmt.Println(p.Scan(10))
Output:

paging: cannot assign int(10) to int64

func (*Paging) SetTotalSize

func (p *Paging) SetTotalSize(totalSize int)

func (*Paging) SetupTotalSize

func (p *Paging) SetupTotalSize(
	firstPageSize int, querier Querier, sql string, args ...interface{},
) error
Example
p := NewFromInt64(1, 100)
fmt.Println(p.SetupTotalSize(50, nil, "SELECT count(*)"))
fmt.Printf("%+v\n", p)

p = NewFromInt64(2, 100)
fmt.Println(p.SetupTotalSize(50, testQuerier{}, "SELECT count(*)"))
fmt.Printf("%+v\n", p)
Output:

<nil>
&{TotalSize:50 TotalPage:1 CurrentPage:1 PageSize:100}
<nil>
&{TotalSize:150 TotalPage:2 CurrentPage:2 PageSize:100}

func (*Paging) SetupTotalSizeFunc

func (p *Paging) SetupTotalSizeFunc(
	firstPageSize int, querier Querier, sqlFunc func() (string, error), args ...interface{},
) error
Example
p := NewFromInt64(1, 100)
fmt.Println(p.SetupTotalSizeFunc(50, nil, func() (string, error) {
	return "", nil
}))
fmt.Printf("%+v\n", p)

p = NewFromInt64(2, 100)
fmt.Println(p.SetupTotalSizeFunc(50, testQuerier{}, func() (string, error) {
	return "", nil
}))
fmt.Printf("%+v\n", p)
Output:

<nil>
&{TotalSize:50 TotalPage:1 CurrentPage:1 PageSize:100}
<nil>
&{TotalSize:150 TotalPage:2 CurrentPage:2 PageSize:100}

type Querier

type Querier interface {
	Query(data interface{}, sql string, args ...interface{}) error
}

Jump to

Keyboard shortcuts

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