Skip to content

Commit 43fafb3

Browse files
committed
支持指定统计总记录数的列
1 parent 0ea6f99 commit 43fafb3

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (b *builder) Page(page, pageSize int) *builder {
2929
//分页
3030
//page 页码
3131
//pageSize 分页大小
32-
func (b *builder) PageWithTotal(page, pageSize int) *builder {
33-
b.ctx = StartPageWithTotal(page, pageSize, b.ctx)
32+
func (b *builder) PageWithTotal(page, pageSize int, countColumn string) *builder {
33+
b.ctx = StartPageWithTotal(page, pageSize, countColumn, b.ctx)
3434
return b
3535
}
3636

pagehelper.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
const (
2626
pageHelperValue = "_page_helper_value"
2727
orderHelperValue = "_order_helper_value"
28-
totalHelperValue = "_total_helper_value"
2928

3029
ASC = "ASC"
3130
DESC = "DESC"
@@ -46,7 +45,8 @@ type PageParam struct {
4645
Page int
4746
PageSize int
4847

49-
total bool
48+
countColumn string
49+
total int64
5050
}
5151

5252
func New(f factory.Factory) *Factory {
@@ -57,10 +57,10 @@ func GetTotal(ctx context.Context) int64 {
5757
if ctx == nil {
5858
return 0
5959
}
60-
p := ctx.Value(totalHelperValue)
60+
p := ctx.Value(pageHelperValue)
6161
if p != nil {
62-
if t, ok := p.(int64); ok {
63-
return t
62+
if param, ok := p.(*PageParam); ok {
63+
return param.total
6464
}
6565
}
6666
return 0
@@ -80,15 +80,15 @@ type Executor struct {
8080
//pageSize 分页大小
8181
//ctx 初始context
8282
func StartPage(page, pageSize int, ctx context.Context) context.Context {
83-
return context.WithValue(ctx, pageHelperValue, &PageParam{Page: page, PageSize: pageSize, total: false})
83+
return context.WithValue(ctx, pageHelperValue, &PageParam{Page: page, PageSize: pageSize, total: 0})
8484
}
8585

8686
//分页(包含total信息)
8787
//page 页码
8888
//pageSize 分页大小
8989
//ctx 初始context
90-
func StartPageWithTotal(page, pageSize int, ctx context.Context) context.Context {
91-
return context.WithValue(ctx, pageHelperValue, &PageParam{Page: page, PageSize: pageSize, total: true})
90+
func StartPageWithTotal(page, pageSize int, countColumn string, ctx context.Context) context.Context {
91+
return context.WithValue(ctx, pageHelperValue, &PageParam{Page: page, PageSize: pageSize, total: -1, countColumn: countColumn})
9292
}
9393

9494
//排序
@@ -132,9 +132,8 @@ func (exec *Executor) Query(ctx context.Context, result reflection.Object, sql s
132132
if p != nil {
133133
if param, ok := p.(*PageParam); ok {
134134
sql = PageModifier(sql, param)
135-
if param.total == true {
136-
total := exec.getTotal(ctx, originSql, params...)
137-
ctx = context.WithValue(ctx, totalHelperValue, total)
135+
if param.total == -1 {
136+
param.total = exec.getTotal(ctx, originSql, param.countColumn, params...)
138137
}
139138
}
140139
}
@@ -183,8 +182,8 @@ func modifyPageSql(sql string, p *PageParam) string {
183182
return b.String()
184183
}
185184

186-
func (exec *Executor) getTotal(ctx context.Context, sql string, params ...interface{}) int64 {
187-
totalSql := CountModifier(sql)
185+
func (exec *Executor) getTotal(ctx context.Context, sql, countColumn string, params ...interface{}) int64 {
186+
totalSql := CountModifier(sql, countColumn)
188187
var total int64
189188
obj, err := gobatis.ParseObject(&total)
190189
if err == nil {
@@ -194,10 +193,15 @@ func (exec *Executor) getTotal(ctx context.Context, sql string, params ...interf
194193
return 0
195194
}
196195

197-
func modifyCountSql(sql string) string {
196+
func modifyCountSql(sql, countColumn string) string {
197+
if countColumn == "" {
198+
countColumn = "0"
199+
}
198200
b := strings.Builder{}
199-
b.WriteString("SELECT COUNT(0) FROM (")
200-
b.WriteString(sql)
201-
b.WriteString(")")
201+
b.WriteString("SELECT COUNT(`")
202+
b.WriteString(countColumn)
203+
b.WriteString("`) FROM (")
204+
b.WriteString(strings.TrimSpace(sql))
205+
b.WriteString(") AS __hp_tempCountTl")
202206
return b.String()
203207
}

pagehelper_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestPageHelper2(t *testing.T) {
124124
}
125125

126126
func TestModifyPage(t *testing.T) {
127-
sql := PageModifier("select * from x", &PageParam{1, 2, false})
127+
sql := PageModifier("select * from x", &PageParam{Page: 1, PageSize: 2,})
128128
t.Log(sql)
129129
if strings.TrimSpace(sql) != `select * from x LIMIT 2, 2` {
130130
t.Fail()
@@ -148,20 +148,30 @@ func TestModifyOrder(t *testing.T) {
148148
}
149149

150150
func TestModifyCount(t *testing.T) {
151-
sql := CountModifier("select ? from x")
152-
t.Log(sql)
151+
t.Run("empty", func(t *testing.T) {
152+
sql := CountModifier("select ? from x", "")
153+
t.Log(sql)
153154

154-
if strings.TrimSpace(sql) != "SELECT COUNT(0) FROM (select ? from x)" {
155-
t.Fail()
156-
}
157-
}
155+
if strings.TrimSpace(sql) != "SELECT COUNT(0) FROM (select ? from x) AS __hp_tempCountTl" {
156+
t.Fail()
157+
}
158+
})
159+
160+
t.Run("test", func(t *testing.T) {
161+
sql := CountModifier("select ? from x", "test")
162+
t.Log(sql)
158163

164+
if strings.TrimSpace(sql) != "SELECT COUNT(`test`) FROM (select ? from x) AS __hp_tempCountTl" {
165+
t.Fail()
166+
}
167+
})
168+
}
159169

160170
func TestChangeModifyCount(t *testing.T) {
161-
CountModifier = func(sql string) string {
171+
CountModifier = func(sql, c string) string {
162172
return "test " + sql
163173
}
164-
sql := CountModifier("select ? from x")
174+
sql := CountModifier("select ? from x", "")
165175
t.Log(sql)
166176

167177
if strings.TrimSpace(sql) != "test select ? from x" {
@@ -185,7 +195,7 @@ func TestModifyOrderAndPage(t *testing.T) {
185195
sql, p := order("select ? from x", "field1")
186196
t.Log(sql)
187197

188-
sql = PageModifier(sql, &PageParam{1, 2, false})
198+
sql = PageModifier(sql, &PageParam{Page: 1, PageSize: 2,})
189199

190200
t.Log(sql)
191201
for _, v := range p {

0 commit comments

Comments
 (0)