fix(clause): when the value of clause.Eq is an empty array, the SQL should be IN (NULL) (#6503)

This commit is contained in:
weih 2023-08-10 13:34:33 +08:00 committed by GitHub
parent 15162afaf2
commit bae684b363
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -246,15 +246,19 @@ func (eq Eq) Build(builder Builder) {
switch eq.Value.(type) {
case []string, []int, []int32, []int64, []uint, []uint32, []uint64, []interface{}:
builder.WriteString(" IN (")
rv := reflect.ValueOf(eq.Value)
for i := 0; i < rv.Len(); i++ {
if i > 0 {
builder.WriteByte(',')
if rv.Len() == 0 {
builder.WriteString(" IN (NULL)")
} else {
builder.WriteString(" IN (")
for i := 0; i < rv.Len(); i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.AddVar(builder, rv.Index(i).Interface())
}
builder.AddVar(builder, rv.Index(i).Interface())
builder.WriteByte(')')
}
builder.WriteByte(')')
default:
if eqNil(eq.Value) {
builder.WriteString(" IS NULL")

View File

@ -199,6 +199,11 @@ func TestExpression(t *testing.T) {
},
ExpectedVars: []interface{}{"a", "b"},
Result: "`column-name` NOT IN (?,?)",
}, {
Expressions: []clause.Expression{
clause.Eq{Column: column, Value: []string{}},
},
Result: "`column-name` IN (NULL)",
}, {
Expressions: []clause.Expression{
clause.Eq{Column: clause.Expr{SQL: "SUM(?)", Vars: []interface{}{clause.Column{Name: "id"}}}, Value: 100},