Fix Eq, Neq support slice of data

This commit is contained in:
Jinzhu 2021-05-31 17:21:27 +08:00
parent 14e96080d8
commit 9abac96546
2 changed files with 17 additions and 8 deletions

View File

@ -241,7 +241,7 @@ func (eq Eq) Build(builder Builder) {
if i > 0 { if i > 0 {
builder.WriteByte(',') builder.WriteByte(',')
} }
builder.AddVar(builder, rv.Index(i)) builder.AddVar(builder, rv.Index(i).Interface())
} }
builder.WriteByte(')') builder.WriteByte(')')
default: default:
@ -272,7 +272,7 @@ func (neq Neq) Build(builder Builder) {
if i > 0 { if i > 0 {
builder.WriteByte(',') builder.WriteByte(',')
} }
builder.AddVar(builder, rv.Index(i)) builder.AddVar(builder, rv.Index(i).Interface())
} }
builder.WriteByte(')') builder.WriteByte(')')
default: default:

View File

@ -105,13 +105,15 @@ 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 ExpectedVars []interface{}
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` = ?", ExpectedVars: []interface{}{"column-value"},
Result: "`column-name` = ?",
}, { }, {
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Eq{Column: column, Value: nil}, clause.Eq{Column: column, Value: nil},
@ -126,7 +128,8 @@ func TestExpression(t *testing.T) {
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Neq{Column: column, Value: "column-value"}, clause.Neq{Column: column, Value: "column-value"},
}, },
Result: "`column-name` <> ?", ExpectedVars: []interface{}{"column-value"},
Result: "`column-name` <> ?",
}, { }, {
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Neq{Column: column, Value: nil}, clause.Neq{Column: column, Value: nil},
@ -140,12 +143,14 @@ func TestExpression(t *testing.T) {
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Eq{Column: column, Value: []string{"a", "b"}}, clause.Eq{Column: column, Value: []string{"a", "b"}},
}, },
Result: "`column-name` IN (?,?)", ExpectedVars: []interface{}{"a", "b"},
Result: "`column-name` IN (?,?)",
}, { }, {
Expressions: []clause.Expression{ Expressions: []clause.Expression{
clause.Neq{Column: column, Value: []string{"a", "b"}}, 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 { for idx, result := range results {
@ -157,6 +162,10 @@ func TestExpression(t *testing.T) {
if stmt.SQL.String() != result.Result { if stmt.SQL.String() != result.Result {
t.Errorf("generated SQL is not equal, expects %v, but got %v", result.Result, stmt.SQL.String()) 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)
}
}) })
} }
} }