Skip to content

Commit fd0b5a9

Browse files
committed
fix #202: NotIn with empty values builds "0 = 0"
1 parent dad603c commit fd0b5a9

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

cond.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,15 @@ func (c *Cond) In(field string, values ...interface{}) string {
207207

208208
// NotIn is used to construct the expression "field NOT IN (value...)".
209209
func (c *Cond) NotIn(field string, values ...interface{}) string {
210-
if len(field) == 0 || len(values) == 0 {
210+
if len(field) == 0 {
211211
return ""
212212
}
213213

214+
// Empty values means "true".
215+
if len(values) == 0 {
216+
return "0 = 0"
217+
}
218+
214219
return c.Var(condBuilder{
215220
Builder: func(ctx *argsCompileContext) {
216221
ctx.WriteString(field)
@@ -427,6 +432,7 @@ func (c *Cond) Not(notExpr string) string {
427432
if len(notExpr) == 0 {
428433
return ""
429434
}
435+
430436
buf := newStringBuilder()
431437

432438
// Ensure that there is only 1 memory allocation.

cond_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func TestCond(t *testing.T) {
4949
newTestPair("$a IN ($1, $2, $3)", func(c *Cond) string { return c.In("$a", 1, 2, 3) }),
5050
newTestPair("0 = 1", func(c *Cond) string { return c.In("$a") }),
5151
newTestPair("$a NOT IN ($1, $2, $3)", func(c *Cond) string { return c.NotIn("$a", 1, 2, 3) }),
52+
newTestPair("0 = 0", func(c *Cond) string { return c.NotIn("$a") }),
5253
newTestPair("$a LIKE $1", func(c *Cond) string { return c.Like("$a", "%Huan%") }),
5354
newTestPair("$a ILIKE $1", func(c *Cond) string { return c.ILike("$a", "%Huan%") }),
5455
newTestPair("$a NOT LIKE $1", func(c *Cond) string { return c.NotLike("$a", "%Huan%") }),
@@ -135,7 +136,6 @@ func TestEmptyCond(t *testing.T) {
135136
newCond().LessEqualThan("", 123),
136137
newCond().In("", 1, 2, 3),
137138
newCond().NotIn("", 1, 2, 3),
138-
newCond().NotIn("a"),
139139
newCond().Like("", "%Huan%"),
140140
newCond().ILike("", "%Huan%"),
141141
newCond().NotLike("", "%Huan%"),

0 commit comments

Comments
 (0)