Skip to content

Commit a6b9300

Browse files
committed
Fix NotEq{"x": []interface{}} generation
This was resolving to FALSE, but should always be TRUE. The only portable way I found to do this was to replace the expression with a portable TRUE (1=1). Also changed Eq{"x": []interface{}} to a portable FALSE (1=0) for consistency to avoid nasty surprises around returning NULL instead of a boolean. Fixes #99
1 parent 09b5995 commit a6b9300

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

expr.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,18 @@ type Eq map[string]interface{}
7373

7474
func (eq Eq) toSql(useNotOpr bool) (sql string, args []interface{}, err error) {
7575
var (
76-
exprs []string
77-
equalOpr string = "="
78-
inOpr string = "IN"
79-
nullOpr string = "IS"
76+
exprs []string
77+
equalOpr = "="
78+
inOpr = "IN"
79+
nullOpr = "IS"
80+
inEmptyExpr = "(1=0)" // Portable FALSE
8081
)
8182

8283
if useNotOpr {
8384
equalOpr = "<>"
8485
inOpr = "NOT IN"
8586
nullOpr = "IS NOT"
87+
inEmptyExpr = "(1=1)" // Portable TRUE
8688
}
8789

8890
for key, val := range eq {
@@ -101,7 +103,7 @@ func (eq Eq) toSql(useNotOpr bool) (sql string, args []interface{}, err error) {
101103
if isListType(val) {
102104
valVal := reflect.ValueOf(val)
103105
if valVal.Len() == 0 {
104-
expr = fmt.Sprintf("%s %s (NULL)", key, inOpr)
106+
expr = inEmptyExpr
105107
if args == nil {
106108
args = []interface{}{}
107109
}

expr_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,19 @@ func TestEqInEmptyToSql(t *testing.T) {
6060
sql, args, err := b.ToSql()
6161
assert.NoError(t, err)
6262

63-
expectedSql := "id IN (NULL)"
63+
expectedSql := "(1=0)"
64+
assert.Equal(t, expectedSql, sql)
65+
66+
expectedArgs := []interface{}{}
67+
assert.Equal(t, expectedArgs, args)
68+
}
69+
70+
func TestNotEqInEmptyToSql(t *testing.T) {
71+
b := NotEq{"id": []int{}}
72+
sql, args, err := b.ToSql()
73+
assert.NoError(t, err)
74+
75+
expectedSql := "(1=1)"
6476
assert.Equal(t, expectedSql, sql)
6577

6678
expectedArgs := []interface{}{}

integration_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,21 @@ func TestMain(m *testing.M) {
7474
os.Exit(m.Run())
7575
}
7676

77-
func assertVals(t *testing.T, s SelectBuilder, vals ...string) {
77+
func assertVals(t *testing.T, s SelectBuilder, expected ...string) {
7878
rows, err := s.Query()
7979
assert.NoError(t, err)
8080
defer rows.Close()
8181

82-
var val string
83-
for _, expected := range vals {
82+
vals := make([]string, len(expected))
83+
for i := range vals {
8484
assert.True(t, rows.Next())
85-
assert.NoError(t, rows.Scan(&val))
86-
assert.Equal(t, expected, val)
85+
assert.NoError(t, rows.Scan(&vals[i]))
8786
}
8887
assert.False(t, rows.Next())
88+
89+
if expected != nil {
90+
assert.Equal(t, expected, vals)
91+
}
8992
}
9093

9194
func TestSimpleSelect(t *testing.T) {
@@ -104,6 +107,7 @@ func TestEq(t *testing.T) {
104107
assertVals(t, s.Where(Eq{"k": nil}))
105108
assertVals(t, s.Where(NotEq{"k": nil}), "foo", "bar", "foo", "baz")
106109
assertVals(t, s.Where(Eq{"k": []int{}}))
110+
assertVals(t, s.Where(NotEq{"k": []int{}}), "foo", "bar", "foo", "baz")
107111
}
108112

109113
func TestIneq(t *testing.T) {

0 commit comments

Comments
 (0)