forked from huandu/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond.go
545 lines (482 loc) · 14.4 KB
/
cond.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
// Copyright 2018 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"github.com/lib/pq"
)
const (
lparen = "("
rparen = ")"
opOR = " OR "
opAND = " AND "
opNOT = "NOT "
opPGContains = " @> "
opPGOverlap = " && "
)
// Cond provides several helper methods to build conditions.
type Cond struct {
Args *Args
}
// NewCond returns a new Cond.
func NewCond() *Cond {
return &Cond{
Args: &Args{},
}
}
// Contains is used to present array comparisons using the containment operator.
// This is meant to express scenarios where the entire array provided must exist
// within the array field.
func (c *Cond) Contains(field string, values ...string) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(opPGContains)
ctx.WriteValue(pq.Array(values))
},
})
}
// Overlaps is used to present array comparisons using the overlap operator.
// This is meant to find any entries where any of the provided array exist in the
// database column
func (c *Cond) Overlaps(field string, values ...string) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(opPGOverlap)
ctx.WriteValue(pq.Array(values))
},
})
}
// Equal is used to construct the expression "field = value".
func (c *Cond) Equal(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" = ")
ctx.WriteValue(value)
},
})
}
// E is an alias of Equal.
func (c *Cond) E(field string, value interface{}) string {
return c.Equal(field, value)
}
// EQ is an alias of Equal.
func (c *Cond) EQ(field string, value interface{}) string {
return c.Equal(field, value)
}
// NotEqual is used to construct the expression "field <> value".
func (c *Cond) NotEqual(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" <> ")
ctx.WriteValue(value)
},
})
}
// NE is an alias of NotEqual.
func (c *Cond) NE(field string, value interface{}) string {
return c.NotEqual(field, value)
}
// NEQ is an alias of NotEqual.
func (c *Cond) NEQ(field string, value interface{}) string {
return c.NotEqual(field, value)
}
// GreaterThan is used to construct the expression "field > value".
func (c *Cond) GreaterThan(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" > ")
ctx.WriteValue(value)
},
})
}
// G is an alias of GreaterThan.
func (c *Cond) G(field string, value interface{}) string {
return c.GreaterThan(field, value)
}
// GT is an alias of GreaterThan.
func (c *Cond) GT(field string, value interface{}) string {
return c.GreaterThan(field, value)
}
// GreaterEqualThan is used to construct the expression "field >= value".
func (c *Cond) GreaterEqualThan(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" >= ")
ctx.WriteValue(value)
},
})
}
// GE is an alias of GreaterEqualThan.
func (c *Cond) GE(field string, value interface{}) string {
return c.GreaterEqualThan(field, value)
}
// GTE is an alias of GreaterEqualThan.
func (c *Cond) GTE(field string, value interface{}) string {
return c.GreaterEqualThan(field, value)
}
// LessThan is used to construct the expression "field < value".
func (c *Cond) LessThan(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" < ")
ctx.WriteValue(value)
},
})
}
// L is an alias of LessThan.
func (c *Cond) L(field string, value interface{}) string {
return c.LessThan(field, value)
}
// LT is an alias of LessThan.
func (c *Cond) LT(field string, value interface{}) string {
return c.LessThan(field, value)
}
// LessEqualThan is used to construct the expression "field <= value".
func (c *Cond) LessEqualThan(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" <= ")
ctx.WriteValue(value)
},
})
}
// LE is an alias of LessEqualThan.
func (c *Cond) LE(field string, value interface{}) string {
return c.LessEqualThan(field, value)
}
// LTE is an alias of LessEqualThan.
func (c *Cond) LTE(field string, value interface{}) string {
return c.LessEqualThan(field, value)
}
// In is used to construct the expression "field IN (value...)".
func (c *Cond) In(field string, values ...interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" IN (")
ctx.WriteValues(values, ", ")
ctx.WriteString(")")
},
})
}
// NotIn is used to construct the expression "field NOT IN (value...)".
func (c *Cond) NotIn(field string, values ...interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" NOT IN (")
ctx.WriteValues(values, ", ")
ctx.WriteString(")")
},
})
}
// Like is used to construct the expression "field LIKE value".
func (c *Cond) Like(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" LIKE ")
ctx.WriteValue(value)
},
})
}
// ILike is used to construct the expression "field ILIKE value".
//
// When the database system does not support the ILIKE operator,
// the ILike method will return "LOWER(field) LIKE LOWER(value)"
// to simulate the behavior of the ILIKE operator.
func (c *Cond) ILike(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
switch ctx.Flavor {
case PostgreSQL, SQLite:
ctx.WriteString(field)
ctx.WriteString(" ILIKE ")
ctx.WriteValue(value)
default:
// Use LOWER to simulate ILIKE.
ctx.WriteString("LOWER(")
ctx.WriteString(field)
ctx.WriteString(") LIKE LOWER(")
ctx.WriteValue(value)
ctx.WriteString(")")
}
},
})
}
// NotLike is used to construct the expression "field NOT LIKE value".
func (c *Cond) NotLike(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" NOT LIKE ")
ctx.WriteValue(value)
},
})
}
// NotILike is used to construct the expression "field NOT ILIKE value".
//
// When the database system does not support the ILIKE operator,
// the NotILike method will return "LOWER(field) NOT LIKE LOWER(value)"
// to simulate the behavior of the ILIKE operator.
func (c *Cond) NotILike(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
switch ctx.Flavor {
case PostgreSQL, SQLite:
ctx.WriteString(field)
ctx.WriteString(" NOT ILIKE ")
ctx.WriteValue(value)
default:
// Use LOWER to simulate ILIKE.
ctx.WriteString("LOWER(")
ctx.WriteString(field)
ctx.WriteString(") NOT LIKE LOWER(")
ctx.WriteValue(value)
ctx.WriteString(")")
}
},
})
}
// IsNull is used to construct the expression "field IS NULL".
func (c *Cond) IsNull(field string) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" IS NULL")
},
})
}
// IsNotNull is used to construct the expression "field IS NOT NULL".
func (c *Cond) IsNotNull(field string) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" IS NOT NULL")
},
})
}
// Between is used to construct the expression "field BETWEEN lower AND upper".
func (c *Cond) Between(field string, lower, upper interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" BETWEEN ")
ctx.WriteValue(lower)
ctx.WriteString(" AND ")
ctx.WriteValue(upper)
},
})
}
// NotBetween is used to construct the expression "field NOT BETWEEN lower AND upper".
func (c *Cond) NotBetween(field string, lower, upper interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" NOT BETWEEN ")
ctx.WriteValue(lower)
ctx.WriteString(" AND ")
ctx.WriteValue(upper)
},
})
}
// Or is used to construct the expression OR logic like "expr1 OR expr2 OR expr3".
func (c *Cond) Or(orExpr ...string) string {
if len(orExpr) == 0 {
return ""
}
buf := newStringBuilder()
// Ensure that there is only 1 memory allocation.
size := len(lparen) + len(rparen) + (len(orExpr)-1)*len(opOR) + estimateStringsBytes(orExpr)
buf.Grow(size)
buf.WriteString(lparen)
buf.WriteStrings(orExpr, opOR)
buf.WriteString(rparen)
return buf.String()
}
// And is used to construct the expression AND logic like "expr1 AND expr2 AND expr3".
func (c *Cond) And(andExpr ...string) string {
if len(andExpr) == 0 {
return ""
}
buf := newStringBuilder()
// Ensure that there is only 1 memory allocation.
size := len(lparen) + len(rparen) + (len(andExpr)-1)*len(opAND) + estimateStringsBytes(andExpr)
buf.Grow(size)
buf.WriteString(lparen)
buf.WriteStrings(andExpr, opAND)
buf.WriteString(rparen)
return buf.String()
}
// Not is used to construct the expression "NOT expr".
func (c *Cond) Not(notExpr string) string {
buf := newStringBuilder()
// Ensure that there is only 1 memory allocation.
size := len(opNOT) + len(notExpr)
buf.Grow(size)
buf.WriteString(opNOT)
buf.WriteString(notExpr)
return buf.String()
}
// Exists is used to construct the expression "EXISTS (subquery)".
func (c *Cond) Exists(subquery interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString("EXISTS (")
ctx.WriteValue(subquery)
ctx.WriteString(")")
},
})
}
// NotExists is used to construct the expression "NOT EXISTS (subquery)".
func (c *Cond) NotExists(subquery interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString("NOT EXISTS (")
ctx.WriteValue(subquery)
ctx.WriteString(")")
},
})
}
// Any is used to construct the expression "field op ANY (value...)".
func (c *Cond) Any(field, op string, values ...interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" ")
ctx.WriteString(op)
ctx.WriteString(" ANY (")
ctx.WriteValues(values, ", ")
ctx.WriteString(")")
},
})
}
// All is used to construct the expression "field op ALL (value...)".
func (c *Cond) All(field, op string, values ...interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" ")
ctx.WriteString(op)
ctx.WriteString(" ALL (")
ctx.WriteValues(values, ", ")
ctx.WriteString(")")
},
})
}
// Some is used to construct the expression "field op SOME (value...)".
func (c *Cond) Some(field, op string, values ...interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
ctx.WriteString(" ")
ctx.WriteString(op)
ctx.WriteString(" SOME (")
ctx.WriteValues(values, ", ")
ctx.WriteString(")")
},
})
}
// IsDistinctFrom is used to construct the expression "field IS DISTINCT FROM value".
//
// When the database system does not support the IS DISTINCT FROM operator,
// the NotILike method will return "NOT field <=> value" for MySQL or a
// "CASE ... WHEN ... ELSE ... END" expression to simulate the behavior of
// the IS DISTINCT FROM operator.
func (c *Cond) IsDistinctFrom(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
switch ctx.Flavor {
case PostgreSQL, SQLite, SQLServer:
ctx.WriteString(field)
ctx.WriteString(" IS DISTINCT FROM ")
ctx.WriteValue(value)
case MySQL:
ctx.WriteString("NOT ")
ctx.WriteString(field)
ctx.WriteString(" <=> ")
ctx.WriteValue(value)
default:
// CASE
// WHEN field IS NULL AND value IS NULL THEN 0
// WHEN field IS NOT NULL AND value IS NOT NULL AND field = value THEN 0
// ELSE 1
// END = 1
ctx.WriteString("CASE WHEN ")
ctx.WriteString(field)
ctx.WriteString(" IS NULL AND ")
ctx.WriteValue(value)
ctx.WriteString(" IS NULL THEN 0 WHEN ")
ctx.WriteString(field)
ctx.WriteString(" IS NOT NULL AND ")
ctx.WriteValue(value)
ctx.WriteString(" IS NOT NULL AND ")
ctx.WriteString(field)
ctx.WriteString(" = ")
ctx.WriteValue(value)
ctx.WriteString(" THEN 0 ELSE 1 END = 1")
}
},
})
}
// IsNotDistinctFrom is used to construct the expression "field IS NOT DISTINCT FROM value".
//
// When the database system does not support the IS NOT DISTINCT FROM operator,
// the NotILike method will return "field <=> value" for MySQL or a
// "CASE ... WHEN ... ELSE ... END" expression to simulate the behavior of
// the IS NOT DISTINCT FROM operator.
func (c *Cond) IsNotDistinctFrom(field string, value interface{}) string {
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
switch ctx.Flavor {
case PostgreSQL, SQLite, SQLServer:
ctx.WriteString(field)
ctx.WriteString(" IS NOT DISTINCT FROM ")
ctx.WriteValue(value)
case MySQL:
ctx.WriteString(field)
ctx.WriteString(" <=> ")
ctx.WriteValue(value)
default:
// CASE
// WHEN field IS NULL AND value IS NULL THEN 1
// WHEN field IS NOT NULL AND value IS NOT NULL AND field = value THEN 1
// ELSE 0
// END = 1
ctx.WriteString("CASE WHEN ")
ctx.WriteString(field)
ctx.WriteString(" IS NULL AND ")
ctx.WriteValue(value)
ctx.WriteString(" IS NULL THEN 1 WHEN ")
ctx.WriteString(field)
ctx.WriteString(" IS NOT NULL AND ")
ctx.WriteValue(value)
ctx.WriteString(" IS NOT NULL AND ")
ctx.WriteString(field)
ctx.WriteString(" = ")
ctx.WriteValue(value)
ctx.WriteString(" THEN 1 ELSE 0 END = 1")
}
},
})
}
// Var returns a placeholder for value.
func (c *Cond) Var(value interface{}) string {
return c.Args.Add(value)
}
type condBuilder struct {
Builder func(ctx *argsCompileContext)
}
func estimateStringsBytes(strs []string) (n int) {
for _, s := range strs {
n += len(s)
}
return
}