forked from ulule/loukoum
/
expression.go
553 lines (472 loc) · 14.3 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
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:
return NewValue(value)
case time.Time:
return NewValue(value)
case *time.Time:
return NewValue(*value)
case driver.Valuer:
return NewValueFromValuer(value)
case StatementEncoder:
return NewValue(value.Statement())
case Int64Encoder:
return NewValue(value.Int64())
case BoolEncoder:
return NewValue(value.Bool())
case TimeEncoder:
return NewValue(value.Time())
case StringEncoder:
return NewValue(value.String())
case []string:
return NewArrayString(value)
case []int:
return NewArrayInt(value)
case []uint:
return NewArrayUint(value)
case []int8:
return NewArrayInt8(value)
case []uint8:
return NewArrayUint8(value)
case []int16:
return NewArrayInt16(value)
case []uint16:
return NewArrayUint16(value)
case []int32:
return NewArrayInt32(value)
case []uint32:
return NewArrayUint32(value)
case []int64:
return NewArrayInt64(value)
case []uint64:
return NewArrayUint64(value)
case []bool:
return NewArrayBool(value)
case []float32:
return NewArrayFloat32(value)
case []float64:
return NewArrayFloat64(value)
default:
panic(fmt.Sprintf("cannot use {%+v}[%T] as loukoum Expression", value, value))
}
}
// 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 {
switch value := values[0].(type) {
case []string, []int, []uint, []int8, []uint8, []int16, []uint16,
[]int32, []uint32, []int64, []uint64, []bool, []float32, []float64:
return NewExpression(value)
case string, int, uint, int8, uint8, int16, uint16,
int32, uint32, int64, uint64, bool, float32, float64:
return NewExpression(value)
case time.Time, *time.Time:
return NewExpression(value)
case driver.Valuer:
return NewExpression(value)
case Select:
return NewExpression(value)
case StatementEncoder:
return NewExpression(value.Statement())
case Int64Encoder, BoolEncoder, TimeEncoder, StringEncoder:
return NewExpression(value)
default:
panic(fmt.Sprintf("cannot use {%+v}[%T] as loukoum Expression", value, value))
}
}
array := NewArray()
for i := range values {
switch value := values[i].(type) {
case string:
array.AddValue(NewValue(value))
case int:
array.AddValue(NewValue(value))
case int8:
array.AddValue(NewValue(value))
case int16:
array.AddValue(NewValue(value))
case int32:
array.AddValue(NewValue(value))
case int64:
array.AddValue(NewValue(value))
case uint:
array.AddValue(NewValue(value))
case uint8:
array.AddValue(NewValue(value))
case uint16:
array.AddValue(NewValue(value))
case uint32:
array.AddValue(NewValue(value))
case uint64:
array.AddValue(NewValue(value))
case bool:
array.AddValue(NewValue(value))
case float32:
array.AddValue(NewValue(value))
case float64:
array.AddValue(NewValue(value))
case time.Time:
array.AddValue(NewValue(value))
case *time.Time:
array.AddValue(NewValue(*value))
case driver.Valuer:
array.AddValue(NewValueFromValuer(value))
case Raw:
array.AddRaw(value)
case StatementEncoder:
array.AddValue(NewValue(value.Statement()))
case Int64Encoder:
array.AddValue(NewValue(value.Int64()))
case BoolEncoder:
array.AddValue(NewValue(value.Bool()))
case TimeEncoder:
array.AddValue(NewValue(value.Time()))
case StringEncoder:
array.AddValue(NewValue(value.String()))
default:
panic(fmt.Sprintf("cannot use {%+v}[%T] as loukoum Value", value, value))
}
}
return array
}
// ----------------------------------------------------------------------------
// 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, NewExpression(value))
}
// NotEqual performs a "not equal" comparison.
func (identifier Identifier) NotEqual(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.NotEqual)
return NewInfixExpression(identifier, operator, 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, 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, NewExpression(value))
}
// LessThan performs a "less than" comparison.
func (identifier Identifier) LessThan(value interface{}) InfixExpression {
operator := NewComparisonOperator(types.LessThan)
return NewInfixExpression(identifier, operator, 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, 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,
}
}
// NewValueFromValuer returns the underlying valuer value.
func NewValueFromValuer(valuer driver.Valuer) Value {
v, err := valuer.Value()
if err != nil {
panic("loukoum: was not able to retrieve valuer value")
}
if v == nil {
return NewValue(nil)
}
return NewValue(v)
}
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
}
// NewArray returns a an expression array.
func NewArray() Array {
return Array{}
}
// NewArrayString returns an expression array for "string" type.
func NewArrayString(values []string) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayInt returns an expression array for "int" type.
func NewArrayInt(values []int) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayInt8 returns an expression array for "int8" type.
func NewArrayInt8(values []int8) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayInt16 returns an expression array for "int16" type.
func NewArrayInt16(values []int16) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayInt32 returns an expression array for "int32" type.
func NewArrayInt32(values []int32) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayInt64 returns an expression array for "int64" type.
func NewArrayInt64(values []int64) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayUint returns an expression array for "uint" type.
func NewArrayUint(values []uint) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayUint8 returns an expression array for "uint8" type.
func NewArrayUint8(values []uint8) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayUint16 returns an expression array for "uint16" type.
func NewArrayUint16(values []uint16) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayUint32 returns an expression array for "uint32" type.
func NewArrayUint32(values []uint32) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayUint64 returns an expression array for "uint64" type.
func NewArrayUint64(values []uint64) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayBool returns an expression array for "bool" type.
func NewArrayBool(values []bool) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayFloat32 returns an expression array for "float32" type.
func NewArrayFloat32(values []float32) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
// NewArrayFloat64 returns an expression array for "float64" type.
func NewArrayFloat64(values []float64) Array {
array := NewArray()
for i := range values {
array.AddValue(NewValue(values[i]))
}
return array
}
func (Array) expression() {}
// Write exposes statement as a SQL query.
func (array Array) Write(ctx *types.Context) {
for i := range array.Values {
if i != 0 {
ctx.Write(", ")
}
array.Values[i].Write(ctx)
}
}
// IsEmpty returns true if statement is undefined.
func (array Array) IsEmpty() bool {
return len(array.Values) == 0
}
// AddValue appends a value to given array.
func (array *Array) AddValue(value Value) {
array.Values = append(array.Values, value)
}
// AddRaw appends a raw value to given array.
func (array *Array) AddRaw(value Raw) {
array.Values = append(array.Values, value)
}
// AddValues appends a collection of expression to given array.
func (array *Array) AddValues(values []Expression) {
array.Values = append(array.Values, values...)
}
// Ensure that Array is an Expression
var _ Expression = Array{}
// ----------------------------------------------------------------------------
// Raw
// ----------------------------------------------------------------------------
// Raw is an raw expression value.
type Raw struct {
Value string
}
// NewRaw returns an 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 false
}
// Ensure that Raw is an Expression
var _ Expression = Raw{}