-
Notifications
You must be signed in to change notification settings - Fork 4
/
page.go
210 lines (185 loc) · 4.38 KB
/
page.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package pgkit
import (
"fmt"
"regexp"
"strings"
sq "github.com/Masterminds/squirrel"
)
const (
// DefaultPageSize is the default number of rows per page.
DefaultPageSize = 10
// MaxPageSize is the maximum number of rows per page.
MaxPageSize = 50
)
type Order string
const (
Desc Order = "DESC"
Asc Order = "ASC"
)
type Sort struct {
Column string
Order Order
}
func (s Sort) String() string {
if s.Column == "" {
return ""
}
if s.Order == "" {
s.Order = Asc
}
return fmt.Sprintf("%s %s", s.Column, s.Order)
}
var _MatcherOrderBy = regexp.MustCompile(`-?([a-zA-Z0-9]+)`)
func NewSort(s string) (Sort, bool) {
if s == "" || !_MatcherOrderBy.MatchString(s) {
return Sort{}, false
}
sort := Sort{
Column: s,
Order: Asc,
}
if strings.HasPrefix(s, "-") {
sort.Column = s[1:]
sort.Order = Desc
}
return sort, true
}
type Page struct {
Size uint32
Page uint32
More bool
Column string
Sort []Sort
}
func NewPage(size, page uint32, sort ...Sort) *Page {
if size == 0 {
size = DefaultPageSize
}
if page == 0 {
page = 1
}
return &Page{
Size: size,
Page: page,
Sort: sort,
}
}
func (p *Page) GetOrder(defaultSort ...string) []Sort {
// if page has sort, use it
if p != nil && len(p.Sort) != 0 {
return p.Sort
}
// if page has column, use default sort
if p == nil || p.Column == "" {
sort := make([]Sort, 0, len(defaultSort))
for _, s := range defaultSort {
if s, ok := NewSort(s); ok {
sort = append(sort, s)
}
}
return sort
}
// use column
sort := make([]Sort, 0)
for _, part := range strings.Split(p.Column, ",") {
if s, ok := NewSort(part); ok {
sort = append(sort, s)
}
}
return sort
}
func (p *Page) Offset() uint64 {
n := uint64(1)
if p != nil && p.Page != 0 {
n = uint64(p.Page)
}
if n < 1 {
n = 1
}
return (n - 1) * p.Limit()
}
func (p *Page) Limit() uint64 {
var n = uint64(DefaultPageSize)
if p != nil && p.Size != 0 {
n = uint64(p.Size)
}
if n > MaxPageSize {
n = MaxPageSize
}
return n
}
// PaginatorOption is a function that sets an option on a paginator.
type PaginatorOption[T any] func(*Paginator[T])
// WithDefaultSize sets the default page size.
func WithDefaultSize[T any](size uint32) PaginatorOption[T] {
return func(p *Paginator[T]) { p.defaultSize = size }
}
// WithMaxSize sets the maximum page size.
func WithMaxSize[T any](size uint32) PaginatorOption[T] {
return func(p *Paginator[T]) { p.maxSize = size }
}
// WithSort sets the default sort order.
func WithSort[T any](sort ...string) PaginatorOption[T] {
return func(p *Paginator[T]) { p.defaultSort = sort }
}
// WithColumnFunc sets a function to transform column names.
func WithColumnFunc[T any](f func(string) string) PaginatorOption[T] {
return func(p *Paginator[T]) { p.columnFunc = f }
}
// NewPaginator creates a new paginator with the given options.
// Default page size is 10 and max size is 50.
func NewPaginator[T any](options ...PaginatorOption[T]) Paginator[T] {
p := Paginator[T]{
defaultSize: DefaultPageSize,
maxSize: MaxPageSize,
}
for _, opt := range options {
opt(&p)
}
return p
}
// Paginator is a helper to paginate results.
type Paginator[T any] struct {
defaultSize uint32
maxSize uint32
defaultSort []string
columnFunc func(string) string
}
func (p Paginator[T]) getOrder(page *Page) []string {
sort := page.GetOrder(p.defaultSort...)
list := make([]string, len(sort))
for i, s := range sort {
if p.columnFunc != nil {
s.Column = p.columnFunc(s.Column)
}
list[i] = s.String()
}
return list
}
// PrepareQuery adds pagination to the query. It sets the number of max rows to limit+1.
func (p Paginator[T]) PrepareQuery(q sq.SelectBuilder, page *Page) ([]T, sq.SelectBuilder) {
if page != nil {
if page.Size == 0 {
page.Size = p.defaultSize
}
if page.Size > p.maxSize {
page.Size = p.maxSize
}
}
limit := page.Limit()
q = q.Limit(page.Limit() + 1).Offset(page.Offset()).OrderBy(p.getOrder(page)...)
return make([]T, 0, limit+1), q
}
// PrepareResult prepares the paginated result. If the number of rows is n+1:
// - it removes the last element, returning n elements
// - it sets more to true in the page object
func (p Paginator[T]) PrepareResult(result []T, page *Page) []T {
limit := int(page.Limit())
page.More = len(result) > limit
if page.More {
result = result[:limit]
}
page.Size = uint32(limit)
page.Page = 1 + uint32(page.Offset())/uint32(limit)
return result
}