@@ -25,6 +25,10 @@ func NewCond() *Cond {
25
25
26
26
// Equal is used to construct the expression "field = value".
27
27
func (c * Cond ) Equal (field string , value interface {}) string {
28
+ if len (field ) == 0 {
29
+ return ""
30
+ }
31
+
28
32
return c .Var (condBuilder {
29
33
Builder : func (ctx * argsCompileContext ) {
30
34
ctx .WriteString (field )
@@ -46,6 +50,10 @@ func (c *Cond) EQ(field string, value interface{}) string {
46
50
47
51
// NotEqual is used to construct the expression "field <> value".
48
52
func (c * Cond ) NotEqual (field string , value interface {}) string {
53
+ if len (field ) == 0 {
54
+ return ""
55
+ }
56
+
49
57
return c .Var (condBuilder {
50
58
Builder : func (ctx * argsCompileContext ) {
51
59
ctx .WriteString (field )
@@ -67,6 +75,10 @@ func (c *Cond) NEQ(field string, value interface{}) string {
67
75
68
76
// GreaterThan is used to construct the expression "field > value".
69
77
func (c * Cond ) GreaterThan (field string , value interface {}) string {
78
+ if len (field ) == 0 {
79
+ return ""
80
+ }
81
+
70
82
return c .Var (condBuilder {
71
83
Builder : func (ctx * argsCompileContext ) {
72
84
ctx .WriteString (field )
@@ -88,6 +100,10 @@ func (c *Cond) GT(field string, value interface{}) string {
88
100
89
101
// GreaterEqualThan is used to construct the expression "field >= value".
90
102
func (c * Cond ) GreaterEqualThan (field string , value interface {}) string {
103
+ if len (field ) == 0 {
104
+ return ""
105
+ }
106
+
91
107
return c .Var (condBuilder {
92
108
Builder : func (ctx * argsCompileContext ) {
93
109
ctx .WriteString (field )
@@ -109,6 +125,10 @@ func (c *Cond) GTE(field string, value interface{}) string {
109
125
110
126
// LessThan is used to construct the expression "field < value".
111
127
func (c * Cond ) LessThan (field string , value interface {}) string {
128
+ if len (field ) == 0 {
129
+ return ""
130
+ }
131
+
112
132
return c .Var (condBuilder {
113
133
Builder : func (ctx * argsCompileContext ) {
114
134
ctx .WriteString (field )
@@ -130,6 +150,9 @@ func (c *Cond) LT(field string, value interface{}) string {
130
150
131
151
// LessEqualThan is used to construct the expression "field <= value".
132
152
func (c * Cond ) LessEqualThan (field string , value interface {}) string {
153
+ if len (field ) == 0 {
154
+ return ""
155
+ }
133
156
return c .Var (condBuilder {
134
157
Builder : func (ctx * argsCompileContext ) {
135
158
ctx .WriteString (field )
@@ -151,6 +174,10 @@ func (c *Cond) LTE(field string, value interface{}) string {
151
174
152
175
// In is used to construct the expression "field IN (value...)".
153
176
func (c * Cond ) In (field string , values ... interface {}) string {
177
+ if len (field ) == 0 {
178
+ return ""
179
+ }
180
+
154
181
return c .Var (condBuilder {
155
182
Builder : func (ctx * argsCompileContext ) {
156
183
ctx .WriteString (field )
@@ -163,6 +190,10 @@ func (c *Cond) In(field string, values ...interface{}) string {
163
190
164
191
// NotIn is used to construct the expression "field NOT IN (value...)".
165
192
func (c * Cond ) NotIn (field string , values ... interface {}) string {
193
+ if len (field ) == 0 {
194
+ return ""
195
+ }
196
+
166
197
return c .Var (condBuilder {
167
198
Builder : func (ctx * argsCompileContext ) {
168
199
ctx .WriteString (field )
@@ -175,6 +206,10 @@ func (c *Cond) NotIn(field string, values ...interface{}) string {
175
206
176
207
// Like is used to construct the expression "field LIKE value".
177
208
func (c * Cond ) Like (field string , value interface {}) string {
209
+ if len (field ) == 0 {
210
+ return ""
211
+ }
212
+
178
213
return c .Var (condBuilder {
179
214
Builder : func (ctx * argsCompileContext ) {
180
215
ctx .WriteString (field )
@@ -190,6 +225,10 @@ func (c *Cond) Like(field string, value interface{}) string {
190
225
// the ILike method will return "LOWER(field) LIKE LOWER(value)"
191
226
// to simulate the behavior of the ILIKE operator.
192
227
func (c * Cond ) ILike (field string , value interface {}) string {
228
+ if len (field ) == 0 {
229
+ return ""
230
+ }
231
+
193
232
return c .Var (condBuilder {
194
233
Builder : func (ctx * argsCompileContext ) {
195
234
switch ctx .Flavor {
@@ -212,6 +251,10 @@ func (c *Cond) ILike(field string, value interface{}) string {
212
251
213
252
// NotLike is used to construct the expression "field NOT LIKE value".
214
253
func (c * Cond ) NotLike (field string , value interface {}) string {
254
+ if len (field ) == 0 {
255
+ return ""
256
+ }
257
+
215
258
return c .Var (condBuilder {
216
259
Builder : func (ctx * argsCompileContext ) {
217
260
ctx .WriteString (field )
@@ -227,6 +270,10 @@ func (c *Cond) NotLike(field string, value interface{}) string {
227
270
// the NotILike method will return "LOWER(field) NOT LIKE LOWER(value)"
228
271
// to simulate the behavior of the ILIKE operator.
229
272
func (c * Cond ) NotILike (field string , value interface {}) string {
273
+ if len (field ) == 0 {
274
+ return ""
275
+ }
276
+
230
277
return c .Var (condBuilder {
231
278
Builder : func (ctx * argsCompileContext ) {
232
279
switch ctx .Flavor {
@@ -249,6 +296,10 @@ func (c *Cond) NotILike(field string, value interface{}) string {
249
296
250
297
// IsNull is used to construct the expression "field IS NULL".
251
298
func (c * Cond ) IsNull (field string ) string {
299
+ if len (field ) == 0 {
300
+ return ""
301
+ }
302
+
252
303
return c .Var (condBuilder {
253
304
Builder : func (ctx * argsCompileContext ) {
254
305
ctx .WriteString (field )
@@ -259,6 +310,9 @@ func (c *Cond) IsNull(field string) string {
259
310
260
311
// IsNotNull is used to construct the expression "field IS NOT NULL".
261
312
func (c * Cond ) IsNotNull (field string ) string {
313
+ if len (field ) == 0 {
314
+ return ""
315
+ }
262
316
return c .Var (condBuilder {
263
317
Builder : func (ctx * argsCompileContext ) {
264
318
ctx .WriteString (field )
@@ -269,6 +323,10 @@ func (c *Cond) IsNotNull(field string) string {
269
323
270
324
// Between is used to construct the expression "field BETWEEN lower AND upper".
271
325
func (c * Cond ) Between (field string , lower , upper interface {}) string {
326
+ if len (field ) == 0 {
327
+ return ""
328
+ }
329
+
272
330
return c .Var (condBuilder {
273
331
Builder : func (ctx * argsCompileContext ) {
274
332
ctx .WriteString (field )
@@ -282,6 +340,10 @@ func (c *Cond) Between(field string, lower, upper interface{}) string {
282
340
283
341
// NotBetween is used to construct the expression "field NOT BETWEEN lower AND upper".
284
342
func (c * Cond ) NotBetween (field string , lower , upper interface {}) string {
343
+ if len (field ) == 0 {
344
+ return ""
345
+ }
346
+
285
347
return c .Var (condBuilder {
286
348
Builder : func (ctx * argsCompileContext ) {
287
349
ctx .WriteString (field )
@@ -299,10 +361,15 @@ func (c *Cond) Or(orExpr ...string) string {
299
361
return ""
300
362
}
301
363
364
+ exprByteLen := estimateStringsBytes (orExpr )
365
+ if exprByteLen == 0 {
366
+ return ""
367
+ }
368
+
302
369
buf := newStringBuilder ()
303
370
304
371
// Ensure that there is only 1 memory allocation.
305
- size := len (lparen ) + len (rparen ) + (len (orExpr )- 1 )* len (opOR ) + estimateStringsBytes ( orExpr )
372
+ size := len (lparen ) + len (rparen ) + (len (orExpr )- 1 )* len (opOR ) + exprByteLen
306
373
buf .Grow (size )
307
374
308
375
buf .WriteString (lparen )
@@ -317,10 +384,15 @@ func (c *Cond) And(andExpr ...string) string {
317
384
return ""
318
385
}
319
386
387
+ exprByteLen := estimateStringsBytes (andExpr )
388
+ if exprByteLen == 0 {
389
+ return ""
390
+ }
391
+
320
392
buf := newStringBuilder ()
321
393
322
394
// Ensure that there is only 1 memory allocation.
323
- size := len (lparen ) + len (rparen ) + (len (andExpr )- 1 )* len (opAND ) + estimateStringsBytes ( andExpr )
395
+ size := len (lparen ) + len (rparen ) + (len (andExpr )- 1 )* len (opAND ) + exprByteLen
324
396
buf .Grow (size )
325
397
326
398
buf .WriteString (lparen )
@@ -331,6 +403,9 @@ func (c *Cond) And(andExpr ...string) string {
331
403
332
404
// Not is used to construct the expression "NOT expr".
333
405
func (c * Cond ) Not (notExpr string ) string {
406
+ if len (notExpr ) == 0 {
407
+ return ""
408
+ }
334
409
buf := newStringBuilder ()
335
410
336
411
// Ensure that there is only 1 memory allocation.
@@ -366,6 +441,10 @@ func (c *Cond) NotExists(subquery interface{}) string {
366
441
367
442
// Any is used to construct the expression "field op ANY (value...)".
368
443
func (c * Cond ) Any (field , op string , values ... interface {}) string {
444
+ if len (field ) == 0 || len (op ) == 0 {
445
+ return ""
446
+ }
447
+
369
448
return c .Var (condBuilder {
370
449
Builder : func (ctx * argsCompileContext ) {
371
450
ctx .WriteString (field )
@@ -380,6 +459,10 @@ func (c *Cond) Any(field, op string, values ...interface{}) string {
380
459
381
460
// All is used to construct the expression "field op ALL (value...)".
382
461
func (c * Cond ) All (field , op string , values ... interface {}) string {
462
+ if len (field ) == 0 || len (op ) == 0 {
463
+ return ""
464
+ }
465
+
383
466
return c .Var (condBuilder {
384
467
Builder : func (ctx * argsCompileContext ) {
385
468
ctx .WriteString (field )
@@ -394,6 +477,10 @@ func (c *Cond) All(field, op string, values ...interface{}) string {
394
477
395
478
// Some is used to construct the expression "field op SOME (value...)".
396
479
func (c * Cond ) Some (field , op string , values ... interface {}) string {
480
+ if len (field ) == 0 || len (op ) == 0 {
481
+ return ""
482
+ }
483
+
397
484
return c .Var (condBuilder {
398
485
Builder : func (ctx * argsCompileContext ) {
399
486
ctx .WriteString (field )
@@ -413,6 +500,10 @@ func (c *Cond) Some(field, op string, values ...interface{}) string {
413
500
// "CASE ... WHEN ... ELSE ... END" expression to simulate the behavior of
414
501
// the IS DISTINCT FROM operator.
415
502
func (c * Cond ) IsDistinctFrom (field string , value interface {}) string {
503
+ if len (field ) == 0 {
504
+ return ""
505
+ }
506
+
416
507
return c .Var (condBuilder {
417
508
Builder : func (ctx * argsCompileContext ) {
418
509
switch ctx .Flavor {
@@ -458,6 +549,10 @@ func (c *Cond) IsDistinctFrom(field string, value interface{}) string {
458
549
// "CASE ... WHEN ... ELSE ... END" expression to simulate the behavior of
459
550
// the IS NOT DISTINCT FROM operator.
460
551
func (c * Cond ) IsNotDistinctFrom (field string , value interface {}) string {
552
+ if len (field ) == 0 {
553
+ return ""
554
+ }
555
+
461
556
return c .Var (condBuilder {
462
557
Builder : func (ctx * argsCompileContext ) {
463
558
switch ctx .Flavor {
0 commit comments