-
Notifications
You must be signed in to change notification settings - Fork 76
/
operators.go
312 lines (269 loc) · 9.73 KB
/
operators.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
package kallax
import (
"database/sql/driver"
"errors"
"fmt"
"strings"
"github.com/src-d/go-kallax/types"
"github.com/Masterminds/squirrel"
)
// Condition represents a condition of filtering in a query.
type Condition func(Schema) squirrel.Sqlizer
// Eq returns a condition that will be true when `col` is equal to `value`.
func Eq(col SchemaField, value interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.Eq{col.QualifiedName(schema): value}
}
}
// Lt returns a condition that will be true when `col` is lower than `value`.
func Lt(col SchemaField, value interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.Lt{col.QualifiedName(schema): value}
}
}
// Gt returns a condition that will be true when `col` is greater than `value`.
func Gt(col SchemaField, n interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.Gt{col.QualifiedName(schema): n}
}
}
// LtOrEq returns a condition that will be true when `col` is lower than
// `value` or equal.
func LtOrEq(col SchemaField, n interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.LtOrEq{col.QualifiedName(schema): n}
}
}
// GtOrEq returns a condition that will be true when `col` is greater than
// `value` or equal.
func GtOrEq(col SchemaField, n interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.GtOrEq{col.QualifiedName(schema): n}
}
}
// Neq returns a condition that will be true when `col` is not `value`.
func Neq(col SchemaField, n interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.NotEq{col.QualifiedName(schema): n}
}
}
// Or returns the given conditions joined by logical ors.
func Or(conds ...Condition) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.Or(condsToSqlizers(conds, schema))
}
}
// And returns the given conditions joined by logical ands.
func And(conds ...Condition) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.And(condsToSqlizers(conds, schema))
}
}
// Not returns the given condition negated.
func Not(cond Condition) Condition {
return func(schema Schema) squirrel.Sqlizer {
return not{cond(schema)}
}
}
// In returns a condition that will be true when `col` is equal to any of the
// passed `values`.
func In(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.Eq{col.QualifiedName(schema): values}
}
}
// NotIn returns a condition that will be true when `col` is distinct to all of the
// passed `values`.
func NotIn(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return squirrel.NotEq{col.QualifiedName(schema): values}
}
}
// ArrayEq returns a condition that will be true when `col` is equal to an
// array with the given elements.
func ArrayEq(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "=", types.Slice(values)}
}
}
// ArrayNotEq returns a condition that will be true when `col` is not equal to
// an array with the given elements.
func ArrayNotEq(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "<>", types.Slice(values)}
}
}
// ArrayLt returns a condition that will be true when all elements in `col`
// are lower or equal than their counterparts in the given values, and one of
// the elements at least is lower than its counterpart in the given values.
// For example: for a col with values [1,2,2] and values [1,2,3], it will be
// true.
func ArrayLt(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "<", types.Slice(values)}
}
}
// ArrayGt returns a condition that will be true when all elements in `col`
// are greater or equal than their counterparts in the given values, and one of
// the elements at least is greater than its counterpart in the given values.
// For example: for a col with values [1,2,3] and values [1,2,2], it will be
// true.
func ArrayGt(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), ">", types.Slice(values)}
}
}
// ArrayLtOrEq returns a condition that will be true when all elements in `col`
// are lower or equal than their counterparts in the given values.
// For example: for a col with values [1,2,2] and values [1,2,2], it will be
// true.
func ArrayLtOrEq(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "<=", types.Slice(values)}
}
}
// ArrayGtOrEq returns a condition that will be true when all elements in `col`
// are greater or equal than their counterparts in the given values.
// For example: for a col with values [1,2,2] and values [1,2,2], it will be
// true.
func ArrayGtOrEq(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), ">=", types.Slice(values)}
}
}
// ArrayContains returns a condition that will be true when `col` contains all the
// given values.
func ArrayContains(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "@>", types.Slice(values)}
}
}
// ArrayContainedBy returns a condition that will be true when `col` has all
// its elements present in the given values.
func ArrayContainedBy(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "<@", types.Slice(values)}
}
}
// ArrayOverlap returns a condition that will be true when `col` has elements
// in common with an array formed by the given values.
func ArrayOverlap(col SchemaField, values ...interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "&&", types.Slice(values)}
}
}
// JSONIsObject returns a condition that will be true when `col` is a JSON
// object.
func JSONIsObject(col SchemaField) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colUnaryOp{col.QualifiedName(schema), " @> '{}'"}
}
}
// JSONIsArray returns a condition that will be true when `col` is a JSON
// array.
func JSONIsArray(col SchemaField) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colUnaryOp{col.QualifiedName(schema), " @> '[]'"}
}
}
// JSONContains returns a condition that will be true when `col` contains
// the given element converted to JSON.
func JSONContains(col SchemaField, elem interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "@>", types.JSON(elem)}
}
}
// JSONContainsAny returns a condition that will be true when `col` contains
// any of the given elements converted to json.
// Giving no elements will cause an error to be returned when the condition is
// evaluated.
func JSONContainsAny(col SchemaField, elems ...interface{}) Condition {
if len(elems) == 1 {
return JSONContains(col, elems[0])
}
return func(schema Schema) squirrel.Sqlizer {
if len(elems) == 0 {
return &errOp{"can't check if json contains 0 elements"}
}
return &containsAny{col.QualifiedName(schema), elems}
}
}
// JSONContainedBy returns a condition that will be true when `col` is
// contained by the given element converted to JSON.
func JSONContainedBy(col SchemaField, elem interface{}) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "<@", types.JSON(elem)}
}
}
// JSONContainsAnyKey returns a condition that will be true when `col` contains
// any of the given keys. Will also match elements if the column is an array.
func JSONContainsAnyKey(col SchemaField, keys ...string) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "??|", types.Slice(keys)}
}
}
// JSONContainsAllKeys returns a condition that will be true when `col`
// contains all the given keys. Will also match elements if the column is an
// array.
func JSONContainsAllKeys(col SchemaField, keys ...string) Condition {
return func(schema Schema) squirrel.Sqlizer {
return &colOp{col.QualifiedName(schema), "??&", types.Slice(keys)}
}
}
type (
not struct {
cond squirrel.Sqlizer
}
colOp struct {
col string
op string
valuer driver.Valuer
}
colUnaryOp struct {
col string
op string
}
errOp struct {
msg string
}
containsAny struct {
col string
values []interface{}
}
)
func (n not) ToSql() (string, []interface{}, error) {
sql, args, err := n.cond.ToSql()
if err != nil {
return "", nil, err
}
return fmt.Sprintf("NOT (%s)", sql), args, err
}
func (o colOp) ToSql() (string, []interface{}, error) {
return fmt.Sprintf("%s %s ?", o.col, o.op), []interface{}{o.valuer}, nil
}
func (o colUnaryOp) ToSql() (string, []interface{}, error) {
return fmt.Sprintf("%s %s", o.col, o.op), nil, nil
}
func (o errOp) ToSql() (string, []interface{}, error) {
return "", nil, errors.New(o.msg)
}
func (o containsAny) ToSql() (string, []interface{}, error) {
var placeholders = make([]string, len(o.values))
var args = make([]interface{}, len(o.values))
for i, el := range o.values {
args[i] = types.JSON(el)
placeholders[i] = "?"
}
return fmt.Sprintf(
"%s @> ANY (ARRAY [%s]::jsonb[])",
o.col,
strings.Join(placeholders, ", "),
), args, nil
}
func condsToSqlizers(conds []Condition, schema Schema) []squirrel.Sqlizer {
var result = make([]squirrel.Sqlizer, len(conds))
for i, v := range conds {
result[i] = v(schema)
}
return result
}