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" {
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 {

View File

@ -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(),

View File

@ -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 {

View File

@ -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

View File

@ -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)
}

View File

@ -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