Allow to use Valuer with Eq expression, #3899

This commit is contained in:
Jinzhu 2020-12-30 10:42:13 +08:00
parent 065787c54e
commit 6c0ee2700a
2 changed files with 10 additions and 5 deletions

View File

@ -301,6 +301,10 @@ func (like Like) NegationBuild(builder Builder) {
} }
func eqNil(value interface{}) bool { func eqNil(value interface{}) bool {
if valuer, ok := value.(driver.Valuer); ok {
value, _ = valuer.Value()
}
return value == nil || eqNilReflect(value) return value == nil || eqNilReflect(value)
} }

View File

@ -105,28 +105,29 @@ func TestNamedExpr(t *testing.T) {
func TestExpression(t *testing.T) { func TestExpression(t *testing.T) {
column := "column-name" column := "column-name"
results := []struct { results := []struct {
Expressions []clause.Expression Expressions []clause.Expression
Result string Result string
}{{ }{{
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Eq{Column: column, Value: "column-value"}, clause.Eq{Column: column, Value: "column-value"},
}, },
Result: "`column-name` = ?", Result: "`column-name` = ?",
},{ }, {
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Eq{Column: column, Value: nil}, clause.Eq{Column: column, Value: nil},
clause.Eq{Column: column, Value: (*string)(nil)}, clause.Eq{Column: column, Value: (*string)(nil)},
clause.Eq{Column: column, Value: (*int)(nil)}, clause.Eq{Column: column, Value: (*int)(nil)},
clause.Eq{Column: column, Value: (*bool)(nil)}, clause.Eq{Column: column, Value: (*bool)(nil)},
clause.Eq{Column: column, Value: (interface{})(nil)}, clause.Eq{Column: column, Value: (interface{})(nil)},
clause.Eq{Column: column, Value: sql.NullString{String: "", Valid: false}},
}, },
Result: "`column-name` IS NULL", Result: "`column-name` IS NULL",
},{ }, {
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Neq{Column: column, Value: "column-value"}, clause.Neq{Column: column, Value: "column-value"},
}, },
Result: "`column-name` <> ?", Result: "`column-name` <> ?",
},{ }, {
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Neq{Column: column, Value: nil}, clause.Neq{Column: column, Value: nil},
clause.Neq{Column: column, Value: (*string)(nil)}, clause.Neq{Column: column, Value: (*string)(nil)},