mirror of https://github.com/go-gorm/gorm.git
Support Scopes in group conditions
This commit is contained in:
parent
b49ae84780
commit
88c26b62ee
|
@ -312,6 +312,10 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
|
||||||
case clause.Expression:
|
case clause.Expression:
|
||||||
conds = append(conds, v)
|
conds = append(conds, v)
|
||||||
case *DB:
|
case *DB:
|
||||||
|
for _, scope := range v.Statement.scopes {
|
||||||
|
v = scope(v)
|
||||||
|
}
|
||||||
|
|
||||||
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 len(where.Exprs) == 1 {
|
||||||
|
|
|
@ -243,6 +243,21 @@ func TestGroupConditions(t *testing.T) {
|
||||||
if !strings.HasSuffix(result, expects) {
|
if !strings.HasSuffix(result, expects) {
|
||||||
t.Errorf("expects: %v, got %v", expects, result)
|
t.Errorf("expects: %v, got %v", expects, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stmt2 := dryRunDB.Where(
|
||||||
|
DB.Scopes(NameIn1And2),
|
||||||
|
).Or(
|
||||||
|
DB.Where("pizza = ?", "hawaiian").Where("size = ?", "xlarge"),
|
||||||
|
).Find(&Pizza{}).Statement
|
||||||
|
|
||||||
|
execStmt2 := dryRunDB.Exec(`WHERE name in ? OR (pizza = ? AND size = ?)`, []string{"ScopeUser1", "ScopeUser2"}, "hawaiian", "xlarge").Statement
|
||||||
|
|
||||||
|
result2 := DB.Dialector.Explain(stmt2.SQL.String(), stmt2.Vars...)
|
||||||
|
expects2 := DB.Dialector.Explain(execStmt2.SQL.String(), execStmt2.Vars...)
|
||||||
|
|
||||||
|
if !strings.HasSuffix(result2, expects2) {
|
||||||
|
t.Errorf("expects: %v, got %v", expects2, result2)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCombineStringConditions(t *testing.T) {
|
func TestCombineStringConditions(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue