forked from ulule/loukoum
/
expression.go
417 lines (358 loc) · 10.6 KB
/
expression.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package stmt
import (
"database/sql/driver"
"fmt"
"time"
"github.com/ulule/loukoum/types"
)
// Expression is a SQL expression.
type Expression interface {
Statement
expression()
}
// NewExpression returns a new Expression instance from arg.
func NewExpression(arg interface{}) Expression { // nolint: gocyclo
if arg == nil {
return NewValue(nil)
}
switch value := arg.(type) {
case Expression:
return value
case string, bool, int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64, float32, float64, []byte:
return NewValue(value)
case time.Time:
return NewValue(value)
case *time.Time:
return NewValue(*value)
case driver.Valuer:
return NewValue(value)
case StatementEncoder:
stmt := value.Statement()
expression, ok := stmt.(Expression)
if !ok {
panic(fmt.Sprintf("cannot use {%+v}[%T] as loukoum Expression", value, value))
}
return expression
case Int64Encoder:
return NewValue(value.Int64())
case BoolEncoder:
return NewValue(value.Bool())
case TimeEncoder:
return NewValue(value.Time())
case StringEncoder:
return NewValue(value.String())
default:
panic(fmt.Sprintf("cannot use {%+v}[%T] as loukoum Expression", arg, arg))
}
}
// ----------------------------------------------------------------------------
// Identifier
// ----------------------------------------------------------------------------
// Identifier is an identifier.
type Identifier struct {
Identifier string
}
// NewIdentifier returns a new Identifier.
func NewIdentifier(identifier string) Identifier {
return Identifier{
Identifier: identifier,
}
}
func (Identifier) expression() {}
// Write exposes statement as a SQL query.
func (identifier Identifier) Write(ctx types.Context) {
ctx.Write(identifier.Identifier)
}
// IsEmpty returns true if statement is undefined.
func (identifier Identifier) IsEmpty() bool {
return identifier.Identifier == ""
}
// Equal performs an "equal" comparison.
func (identifier Identifier) Equal(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.Equal)
return NewInfixExpression(identifier, operator, NewWrapper(NewExpression(value)))
}
// NotEqual performs a "not equal" comparison.
func (identifier Identifier) NotEqual(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.NotEqual)
return NewInfixExpression(identifier, operator, NewWrapper(NewExpression(value)))
}
// Is performs a "is" comparison.
func (identifier Identifier) Is(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.Is)
return NewInfixExpression(identifier, operator, NewExpression(value))
}
// IsNot performs a "is not" comparison.
func (identifier Identifier) IsNot(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.IsNot)
return NewInfixExpression(identifier, operator, NewExpression(value))
}
// IsNull performs a "is null" comparison.
func (identifier Identifier) IsNull(value bool) InfixExpression {
if value {
return identifier.Is(nil)
}
return identifier.IsNot(nil)
}
// GreaterThan performs a "greater than" comparison.
func (identifier Identifier) GreaterThan(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.GreaterThan)
return NewInfixExpression(identifier, operator, NewWrapper(NewExpression(value)))
}
// GreaterThanOrEqual performs a "greater than or equal to" comparison.
func (identifier Identifier) GreaterThanOrEqual(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.GreaterThanOrEqual)
return NewInfixExpression(identifier, operator, NewWrapper(NewExpression(value)))
}
// LessThan performs a "less than" comparison.
func (identifier Identifier) LessThan(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.LessThan)
return NewInfixExpression(identifier, operator, NewWrapper(NewExpression(value)))
}
// LessThanOrEqual performs a "less than or equal to" comparison.
func (identifier Identifier) LessThanOrEqual(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.LessThanOrEqual)
return NewInfixExpression(identifier, operator, NewWrapper(NewExpression(value)))
}
// In performs a "in" condition.
func (identifier Identifier) In(value ...interface{}) In {
return NewIn(identifier, NewArrayExpression(value...))
}
// NotIn performs a "not in" condition.
func (identifier Identifier) NotIn(value ...interface{}) In {
return NewNotIn(identifier, NewArrayExpression(value...))
}
// Like performs a "like" condition.
func (identifier Identifier) Like(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.Like)
return NewInfixExpression(identifier, operator, NewExpression(value))
}
// NotLike performs a "not like" condition.
func (identifier Identifier) NotLike(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.NotLike)
return NewInfixExpression(identifier, operator, NewExpression(value))
}
// ILike performs a "ilike" condition.
func (identifier Identifier) ILike(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.ILike)
return NewInfixExpression(identifier, operator, NewExpression(value))
}
// NotILike performs a "not ilike" condition.
func (identifier Identifier) NotILike(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.NotILike)
return NewInfixExpression(identifier, operator, NewExpression(value))
}
// Between performs a "between" condition.
func (identifier Identifier) Between(from, to interface{}) Between {
return NewBetween(identifier, NewExpression(from), NewExpression(to))
}
// NotBetween performs a "not between" condition.
func (identifier Identifier) NotBetween(from, to interface{}) Between {
return NewNotBetween(identifier, NewExpression(from), NewExpression(to))
}
// Ensure that Identifier is an Expression
var _ Expression = Identifier{}
// ----------------------------------------------------------------------------
// Value
// ----------------------------------------------------------------------------
// Value is an expression value.
type Value struct {
Value interface{}
}
// NewValue returns an expression value.
func NewValue(value interface{}) Value {
return Value{
Value: value,
}
}
func (Value) expression() {}
// Write exposes statement as a SQL query.
func (value Value) Write(ctx types.Context) {
if value.Value == nil {
ctx.Write("NULL")
} else {
ctx.Bind(value.Value)
}
}
// IsEmpty returns true if statement is undefined.
func (value Value) IsEmpty() bool {
return false
}
// Ensure that Value is an Expression
var _ Expression = Value{}
// ----------------------------------------------------------------------------
// Array
// ----------------------------------------------------------------------------
// Array contains a list of expression values.
type Array struct {
Values []Expression
}
// NewArrayExpression creates a new Expression using a list of values.
func NewArrayExpression(values ...interface{}) Expression { // nolint: gocyclo
// We pass only one argument and it's a slice or an expression.
if len(values) == 1 {
return toArray(values[0])
}
array := Array{}
for _, value := range values {
array.Append(value)
}
return array
}
// toArray tries to cast the value to a slice.
// It returns a single element Array otherwise.
func toArray(value interface{}) Array { // nolint: gocyclo
array := Array{}
if value == nil {
return array
}
switch values := value.(type) {
case []string:
for _, v := range values {
array.Append(v)
}
case []int:
for _, v := range values {
array.Append(v)
}
case []uint:
for _, v := range values {
array.Append(v)
}
case []int8:
for _, v := range values {
array.Append(v)
}
case []int16:
for _, v := range values {
array.Append(v)
}
case []uint16:
for _, v := range values {
array.Append(v)
}
case []int32:
for _, v := range values {
array.Append(v)
}
case []uint32:
for _, v := range values {
array.Append(v)
}
case []int64:
for _, v := range values {
array.Append(v)
}
case []uint64:
for _, v := range values {
array.Append(v)
}
case []bool:
for _, v := range values {
array.Append(v)
}
case []float32:
for _, v := range values {
array.Append(v)
}
case []float64:
for _, v := range values {
array.Append(v)
}
case [][]byte:
for _, v := range values {
array.Append(v)
}
case []Expression:
for _, v := range values {
array.Append(v)
}
case []interface{}:
for _, v := range values {
array.Append(v)
}
default:
array.Append(value)
}
return array
}
func (Array) expression() {}
// Write exposes statement as a SQL query.
func (array Array) Write(ctx types.Context) {
for i, value := range array.Values {
if i > 0 {
ctx.Write(", ")
}
value.Write(ctx)
}
}
// IsEmpty returns true if statement is undefined.
func (array Array) IsEmpty() bool {
return len(array.Values) == 0
}
// Append an expression to the given array.
func (array *Array) Append(value interface{}) {
array.Values = append(array.Values, NewExpression(value))
}
// Ensure that Array is an Expression
var _ Expression = Array{}
// ----------------------------------------------------------------------------
// Raw
// ----------------------------------------------------------------------------
// Raw is a raw expression value.
type Raw struct {
Value string
}
// NewRaw returns a raw expression value.
func NewRaw(value string) Raw {
return Raw{
Value: value,
}
}
func (Raw) expression() {}
// Write exposes statement as a SQL query.
func (raw Raw) Write(ctx types.Context) {
ctx.Write(raw.Value)
}
// IsEmpty returns true if statement is undefined.
func (raw Raw) IsEmpty() bool {
return raw.Value == ""
}
// Ensure that Raw is an Expression
var _ Expression = Raw{}
// ----------------------------------------------------------------------------
// Wrapper
// ----------------------------------------------------------------------------
// Wrapper encapsulates an expression between parenthesis.
type Wrapper struct {
Value Expression
}
// NewWrapper returns a new Wrapper expression when it's required.
func NewWrapper(arg Expression) Expression {
switch value := arg.(type) {
case Select:
return &Wrapper{
Value: value,
}
case Exists:
return &Wrapper{
Value: value,
}
default:
return arg
}
}
func (Wrapper) expression() {}
// Write exposes statement as a SQL query.
func (wrapper Wrapper) Write(ctx types.Context) {
ctx.Write("(")
wrapper.Value.Write(ctx)
ctx.Write(")")
}
// IsEmpty returns true if statement is undefined.
func (wrapper Wrapper) IsEmpty() bool {
return wrapper.Value.IsEmpty()
}
// Ensure that Wrapper is an Expression
var _ Expression = Wrapper{}