Move 'unscoped' field from Engine to Statement.

See https://github.com/go-xorm/xorm/pull/175#issuecomment-61599948
This commit is contained in:
oinume 2014-11-05 16:04:53 +09:00
parent 42f0fc27ea
commit f087929082
3 changed files with 28 additions and 13 deletions

View File

@ -45,7 +45,6 @@ type Engine struct {
TZLocation *time.Location TZLocation *time.Location
disableGlobalCache bool disableGlobalCache bool
unscoped bool
} }
func (engine *Engine) SetDisableGlobalCache(disable bool) { func (engine *Engine) SetDisableGlobalCache(disable bool) {
@ -1418,8 +1417,9 @@ func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}
return return
} }
// Disable soft delete // Always disable struct tag "deleted"
func (engine *Engine) Unscoped() *Engine { func (engine *Engine) Unscoped() *Session {
engine.unscoped = true session := engine.NewSession()
return engine defer session.Close()
return session.Unscoped()
} }

View File

@ -1079,7 +1079,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
if len(condiBean) > 0 { if len(condiBean) > 0 {
colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true, colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true,
false, true, session.Statement.allUseBool, session.Statement.useAllCols, false, true, session.Statement.allUseBool, session.Statement.useAllCols,
session.Statement.mustColumnMap) session.Statement.unscoped, session.Statement.mustColumnMap)
session.Statement.ConditionStr = strings.Join(colNames, " AND ") session.Statement.ConditionStr = strings.Join(colNames, " AND ")
session.Statement.BeanArgs = args session.Statement.BeanArgs = args
} }
@ -3172,7 +3172,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
if len(condiBean) > 0 { if len(condiBean) > 0 {
condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true, condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true,
false, true, session.Statement.allUseBool, session.Statement.useAllCols, false, true, session.Statement.allUseBool, session.Statement.useAllCols,
session.Statement.mustColumnMap) session.Statement.unscoped, session.Statement.mustColumnMap)
} }
var condition = "" var condition = ""
@ -3376,7 +3376,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
session.Statement.RefTable = table session.Statement.RefTable = table
colNames, args := buildConditions(session.Engine, table, bean, true, true, colNames, args := buildConditions(session.Engine, table, bean, true, true,
false, true, session.Statement.allUseBool, session.Statement.useAllCols, false, true, session.Statement.allUseBool, session.Statement.useAllCols,
session.Statement.mustColumnMap) session.Statement.unscoped, session.Statement.mustColumnMap)
var condition = "" var condition = ""
var andStr = session.Engine.dialect.AndStr() var andStr = session.Engine.dialect.AndStr()
@ -3404,7 +3404,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
sqlStr, sqlStrForCache := "", "" sqlStr, sqlStrForCache := "", ""
argsForCache := make([]interface{}, 0, len(args) * 2) argsForCache := make([]interface{}, 0, len(args) * 2)
if session.Engine.unscoped || table.DeletedColumn() == nil { // deleted is disabled if session.Statement.unscoped || table.DeletedColumn() == nil { // tag "deleted" is disabled
sqlStr = fmt.Sprintf("DELETE FROM %v WHERE %v", sqlStr = fmt.Sprintf("DELETE FROM %v WHERE %v",
session.Engine.Quote(session.Statement.TableName()), condition) session.Engine.Quote(session.Statement.TableName()), condition)
@ -3638,6 +3638,12 @@ func (s *Session) Sync2(beans ...interface{}) error {
return nil return nil
} }
// Always disable struct tag "deleted"
func (session *Session) Unscoped() *Session {
session.Statement.Unscoped()
return session
}
func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) { func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
colNames := make([]string, 0) colNames := make([]string, 0)
args := make([]interface{}, 0) args := make([]interface{}, 0)

View File

@ -57,6 +57,7 @@ type Statement struct {
IsDistinct bool IsDistinct bool
allUseBool bool allUseBool bool
checkVersion bool checkVersion bool
unscoped bool
mustColumnMap map[string]bool mustColumnMap map[string]bool
inColumns map[string]*inParam inColumns map[string]*inParam
incrColumns map[string]incrParam incrColumns map[string]incrParam
@ -91,6 +92,7 @@ func (statement *Statement) Init() {
statement.useAllCols = false statement.useAllCols = false
statement.mustColumnMap = make(map[string]bool) statement.mustColumnMap = make(map[string]bool)
statement.checkVersion = true statement.checkVersion = true
statement.unscoped = false
statement.inColumns = make(map[string]*inParam) statement.inColumns = make(map[string]*inParam)
statement.incrColumns = make(map[string]incrParam) statement.incrColumns = make(map[string]incrParam)
statement.decrColumns = make(map[string]decrParam) statement.decrColumns = make(map[string]decrParam)
@ -468,7 +470,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
// Auto generating conditions according a struct // Auto generating conditions according a struct
func buildConditions(engine *Engine, table *core.Table, bean interface{}, func buildConditions(engine *Engine, table *core.Table, bean interface{},
includeVersion bool, includeUpdated bool, includeNil bool, includeVersion bool, includeUpdated bool, includeNil bool,
includeAutoIncr bool, allUseBool bool, useAllCols bool, includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
mustColumnMap map[string]bool) ([]string, []interface{}) { mustColumnMap map[string]bool) ([]string, []interface{}) {
colNames := make([]string, 0) colNames := make([]string, 0)
@ -493,7 +495,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
continue continue
} }
if col.IsDeleted && !engine.unscoped { // deleted enabled if col.IsDeleted && !unscoped { // tag "deleted" is enabled
colNames = append(colNames, fmt.Sprintf("%v IS NULL", engine.Quote(col.Name))) colNames = append(colNames, fmt.Sprintf("%v IS NULL", engine.Quote(col.Name)))
} }
@ -933,6 +935,12 @@ func (statement *Statement) Having(conditions string) *Statement {
return statement return statement
} }
// Always disable struct tag "deleted"
func (statement *Statement) Unscoped() *Statement {
statement.unscoped = true
return statement
}
func (statement *Statement) genColumnStr() string { func (statement *Statement) genColumnStr() string {
table := statement.RefTable table := statement.RefTable
colNames := make([]string, 0) colNames := make([]string, 0)
@ -1037,7 +1045,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
colNames, args := buildConditions(statement.Engine, table, bean, true, true, colNames, args := buildConditions(statement.Engine, table, bean, true, true,
false, true, statement.allUseBool, statement.useAllCols, false, true, statement.allUseBool, statement.useAllCols,
statement.mustColumnMap) statement.unscoped, statement.mustColumnMap)
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ")
statement.BeanArgs = args statement.BeanArgs = args
@ -1083,7 +1091,8 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}
statement.RefTable = table statement.RefTable = table
colNames, args := buildConditions(statement.Engine, table, bean, true, true, false, colNames, args := buildConditions(statement.Engine, table, bean, true, true, false,
true, statement.allUseBool, statement.useAllCols, statement.mustColumnMap) true, statement.allUseBool, statement.useAllCols,
statement.unscoped, statement.mustColumnMap)
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
statement.BeanArgs = args statement.BeanArgs = args