-
Notifications
You must be signed in to change notification settings - Fork 329
/
cursor.go
359 lines (306 loc) · 9.16 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package rdbms
import (
"fmt"
"reflect"
"strings"
"github.com/cortezaproject/corteza/server/pkg/filter"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
)
type (
cursorCondition struct {
cur pagingCursor
keyMapper cursorKeyMapper
sortableCols map[string]string
}
pagingCursor interface {
Keys() []string
KK() [][]string
Modifiers() []string
Values() []interface{}
IsLThen() bool
Desc() []bool
IsROrder() bool
}
KeyMap struct {
FieldCast string
TypeCast string
TypeCastPtrn string
}
// translates field to (store) column
// this helps us with upper/lower case, underscore/camel-case and
// when using fields on records
//
// @todo extend the return args to provide additional info (like is-nullable)
// to avoid IS NULL checks (see sql() fn)
cursorKeyMapper func(string) (KeyMap, error)
)
func cursor(cursor *filter.PagingCursor) ([]goqu.Expression, error) {
sql, args, err := CursorCondition(cursor, nil, nil).ToSQL()
if err != nil {
return nil, err
}
return []goqu.Expression{goqu.Literal(sql, args...)}, nil
}
func cursorWithSorting(cursor *filter.PagingCursor, sortableCols map[string]string) ([]goqu.Expression, error) {
sql, args, err := CursorCondition(cursor, nil, sortableCols).ToSQL()
if err != nil {
return nil, err
}
return []goqu.Expression{goqu.Literal(sql, args...)}, nil
}
// CursorCondition builds a complex condition to filter rows before/after row that
// the paging cursor points to
func CursorCondition(pc pagingCursor, keyMapper cursorKeyMapper, sortableCols map[string]string) *cursorCondition {
if keyMapper == nil {
keyMapper = func(s string) (KeyMap, error) {
return KeyMap{
FieldCast: s,
TypeCast: s,
TypeCastPtrn: "%s",
}, nil
}
}
return &cursorCondition{cur: pc, keyMapper: keyMapper, sortableCols: sortableCols}
}
func (c *cursorCondition) ToSQL() (string, []interface{}, error) {
sql, err := c.sql()
if err != nil {
return "", nil, err
}
return sql, c.values(), nil
}
// generates all values that we need for the generated condition SQL
func (c *cursorCondition) values() []interface{} {
if len(c.cur.Keys()) == 0 {
return nil
}
l := len(c.cur.Values())
vv := make([]interface{}, 0, l*2-1)
for i, v := range c.cur.Values() {
vv = append(vv, v)
if i < l-1 {
// for all but 1st, use values 2 times.
vv = append(vv, v)
}
}
return vv
}
// builds cursor SQL expression
//
// this could be simple (f1, f2, ...) < (v1, v2, ...) but we need to be a bit careful with NULL values
// So we need (f1 < v1 OR (f1 = v1 AND f2 < v2) pattern, extended to:
// ((f1 IS NULL AND v1 IS NOT NULL) OR f1 < v1 OR (((f1 IS NULL AND v1 IS NULL) OR f1 = v1) AND (f2...)
//
// Due to issues with param biding & types in Postgres (using ? IS NULL results in an error), we need do
// check (on app-side) if value is nil to replace "? IS (NOT) NULL" check with TRUE/FALSE constants.
func (c *cursorCondition) sql() (cnd string, err error) {
const (
// we start with this
baseTpl = "((%s IS %s AND %s) OR (%s %s %s))"
// and then wrap each iteration with base and this
wrapTpl = "(%s OR (((%s IS NULL AND %s) OR %s = %s) AND %s))"
)
var (
cc = c.cur.Keys()
kk = c.cur.KK()
mm = c.cur.Modifiers()
vv = c.cur.Values()
ltOp = map[bool]string{
true: "<",
false: ">",
}
notOp = map[bool]string{
true: "NOT NULL",
false: "NULL",
}
// Little utility to know for sure if some value is nil or not
//
// Interface variables can be a bit tricky here, so this is required.
nilCheck = func(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}
// Modifying this function to use expressions instead of constant boolean
// values because MSSQL doesn't have those.
//
// @todo rethink and redo the whole/all of the filtering logic surrounding paging
// cursors to make them consistent/reusable
isNull = func(i int, neg bool) string {
if (nilCheck(vv[i]) && !neg) || (!nilCheck(vv[i]) && neg) {
return "1=1"
}
return "1=0"
}
)
if len(cc) == 0 {
return
}
// going from the last key/column to the 1st one
for i := len(cc) - 1; i >= 0; i-- {
// Get the key context so we know how to format fields and format typecasts
colName := cc[i]
if c.sortableCols != nil {
if v, ok := c.sortableCols[strings.ToLower(cc[i])]; ok {
colName = v
}
// update column name as per the modifier if needed
if len(kk[i]) > 0 && strings.ToLower(mm[i]) == filter.COALESCE {
var tmp []string
for _, k := range kk[i] {
if v, ok := c.sortableCols[strings.ToLower(k)]; ok {
tmp = append(tmp, v)
}
}
colName = fmt.Sprintf("COALESCE(%s)", strings.Join(tmp, ", "))
}
}
km, err := c.keyMapper(colName)
if err != nil {
return "", err
}
// We need to cut off the values that are before the cursor (when ascending)
// and vice-versa for descending.
lt := c.cur.Desc()[i]
if c.cur.IsROrder() {
lt = !lt
}
op := ltOp[lt]
// Typecast the value so comparasement can work properly
vc := fmt.Sprintf(km.TypeCastPtrn, "?")
base := fmt.Sprintf(baseTpl, km.FieldCast, notOp[!lt], isNull(i, lt), km.TypeCast, op, vc)
if cnd == "" {
cnd = base
} else {
// wrap existing conditions (next key) and the generated base for the current key
cnd = fmt.Sprintf(wrapTpl, base, km.FieldCast, isNull(i, false), km.TypeCast, vc, cnd)
}
}
return
}
// CursorExpression builds cursor SQL expression using goqu/exp package
//
// this could be simple (f1, f2, ...) < (v1, v2, ...) but we need to be a bit careful with NULL values
// So we need (f1 < v1 OR (f1 = v1 AND f2 < v2) pattern, extended to:
// ((f1 IS NULL AND v1 IS NOT NULL) OR f1 < v1 OR (((f1 IS NULL AND v1 IS NULL) OR f1 = v1) AND (f2...)
//
// Due to issues with param biding & types in Postgres (using ? IS NULL results in an error), we need do
// check (on app-side) if value is nil to replace "? IS (NOT) NULL" check with TRUE/FALSE constants.
func CursorExpression(
cur *filter.PagingCursor,
identLookup func(i string) (exp.Expression, error),
castFn func(i string, val any) (exp.Expression, error),
) (e exp.Expression, err error) {
var (
cc = cur.Keys()
vv = cur.Values()
value any
ident exp.Expression
ltOp = map[bool]exp.BooleanOperation{
true: exp.LtOp,
false: exp.GtOp,
}
notOp = map[bool]exp.LiteralExpression{
true: exp.NewLiteralExpression("NOT NULL"),
false: exp.NewLiteralExpression("NULL"),
}
// Little utility to know for sure if some value is nil or not
//
// Interface variables can be a bit tricky here, so this is required.
nilCheck = func(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}
// Modifying this function to use expressions instead of constant boolean
// values because MSSQL doesn't have those.
//
// @todo rethink and redo the whole/all of the filtering logic surrounding paging
// cursors to make them consistent/reusable
isValueNull = func(i int, neg bool) exp.Expression {
if (nilCheck(vv[i]) && !neg) || (!nilCheck(vv[i]) && neg) {
return exp.NewLiteralExpression("1=1")
}
return exp.NewLiteralExpression("1=0")
}
curCond exp.ExpressionList
)
if len(cc) == 0 {
return
}
// ((($1 IS NOT NULL AND FALSE) OR ($2 > $3)) OR (((? IS NULL AND FALSE) OR ? = ?) AND (("test_tbl"."id" IS NOT NULL AND FALSE) OR ("test_tbl"."id" > ?))))
// going from the last key/column to the 1st one
for i := len(cc) - 1; i >= 0; i-- {
if identLookup != nil {
// Get the key context so we know how to format fields and format typecasts
ident, err = identLookup(cc[i])
if err != nil {
return
}
} else {
ident = exp.NewLiteralExpression("?", exp.NewIdentifierExpression("", "", cc[i]))
}
if castFn == nil {
value = vv[i]
} else {
value, err = castFn(cc[i], vv[i])
if err != nil {
return
}
}
// We need to cut off the values that are before the cursor (when ascending)
// and vice-versa for descending.
lt := cur.Desc()[i]
if cur.IsROrder() {
lt = !lt
}
op := ltOp[lt]
// // Typecast the value so comparison can work properly
// Either BOTH (field and value) are NULL or field is grater-then value
base := exp.NewExpressionList(
exp.OrType,
// both NULL
exp.NewExpressionList(
exp.AndType,
exp.NewLiteralExpression(`(? IS ?)`, ident, notOp[!lt]),
isValueNull(i, lt),
),
// or GT/LT value
exp.NewBooleanExpression(op, ident, value),
)
if curCond == nil {
curCond = base
} else {
curCond = exp.NewExpressionList(
exp.OrType,
base,
exp.NewExpressionList(
exp.AndType,
exp.NewExpressionList(
exp.OrType,
// both NULL
exp.NewExpressionList(
exp.AndType,
exp.NewLiteralExpression(`(? IS NULL)`, ident),
isValueNull(i, false),
),
exp.NewBooleanExpression(exp.EqOp, ident, value),
),
curCond,
),
)
}
}
return curCond.Expression(), nil
}