From 9abac96546ed2497c43d1487f6f9c13afb852f4a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 31 May 2021 17:21:27 +0800 Subject: [PATCH] Fix Eq, Neq support slice of data --- clause/expression.go | 4 ++-- clause/expression_test.go | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/clause/expression.go b/clause/expression.go index a0933ad2..2bdd4a30 100644 --- a/clause/expression.go +++ b/clause/expression.go @@ -241,7 +241,7 @@ func (eq Eq) Build(builder Builder) { if i > 0 { builder.WriteByte(',') } - builder.AddVar(builder, rv.Index(i)) + builder.AddVar(builder, rv.Index(i).Interface()) } builder.WriteByte(')') default: @@ -272,7 +272,7 @@ func (neq Neq) Build(builder Builder) { if i > 0 { builder.WriteByte(',') } - builder.AddVar(builder, rv.Index(i)) + builder.AddVar(builder, rv.Index(i).Interface()) } builder.WriteByte(')') default: diff --git a/clause/expression_test.go b/clause/expression_test.go index e0e192f7..1c8217ed 100644 --- a/clause/expression_test.go +++ b/clause/expression_test.go @@ -105,13 +105,15 @@ func TestNamedExpr(t *testing.T) { func TestExpression(t *testing.T) { column := "column-name" results := []struct { - Expressions []clause.Expression - Result string + Expressions []clause.Expression + ExpectedVars []interface{} + Result string }{{ Expressions: []clause.Expression{ clause.Eq{Column: column, Value: "column-value"}, }, - Result: "`column-name` = ?", + ExpectedVars: []interface{}{"column-value"}, + Result: "`column-name` = ?", }, { Expressions: []clause.Expression{ clause.Eq{Column: column, Value: nil}, @@ -126,7 +128,8 @@ func TestExpression(t *testing.T) { Expressions: []clause.Expression{ clause.Neq{Column: column, Value: "column-value"}, }, - Result: "`column-name` <> ?", + ExpectedVars: []interface{}{"column-value"}, + Result: "`column-name` <> ?", }, { Expressions: []clause.Expression{ clause.Neq{Column: column, Value: nil}, @@ -140,12 +143,14 @@ func TestExpression(t *testing.T) { Expressions: []clause.Expression{ clause.Eq{Column: column, Value: []string{"a", "b"}}, }, - Result: "`column-name` IN (?,?)", + ExpectedVars: []interface{}{"a", "b"}, + Result: "`column-name` IN (?,?)", }, { Expressions: []clause.Expression{ clause.Neq{Column: column, Value: []string{"a", "b"}}, }, - Result: "`column-name` NOT IN (?,?)", + ExpectedVars: []interface{}{"a", "b"}, + Result: "`column-name` NOT IN (?,?)", }} for idx, result := range results { @@ -157,6 +162,10 @@ func TestExpression(t *testing.T) { if stmt.SQL.String() != result.Result { t.Errorf("generated SQL is not equal, expects %v, but got %v", result.Result, stmt.SQL.String()) } + + if !reflect.DeepEqual(result.ExpectedVars, stmt.Vars) { + t.Errorf("generated vars is not equal, expects %v, but got %v", result.ExpectedVars, stmt.Vars) + } }) } }