Move 'unscoped' field from Engine to Statement.
See https://github.com/go-xorm/xorm/pull/175#issuecomment-61599948
This commit is contained in:
parent
42f0fc27ea
commit
f087929082
10
engine.go
10
engine.go
|
@ -45,7 +45,6 @@ type Engine struct {
|
|||
TZLocation *time.Location
|
||||
|
||||
disableGlobalCache bool
|
||||
unscoped bool
|
||||
}
|
||||
|
||||
func (engine *Engine) SetDisableGlobalCache(disable bool) {
|
||||
|
@ -1418,8 +1417,9 @@ func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}
|
|||
return
|
||||
}
|
||||
|
||||
// Disable soft delete
|
||||
func (engine *Engine) Unscoped() *Engine {
|
||||
engine.unscoped = true
|
||||
return engine
|
||||
// Always disable struct tag "deleted"
|
||||
func (engine *Engine) Unscoped() *Session {
|
||||
session := engine.NewSession()
|
||||
defer session.Close()
|
||||
return session.Unscoped()
|
||||
}
|
||||
|
|
14
session.go
14
session.go
|
@ -1079,7 +1079,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
|||
if len(condiBean) > 0 {
|
||||
colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true,
|
||||
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.BeanArgs = args
|
||||
}
|
||||
|
@ -3172,7 +3172,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
|||
if len(condiBean) > 0 {
|
||||
condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true,
|
||||
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
||||
session.Statement.mustColumnMap)
|
||||
session.Statement.unscoped, session.Statement.mustColumnMap)
|
||||
}
|
||||
|
||||
var condition = ""
|
||||
|
@ -3376,7 +3376,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
|
|||
session.Statement.RefTable = table
|
||||
colNames, args := buildConditions(session.Engine, table, bean, true, true,
|
||||
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
||||
session.Statement.mustColumnMap)
|
||||
session.Statement.unscoped, session.Statement.mustColumnMap)
|
||||
|
||||
var condition = ""
|
||||
var andStr = session.Engine.dialect.AndStr()
|
||||
|
@ -3404,7 +3404,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
|
|||
|
||||
sqlStr, sqlStrForCache := "", ""
|
||||
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",
|
||||
session.Engine.Quote(session.Statement.TableName()), condition)
|
||||
|
||||
|
@ -3638,6 +3638,12 @@ func (s *Session) Sync2(beans ...interface{}) error {
|
|||
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) {
|
||||
colNames := make([]string, 0)
|
||||
args := make([]interface{}, 0)
|
||||
|
|
17
statement.go
17
statement.go
|
@ -57,6 +57,7 @@ type Statement struct {
|
|||
IsDistinct bool
|
||||
allUseBool bool
|
||||
checkVersion bool
|
||||
unscoped bool
|
||||
mustColumnMap map[string]bool
|
||||
inColumns map[string]*inParam
|
||||
incrColumns map[string]incrParam
|
||||
|
@ -91,6 +92,7 @@ func (statement *Statement) Init() {
|
|||
statement.useAllCols = false
|
||||
statement.mustColumnMap = make(map[string]bool)
|
||||
statement.checkVersion = true
|
||||
statement.unscoped = false
|
||||
statement.inColumns = make(map[string]*inParam)
|
||||
statement.incrColumns = make(map[string]incrParam)
|
||||
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
|
||||
func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||
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{}) {
|
||||
|
||||
colNames := make([]string, 0)
|
||||
|
@ -493,7 +495,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|||
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)))
|
||||
}
|
||||
|
||||
|
@ -933,6 +935,12 @@ func (statement *Statement) Having(conditions string) *Statement {
|
|||
return statement
|
||||
}
|
||||
|
||||
// Always disable struct tag "deleted"
|
||||
func (statement *Statement) Unscoped() *Statement {
|
||||
statement.unscoped = true
|
||||
return statement
|
||||
}
|
||||
|
||||
func (statement *Statement) genColumnStr() string {
|
||||
table := statement.RefTable
|
||||
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,
|
||||
false, true, statement.allUseBool, statement.useAllCols,
|
||||
statement.mustColumnMap)
|
||||
statement.unscoped, statement.mustColumnMap)
|
||||
|
||||
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ")
|
||||
statement.BeanArgs = args
|
||||
|
@ -1083,7 +1091,8 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}
|
|||
statement.RefTable = table
|
||||
|
||||
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.BeanArgs = args
|
||||
|
|
Loading…
Reference in New Issue