From 341d047aa7ae166f12478d4c3c0941681aa22323 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 12 Jan 2016 15:27:25 +0800 Subject: [PATCH] Rollback to old Unscoped API --- association.go | 2 +- callback_delete.go | 2 +- main.go | 5 +---- main_private.go | 2 +- scope_private.go | 2 +- search.go | 6 ++++++ 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/association.go b/association.go index 4660cb27..30ea36b2 100644 --- a/association.go +++ b/association.go @@ -306,7 +306,7 @@ func (association *Association) Count() int { ) if relationship.Kind == "many_to_many" { - relationship.JoinTableHandler.JoinWith(relationship.JoinTableHandler, scope.NewDB(), association.Scope.Value).Model(fieldValue).Count(&count) + relationship.JoinTableHandler.JoinWith(relationship.JoinTableHandler, scope.DB(), association.Scope.Value).Model(fieldValue).Count(&count) } else if relationship.Kind == "has_many" || relationship.Kind == "has_one" { query := scope.DB() for idx, foreignKey := range relationship.ForeignDBNames { diff --git a/callback_delete.go b/callback_delete.go index 8e56196b..72236659 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -8,7 +8,7 @@ func BeforeDelete(scope *Scope) { func Delete(scope *Scope) { if !scope.HasError() { - if !scope.db.unscoped && scope.HasColumn("DeletedAt") { + if !scope.Search.Unscoped && scope.HasColumn("DeletedAt") { scope.Raw( fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", scope.QuotedTableName(), diff --git a/main.go b/main.go index d84c139b..ff707f3f 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,6 @@ type DB struct { parent *DB search *search logMode int - unscoped bool logger logger dialect Dialect singularTable bool @@ -187,9 +186,7 @@ func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB { } func (s *DB) Unscoped() *DB { - clone := s.clone() - clone.unscoped = true - return clone + return s.clone().search.unscoped().db } func (s *DB) Attrs(attrs ...interface{}) *DB { diff --git a/main_private.go b/main_private.go index 3431de81..bd097ce0 100644 --- a/main_private.go +++ b/main_private.go @@ -3,7 +3,7 @@ package gorm import "time" func (s *DB) clone() *DB { - db := DB{db: s.db, parent: s.parent, logger: s.logger, logMode: s.logMode, unscoped: s.unscoped, values: map[string]interface{}{}, Value: s.Value, Error: s.Error} + db := DB{db: s.db, parent: s.parent, logger: s.logger, logMode: s.logMode, values: map[string]interface{}{}, Value: s.Value, Error: s.Error} for key, value := range s.values { db.values[key] = value diff --git a/scope_private.go b/scope_private.go index 36292423..a154c426 100644 --- a/scope_private.go +++ b/scope_private.go @@ -161,7 +161,7 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string) func (scope *Scope) whereSql() (sql string) { var primaryConditions, andConditions, orConditions []string - if !scope.db.unscoped && scope.Fields()["deleted_at"] != nil { + if !scope.Search.Unscoped && scope.Fields()["deleted_at"] != nil { sql := fmt.Sprintf("(%v.deleted_at IS NULL OR %v.deleted_at <= '0001-01-02')", scope.QuotedTableName(), scope.QuotedTableName()) primaryConditions = append(primaryConditions, sql) } diff --git a/search.go b/search.go index cabce05c..166b9a86 100644 --- a/search.go +++ b/search.go @@ -20,6 +20,7 @@ type search struct { group string tableName string raw bool + Unscoped bool countingQuery bool } @@ -123,6 +124,11 @@ func (s *search) Raw(b bool) *search { return s } +func (s *search) unscoped() *search { + s.Unscoped = true + return s +} + func (s *search) Table(name string) *search { s.tableName = name return s