-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathconditions.go
785 lines (666 loc) · 20.9 KB
/
conditions.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlchemy
import (
"bytes"
"fmt"
"yunion.io/x/pkg/util/reflectutils"
)
type sNoop struct{}
var noop = &sNoop{}
func (s sNoop) WhereClause() string {
return ""
}
func (s sNoop) Variables() []interface{} {
return nil
}
func (s sNoop) database() *SDatabase {
return nil
}
func Noop() ICondition {
return noop
}
// ICondition is the interface representing a condition for SQL query
// e.g. WHERE a1 = b1 is a condition of equal
// the condition support nested condition, with AND, OR and NOT boolean operators
type ICondition interface {
WhereClause() string
Variables() []interface{}
database() *SDatabase
}
// SCompoundConditions is a Compound condition represents AND or OR boolean operation
// Compound condition also follows the ICondition interface
type SCompoundConditions struct {
conditions []ICondition
}
func compoundWhereClause(c *SCompoundConditions, op string) string {
var buf bytes.Buffer
for _, cond := range c.conditions {
if buf.Len() > 0 {
buf.WriteByte(' ')
buf.WriteString(op)
buf.WriteByte(' ')
}
buf.WriteByte('(')
buf.WriteString(cond.WhereClause())
buf.WriteByte(')')
}
return buf.String()
}
// WhereClause implementation of SCompoundConditions for ICondition
func (c *SCompoundConditions) WhereClause() string {
return ""
}
// Variables implementation of SCompoundConditions for ICondition
func (c *SCompoundConditions) Variables() []interface{} {
vars := make([]interface{}, 0)
for _, cond := range c.conditions {
nvars := cond.Variables()
if len(nvars) > 0 {
vars = append(vars, nvars...)
}
}
return vars
}
// database implementation of SCompoundConditions for ICondition
func (c *SCompoundConditions) database() *SDatabase {
for _, c := range c.conditions {
db := c.database()
if db != nil {
return db
}
}
return nil
}
// SAndConditions represents the AND condition, which is a SCompoundConditions
type SAndConditions struct {
SCompoundConditions
}
// WhereClause implementation of SAndConditions for IConditionq
func (c *SAndConditions) WhereClause() string {
return compoundWhereClause(&c.SCompoundConditions, SQL_OP_AND)
}
// SOrConditions represents the OR condition, which is a SCompoundConditions
type SOrConditions struct {
SCompoundConditions
}
// WhereClause implementation of SOrConditions for ICondition
func (c *SOrConditions) WhereClause() string {
return compoundWhereClause(&c.SCompoundConditions, SQL_OP_OR)
}
// AND method that combines many conditions with AND operator
func AND(cond ...ICondition) ICondition {
conds := make([]ICondition, 0)
for _, c := range cond {
if c == nil || c == noop {
continue
}
andCond, ok := c.(*SAndConditions)
if ok {
conds = append(conds, andCond.conditions...)
} else {
conds = append(conds, c)
}
}
cc := SAndConditions{SCompoundConditions{conditions: conds}}
return &cc
}
// OR method that combines many conditions with OR operator
func OR(cond ...ICondition) ICondition {
conds := make([]ICondition, 0)
for _, c := range cond {
if c == nil || c == noop {
conds = conds[0:0]
break
}
orCond, ok := c.(*SOrConditions)
if ok {
conds = append(conds, orCond.conditions...)
} else {
conds = append(conds, c)
}
}
cc := SOrConditions{SCompoundConditions{conditions: conds}}
return &cc
}
// SNotCondition represents the NOT condition, which is a boolean operator
type SNotCondition struct {
condition ICondition
}
// WhereClause implementationq of SNotCondition for ICondition
func (c *SNotCondition) WhereClause() string {
if c.condition == nil || c.condition == noop {
return "1!=1"
}
return fmt.Sprintf("%s (%s)", SQL_OP_NOT, c.condition.WhereClause())
}
// Variables implementation of SNotCondition for ICondition
func (c *SNotCondition) Variables() []interface{} {
if c.condition == nil {
return nil
}
return c.condition.Variables()
}
// database implementation of SNotCondition for ICondition
func (c *SNotCondition) database() *SDatabase {
if c.condition == nil {
return nil
}
return c.condition.database()
}
// NOT method that makes negative operator on a condition
func NOT(cond ICondition) ICondition {
cc := SNotCondition{condition: cond}
return &cc
}
// SSingleCondition represents a kind of condition that composed of one query field
type SSingleCondition struct {
field IQueryField
}
// Variables implementation of SSingleCondition for ICondition
func (c *SSingleCondition) Variables() []interface{} {
return c.field.Variables()
}
// database implementation of SSingleCondition for ICondition
func (c *SSingleCondition) database() *SDatabase {
return c.field.database()
}
// NewSingleCondition returns an instance of SSingleCondition
func NewSingleCondition(field IQueryField) SSingleCondition {
return SSingleCondition{field: field}
}
// SIsNullCondition is a condition representing a comparison with null, e.g. a is null
type SIsNullCondition struct {
SSingleCondition
}
// WhereClause implementation for SIsNullCondition for ICondition
func (c *SIsNullCondition) WhereClause() string {
return fmt.Sprintf("%s IS NULL", c.field.Reference())
}
// IsNull methods that justifies a field is null
func IsNull(f IQueryField) ICondition {
c := SIsNullCondition{NewSingleCondition(f)}
return &c
}
// SIsNotNullCondition is a condition represents a comparison with not null, e.g. a is not null
type SIsNotNullCondition struct {
SSingleCondition
}
// WhereClause implementation of SIsNotNullCondition for ICondition
func (c *SIsNotNullCondition) WhereClause() string {
return fmt.Sprintf("%s IS NOT NULL", c.field.Reference())
}
// IsNotNull methods that justifies a field is not null
func IsNotNull(f IQueryField) ICondition {
c := SIsNotNullCondition{NewSingleCondition(f)}
return &c
}
// SIsEmptyCondition is a condition representing the empty status of a field
type SIsEmptyCondition struct {
SSingleCondition
}
// WhereClause implementation of SIsEmptyCondition for ICondition
func (c *SIsEmptyCondition) WhereClause() string {
// DAMENG LENGTH('') = NULL
return fmt.Sprintf("%s = 0 OR %s IS NULL", c.field.Reference(), c.field.Reference())
}
// IsEmpty method that justifies where a text field is empty, e.g. length is zero
func IsEmpty(f IQueryField) ICondition {
c := SIsEmptyCondition{NewSingleCondition(LENGTH("", f))}
return &c
}
// SIsNullOrEmptyCondition is a condition that justifies a field is null or empty
type SIsNullOrEmptyCondition struct {
SSingleCondition
}
// WhereClause implementation of SIsNullOrEmptyCondition for ICondition
func (c *SIsNullOrEmptyCondition) WhereClause() string {
originField := c.field.(*SFunctionFieldBase).queryFields()[0]
// DAMENG: LENGTH('') = NULL
return fmt.Sprintf("%s IS NULL OR %s = 0 OR %s IS NULL", originField.Reference(), c.field.Reference(), c.field.Reference())
}
// IsNullOrEmpty is the ethod justifies a field is null or empty, e.g. a is null or length(a) == 0
func IsNullOrEmpty(f IQueryField) ICondition {
c := SIsNullOrEmptyCondition{NewSingleCondition(LENGTH("", f))}
return &c
}
// SIsNotEmptyCondition represents a condition that represents a field is not empty
type SIsNotEmptyCondition struct {
SSingleCondition
}
// WhereClause implementation of SIsNotEmptyCondition for ICondition
func (c *SIsNotEmptyCondition) WhereClause() string {
originField := c.field.(*SFunctionFieldBase).queryFields()[0]
return fmt.Sprintf("%s IS NOT NULL AND %s > 0", originField.Reference(), c.field.Reference())
}
// IsNotEmpty method justifies a field is not empty
func IsNotEmpty(f IQueryField) ICondition {
c := SIsNotEmptyCondition{NewSingleCondition(LENGTH("", f))}
return &c
}
// SIsTrueCondition represents a boolean field (TINYINT) is true, e.g. a == 1
type SIsTrueCondition struct {
SSingleCondition
}
// WhereClause implementation of SIsTrueCondition for ICondition
func (c *SIsTrueCondition) WhereClause() string {
return fmt.Sprintf("%s = 1", c.field.Reference())
}
// IsTrue method that justifies a field is true, e.g. field == 1
func IsTrue(f IQueryField) ICondition {
c := SIsTrueCondition{NewSingleCondition(f)}
return &c
}
// SIsFalseCondition represents a boolean is false
type SIsFalseCondition struct {
SSingleCondition
}
// WhereClause implementation of SIsFalseCondition for ICondition
func (c *SIsFalseCondition) WhereClause() string {
return fmt.Sprintf("%s = 0", c.field.Reference())
}
// IsFalse method justifies a boolean is false
func IsFalse(f IQueryField) ICondition {
c := SIsFalseCondition{NewSingleCondition(f)}
return &c
}
// SNoLaterThanCondition compares a DATETIME field with current time and ensure the field is no later than now, e.g. a <= NOW()
type SNoLaterThanCondition struct {
SSingleCondition
}
// WhereClause implementation of SNoLaterThanCondition for ICondition
func (c *SNoLaterThanCondition) WhereClause() string {
nowStr := c.field.database().backend.CurrentTimeStampString()
return fmt.Sprintf("%s <= %s", c.field.Reference(), nowStr)
}
// NoLaterThan method justifies a DATETIME field is before current time
func NoLaterThan(f IQueryField) ICondition {
c := SNoLaterThanCondition{NewSingleCondition(f)}
return &c
}
// SNoEarlierThanCondition compares a field with current time and ensure the field is no earlier than NOW, e.g. a >= NOW()
type SNoEarlierThanCondition struct {
SSingleCondition
}
// WhereClause implementation of SNoEarlierThanCondition for ICondition
func (c *SNoEarlierThanCondition) WhereClause() string {
nowStr := c.field.database().backend.CurrentTimeStampString()
return fmt.Sprintf("%s >= %s", c.field.Reference(), nowStr)
}
// NoEarlierThan justifies a field is no earlier than current time
func NoEarlierThan(f IQueryField) ICondition {
c := SNoEarlierThanCondition{NewSingleCondition(f)}
return &c
}
// STupleCondition is a base condition that composed of two fields
type STupleCondition struct {
left IQueryField
right interface{}
}
func tupleConditionWhereClause(t *STupleCondition, op string) string {
return tupleConditionWhereClauseInternal(t, op, "")
}
func TupleConditionWhereClauseWithFuncname(t *STupleCondition, funcName string) string {
return tupleConditionWhereClauseInternal(t, ",", funcName)
}
func tupleConditionWhereClauseInternal(t *STupleCondition, op string, funcName string) string {
if isFieldRequireAscii(t.left) && !isVariableAscii(t.right) {
return "0"
}
var buf bytes.Buffer
if len(funcName) > 0 {
buf.WriteString(funcName)
buf.WriteByte('(')
}
buf.WriteString(t.left.Reference())
buf.WriteByte(' ')
buf.WriteString(op)
buf.WriteByte(' ')
buf.WriteString(VarConditionWhereClause(t.right))
if len(funcName) > 0 {
buf.WriteByte(')')
}
return buf.String()
}
func questionMark(count int) string {
if count == 0 {
return ""
} else if count == 1 {
return " ? "
} else {
var buf bytes.Buffer
buf.WriteString("( ")
for i := 0; i < count; i++ {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString("?")
}
buf.WriteString(" )")
return buf.String()
}
}
func VarConditionWhereClause(v interface{}) string {
switch q := v.(type) {
case IQueryField:
return q.Reference()
case *SQuery:
return fmt.Sprintf("(%s)", q.String())
case *SSubQuery:
return q.Expression()
default:
expandV := reflectutils.ExpandInterface(v)
return questionMark(len(expandV))
}
}
func varConditionVariables(v interface{}) []interface{} {
switch vv := v.(type) {
case IQueryField:
return []interface{}{}
case *SQuery:
return vv.Variables()
case *SSubQuery:
return vv.query.Variables()
default:
return reflectutils.ExpandInterface(v)
}
}
// NewTupleCondition returns an instance of tuple condition
func NewTupleCondition(l IQueryField, r interface{}) STupleCondition {
return STupleCondition{left: l, right: r}
}
func (t *STupleCondition) GetLeft() IQueryField {
return t.left
}
func (t *STupleCondition) GetRight() interface{} {
return t.right
}
// Variables implementation of STupleCondition for ICondition
func (t *STupleCondition) Variables() []interface{} {
if isFieldRequireAscii(t.left) && !isVariableAscii(t.right) {
return []interface{}{}
}
vars := varConditionVariables(t.right)
for i := range vars {
vars[i] = t.left.ConvertFromValue(vars[i])
}
return vars
}
// database implementation of STupleCondition for ICondition
func (t *STupleCondition) database() *SDatabase {
return t.left.database()
}
// SInCondition represents a IN operation in SQL query
type SInCondition struct {
STupleCondition
op string
}
func inConditionWhereClause(t *STupleCondition, op string) string {
v := VarConditionWhereClause(t.right)
if len(v) != 0 {
return tupleConditionWhereClause(t, op)
}
return "0"
}
// WhereClause implementation of SInCondition for ICondition
func (t *SInCondition) WhereClause() string {
return inConditionWhereClause(&t.STupleCondition, t.op)
}
// In SQL operator
func In(f IQueryField, v interface{}) ICondition {
switch v.(type) {
case IQueryField, *SQuery, *SSubQuery:
default:
expandV := reflectutils.ExpandInterface(v)
switch len(expandV) {
case 0:
return &SFalseCondition{}
case 1:
return Equals(f, expandV[0])
default:
}
}
c := SInCondition{
NewTupleCondition(f, v),
SQL_OP_IN,
}
return &c
}
// NotIn SQL operator
func NotIn(f IQueryField, v interface{}) ICondition {
switch v.(type) {
case IQueryField, *SQuery, *SSubQuery:
default:
expandV := reflectutils.ExpandInterface(v)
switch len(expandV) {
case 0:
return &STrueCondition{}
case 1:
return NotEquals(f, expandV[0])
default:
}
}
c := SInCondition{
NewTupleCondition(f, v),
SQL_OP_NOTIN,
}
return &c
}
// SLikeCondition represents LIKE operation in a SQL query
type SLikeCondition struct {
STupleCondition
}
func likeEscape(s string) string {
var res bytes.Buffer
for i := 0; i < len(s); i++ {
if s[i] == '_' || s[i] == '%' {
res.WriteByte('\\')
}
res.WriteByte(s[i])
}
return res.String()
}
// WhereClause implementation for SLikeCondition for ICondition
func (t *SLikeCondition) WhereClause() string {
op := t.left.database().backend.CaseInsensitiveLikeString()
return tupleConditionWhereClause(&t.STupleCondition, op)
}
// Like SQL operator
func Like(f IQueryField, v string) ICondition {
c := SLikeCondition{NewTupleCondition(f, v)}
return &c
}
// SRegexpConition represents REGEXP operation in a SQL query
type SRegexpConition struct {
STupleCondition
}
// WhereClause implementation for SRegexpConition for ICondition
func (t *SRegexpConition) WhereClause() string {
return t.left.database().backend.RegexpWhereClause(t)
}
// Regexp SQL operator
func Regexp(f IQueryField, v string) ICondition {
c := SRegexpConition{NewTupleCondition(f, v)}
return &c
}
// ContainsAny is a OR combination of serveral Contains conditions
func ContainsAny(f IQueryField, v []string) ICondition {
conds := make([]ICondition, len(v))
for i := range v {
conds[i] = Contains(f, v[i])
}
return OR(conds...)
}
// Contains method is a shortcut of LIKE method, Contains represents the condtion that a field contains a substring
func Contains(f IQueryField, v string) ICondition {
v = likeEscape(v)
nv := fmt.Sprintf("%%%s%%", v)
return Like(f, nv)
}
// Startswith method is a shortcut of LIKE method, Startswith represents the condition that field starts with a substring
func Startswith(f IQueryField, v string) ICondition {
v = likeEscape(v)
nv := fmt.Sprintf("%s%%", v)
return Like(f, nv)
}
// Endswith method is a shortcut of LIKE condition, Endswith represents that condition that field endswith a substring
func Endswith(f IQueryField, v string) ICondition {
v = likeEscape(v)
nv := fmt.Sprintf("%%%s", v)
return Like(f, nv)
}
// SEqualsCondition represents equal operation between two fields
type SEqualsCondition struct {
STupleCondition
}
// WhereClause implementation of SEqualsCondition for ICondition
func (t *SEqualsCondition) WhereClause() string {
return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_EQUAL)
}
// Equals method represents equal of two fields
func Equals(f IQueryField, v interface{}) ICondition {
return f.database().backend.Equals(f, v)
}
// SNotEqualsCondition is the opposite of equal condition
type SNotEqualsCondition struct {
STupleCondition
}
// WhereClause implementation of SNotEqualsCondition for ICondition
func (t *SNotEqualsCondition) WhereClause() string {
return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_NOTEQUAL)
}
// NotEquals method represents not equal of two fields
func NotEquals(f IQueryField, v interface{}) ICondition {
c := SNotEqualsCondition{NewTupleCondition(f, v)}
return &c
}
// SGreatEqualCondition represents >= operation on two fields
type SGreatEqualCondition struct {
STupleCondition
}
// WhereClause implementation of SGreatEqualCondition for ICondition
func (t *SGreatEqualCondition) WhereClause() string {
return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_GE)
}
// GE method represetns operation of Greate Than Or Equal to, e.g. a >= b
func GE(f IQueryField, v interface{}) ICondition {
c := SGreatEqualCondition{NewTupleCondition(f, v)}
return &c
}
// SGreatThanCondition represetns > operation on two fields
type SGreatThanCondition struct {
STupleCondition
}
// WhereClause implementation of SGreatThanCondition for ICondition
func (t *SGreatThanCondition) WhereClause() string {
return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_GT)
}
// GT method represents operation of Great Than, e.g. a > b
func GT(f IQueryField, v interface{}) ICondition {
c := SGreatThanCondition{NewTupleCondition(f, v)}
return &c
}
// SLessEqualCondition represents <= operation on two fields
type SLessEqualCondition struct {
STupleCondition
}
// WhereClause implementation of SLessEqualCondition for ICondition
func (t *SLessEqualCondition) WhereClause() string {
return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_LE)
}
// LE method represents operation of Less Than Or Equal to, e.q. a <= b
func LE(f IQueryField, v interface{}) ICondition {
c := SLessEqualCondition{NewTupleCondition(f, v)}
return &c
}
// SLessThanCondition represents < operation on two fields
type SLessThanCondition struct {
STupleCondition
}
// WhereClause implementation of SLessThanCondition for ICondition
func (t *SLessThanCondition) WhereClause() string {
return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_LT)
}
// LT method represents operation of Less Than, e.g. a < b
func LT(f IQueryField, v interface{}) ICondition {
c := SLessThanCondition{NewTupleCondition(f, v)}
return &c
}
// STripleCondition represents a base condition that composed of THREE fields
type STripleCondition struct {
STupleCondition
right2 interface{}
}
// Variables implementation of STripleCondition for ICondition
func (t *STripleCondition) Variables() []interface{} {
ret := make([]interface{}, 0)
vars := varConditionVariables(t.right)
for i := range vars {
ret = append(ret, t.left.ConvertFromValue(vars[i]))
}
vars = varConditionVariables(t.right2)
for i := range vars {
ret = append(ret, t.left.ConvertFromValue(vars[i]))
}
return ret
}
// NewTripleCondition return an instance of STripleCondition
func NewTripleCondition(l IQueryField, r interface{}, r2 interface{}) STripleCondition {
return STripleCondition{STupleCondition: NewTupleCondition(l, r),
right2: r2}
}
// SBetweenCondition represents BETWEEN operator, e.g. c between a and b
type SBetweenCondition struct {
STripleCondition
}
// WhereClause implementation of SBetweenCondition for ICondition
func (t *SBetweenCondition) WhereClause() string {
ret := tupleConditionWhereClause(&t.STupleCondition, SQL_OP_BETWEEN)
return fmt.Sprintf("%s AND %s", ret, VarConditionWhereClause(t.right2))
}
// Between SQL operator
func Between(f IQueryField, r1, r2 interface{}) ICondition {
c := SBetweenCondition{NewTripleCondition(f, r1, r2)}
return &c
}
// STrueCondition represents a dummy condition that is always true
type STrueCondition struct{}
// WhereClause implementation of STrueCondition for ICondition
func (t *STrueCondition) WhereClause() string {
return "1"
}
// Variables implementation of STrueCondition for ICondition
func (t *STrueCondition) Variables() []interface{} {
return nil
}
func (t *STrueCondition) database() *SDatabase {
return nil
}
// SFalseCondition is a dummy condition that is always false
type SFalseCondition struct{}
// WhereClause implementation of SFalseCondition for ICondition
func (t *SFalseCondition) WhereClause() string {
return "0"
}
// Variables implementation of SFalseCondition for ICondition
func (t *SFalseCondition) Variables() []interface{} {
return nil
}
func (t *SFalseCondition) database() *SDatabase {
return nil
}
var (
AlwaysTrue = &STrueCondition{}
AlwaysFalse = &SFalseCondition{}
)