This repository was archived by the owner on Mar 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwhere.go
More file actions
340 lines (293 loc) · 7.11 KB
/
Copy pathwhere.go
File metadata and controls
340 lines (293 loc) · 7.11 KB
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
package memeduck
import (
"github.com/MakeNowJust/memefish/pkg/ast"
"github.com/pkg/errors"
"github.com/genkami/memeduck/internal"
)
// WhereCond is a conditional expression that appears in WHERE clauses.
type WhereCond interface {
ToASTWhere() (*ast.Where, error)
}
// ExprCond is a boolean expression to filter records.
type ExprCond struct {
expr ast.Expr
}
func (c *ExprCond) ToASTWhere() (*ast.Where, error) {
return &ast.Where{
Expr: c.expr,
}, nil
}
// Bool creates a new boolean literal.
func Bool(v bool) *ExprCond {
return &ExprCond{expr: internal.BoolLit(v)}
}
// OpCond is a binary operator expression.
type OpCond struct {
lhs, rhs interface{}
op BinaryOp
}
// Op is a binary operator
type BinaryOp ast.BinaryOp
const (
EQ BinaryOp = BinaryOp(ast.OpEqual)
NE BinaryOp = BinaryOp(ast.OpNotEqual)
LT BinaryOp = BinaryOp(ast.OpLess)
GT BinaryOp = BinaryOp(ast.OpGreater)
LE BinaryOp = BinaryOp(ast.OpLessEqual)
GE BinaryOp = BinaryOp(ast.OpGreaterEqual)
LIKE BinaryOp = BinaryOp(ast.OpLike)
NOT_LIKE BinaryOp = BinaryOp(ast.OpNotLike)
)
func (c *OpCond) ToASTWhere() (*ast.Where, error) {
lhs, err := internal.ToExpr(c.lhs)
if err != nil {
return nil, err
}
rhs, err := internal.ToExpr(c.rhs)
if err != nil {
return nil, err
}
return &ast.Where{
Expr: &ast.BinaryExpr{
Op: ast.BinaryOp(c.op),
Left: lhs,
Right: rhs,
},
}, nil
}
// Op creates a new binary operator expression.
func Op(lhs interface{}, op BinaryOp, rhs interface{}) *OpCond {
return &OpCond{
lhs: lhs,
rhs: rhs,
op: op,
}
}
// Eq(x, y) is a shorthand for Op(x, EQ, y)
func Eq(lhs, rhs interface{}) *OpCond {
return Op(lhs, EQ, rhs)
}
// Ne(x, y) is a shorthand for Op(x, NE, y)
func Ne(lhs, rhs interface{}) *OpCond {
return Op(lhs, NE, rhs)
}
// Lt(x, y) is a shorthand for Op(x, LT, y)
func Lt(lhs, rhs interface{}) *OpCond {
return Op(lhs, LT, rhs)
}
// Gt(x, y) is a shorthand for Op(x, GT, y)
func Gt(lhs, rhs interface{}) *OpCond {
return Op(lhs, GT, rhs)
}
// Le(x, y) is a shorthand for Op(x, LE, y)
func Le(lhs, rhs interface{}) *OpCond {
return Op(lhs, LE, rhs)
}
// Ge(x, y) is a shorthand for Op(x, GE, y)
func Ge(lhs, rhs interface{}) *OpCond {
return Op(lhs, GE, rhs)
}
// Like(x, y) is a shorthand for Op(x, LIKE, y)
func Like(lhs, rhs interface{}) *OpCond {
return Op(lhs, LIKE, rhs)
}
// NotLike(x, y) is a shorthand for Op(x, NOT_LIKE, y)
func NotLike(lhs, rhs interface{}) *OpCond {
return Op(lhs, NOT_LIKE, rhs)
}
// NullCond represents IS NULL or IS NOT NULL predicate.
type NullCond struct {
not bool
arg interface{}
}
// IsNull creates `x IS NULL` predicate.
func IsNull(arg interface{}) *NullCond {
return &NullCond{arg: arg}
}
// IsNotNull creates `x IS NOT NULL` predicate.
func IsNotNull(arg interface{}) *NullCond {
return &NullCond{arg: arg, not: true}
}
func (c *NullCond) ToASTWhere() (*ast.Where, error) {
expr, err := internal.ToExpr(c.arg)
if err != nil {
return nil, err
}
return &ast.Where{
Expr: &ast.IsNullExpr{
Not: c.not,
Left: expr,
},
}, nil
}
// InConditionValue is a value expression in IN clauses.
type InConditionValue interface {
ToASTInConditionValue() (ast.InCondition, error)
}
// UnnestInConditionValue is a UNNEST operator in IN clauses.
type UnnestInConditionValue struct {
value interface{}
}
func (v *UnnestInConditionValue) ToASTInConditionValue() (ast.InCondition, error) {
value, err := internal.ToExpr(v.value)
if err != nil {
return nil, err
}
return &ast.UnnestInCondition{
Expr: value,
}, nil
}
// Unnest(v) creates `UNNEST(v)` predicate.
func Unnest(v interface{}) *UnnestInConditionValue {
return &UnnestInConditionValue{
value: v,
}
}
// InCond represents IN or NOT IN predicates.
type InCond struct {
lhs interface{}
rhs InConditionValue
not bool
}
// In(x, y) creates `x IN y` predicate.
func In(x interface{}, y InConditionValue) *InCond {
return &InCond{lhs: x, rhs: y, not: false}
}
// In(x, y) creates `x NOT IN y` predicate.
func NotIn(x interface{}, y InConditionValue) *InCond {
return &InCond{lhs: x, rhs: y, not: true}
}
func (c *InCond) ToASTWhere() (*ast.Where, error) {
lhs, err := internal.ToExpr(c.lhs)
if err != nil {
return nil, err
}
rhs, err := c.rhs.ToASTInConditionValue()
if err != nil {
return nil, err
}
return &ast.Where{
Expr: &ast.InExpr{
Not: c.not,
Left: lhs,
Right: rhs,
},
}, nil
}
// BetweenCond represents BETWEEN or NOT BETWEEN predicates.
type BetweenCond struct {
arg interface{}
min interface{}
max interface{}
not bool
}
// Between(x, min, max) creates `x BETWEEN min AND max` predicate.
func Between(x, min, max interface{}) *BetweenCond {
return &BetweenCond{arg: x, min: min, max: max}
}
// NotBetween(x, min, max) creates `x NOT BETWEEN min AND max` predicate.
func NotBetween(x, min, max interface{}) *BetweenCond {
return &BetweenCond{arg: x, min: min, max: max, not: true}
}
func (c *BetweenCond) ToASTWhere() (*ast.Where, error) {
arg, err := internal.ToExpr(c.arg)
if err != nil {
return nil, err
}
min, err := internal.ToExpr(c.min)
if err != nil {
return nil, err
}
max, err := internal.ToExpr(c.max)
if err != nil {
return nil, err
}
return &ast.Where{
Expr: &ast.BetweenExpr{
Not: c.not,
Left: arg,
RightStart: min,
RightEnd: max,
},
}, nil
}
// IdentExpr is an identifier.
type IdentExpr struct {
names []string
}
// Ident creates a new IdentExpr.
// Path expression can be created by passing more than one elements.
func Ident(names ...string) *IdentExpr {
return &IdentExpr{names: names}
}
func (e *IdentExpr) ToASTExpr() (ast.Expr, error) {
if len(e.names) <= 0 {
return nil, errors.New("empty identifier")
}
path := &ast.Path{}
for _, name := range e.names {
path.Idents = append(path.Idents, &ast.Ident{
Name: name,
})
}
return path, nil
}
// ParamExpr is a query parameter.
type ParamExpr struct {
name string
}
// Param createsa new ParamExpr.
func Param(name string) *ParamExpr {
return &ParamExpr{name: name}
}
func (e *ParamExpr) ToASTExpr() (ast.Expr, error) {
return &ast.Param{Name: e.name}, nil
}
// LogicalOpCond represents AND/OR operator.
type LogicalOpCond struct {
op logicalOp
conds []WhereCond
}
type logicalOp ast.BinaryOp
const (
logicalOpAnd logicalOp = logicalOp(ast.OpAnd)
logicalOpOr logicalOp = logicalOp(ast.OpOr)
)
// And concatenates more than one WhereConds with AND operator.
func And(conds ...WhereCond) *LogicalOpCond {
return &LogicalOpCond{
op: logicalOpAnd,
conds: conds,
}
}
// Or concatenates more than one WhereConds with OR operator.
func Or(conds ...WhereCond) *LogicalOpCond {
return &LogicalOpCond{
op: logicalOpOr,
conds: conds,
}
}
func (c *LogicalOpCond) ToASTWhere() (*ast.Where, error) {
if len(c.conds) <= 0 {
return nil, errors.New("no conditions")
}
where, err := c.conds[0].ToASTWhere()
if err != nil {
return nil, err
}
acc := where
for _, cond := range c.conds[1:] {
where, err = cond.ToASTWhere()
if err != nil {
return nil, err
}
acc = &ast.Where{
Expr: &ast.BinaryExpr{
Op: ast.BinaryOp(c.op),
Left: acc.Expr,
Right: where.Expr,
},
}
}
return acc, nil
}