From 4aa4e00cdb7b1f95cfe35ba799a63aec5d6ac3c1 Mon Sep 17 00:00:00 2001 From: Richard Jose Date: Mon, 17 Oct 2016 14:23:28 -0600 Subject: [PATCH] Add test for correct functioning of nil pointer parameters and test for them appropriately so that they give IS NULL. --- expr.go | 2 +- select_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/expr.go b/expr.go index edfd9a23..942f5d06 100644 --- a/expr.go +++ b/expr.go @@ -95,7 +95,7 @@ func (eq Eq) toSql(useNotOpr bool) (sql string, args []interface{}, err error) { } } - if val == nil { + if (reflect.ValueOf(val).Kind() == reflect.Ptr && reflect.ValueOf(val).IsNil()) || val == nil { expr = fmt.Sprintf("%s %s NULL", key, nullOpr) } else { valVal := reflect.ValueOf(val) diff --git a/select_test.go b/select_test.go index 98d6a963..8788ccd6 100644 --- a/select_test.go +++ b/select_test.go @@ -155,3 +155,14 @@ func TestSelectBuilderNestedSelectJoin(t *testing.T) { assert.Equal(t, expectedSql, sql) assert.Equal(t, args, expectedArgs) } + +func TestSelectBuilderNilParam(t *testing.T) { + var nilVar *string + a := Select("test").Where(Eq{"param": nil}) + b := Select("test").Where(Eq{"param": nilVar}) + + aSql, _, _ := a.ToSql() + bSql, _, _ := b.ToSql() + assert.Equal(t, aSql, bSql) +} +