Skip to content

Commit

Permalink
Fix NotEq{"x": []interface{}} generation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lann committed Aug 25, 2017
1 parent 09b5995 commit a6b9300
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
12 changes: 7 additions & 5 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,18 @@ type Eq map[string]interface{}

func (eq Eq) toSql(useNotOpr bool) (sql string, args []interface{}, err error) {
var (
exprs []string
equalOpr string = "="
inOpr string = "IN"
nullOpr string = "IS"
exprs []string
equalOpr = "="
inOpr = "IN"
nullOpr = "IS"
inEmptyExpr = "(1=0)" // Portable FALSE
)

if useNotOpr {
equalOpr = "<>"
inOpr = "NOT IN"
nullOpr = "IS NOT"
inEmptyExpr = "(1=1)" // Portable TRUE
}

for key, val := range eq {
Expand All @@ -101,7 +103,7 @@ func (eq Eq) toSql(useNotOpr bool) (sql string, args []interface{}, err error) {
if isListType(val) {
valVal := reflect.ValueOf(val)
if valVal.Len() == 0 {
expr = fmt.Sprintf("%s %s (NULL)", key, inOpr)
expr = inEmptyExpr
if args == nil {
args = []interface{}{}
}
Expand Down
14 changes: 13 additions & 1 deletion expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@ func TestEqInEmptyToSql(t *testing.T) {
sql, args, err := b.ToSql()
assert.NoError(t, err)

expectedSql := "id IN (NULL)"
expectedSql := "(1=0)"
assert.Equal(t, expectedSql, sql)

expectedArgs := []interface{}{}
assert.Equal(t, expectedArgs, args)
}

func TestNotEqInEmptyToSql(t *testing.T) {
b := NotEq{"id": []int{}}
sql, args, err := b.ToSql()
assert.NoError(t, err)

expectedSql := "(1=1)"
assert.Equal(t, expectedSql, sql)

expectedArgs := []interface{}{}
Expand Down
14 changes: 9 additions & 5 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func assertVals(t *testing.T, s SelectBuilder, vals ...string) {
func assertVals(t *testing.T, s SelectBuilder, expected ...string) {
rows, err := s.Query()
assert.NoError(t, err)
defer rows.Close()

var val string
for _, expected := range vals {
vals := make([]string, len(expected))
for i := range vals {
assert.True(t, rows.Next())
assert.NoError(t, rows.Scan(&val))
assert.Equal(t, expected, val)
assert.NoError(t, rows.Scan(&vals[i]))
}
assert.False(t, rows.Next())

if expected != nil {
assert.Equal(t, expected, vals)
}
}

func TestSimpleSelect(t *testing.T) {
Expand All @@ -104,6 +107,7 @@ func TestEq(t *testing.T) {
assertVals(t, s.Where(Eq{"k": nil}))
assertVals(t, s.Where(NotEq{"k": nil}), "foo", "bar", "foo", "baz")
assertVals(t, s.Where(Eq{"k": []int{}}))
assertVals(t, s.Where(NotEq{"k": []int{}}), "foo", "bar", "foo", "baz")
}

func TestIneq(t *testing.T) {
Expand Down

0 comments on commit a6b9300

Please sign in to comment.