From 6c0ee2700a1282fe0e2eb669cf57641f01fcf9bc Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 30 Dec 2020 10:42:13 +0800 Subject: [PATCH] Allow to use Valuer with Eq expression, #3899 --- clause/expression.go | 4 ++++ clause/expression_test.go | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/clause/expression.go b/clause/expression.go index 3844d66b..7a4c09f4 100644 --- a/clause/expression.go +++ b/clause/expression.go @@ -301,6 +301,10 @@ func (like Like) NegationBuild(builder Builder) { } func eqNil(value interface{}) bool { + if valuer, ok := value.(driver.Valuer); ok { + value, _ = valuer.Value() + } + return value == nil || eqNilReflect(value) } diff --git a/clause/expression_test.go b/clause/expression_test.go index 9e3d7bad..4472bdb1 100644 --- a/clause/expression_test.go +++ b/clause/expression_test.go @@ -105,28 +105,29 @@ func TestNamedExpr(t *testing.T) { func TestExpression(t *testing.T) { column := "column-name" results := []struct { - Expressions []clause.Expression - Result string + Expressions []clause.Expression + Result string }{{ Expressions: []clause.Expression{ clause.Eq{Column: column, Value: "column-value"}, }, Result: "`column-name` = ?", - },{ + }, { Expressions: []clause.Expression{ clause.Eq{Column: column, Value: nil}, clause.Eq{Column: column, Value: (*string)(nil)}, clause.Eq{Column: column, Value: (*int)(nil)}, clause.Eq{Column: column, Value: (*bool)(nil)}, clause.Eq{Column: column, Value: (interface{})(nil)}, + clause.Eq{Column: column, Value: sql.NullString{String: "", Valid: false}}, }, Result: "`column-name` IS NULL", - },{ + }, { Expressions: []clause.Expression{ clause.Neq{Column: column, Value: "column-value"}, }, Result: "`column-name` <> ?", - },{ + }, { Expressions: []clause.Expression{ clause.Neq{Column: column, Value: nil}, clause.Neq{Column: column, Value: (*string)(nil)},