Skip to content

Commit ef49b85

Browse files
committed
Paginator getters
1 parent eb536c5 commit ef49b85

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

select.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ const (
1818
Desc
1919
)
2020

21-
type paginatorType int
21+
type PaginatorType int
2222

2323
const (
24-
paginatorUndefined paginatorType = iota
25-
paginatorByPage
26-
paginatorByID
24+
PaginatorTypeUndefined PaginatorType = iota
25+
PaginatorTypeByPage
26+
PaginatorTypeByID
2727
)
2828

2929
// Paginator is a helper object to paginate results.
3030
type Paginator struct {
3131
limit uint64
3232
page uint64
3333
lastID int64
34-
pType paginatorType
34+
pType PaginatorType
3535
}
3636

3737
// PaginatorByPage creates a new Paginator for pagination by page.
38-
func PaginatorByPage(limit, page uint64) Paginator {
38+
func PaginatorByPage(pageSize, pageNum uint64) Paginator {
3939
return Paginator{
40-
limit: limit,
41-
page: page,
42-
pType: paginatorByPage,
40+
limit: pageSize,
41+
page: pageNum,
42+
pType: PaginatorTypeByPage,
4343
}
4444
}
4545

@@ -48,10 +48,35 @@ func PaginatorByID(limit uint64, lastID int64) Paginator {
4848
return Paginator{
4949
limit: limit,
5050
lastID: lastID,
51-
pType: paginatorByID,
51+
pType: PaginatorTypeByID,
5252
}
5353
}
5454

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+
5580
// String returns the string representation of the direction.
5681
func (d Direction) String() string {
5782
if d == Asc {
@@ -144,7 +169,7 @@ func (d *selectData) toSqlRaw() (sqlStr string, args []any, err error) {
144169
whereParts := make([]Sqlizer, len(d.WhereParts))
145170
copy(whereParts, d.WhereParts)
146171

147-
if d.Paginator.pType == paginatorByID {
172+
if d.Paginator.pType == PaginatorTypeByID {
148173
if d.IDColumn == "" {
149174
return "", nil, fmt.Errorf("IDColumn is required for pagination by ID")
150175
}
@@ -182,7 +207,7 @@ func (d *selectData) toSqlRaw() (sqlStr string, args []any, err error) {
182207
}
183208

184209
if len(d.Limit) > 0 {
185-
if d.Paginator.pType != paginatorUndefined {
210+
if d.Paginator.pType != PaginatorTypeUndefined {
186211
return "", nil, fmt.Errorf("limit and paginator cannot be used together")
187212
}
188213

@@ -191,20 +216,20 @@ func (d *selectData) toSqlRaw() (sqlStr string, args []any, err error) {
191216
}
192217

193218
if len(d.Offset) > 0 {
194-
if d.Paginator.pType != paginatorUndefined {
219+
if d.Paginator.pType != PaginatorTypeUndefined {
195220
return "", nil, fmt.Errorf("offset and paginator cannot be used together")
196221
}
197222

198223
_, _ = sql.WriteString(" OFFSET ")
199224
_, _ = sql.WriteString(d.Offset)
200225
}
201226

202-
if d.Paginator.pType == paginatorByPage {
227+
if d.Paginator.pType == PaginatorTypeByPage {
203228
_, _ = sql.WriteString(fmt.Sprintf(" LIMIT %d", d.Paginator.limit))
204229
if d.Paginator.page > 1 {
205230
_, _ = sql.WriteString(fmt.Sprintf(" OFFSET %d", d.Paginator.limit*(d.Paginator.page-1)))
206231
}
207-
} else if d.Paginator.pType == paginatorByID {
232+
} else if d.Paginator.pType == PaginatorTypeByID {
208233
_, _ = sql.WriteString(fmt.Sprintf(" LIMIT %d", d.Paginator.limit))
209234
}
210235

select_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,14 @@ func TestPaginateByPage(t *testing.T) {
462462
}
463463

464464
func TestPaginate(t *testing.T) {
465+
pByID := PaginatorByID(10, 20)
466+
assert.Equal(t, pByID.Limit(), uint64(10))
467+
assert.Equal(t, pByID.LastID(), int64(20))
468+
assert.Equal(t, pByID.Type(), PaginatorTypeByID)
469+
465470
sql, args, err := Select("id", "name").
466471
From("users").
467-
Paginate(PaginatorByID(10, 20)).
472+
Paginate(pByID).
468473
OrderBy("id ASC").
469474
SetIDColumn("id").
470475
ToSql()
@@ -474,14 +479,18 @@ func TestPaginate(t *testing.T) {
474479

475480
_, _, err = Select("id", "name").
476481
From("users").
477-
Paginate(PaginatorByID(10, 20)).
482+
Paginate(pByID).
478483
OrderBy("id ASC").
479484
ToSql()
480485
assert.ErrorContains(t, err, "IDColumn is required for pagination by ID")
481486

487+
pByPage := PaginatorByPage(10, 2)
488+
assert.Equal(t, pByPage.PageSize(), uint64(10))
489+
assert.Equal(t, pByPage.PageNumber(), uint64(2))
490+
482491
sql, args, err = Select("id", "name").
483492
From("users").
484-
Paginate(PaginatorByPage(10, 2)).
493+
Paginate(pByPage).
485494
OrderBy("id ASC").
486495
ToSql()
487496

0 commit comments

Comments
 (0)