Skip to content

Commit 4f1a78b

Browse files
authored
feat: add selectPage ignore total #37 (#45)
1 parent 563112f commit 4f1a78b

File tree

2 files changed

+73
-36
lines changed

2 files changed

+73
-36
lines changed

gplus/dao.go

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,17 @@ func SelectListMaps[T any](q *Query[T], opts ...OptionFunc) ([]map[string]any, *
197197

198198
// SelectPage 根据条件分页查询记录
199199
func SelectPage[T any](page *Page[T], q *Query[T], opts ...OptionFunc) (*Page[T], *gorm.DB) {
200-
total, countDb := SelectCount[T](q, opts...)
201-
if countDb.Error != nil {
202-
return page, countDb
200+
option := getOption(opts)
201+
202+
// 如果需要分页忽略总数,不查询总数
203+
if !option.IgnoreTotal {
204+
total, countDb := SelectCount[T](q, opts...)
205+
if countDb.Error != nil {
206+
return page, countDb
207+
}
208+
page.Total = total
203209
}
204-
page.Total = total
210+
205211
resultDb := buildCondition(q, opts...)
206212
var results []*T
207213
resultDb.Scopes(paginate(page)).Find(&results)
@@ -213,11 +219,15 @@ func SelectPage[T any](page *Page[T], q *Query[T], opts ...OptionFunc) (*Page[T]
213219
// 第一个泛型代表数据库表实体
214220
// 第二个泛型代表返回记录实体
215221
func SelectPageModel[T any, R any](page *Page[R], q *Query[T], opts ...OptionFunc) (*Page[R], *gorm.DB) {
216-
total, countDb := SelectCount[T](q, opts...)
217-
if countDb.Error != nil {
218-
return page, countDb
222+
option := getOption(opts)
223+
// 如果需要分页忽略总数,不查询总数
224+
if !option.IgnoreTotal {
225+
total, countDb := SelectCount[T](q, opts...)
226+
if countDb.Error != nil {
227+
return page, countDb
228+
}
229+
page.Total = total
219230
}
220-
page.Total = total
221231
resultDb := buildCondition(q, opts...)
222232
var results []*R
223233
resultDb.Scopes(paginate(page)).Scan(&results)
@@ -227,11 +237,15 @@ func SelectPageModel[T any, R any](page *Page[R], q *Query[T], opts ...OptionFun
227237

228238
// SelectPageMaps 根据条件分页查询,返回分页Map记录
229239
func SelectPageMaps[T any](page *Page[map[string]any], q *Query[T], opts ...OptionFunc) (*Page[map[string]any], *gorm.DB) {
230-
total, countDb := SelectCount[T](q, opts...)
231-
if countDb.Error != nil {
232-
return page, countDb
240+
option := getOption(opts)
241+
// 如果需要分页忽略总数,不查询总数
242+
if !option.IgnoreTotal {
243+
total, countDb := SelectCount[T](q, opts...)
244+
if countDb.Error != nil {
245+
return page, countDb
246+
}
247+
page.Total = total
233248
}
234-
page.Total = total
235249
resultDb := buildCondition(q, opts...)
236250
var results []map[string]any
237251
resultDb.Scopes(paginate(page)).Find(&results)
@@ -249,6 +263,11 @@ func SelectCount[T any](q *Query[T], opts ...OptionFunc) (int64, *gorm.DB) {
249263
return count, resultDb
250264
}
251265

266+
func Begin(opts ...*sql.TxOptions) *gorm.DB {
267+
db := getDb()
268+
return db.Begin(opts...)
269+
}
270+
252271
func paginate[T any](p *Page[T]) func(db *gorm.DB) *gorm.DB {
253272
page := p.Current
254273
pageSize := p.Size
@@ -341,50 +360,49 @@ func getPkColumnName[T any]() string {
341360
}
342361

343362
func getDb(opts ...OptionFunc) *gorm.DB {
344-
var config Option
345-
for _, op := range opts {
346-
op(&config)
347-
}
348-
363+
option := getOption(opts)
349364
// Clauses()目的是为了初始化Db,如果db已经被初始化了,会直接返回db
350365
var db = globalDb.Clauses()
351366

352-
if config.Db != nil {
353-
db = config.Db.Clauses()
367+
if option.Db != nil {
368+
db = option.Db.Clauses()
354369
}
355370

356371
// 设置需要忽略的字段
357-
setOmitIfNeed(config, db)
372+
setOmitIfNeed(option, db)
358373

359374
// 设置选择的字段
360-
setSelectIfNeed(config, db)
375+
setSelectIfNeed(option, db)
361376

362377
return db
363378
}
364379

365-
func setSelectIfNeed(config Option, db *gorm.DB) {
366-
if len(config.Selects) > 0 {
380+
func setSelectIfNeed(option Option, db *gorm.DB) {
381+
if len(option.Selects) > 0 {
367382
var columnNames []string
368-
for _, column := range config.Selects {
383+
for _, column := range option.Selects {
369384
columnName := getColumnName(column)
370385
columnNames = append(columnNames, columnName)
371386
}
372387
db.Select(columnNames)
373388
}
374389
}
375390

376-
func setOmitIfNeed(config Option, db *gorm.DB) {
377-
if len(config.Omits) > 0 {
391+
func setOmitIfNeed(option Option, db *gorm.DB) {
392+
if len(option.Omits) > 0 {
378393
var columnNames []string
379-
for _, column := range config.Omits {
394+
for _, column := range option.Omits {
380395
columnName := getColumnName(column)
381396
columnNames = append(columnNames, columnName)
382397
}
383398
db.Omit(columnNames...)
384399
}
385400
}
386401

387-
func Begin(opts ...*sql.TxOptions) *gorm.DB {
388-
db := getDb()
389-
return db.Begin(opts...)
402+
func getOption(opts []OptionFunc) Option {
403+
var config Option
404+
for _, op := range opts {
405+
op(&config)
406+
}
407+
return config
390408
}

gplus/option.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,46 @@ package gplus
2020
import "gorm.io/gorm"
2121

2222
type Option struct {
23-
Omits []any
24-
Selects []any
2523
Db *gorm.DB
24+
Selects []any
25+
Omits []any
26+
27+
IgnoreTotal bool
2628
}
2729

2830
type OptionFunc func(*Option)
2931

30-
func Omit(columns ...any) OptionFunc {
32+
// Db 使用传入的Db对象
33+
func Db(db *gorm.DB) OptionFunc {
3134
return func(o *Option) {
32-
o.Omits = append(o.Omits, columns...)
35+
o.Db = db
3336
}
3437
}
3538

39+
// Session 创建回话
40+
func Session(session *gorm.Session) OptionFunc {
41+
return func(o *Option) {
42+
o.Db = globalDb.Session(session)
43+
}
44+
}
45+
46+
// Select 指定需要查询的字段
3647
func Select(columns ...any) OptionFunc {
3748
return func(o *Option) {
3849
o.Selects = append(o.Selects, columns...)
3950
}
4051
}
4152

42-
func Db(db *gorm.DB) OptionFunc {
53+
// Omit 指定需要忽略的字段
54+
func Omit(columns ...any) OptionFunc {
4355
return func(o *Option) {
44-
o.Db = db
56+
o.Omits = append(o.Omits, columns...)
57+
}
58+
}
59+
60+
// IgnoreTotal 分页查询忽略总数 issue: https://github.com/acmestack/gorm-plus/issues/37
61+
func IgnoreTotal() OptionFunc {
62+
return func(o *Option) {
63+
o.IgnoreTotal = true
4564
}
4665
}

0 commit comments

Comments
 (0)