From d69be45dd2a05ea04a1079f68a71f18722a17edc Mon Sep 17 00:00:00 2001 From: longlihale <1510613524@qq.com> Date: Wed, 29 Dec 2021 22:24:48 +0800 Subject: [PATCH 1/3] fix: generate sql incorrect when use soft_delete and only one OR --- clause/where.go | 5 ++++- soft_delete.go | 2 +- tests/soft_delete_test.go | 10 ++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/clause/where.go b/clause/where.go index 61aa73a87..02843e04c 100644 --- a/clause/where.go +++ b/clause/where.go @@ -92,9 +92,12 @@ func (where Where) MergeClause(clause *Clause) { func And(exprs ...Expression) Expression { if len(exprs) == 0 { return nil - } else if len(exprs) == 1 { + } + + if _, ok := exprs[0].(OrConditions); !ok && len(exprs) == 1 { return exprs[0] } + return AndConditions{Exprs: exprs} } diff --git a/soft_delete.go b/soft_delete.go index 51e4c0d7e..4582161dd 100644 --- a/soft_delete.go +++ b/soft_delete.go @@ -65,7 +65,7 @@ func (sd SoftDeleteQueryClause) MergeClause(*clause.Clause) { func (sd SoftDeleteQueryClause) ModifyStatement(stmt *Statement) { if _, ok := stmt.Clauses["soft_delete_enabled"]; !ok && !stmt.Statement.Unscoped { if c, ok := stmt.Clauses["WHERE"]; ok { - if where, ok := c.Expression.(clause.Where); ok && len(where.Exprs) > 1 { + if where, ok := c.Expression.(clause.Where); ok && len(where.Exprs) >= 1 { for _, expr := range where.Exprs { if orCond, ok := expr.(clause.OrConditions); ok && len(orCond.Exprs) == 1 { where.Exprs = []clause.Expression{clause.And(where.Exprs...)} diff --git a/tests/soft_delete_test.go b/tests/soft_delete_test.go index 0dfe24d5a..9ac8da10d 100644 --- a/tests/soft_delete_test.go +++ b/tests/soft_delete_test.go @@ -83,3 +83,13 @@ func TestDeletedAtUnMarshal(t *testing.T) { t.Errorf("Failed, result.DeletedAt: %v is not same as expected.DeletedAt: %v", result.DeletedAt, expected.DeletedAt) } } + +func TestDeletedAtOneOr(t *testing.T) { + actualSQL := DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + return tx.Or("id = ?", 1).Find(&User{}) + }) + + if !regexp.MustCompile(` WHERE id = 1 AND .users.\..deleted_at. IS NULL`).MatchString(actualSQL) { + t.Fatalf("invalid sql generated, got %v", actualSQL) + } +} From 8846f3474764a406ff4830a1a9e57a0598a5adeb Mon Sep 17 00:00:00 2001 From: longlihale <1510613524@qq.com> Date: Thu, 30 Dec 2021 11:36:59 +0800 Subject: [PATCH 2/3] fix: generate sql incorrect when use soft_delete and only one OR --- clause/where.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/clause/where.go b/clause/where.go index 02843e04c..d04cdc5a7 100644 --- a/clause/where.go +++ b/clause/where.go @@ -94,8 +94,12 @@ func And(exprs ...Expression) Expression { return nil } - if _, ok := exprs[0].(OrConditions); !ok && len(exprs) == 1 { - return exprs[0] + if len(exprs) == 1 { + + if _, ok := exprs[0].(OrConditions); !ok { + return exprs[0] + } + } return AndConditions{Exprs: exprs} From 9b193081ec710ab5573e85832a4d212efd454ba3 Mon Sep 17 00:00:00 2001 From: longlihale <1510613524@qq.com> Date: Thu, 30 Dec 2021 11:44:44 +0800 Subject: [PATCH 3/3] fix: generate sql incorrect when use soft_delete and only one OR --- clause/where.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/clause/where.go b/clause/where.go index d04cdc5a7..20a011362 100644 --- a/clause/where.go +++ b/clause/where.go @@ -95,11 +95,9 @@ func And(exprs ...Expression) Expression { } if len(exprs) == 1 { - if _, ok := exprs[0].(OrConditions); !ok { return exprs[0] } - } return AndConditions{Exprs: exprs}