pagehelper

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

README

pagehelper

license card go version go report codecov report workflow lasted release

介绍

pagehelper是与gobatis配套的分页工具

待完成项

v0.1.0已添加此特性 :返回总记录数

select查询返回的result还未能携带page、pageSize、total信息,需要自己组装。

使用

1、使用pagehelper的Factory生成SessionManager
    pFac := pagehelper.New(gobatis.NewFactory(
                           		gobatis.SetMaxConn(100),
                           		gobatis.SetMaxIdleConn(50),
                           		gobatis.SetDataSource(&datasource.MysqlDataSource{
                           			Host:     "localhost",
                           			Port:     3306,
                           			DBName:   "test",
                           			Username: "root",
                           			Password: "123",
                           			Charset:  "utf8",
                           		})))
    sessMgr := gobatis.NewSessionManager(pFac)
2、配置分页参数
    session := sessMgr.NewSession()
    ctx := pagehelper.StartPage(context.Background(), 1, 10)

    var ret []TestTable
    session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)
3、配置分页参数(带自动统计总记录数功能)
    session := sessMgr.NewSession()
    ctx := pagehelper.StartPageWithCount(context.Background(), 1, 10, "")

    var ret []TestTable
    session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)

获得分页信息(以1001条记录为例)

    pageInfo := pagehelper.GetPageInfo(ctx)
    t.Log(
        "pageNum: ", pageInfo.GetPageNum(),
        "totalPage: ", pageInfo.GetTotalPage(),
        "pageSize: ", pageInfo.GetPageSize(),
        "total: ", pageInfo.GetTotal())

输出:

pageNum:        1 
totalPage:      101 
pageSize:       10 
total:          1001

注意:

会自动生成和执行带子查询的countSQL,请自行评估是否使用此功能,转而使用自定义SQL获取总记录数。

4、配置排序参数
    session := sessMgr.NewSession()
    ctx := pagehelper.OrderBy(context.Background(), "myfield", pagehelper.DESC)

    var ret []TestTable
    session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)

注意:

由于golang对order by不能使用placeholder的方式,所以存在注入风险,请谨慎使用排序功能,如果使用,则需要自己做防注入的工作。

举例:

在获得order by参数时做参数校验

valid := regexp.MustCompile("^[A-Za-z0-9_]+$")
if !valid.MatchString(ordCol) {
    // invalid column name, do not proceed in order to prevent SQL injection
}
5、使用builder
session := sessMgr.NewSession()
ctx := pagehelper.C(context.Background()).Page(1, 3).OrderBy("test", pagehelper.ASC).Build()
var ret []TestTable
session.SetContext(ctx).Select("SELECT * FROM TBL_TEST").Param().Result(&ret)

Documentation

Index

Constants

View Source
const (
	DriverDummy     = "default"
	DriverMysql     = "mysql"
	DriverPostgre   = "postgre"
	DriverOracle    = "oracle"
	DriverSqlServer = "sqlserver"
)
View Source
const (
	ASC  = "ASC"
	DESC = "DESC"
)

Variables

View Source
var DummyModifier = Modifier{
	OrderBy: DummyModifyOrderBy,
	Page:    DummyModifyPage,
	Count:   DummyModifyCount,
}
View Source
var MysqlModifier = Modifier{
	OrderBy: MysqlModifyOrderBy,
	Page:    MysqlModifyPage,
	Count:   MysqlModifyCount,
}
View Source
var OracleModifier = Modifier{
	OrderBy: OracleModifyOrderBy,
	Page:    OracleModifyPage,
	Count:   OracleModifyCount,
}
View Source
var PostgreModifier = Modifier{
	OrderBy: PostgreModifyOrderBy,
	Page:    PostgreModifyPage,
	Count:   PostgreModifyCount,
}
View Source
var SqlServerModifier = Modifier{
	OrderBy: SqlServerModifyOrderBy,
	Page:    SqlServerModifyPage,
	Count:   SqlServerModifyCount,
}

Functions

func C

func C(ctx context.Context) *builder

创建builder ctx 初始context

func DummyModifyCount

func DummyModifyCount(sql, countColumn string) string

func DummyModifyOrderBy

func DummyModifyOrderBy(sql string, p *OrderByInfo) string

func DummyModifyPage

func DummyModifyPage(sql string, p *PageInfo) string

func GetTotal

func GetTotal(ctx context.Context) int64

func MysqlModifyCount

func MysqlModifyCount(sql, countColumn string) string

func MysqlModifyOrderBy

func MysqlModifyOrderBy(sql string, p *OrderByInfo) string

func MysqlModifyPage

func MysqlModifyPage(sql string, p *PageInfo) string

func OracleModifyCount

func OracleModifyCount(sql, countColumn string) string

func OracleModifyOrderBy

func OracleModifyOrderBy(sql string, p *OrderByInfo) string

func OracleModifyPage

func OracleModifyPage(sql string, p *PageInfo) string

func OrderBy

func OrderBy(ctx context.Context, field, order string) context.Context

排序 field 字段 order 排序 [ASC | DESC] ctx 初始context

func PostgreModifyCount

func PostgreModifyCount(sql, countColumn string) string

func PostgreModifyOrderBy

func PostgreModifyOrderBy(sql string, p *OrderByInfo) string

func PostgreModifyPage

func PostgreModifyPage(sql string, p *PageInfo) string

func RegisterModifier

func RegisterModifier(driver string, m Modifier)

func SqlServerModifyCount

func SqlServerModifyCount(sql, countColumn string) string

func SqlServerModifyOrderBy

func SqlServerModifyOrderBy(sql string, p *OrderByInfo) string

func SqlServerModifyPage

func SqlServerModifyPage(sql string, p *PageInfo) string

func StartPage

func StartPage(ctx context.Context, page, pageSize int64) context.Context

分页 page 页码 pageSize 分页大小 ctx 初始context

func StartPageWithCount

func StartPageWithCount(ctx context.Context, page, pageSize int64, countColumn string) context.Context

分页(包含total信息) page 页码 pageSize 分页大小 用于统计的列名称,如果为空,则默认使用COUNT(0)统计 ctx 初始context

Types

type Executor

type Executor struct {
	// contains filtered or unexported fields
}

func (*Executor) Begin

func (exec *Executor) Begin() error

func (*Executor) Close

func (exec *Executor) Close(rollback bool)

func (*Executor) Commit

func (exec *Executor) Commit(require bool) error

func (*Executor) Exec

func (exec *Executor) Exec(ctx context.Context, sql string, params ...interface{}) (common.Result, error)

func (*Executor) Query

func (exec *Executor) Query(ctx context.Context, result reflection.Object, sql string, params ...interface{}) error

func (*Executor) Rollback

func (exec *Executor) Rollback(require bool) error

type Factory

type Factory struct {
	// contains filtered or unexported fields
}

func New

func New(f factory.Factory) *Factory

func (*Factory) Close

func (f *Factory) Close() error

func (*Factory) CreateExecutor

func (f *Factory) CreateExecutor(transaction transaction.Transaction) executor.Executor

func (*Factory) CreateSession

func (f *Factory) CreateSession() session.SqlSession

func (*Factory) CreateTransaction

func (f *Factory) CreateTransaction() transaction.Transaction

func (*Factory) GetDataSource

func (f *Factory) GetDataSource() datasource.DataSource

func (*Factory) LogFunc

func (f *Factory) LogFunc() logging.LogFunc

func (*Factory) Open

func (f *Factory) Open(source datasource.DataSource) error

type IFactory

type IFactory struct {
	InitDBFunc            func() error
	CreateTransactionFunc func() transaction.Transaction
	CreateExecutorFunc    func(transaction.Transaction) executor.Executor
	CreateSessionFunc     func() session.SqlSession
	LogFuncFunc           func() logging.LogFunc
}

func (*IFactory) CreateExecutor

func (f *IFactory) CreateExecutor(transaction transaction.Transaction) executor.Executor

func (*IFactory) CreateSession

func (f *IFactory) CreateSession() session.SqlSession

func (*IFactory) CreateTransaction

func (f *IFactory) CreateTransaction() transaction.Transaction

func (*IFactory) InitDB

func (f *IFactory) InitDB() error

func (*IFactory) LogFunc

func (f *IFactory) LogFunc() logging.LogFunc

type Modifier

type Modifier struct {
	OrderBy func(sql string, p *OrderByInfo) string
	Page    func(sql string, p *PageInfo) string
	Count   func(sql, countColumn string) string
}

func SelectModifier

func SelectModifier(driver string) Modifier

type OrderByInfo

type OrderByInfo struct {
	Field string
	Order string
}

type PageInfo

type PageInfo struct {
	Page     int64
	PageSize int64
	// contains filtered or unexported fields
}

func GetPageInfo

func GetPageInfo(ctx context.Context) *PageInfo

func (*PageInfo) GetPageNum

func (p *PageInfo) GetPageNum() int64

获得当前页码

func (*PageInfo) GetPageSize

func (p *PageInfo) GetPageSize() int64

获得每页的记录数

func (*PageInfo) GetTotal

func (p *PageInfo) GetTotal() int64

获得总记录数

func (*PageInfo) GetTotalPage

func (p *PageInfo) GetTotalPage() int64

获得总页码

func (*PageInfo) SetTotal

func (p *PageInfo) SetTotal(total int64) *PageInfo

设置总记录数,用户手动查询总记录数时使用

func (*PageInfo) String

func (p *PageInfo) String() string

Jump to

Keyboard shortcuts

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