should check inner condition length (#7512)

This commit is contained in:
Riseif 2025-07-21 12:57:12 +09:00 committed by GitHub
parent 991c2d4891
commit 985940f0d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View File

@ -341,7 +341,9 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
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(orConds)
if len(orConds.Exprs) == 1 {
where.Exprs[0] = clause.AndConditions(orConds)
}
}
}
conds = append(conds, clause.And(where.Exprs...))

View File

@ -632,6 +632,21 @@ func TestOr(t *testing.T) {
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
}
sub := dryDB.Clauses(clause.Where{
Exprs: []clause.Expression{
clause.OrConditions{
Exprs: []clause.Expression{
clause.Expr{SQL: "role = ?", Vars: []interface{}{"super_admin"}},
clause.Expr{SQL: "role = ?", Vars: []interface{}{"admin"}},
},
},
},
})
result = dryDB.Where(sub).Find(&User{})
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())
}
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()) {
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())