-
Notifications
You must be signed in to change notification settings - Fork 3
/
cursor.go
210 lines (174 loc) · 4.29 KB
/
cursor.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 eventline
import (
"encoding/base64"
"fmt"
"net/url"
"strconv"
"github.com/exograd/eventline/pkg/utils"
"github.com/exograd/go-daemon/dhttp"
"github.com/exograd/go-daemon/pg"
)
type Order string
const (
OrderAsc Order = "asc"
OrderDesc Order = "desc"
)
const (
DefaultCursorSize = 20
MinCursorSize = 1
MaxCursorSize = 100
)
type Cursor struct {
Before string `json:"before,omitempty"`
After string `json:"after,omitempty"`
Size int `json:"size,omitempty"`
Sort string `json:"sort,omitempty"`
Order Order `json:"order,omitempty"`
}
func (pc *Cursor) ParseQuery(query url.Values, sorts Sorts, accountSettings *AccountSettings) error {
var c Cursor
// Before
if s := query.Get("before"); s != "" {
key, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return dhttp.NewInvalidQueryParameterError("before",
"invalid value")
}
c.Before = string(key)
}
// After
if s := query.Get("after"); s != "" {
if c.Before != "" {
return dhttp.NewInvalidQueryParameterError("after",
"%q and %q are both set", "before", "after")
}
key, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return dhttp.NewInvalidQueryParameterError("after",
"invalid value")
}
c.After = string(key)
}
// Size
if s := query.Get("size"); s != "" {
i64, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return dhttp.NewInvalidQueryParameterError("size",
"invalid format")
} else if i64 < MinCursorSize {
return dhttp.NewInvalidQueryParameterError("size",
"value must be greater or equal to %d", MinCursorSize)
} else if i64 > MaxCursorSize {
return dhttp.NewInvalidQueryParameterError("size",
"value must be lower or equal to %d", MaxCursorSize)
}
c.Size = int(i64)
} else if accountSettings != nil && accountSettings.PageSize > 0 {
c.Size = accountSettings.PageSize
}
// Sort
if s := query.Get("sort"); s != "" {
if !sorts.Contains(s) {
return dhttp.NewInvalidQueryParameterError("sort",
"unsupported sort")
}
c.Sort = s
}
// Order
if s := query.Get("order"); s != "" {
switch Order(s) {
case OrderAsc:
c.Order = OrderAsc
case OrderDesc:
c.Order = OrderDesc
default:
return dhttp.NewInvalidQueryParameterError("order",
"invalid value")
}
}
*pc = c
return nil
}
func (c *Cursor) Query() url.Values {
query := make(url.Values)
base64Encode := func(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
if c.Before != "" {
query.Add("before", base64Encode(c.Before))
}
if c.After != "" {
query.Add("after", base64Encode(c.After))
}
if c.Size > 0 {
query.Add("size", strconv.Itoa(c.Size))
}
if c.Sort != "" {
query.Add("sort", c.Sort)
}
if c.Order != "" {
query.Add("order", string(c.Order))
}
return query
}
func (c *Cursor) URL() *url.URL {
query := c.Query()
return &url.URL{
RawQuery: query.Encode(),
}
}
func (c *Cursor) SQLConditionOrderLimit(sorts Sorts) string {
return c.SQLConditionOrderLimit2(sorts, "")
}
func (c *Cursor) SQLConditionOrderLimit2(sorts Sorts, correlation string) string {
size := c.Size
if size == 0 {
size = DefaultCursorSize
}
order := c.Order
if order == "" {
order = OrderAsc
}
sort := c.Sort
if sort == "" {
sort = sorts.Default
}
sortPart := sorts.Column(sort)
if sortPart == "" {
utils.Panicf("unknown sort %q", sort)
}
if correlation != "" {
sortPart = correlation + "." + sortPart
}
var orderPart string
if order == OrderAsc {
if c.Before == "" {
orderPart = "ASC"
} else {
orderPart = "DESC"
}
} else if order == OrderDesc {
if c.Before == "" {
orderPart = "DESC"
} else {
orderPart = "ASC"
}
} else {
utils.Panicf("unsupported order %q", order)
}
var condPart string
switch {
case c.After != "" && order == OrderAsc:
condPart = fmt.Sprintf(`%s > %s `, sortPart, pg.QuoteString(c.After))
case c.After != "" && order == OrderDesc:
condPart = fmt.Sprintf(`%s < %s `, sortPart, pg.QuoteString(c.After))
case c.Before != "" && order == OrderAsc:
condPart = fmt.Sprintf(`%s < %s `, sortPart, pg.QuoteString(c.Before))
case c.Before != "" && order == OrderDesc:
condPart = fmt.Sprintf(`%s > %s `, sortPart, pg.QuoteString(c.Before))
default:
condPart = `TRUE `
}
return fmt.Sprintf(`%sORDER BY (%s) %s LIMIT %d`,
condPart, sortPart, orderPart, size+1)
}