forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pager.go
115 lines (96 loc) · 1.93 KB
/
pager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package urlvalues
import (
"github.com/go-pg/pg/orm"
)
type Pager struct {
Limit int
Offset int
// Default max limit is 1000.
MaxLimit int
// Default max offset is 1000000.
MaxOffset int
stickyErr error
}
func NewPager(values Values) *Pager {
p := new(Pager)
p.stickyErr = p.FromValues(values)
return p
}
func (p *Pager) FromURLValues(values Values) error {
return p.FromValues(Values(values))
}
func (p *Pager) FromValues(values Values) error {
limit, err := values.Int("limit")
if err != nil {
return err
}
p.Limit = int(limit)
page, err := values.Int("page")
if err != nil {
return err
}
p.SetPage(int(page))
return nil
}
func (p *Pager) maxLimit() int {
if p.MaxLimit > 0 {
return p.MaxLimit
}
return 1000
}
func (p *Pager) maxOffset() int {
if p.MaxOffset > 0 {
return p.MaxOffset
}
return 1000000
}
func (p *Pager) GetLimit() int {
const defaultLimit = 100
if p == nil {
return defaultLimit
}
if p.Limit < 0 {
return p.Limit
}
if p.Limit == 0 {
return defaultLimit
}
if p.Limit > p.maxLimit() {
return p.maxLimit()
}
return p.Limit
}
func (p *Pager) GetOffset() int {
if p == nil {
return 0
}
if p.Offset > p.maxOffset() {
return p.maxOffset()
}
return p.Offset
}
func (p *Pager) SetPage(page int) {
if page < 1 {
page = 1
}
p.Offset = (page - 1) * p.GetLimit()
}
func (p *Pager) GetPage() int {
return (p.GetOffset() / p.GetLimit()) + 1
}
func (p *Pager) Pagination(q *orm.Query) (*orm.Query, error) {
if p == nil {
return q, nil
}
if p.stickyErr != nil {
return nil, p.stickyErr
}
q = q.Limit(p.GetLimit()).Offset(p.GetOffset())
return q, nil
}
// Pagination is used with Query.Apply to set LIMIT and OFFSET from the URL values:
// - ?limit=10 - sets q.Limit(10), max limit is 1000.
// - ?page=5 - sets q.Offset((page - 1) * limit), max offset is 1000000.
func Pagination(values Values) func(*orm.Query) (*orm.Query, error) {
return NewPager(values).Pagination
}