Skip to content

Commit acb75ac

Browse files
Vasily RodionovVasily Rodionov
Vasily Rodionov
authored and
Vasily Rodionov
committed
fix huandu#172: added checks for empty strings in conds.
1 parent c329655 commit acb75ac

File tree

5 files changed

+252
-44
lines changed

5 files changed

+252
-44
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ _testmain.go
3030
# VS Code
3131
debug
3232
debug_test
33+
.vscode/
3334

3435
# Mac
3536
.DS_Store

Diff for: cond.go

+97-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func NewCond() *Cond {
2525

2626
// Equal is used to construct the expression "field = value".
2727
func (c *Cond) Equal(field string, value interface{}) string {
28+
if len(field) == 0 {
29+
return ""
30+
}
31+
2832
return c.Var(condBuilder{
2933
Builder: func(ctx *argsCompileContext) {
3034
ctx.WriteString(field)
@@ -46,6 +50,10 @@ func (c *Cond) EQ(field string, value interface{}) string {
4650

4751
// NotEqual is used to construct the expression "field <> value".
4852
func (c *Cond) NotEqual(field string, value interface{}) string {
53+
if len(field) == 0 {
54+
return ""
55+
}
56+
4957
return c.Var(condBuilder{
5058
Builder: func(ctx *argsCompileContext) {
5159
ctx.WriteString(field)
@@ -67,6 +75,10 @@ func (c *Cond) NEQ(field string, value interface{}) string {
6775

6876
// GreaterThan is used to construct the expression "field > value".
6977
func (c *Cond) GreaterThan(field string, value interface{}) string {
78+
if len(field) == 0 {
79+
return ""
80+
}
81+
7082
return c.Var(condBuilder{
7183
Builder: func(ctx *argsCompileContext) {
7284
ctx.WriteString(field)
@@ -88,6 +100,10 @@ func (c *Cond) GT(field string, value interface{}) string {
88100

89101
// GreaterEqualThan is used to construct the expression "field >= value".
90102
func (c *Cond) GreaterEqualThan(field string, value interface{}) string {
103+
if len(field) == 0 {
104+
return ""
105+
}
106+
91107
return c.Var(condBuilder{
92108
Builder: func(ctx *argsCompileContext) {
93109
ctx.WriteString(field)
@@ -109,6 +125,10 @@ func (c *Cond) GTE(field string, value interface{}) string {
109125

110126
// LessThan is used to construct the expression "field < value".
111127
func (c *Cond) LessThan(field string, value interface{}) string {
128+
if len(field) == 0 {
129+
return ""
130+
}
131+
112132
return c.Var(condBuilder{
113133
Builder: func(ctx *argsCompileContext) {
114134
ctx.WriteString(field)
@@ -130,6 +150,9 @@ func (c *Cond) LT(field string, value interface{}) string {
130150

131151
// LessEqualThan is used to construct the expression "field <= value".
132152
func (c *Cond) LessEqualThan(field string, value interface{}) string {
153+
if len(field) == 0 {
154+
return ""
155+
}
133156
return c.Var(condBuilder{
134157
Builder: func(ctx *argsCompileContext) {
135158
ctx.WriteString(field)
@@ -151,6 +174,10 @@ func (c *Cond) LTE(field string, value interface{}) string {
151174

152175
// In is used to construct the expression "field IN (value...)".
153176
func (c *Cond) In(field string, values ...interface{}) string {
177+
if len(field) == 0 {
178+
return ""
179+
}
180+
154181
return c.Var(condBuilder{
155182
Builder: func(ctx *argsCompileContext) {
156183
ctx.WriteString(field)
@@ -163,6 +190,10 @@ func (c *Cond) In(field string, values ...interface{}) string {
163190

164191
// NotIn is used to construct the expression "field NOT IN (value...)".
165192
func (c *Cond) NotIn(field string, values ...interface{}) string {
193+
if len(field) == 0 {
194+
return ""
195+
}
196+
166197
return c.Var(condBuilder{
167198
Builder: func(ctx *argsCompileContext) {
168199
ctx.WriteString(field)
@@ -175,6 +206,10 @@ func (c *Cond) NotIn(field string, values ...interface{}) string {
175206

176207
// Like is used to construct the expression "field LIKE value".
177208
func (c *Cond) Like(field string, value interface{}) string {
209+
if len(field) == 0 {
210+
return ""
211+
}
212+
178213
return c.Var(condBuilder{
179214
Builder: func(ctx *argsCompileContext) {
180215
ctx.WriteString(field)
@@ -190,6 +225,10 @@ func (c *Cond) Like(field string, value interface{}) string {
190225
// the ILike method will return "LOWER(field) LIKE LOWER(value)"
191226
// to simulate the behavior of the ILIKE operator.
192227
func (c *Cond) ILike(field string, value interface{}) string {
228+
if len(field) == 0 {
229+
return ""
230+
}
231+
193232
return c.Var(condBuilder{
194233
Builder: func(ctx *argsCompileContext) {
195234
switch ctx.Flavor {
@@ -212,6 +251,10 @@ func (c *Cond) ILike(field string, value interface{}) string {
212251

213252
// NotLike is used to construct the expression "field NOT LIKE value".
214253
func (c *Cond) NotLike(field string, value interface{}) string {
254+
if len(field) == 0 {
255+
return ""
256+
}
257+
215258
return c.Var(condBuilder{
216259
Builder: func(ctx *argsCompileContext) {
217260
ctx.WriteString(field)
@@ -227,6 +270,10 @@ func (c *Cond) NotLike(field string, value interface{}) string {
227270
// the NotILike method will return "LOWER(field) NOT LIKE LOWER(value)"
228271
// to simulate the behavior of the ILIKE operator.
229272
func (c *Cond) NotILike(field string, value interface{}) string {
273+
if len(field) == 0 {
274+
return ""
275+
}
276+
230277
return c.Var(condBuilder{
231278
Builder: func(ctx *argsCompileContext) {
232279
switch ctx.Flavor {
@@ -249,6 +296,10 @@ func (c *Cond) NotILike(field string, value interface{}) string {
249296

250297
// IsNull is used to construct the expression "field IS NULL".
251298
func (c *Cond) IsNull(field string) string {
299+
if len(field) == 0 {
300+
return ""
301+
}
302+
252303
return c.Var(condBuilder{
253304
Builder: func(ctx *argsCompileContext) {
254305
ctx.WriteString(field)
@@ -259,6 +310,9 @@ func (c *Cond) IsNull(field string) string {
259310

260311
// IsNotNull is used to construct the expression "field IS NOT NULL".
261312
func (c *Cond) IsNotNull(field string) string {
313+
if len(field) == 0 {
314+
return ""
315+
}
262316
return c.Var(condBuilder{
263317
Builder: func(ctx *argsCompileContext) {
264318
ctx.WriteString(field)
@@ -269,6 +323,10 @@ func (c *Cond) IsNotNull(field string) string {
269323

270324
// Between is used to construct the expression "field BETWEEN lower AND upper".
271325
func (c *Cond) Between(field string, lower, upper interface{}) string {
326+
if len(field) == 0 {
327+
return ""
328+
}
329+
272330
return c.Var(condBuilder{
273331
Builder: func(ctx *argsCompileContext) {
274332
ctx.WriteString(field)
@@ -282,6 +340,10 @@ func (c *Cond) Between(field string, lower, upper interface{}) string {
282340

283341
// NotBetween is used to construct the expression "field NOT BETWEEN lower AND upper".
284342
func (c *Cond) NotBetween(field string, lower, upper interface{}) string {
343+
if len(field) == 0 {
344+
return ""
345+
}
346+
285347
return c.Var(condBuilder{
286348
Builder: func(ctx *argsCompileContext) {
287349
ctx.WriteString(field)
@@ -299,10 +361,15 @@ func (c *Cond) Or(orExpr ...string) string {
299361
return ""
300362
}
301363

364+
exprByteLen := estimateStringsBytes(orExpr)
365+
if exprByteLen == 0 {
366+
return ""
367+
}
368+
302369
buf := newStringBuilder()
303370

304371
// 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
306373
buf.Grow(size)
307374

308375
buf.WriteString(lparen)
@@ -317,10 +384,15 @@ func (c *Cond) And(andExpr ...string) string {
317384
return ""
318385
}
319386

387+
exprByteLen := estimateStringsBytes(andExpr)
388+
if exprByteLen == 0 {
389+
return ""
390+
}
391+
320392
buf := newStringBuilder()
321393

322394
// 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
324396
buf.Grow(size)
325397

326398
buf.WriteString(lparen)
@@ -331,6 +403,9 @@ func (c *Cond) And(andExpr ...string) string {
331403

332404
// Not is used to construct the expression "NOT expr".
333405
func (c *Cond) Not(notExpr string) string {
406+
if len(notExpr) == 0 {
407+
return ""
408+
}
334409
buf := newStringBuilder()
335410

336411
// Ensure that there is only 1 memory allocation.
@@ -366,6 +441,10 @@ func (c *Cond) NotExists(subquery interface{}) string {
366441

367442
// Any is used to construct the expression "field op ANY (value...)".
368443
func (c *Cond) Any(field, op string, values ...interface{}) string {
444+
if len(field) == 0 || len(op) == 0 {
445+
return ""
446+
}
447+
369448
return c.Var(condBuilder{
370449
Builder: func(ctx *argsCompileContext) {
371450
ctx.WriteString(field)
@@ -380,6 +459,10 @@ func (c *Cond) Any(field, op string, values ...interface{}) string {
380459

381460
// All is used to construct the expression "field op ALL (value...)".
382461
func (c *Cond) All(field, op string, values ...interface{}) string {
462+
if len(field) == 0 || len(op) == 0 {
463+
return ""
464+
}
465+
383466
return c.Var(condBuilder{
384467
Builder: func(ctx *argsCompileContext) {
385468
ctx.WriteString(field)
@@ -394,6 +477,10 @@ func (c *Cond) All(field, op string, values ...interface{}) string {
394477

395478
// Some is used to construct the expression "field op SOME (value...)".
396479
func (c *Cond) Some(field, op string, values ...interface{}) string {
480+
if len(field) == 0 || len(op) == 0 {
481+
return ""
482+
}
483+
397484
return c.Var(condBuilder{
398485
Builder: func(ctx *argsCompileContext) {
399486
ctx.WriteString(field)
@@ -413,6 +500,10 @@ func (c *Cond) Some(field, op string, values ...interface{}) string {
413500
// "CASE ... WHEN ... ELSE ... END" expression to simulate the behavior of
414501
// the IS DISTINCT FROM operator.
415502
func (c *Cond) IsDistinctFrom(field string, value interface{}) string {
503+
if len(field) == 0 {
504+
return ""
505+
}
506+
416507
return c.Var(condBuilder{
417508
Builder: func(ctx *argsCompileContext) {
418509
switch ctx.Flavor {
@@ -458,6 +549,10 @@ func (c *Cond) IsDistinctFrom(field string, value interface{}) string {
458549
// "CASE ... WHEN ... ELSE ... END" expression to simulate the behavior of
459550
// the IS NOT DISTINCT FROM operator.
460551
func (c *Cond) IsNotDistinctFrom(field string, value interface{}) string {
552+
if len(field) == 0 {
553+
return ""
554+
}
555+
461556
return c.Var(condBuilder{
462557
Builder: func(ctx *argsCompileContext) {
463558
switch ctx.Flavor {

0 commit comments

Comments
 (0)