fix #202: NotIn with empty values builds "0 = 0"

This commit is contained in:
Huan Du
2025-06-13 00:58:47 +08:00
parent 8ed4b5374f
commit d882448972
2 changed files with 8 additions and 2 deletions
+7 -1
View File
@@ -207,10 +207,15 @@ func (c *Cond) In(field string, values ...interface{}) string {
// NotIn is used to construct the expression "field NOT IN (value...)".
func (c *Cond) NotIn(field string, values ...interface{}) string {
if len(field) == 0 || len(values) == 0 {
if len(field) == 0 {
return ""
}
// Empty values means "true".
if len(values) == 0 {
return "0 = 0"
}
return c.Var(condBuilder{
Builder: func(ctx *argsCompileContext) {
ctx.WriteString(field)
@@ -427,6 +432,7 @@ func (c *Cond) Not(notExpr string) string {
if len(notExpr) == 0 {
return ""
}
buf := newStringBuilder()
// Ensure that there is only 1 memory allocation.
+1 -1
View File
@@ -49,6 +49,7 @@ func TestCond(t *testing.T) {
newTestPair("$a IN ($1, $2, $3)", func(c *Cond) string { return c.In("$a", 1, 2, 3) }),
newTestPair("0 = 1", func(c *Cond) string { return c.In("$a") }),
newTestPair("$a NOT IN ($1, $2, $3)", func(c *Cond) string { return c.NotIn("$a", 1, 2, 3) }),
newTestPair("0 = 0", func(c *Cond) string { return c.NotIn("$a") }),
newTestPair("$a LIKE $1", func(c *Cond) string { return c.Like("$a", "%Huan%") }),
newTestPair("$a ILIKE $1", func(c *Cond) string { return c.ILike("$a", "%Huan%") }),
newTestPair("$a NOT LIKE $1", func(c *Cond) string { return c.NotLike("$a", "%Huan%") }),
@@ -135,7 +136,6 @@ func TestEmptyCond(t *testing.T) {
newCond().LessEqualThan("", 123),
newCond().In("", 1, 2, 3),
newCond().NotIn("", 1, 2, 3),
newCond().NotIn("a"),
newCond().Like("", "%Huan%"),
newCond().ILike("", "%Huan%"),
newCond().NotLike("", "%Huan%"),