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 {
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:

View File

@ -106,11 +106,13 @@ func TestExpression(t *testing.T) {
column := "column-name"
results := []struct {
Expressions []clause.Expression
ExpectedVars []interface{}
Result string
}{{
Expressions: []clause.Expression{
clause.Eq{Column: column, Value: "column-value"},
},
ExpectedVars: []interface{}{"column-value"},
Result: "`column-name` = ?",
}, {
Expressions: []clause.Expression{
@ -126,6 +128,7 @@ func TestExpression(t *testing.T) {
Expressions: []clause.Expression{
clause.Neq{Column: column, Value: "column-value"},
},
ExpectedVars: []interface{}{"column-value"},
Result: "`column-name` <> ?",
}, {
Expressions: []clause.Expression{
@ -140,11 +143,13 @@ func TestExpression(t *testing.T) {
Expressions: []clause.Expression{
clause.Eq{Column: column, Value: []string{"a", "b"}},
},
ExpectedVars: []interface{}{"a", "b"},
Result: "`column-name` IN (?,?)",
}, {
Expressions: []clause.Expression{
clause.Neq{Column: column, Value: []string{"a", "b"}},
},
ExpectedVars: []interface{}{"a", "b"},
Result: "`column-name` NOT IN (?,?)",
}}
@ -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)
}
})
}
}