PageQuery对象

定义

PageQuery实现了Query接口,定义了三个字段, 其中,PageNumberPageSize用于构建分页子句,Sort 用于构建排序子句。

package core

type PageQuery struct {
	PageNumber *int    `json:"page,omitempty"`
	PageSize   *int    `json:"size,omitempty"`
	Sort       *string `json:"sort,omitempty"`
}

分页示例

userQuery := UserQuery{PageQuery: PageQuery{}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User

userQuery := UserQuery{PageQuery: PageQuery{PageSize: P(20)}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User LIMIT 20 OFFSET 0

// When only PageNumber is set, PageSize will be set to 10
userQuery := UserQuery{PageQuery: PageQuery{PageNumber: P(5)}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User LIMIT 10 OFFSET 40

userQuery := UserQuery{PageQuery: PageQuery{PageSize: P(50), PageNumber: P(3)}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User LIMIT 10 OFFSET 100	

排序示例

Sort赋值的字符串需要符合正则表达式:regexp.MustCompile("(?i)(\w+)(,(asC|dEsc))?;?")

userQuery := UserQuery{PageQuery: PageQuery{Sort: P("id,desc;score,asc;memo")}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User ORDER BY id DESC, score ASC, memo

最后更新于