forked from mirror/gorm
Fix extra 'AND' when len(values) == 0 ON IN.NegationBuild() (#4618)
This commit is contained in:
parent
7a53d8e46b
commit
093694fbf2
|
@ -210,11 +210,12 @@ func (in IN) Build(builder Builder) {
|
|||
}
|
||||
|
||||
func (in IN) NegationBuild(builder Builder) {
|
||||
builder.WriteQuoted(in.Column)
|
||||
switch len(in.Values) {
|
||||
case 0:
|
||||
builder.WriteString(" IS NOT NULL")
|
||||
case 1:
|
||||
if _, ok := in.Values[0].([]interface{}); !ok {
|
||||
builder.WriteQuoted(in.Column)
|
||||
builder.WriteString(" <> ")
|
||||
builder.AddVar(builder, in.Values[0])
|
||||
break
|
||||
|
@ -222,7 +223,6 @@ func (in IN) NegationBuild(builder Builder) {
|
|||
|
||||
fallthrough
|
||||
default:
|
||||
builder.WriteQuoted(in.Column)
|
||||
builder.WriteString(" NOT IN (")
|
||||
builder.AddVar(builder, in.Values...)
|
||||
builder.WriteByte(')')
|
||||
|
|
|
@ -436,6 +436,11 @@ func TestNot(t *testing.T) {
|
|||
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
||||
}
|
||||
|
||||
result = dryDB.Not(map[string]interface{}{"name": []string{}}).Find(&User{})
|
||||
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* IS NOT NULL").MatchString(result.Statement.SQL.String()) {
|
||||
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
||||
}
|
||||
|
||||
result = dryDB.Not(map[string]interface{}{"name": []string{"jinzhu", "jinzhu 2"}}).Find(&User{})
|
||||
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* NOT IN \\(.+,.+\\)").MatchString(result.Statement.SQL.String()) {
|
||||
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
||||
|
|
Loading…
Reference in New Issue