@@ -18,28 +18,28 @@ const (
18
18
Desc
19
19
)
20
20
21
- type paginatorType int
21
+ type PaginatorType int
22
22
23
23
const (
24
- paginatorUndefined paginatorType = iota
25
- paginatorByPage
26
- paginatorByID
24
+ PaginatorTypeUndefined PaginatorType = iota
25
+ PaginatorTypeByPage
26
+ PaginatorTypeByID
27
27
)
28
28
29
29
// Paginator is a helper object to paginate results.
30
30
type Paginator struct {
31
31
limit uint64
32
32
page uint64
33
33
lastID int64
34
- pType paginatorType
34
+ pType PaginatorType
35
35
}
36
36
37
37
// PaginatorByPage creates a new Paginator for pagination by page.
38
- func PaginatorByPage (limit , page uint64 ) Paginator {
38
+ func PaginatorByPage (pageSize , pageNum uint64 ) Paginator {
39
39
return Paginator {
40
- limit : limit ,
41
- page : page ,
42
- pType : paginatorByPage ,
40
+ limit : pageSize ,
41
+ page : pageNum ,
42
+ pType : PaginatorTypeByPage ,
43
43
}
44
44
}
45
45
@@ -48,10 +48,35 @@ func PaginatorByID(limit uint64, lastID int64) Paginator {
48
48
return Paginator {
49
49
limit : limit ,
50
50
lastID : lastID ,
51
- pType : paginatorByID ,
51
+ pType : PaginatorTypeByID ,
52
52
}
53
53
}
54
54
55
+ // PageSize returns the page size for PaginatorTypeByPage
56
+ func (p Paginator ) PageSize () uint64 {
57
+ return p .limit
58
+ }
59
+
60
+ // PageNumber returns the page number for PaginatorTypeByPage
61
+ func (p Paginator ) PageNumber () uint64 {
62
+ return p .page
63
+ }
64
+
65
+ // Limit returns the limit for PaginatorTypeByID
66
+ func (p Paginator ) Limit () uint64 {
67
+ return p .limit
68
+ }
69
+
70
+ // LastID returns the last ID for PaginatorTypeByID
71
+ func (p Paginator ) LastID () int64 {
72
+ return p .lastID
73
+ }
74
+
75
+ // Type returns the type of the paginator.
76
+ func (p Paginator ) Type () PaginatorType {
77
+ return p .pType
78
+ }
79
+
55
80
// String returns the string representation of the direction.
56
81
func (d Direction ) String () string {
57
82
if d == Asc {
@@ -144,7 +169,7 @@ func (d *selectData) toSqlRaw() (sqlStr string, args []any, err error) {
144
169
whereParts := make ([]Sqlizer , len (d .WhereParts ))
145
170
copy (whereParts , d .WhereParts )
146
171
147
- if d .Paginator .pType == paginatorByID {
172
+ if d .Paginator .pType == PaginatorTypeByID {
148
173
if d .IDColumn == "" {
149
174
return "" , nil , fmt .Errorf ("IDColumn is required for pagination by ID" )
150
175
}
@@ -182,7 +207,7 @@ func (d *selectData) toSqlRaw() (sqlStr string, args []any, err error) {
182
207
}
183
208
184
209
if len (d .Limit ) > 0 {
185
- if d .Paginator .pType != paginatorUndefined {
210
+ if d .Paginator .pType != PaginatorTypeUndefined {
186
211
return "" , nil , fmt .Errorf ("limit and paginator cannot be used together" )
187
212
}
188
213
@@ -191,20 +216,20 @@ func (d *selectData) toSqlRaw() (sqlStr string, args []any, err error) {
191
216
}
192
217
193
218
if len (d .Offset ) > 0 {
194
- if d .Paginator .pType != paginatorUndefined {
219
+ if d .Paginator .pType != PaginatorTypeUndefined {
195
220
return "" , nil , fmt .Errorf ("offset and paginator cannot be used together" )
196
221
}
197
222
198
223
_ , _ = sql .WriteString (" OFFSET " )
199
224
_ , _ = sql .WriteString (d .Offset )
200
225
}
201
226
202
- if d .Paginator .pType == paginatorByPage {
227
+ if d .Paginator .pType == PaginatorTypeByPage {
203
228
_ , _ = sql .WriteString (fmt .Sprintf (" LIMIT %d" , d .Paginator .limit ))
204
229
if d .Paginator .page > 1 {
205
230
_ , _ = sql .WriteString (fmt .Sprintf (" OFFSET %d" , d .Paginator .limit * (d .Paginator .page - 1 )))
206
231
}
207
- } else if d .Paginator .pType == paginatorByID {
232
+ } else if d .Paginator .pType == PaginatorTypeByID {
208
233
_ , _ = sql .WriteString (fmt .Sprintf (" LIMIT %d" , d .Paginator .limit ))
209
234
}
210
235
0 commit comments