Rollback to old Unscoped API

This commit is contained in:
Jinzhu 2016-01-12 15:27:25 +08:00
parent 43e9035dad
commit 341d047aa7
6 changed files with 11 additions and 8 deletions

View File

@ -306,7 +306,7 @@ func (association *Association) Count() int {
) )
if relationship.Kind == "many_to_many" { 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" { } else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
query := scope.DB() query := scope.DB()
for idx, foreignKey := range relationship.ForeignDBNames { for idx, foreignKey := range relationship.ForeignDBNames {

View File

@ -8,7 +8,7 @@ func BeforeDelete(scope *Scope) {
func Delete(scope *Scope) { func Delete(scope *Scope) {
if !scope.HasError() { if !scope.HasError() {
if !scope.db.unscoped && scope.HasColumn("DeletedAt") { if !scope.Search.Unscoped && scope.HasColumn("DeletedAt") {
scope.Raw( scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
scope.QuotedTableName(), scope.QuotedTableName(),

View File

@ -28,7 +28,6 @@ type DB struct {
parent *DB parent *DB
search *search search *search
logMode int logMode int
unscoped bool
logger logger logger logger
dialect Dialect dialect Dialect
singularTable bool singularTable bool
@ -187,9 +186,7 @@ func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB {
} }
func (s *DB) Unscoped() *DB { func (s *DB) Unscoped() *DB {
clone := s.clone() return s.clone().search.unscoped().db
clone.unscoped = true
return clone
} }
func (s *DB) Attrs(attrs ...interface{}) *DB { func (s *DB) Attrs(attrs ...interface{}) *DB {

View File

@ -3,7 +3,7 @@ package gorm
import "time" import "time"
func (s *DB) clone() *DB { 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 { for key, value := range s.values {
db.values[key] = value db.values[key] = value

View File

@ -161,7 +161,7 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
func (scope *Scope) whereSql() (sql string) { func (scope *Scope) whereSql() (sql string) {
var primaryConditions, andConditions, orConditions []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()) sql := fmt.Sprintf("(%v.deleted_at IS NULL OR %v.deleted_at <= '0001-01-02')", scope.QuotedTableName(), scope.QuotedTableName())
primaryConditions = append(primaryConditions, sql) primaryConditions = append(primaryConditions, sql)
} }

View File

@ -20,6 +20,7 @@ type search struct {
group string group string
tableName string tableName string
raw bool raw bool
Unscoped bool
countingQuery bool countingQuery bool
} }
@ -123,6 +124,11 @@ func (s *search) Raw(b bool) *search {
return s return s
} }
func (s *search) unscoped() *search {
s.Unscoped = true
return s
}
func (s *search) Table(name string) *search { func (s *search) Table(name string) *search {
s.tableName = name s.tableName = name
return s return s