Skip to content

Commit

Permalink
fix(clause): when the value of clause.Eq is an empty array, the SQL s…
Browse files Browse the repository at this point in the history
…hould be IN (NULL) (#6503)
  • Loading branch information
whcao authored Aug 10, 2023
1 parent 15162af commit bae684b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 10 additions & 6 deletions clause/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions clause/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit bae684b

Please sign in to comment.