mirror of https://github.com/go-gorm/gorm.git
fix: not clause with or condition (#6984)
This commit is contained in:
parent
85299bfca7
commit
207f1ac68f
|
@ -215,8 +215,13 @@ func (not NotConditions) Build(builder Builder) {
|
||||||
|
|
||||||
for idx, c := range not.Exprs {
|
for idx, c := range not.Exprs {
|
||||||
if idx > 0 {
|
if idx > 0 {
|
||||||
|
switch c.(type) {
|
||||||
|
case OrConditions:
|
||||||
|
builder.WriteString(OrWithSpace)
|
||||||
|
default:
|
||||||
builder.WriteString(AndWithSpace)
|
builder.WriteString(AndWithSpace)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
e, wrapInParentheses := c.(Expr)
|
e, wrapInParentheses := c.(Expr)
|
||||||
if wrapInParentheses {
|
if wrapInParentheses {
|
||||||
|
|
|
@ -113,6 +113,22 @@ func TestWhere(t *testing.T) {
|
||||||
"SELECT * FROM `users` WHERE NOT (`score` <= ? AND `age` <= ?)",
|
"SELECT * FROM `users` WHERE NOT (`score` <= ? AND `age` <= ?)",
|
||||||
[]interface{}{100, 60},
|
[]interface{}{100, 60},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.Where{
|
||||||
|
Exprs: []clause.Expression{
|
||||||
|
clause.Not(clause.AndConditions{
|
||||||
|
Exprs: []clause.Expression{
|
||||||
|
clause.Eq{Column: clause.PrimaryColumn, Value: "1"},
|
||||||
|
clause.Gt{Column: "age", Value: 18},
|
||||||
|
}}, clause.OrConditions{
|
||||||
|
Exprs: []clause.Expression{
|
||||||
|
clause.Lt{Column: "score", Value: 100},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}}},
|
||||||
|
"SELECT * FROM `users` WHERE NOT ((`users`.`id` = ? AND `age` > ?) OR `score` < ?)",
|
||||||
|
[]interface{}{"1", 18, 100},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, result := range results {
|
for idx, result := range results {
|
||||||
|
|
|
@ -559,6 +559,11 @@ func TestNot(t *testing.T) {
|
||||||
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE NOT \\(manager IS NULL AND age >= .+\\) AND .users.\\..deleted_at. IS NULL").MatchString(result.Statement.SQL.String()) {
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE NOT \\(manager IS NULL AND age >= .+\\) AND .users.\\..deleted_at. IS NULL").MatchString(result.Statement.SQL.String()) {
|
||||||
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result = dryDB.Not(DB.Where("manager IS NULL").Or("age >= ?", 20)).Find(&User{})
|
||||||
|
if !regexp.MustCompile(`SELECT \* FROM .*users.* WHERE NOT \(manager IS NULL OR age >= .+\) AND .users.\..deleted_at. IS NULL`).MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotWithAllFields(t *testing.T) {
|
func TestNotWithAllFields(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue