@@ -11,6 +11,7 @@ package pagehelper
11
11
import (
12
12
"context"
13
13
"fmt"
14
+ "github.com/xfali/gobatis"
14
15
"github.com/xfali/gobatis/common"
15
16
"github.com/xfali/gobatis/executor"
16
17
"github.com/xfali/gobatis/factory"
@@ -24,11 +25,18 @@ import (
24
25
const (
25
26
pageHelperValue = "_page_helper_value"
26
27
orderHelperValue = "_order_helper_value"
28
+ totalHelperValue = "_total_helper_value"
27
29
28
30
ASC = "ASC"
29
31
DESC = "DESC"
30
32
)
31
33
34
+ var (
35
+ OrderByModifier = modifyOrderSql
36
+ PageModifier = modifyPageSql
37
+ CountModifier = modifyCountSql
38
+ )
39
+
32
40
type OrderParam struct {
33
41
Field string
34
42
Order string
@@ -37,12 +45,27 @@ type OrderParam struct {
37
45
type PageParam struct {
38
46
Page int
39
47
PageSize int
48
+
49
+ total bool
40
50
}
41
51
42
52
func New (f factory.Factory ) * Factory {
43
53
return & Factory {f }
44
54
}
45
55
56
+ func GetTotal (ctx context.Context ) int64 {
57
+ if ctx == nil {
58
+ return 0
59
+ }
60
+ p := ctx .Value (totalHelperValue )
61
+ if p != nil {
62
+ if t , ok := p .(int64 ); ok {
63
+ return t
64
+ }
65
+ }
66
+ return 0
67
+ }
68
+
46
69
type Factory struct {
47
70
fac factory.Factory
48
71
}
@@ -57,7 +80,15 @@ type Executor struct {
57
80
//pageSize 分页大小
58
81
//ctx 初始context
59
82
func StartPage (page , pageSize int , ctx context.Context ) context.Context {
60
- return context .WithValue (ctx , pageHelperValue , & PageParam {Page : page , PageSize : pageSize })
83
+ return context .WithValue (ctx , pageHelperValue , & PageParam {Page : page , PageSize : pageSize , total : false })
84
+ }
85
+
86
+ //分页(包含total信息)
87
+ //page 页码
88
+ //pageSize 分页大小
89
+ //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 })
61
92
}
62
93
63
94
//排序
@@ -89,17 +120,22 @@ func (exec *Executor) Rollback(require bool) error {
89
120
}
90
121
91
122
func (exec * Executor ) Query (ctx context.Context , result reflection.Object , sql string , params ... interface {}) error {
123
+ originSql := sql
92
124
o := ctx .Value (orderHelperValue )
93
125
if o != nil {
94
126
if param , ok := o .(* OrderParam ); ok {
95
- sql = modifySqlOrder (sql , param )
127
+ sql = OrderByModifier (sql , param )
96
128
}
97
129
}
98
130
99
131
p := ctx .Value (pageHelperValue )
100
132
if p != nil {
101
133
if param , ok := p .(* PageParam ); ok {
102
- sql = modifySql (sql , param )
134
+ sql = PageModifier (sql , param )
135
+ if param .total == true {
136
+ total := exec .getTotal (ctx , originSql , params ... )
137
+ ctx = context .WithValue (ctx , totalHelperValue , total )
138
+ }
103
139
}
104
140
}
105
141
exec .log (logging .DEBUG , "PageHelper Query: [%s], params: %s\n " , sql , fmt .Sprint (params ))
@@ -130,7 +166,7 @@ func (f *Factory) CreateExecutor(transaction transaction.Transaction) executor.E
130
166
}
131
167
}
132
168
133
- func modifySqlOrder (sql string , p * OrderParam ) string {
169
+ func modifyOrderSql (sql string , p * OrderParam ) string {
134
170
if p .Field == "" {
135
171
return sql
136
172
}
@@ -140,9 +176,28 @@ func modifySqlOrder(sql string, p *OrderParam) string {
140
176
return b .String ()
141
177
}
142
178
143
- func modifySql (sql string , p * PageParam ) string {
179
+ func modifyPageSql (sql string , p * PageParam ) string {
144
180
b := strings.Builder {}
145
181
b .WriteString (strings .TrimSpace (sql ))
146
182
b .WriteString (fmt .Sprintf (" LIMIT %d, %d " , p .Page * p .PageSize , p .PageSize ))
147
183
return b .String ()
148
184
}
185
+
186
+ func (exec * Executor ) getTotal (ctx context.Context , sql string , params ... interface {}) int64 {
187
+ totalSql := CountModifier (sql )
188
+ var total int64
189
+ obj , err := gobatis .ParseObject (& total )
190
+ if err == nil {
191
+ exec .exec .Query (ctx , obj , totalSql , params ... )
192
+ return total
193
+ }
194
+ return 0
195
+ }
196
+
197
+ func modifyCountSql (sql string ) string {
198
+ b := strings.Builder {}
199
+ b .WriteString ("SELECT COUNT(0) FROM (" )
200
+ b .WriteString (sql )
201
+ b .WriteString (")" )
202
+ return b .String ()
203
+ }
0 commit comments