From 985940f0d855dcefa2a1a847a1d37795335d4579 Mon Sep 17 00:00:00 2001 From: Riseif Date: Mon, 21 Jul 2025 12:57:12 +0900 Subject: [PATCH] should check inner condition length (#7512) --- statement.go | 4 +++- tests/query_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/statement.go b/statement.go index c6183724..0218064e 100644 --- a/statement.go +++ b/statement.go @@ -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...)) diff --git a/tests/query_test.go b/tests/query_test.go index 566763c5..19cdeb59 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -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())