mirror of https://github.com/go-gorm/gorm.git
fix(clause): when the value of clause.Eq is an empty array, the SQL should be IN (NULL) (#6503)
This commit is contained in:
parent
15162afaf2
commit
bae684b363
|
@ -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")
|
||||
|
|
|
@ -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},
|
||||
|
|
Loading…
Reference in New Issue