Support group conditions with single OR condition
This commit is contained in:
parent
9790103e68
commit
35ebfe6874
@ -261,6 +261,11 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
|
|||||||
case *DB:
|
case *DB:
|
||||||
if cs, ok := v.Statement.Clauses["WHERE"]; ok {
|
if cs, ok := v.Statement.Clauses["WHERE"]; ok {
|
||||||
if where, ok := cs.Expression.(clause.Where); ok {
|
if where, ok := cs.Expression.(clause.Where); ok {
|
||||||
|
if len(where.Exprs) == 1 {
|
||||||
|
if orConds, ok := where.Exprs[0].(clause.OrConditions); ok {
|
||||||
|
where.Exprs[0] = clause.AndConditions{Exprs: orConds.Exprs}
|
||||||
|
}
|
||||||
|
}
|
||||||
conds = append(conds, clause.And(where.Exprs...))
|
conds = append(conds, clause.And(where.Exprs...))
|
||||||
} else if cs.Expression != nil {
|
} else if cs.Expression != nil {
|
||||||
conds = append(conds, cs.Expression)
|
conds = append(conds, cs.Expression)
|
||||||
|
@ -475,7 +475,17 @@ func TestNotWithAllFields(t *testing.T) {
|
|||||||
func TestOr(t *testing.T) {
|
func TestOr(t *testing.T) {
|
||||||
dryDB := DB.Session(&gorm.Session{DryRun: true})
|
dryDB := DB.Session(&gorm.Session{DryRun: true})
|
||||||
|
|
||||||
result := dryDB.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&User{})
|
result := dryDB.Where("role = ?", "admin").Where(DB.Or("role = ?", "super_admin")).Find(&User{})
|
||||||
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ AND .*role.* = .+").MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
result = dryDB.Where("role = ?", "admin").Where(DB.Or("role = ?", "super_admin").Or("role = ?", "admin")).Find(&User{})
|
||||||
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ AND (.*role.* = .+ OR .*role.* = .+)").MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
result = dryDB.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&User{})
|
||||||
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) {
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) {
|
||||||
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
|
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user