Skip to content

Commit

Permalink
Select statement has invalid NULL check syntax when using implicit ni…
Browse files Browse the repository at this point in the history
…l value #272
  • Loading branch information
XIELongDragon committed Nov 23, 2021
1 parent e40b45c commit 49d74cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
4 changes: 3 additions & 1 deletion exp/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package exp
import (
"reflect"
"regexp"

"github.com/doug-martin/goqu/v9/internal/util"
)

type boolean struct {
Expand Down Expand Up @@ -155,7 +157,7 @@ func checkLikeExp(op BooleanOperation, lhs Expression, val interface{}, invert b

// checks a boolean operation normalizing the operation based on the RHS (e.g. "a" = true vs "a" IS TRUE
func checkBoolExpType(op BooleanOperation, lhs Expression, rhs interface{}, invert bool) BooleanExpression {
if rhs == nil {
if rhs == nil || util.IsNil(rhs) {
op = IsOp
} else {
switch reflect.Indirect(reflect.ValueOf(rhs)).Kind() {
Expand Down
6 changes: 4 additions & 2 deletions expressions_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ func ExampleOr_withAnd() {
}

func ExampleOr_withExMap() {
var val *int
ds := goqu.From("test").Where(
goqu.Or(
// Ex will be anded together
Expand All @@ -1018,6 +1019,7 @@ func ExampleOr_withExMap() {
goqu.Ex{
"col3": nil,
"col4": "foo",
"col5": val,
},
),
)
Expand All @@ -1028,8 +1030,8 @@ func ExampleOr_withExMap() {
fmt.Println(sql, args)

// Output:
// SELECT * FROM "test" WHERE ((("col1" = 1) AND ("col2" IS TRUE)) OR (("col3" IS NULL) AND ("col4" = 'foo'))) []
// SELECT * FROM "test" WHERE ((("col1" = ?) AND ("col2" IS TRUE)) OR (("col3" IS NULL) AND ("col4" = ?))) [1 foo]
// SELECT * FROM "test" WHERE ((("col1" = 1) AND ("col2" IS TRUE)) OR (("col3" IS NULL) AND ("col4" = 'foo') AND ("col5" IS NULL))) []
// SELECT * FROM "test" WHERE ((("col1" = ?) AND ("col2" IS TRUE)) OR (("col3" IS NULL) AND ("col4" = ?) AND ("col5" IS NULL))) [1 foo]
}

func ExampleRange_numbers() {
Expand Down
12 changes: 12 additions & 0 deletions internal/util/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ func IsEmptyValue(v reflect.Value) bool {
}
}

func IsNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
default:
return false
}
}

var (
structMapCache = make(map[interface{}]ColumnMap)
structMapCacheLock = sync.Mutex{}
Expand Down
14 changes: 8 additions & 6 deletions sqlgen/expression_sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,15 @@ func (esg *expressionSQLGenerator) booleanExpressionSQL(b sb.SQLBuilder, operato

if (operatorOp == exp.IsOp || operatorOp == exp.IsNotOp) && esg.dialectOptions.UseLiteralIsBools {
// these values must be interpolated because preparing them generates invalid SQL
switch rhs {
case true:
rhs = TrueLiteral
case false:
rhs = FalseLiteral
case nil:
if util.IsNil(rhs) {
rhs = exp.NewLiteralExpression(string(esg.dialectOptions.Null))
} else {
switch rhs {
case true:
rhs = TrueLiteral
case false:
rhs = FalseLiteral
}
}
}
b.WriteRunes(esg.dialectOptions.SpaceRune)
Expand Down

0 comments on commit 49d74cb

Please sign in to comment.