Add softdelete feature
This commit is contained in:
parent
bcd3e77783
commit
b510fc584f
|
@ -45,6 +45,7 @@ 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) {
|
||||||
|
@ -796,6 +797,8 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table {
|
||||||
if !hasNoCacheTag {
|
if !hasNoCacheTag {
|
||||||
hasNoCacheTag = true
|
hasNoCacheTag = true
|
||||||
}
|
}
|
||||||
|
case k == "SOFTDELETE":
|
||||||
|
col.IsSoftDelete = true
|
||||||
case k == "NOT":
|
case k == "NOT":
|
||||||
default:
|
default:
|
||||||
if strings.HasPrefix(k, "'") && strings.HasSuffix(k, "'") {
|
if strings.HasPrefix(k, "'") && strings.HasSuffix(k, "'") {
|
||||||
|
@ -1414,3 +1417,9 @@ func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Disable soft delete
|
||||||
|
func (engine *Engine) Unscoped() *Engine {
|
||||||
|
engine.unscoped = true
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
|
36
session.go
36
session.go
|
@ -3402,13 +3402,39 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
|
||||||
return 0, ErrNeedDeletedCond
|
return 0, ErrNeedDeletedCond
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlStr := fmt.Sprintf("DELETE FROM %v WHERE %v",
|
sqlStr, sqlStrForCache := "", ""
|
||||||
session.Engine.Quote(session.Statement.TableName()), condition)
|
argsForCache := make([]interface{}, 0, len(args) * 2)
|
||||||
|
if session.Engine.unscoped || table.SoftDeleteColumn() == nil { // softdelete is disabled
|
||||||
|
sqlStr = fmt.Sprintf("DELETE FROM %v WHERE %v",
|
||||||
|
session.Engine.Quote(session.Statement.TableName()), condition)
|
||||||
|
|
||||||
|
sqlStrForCache = sqlStr
|
||||||
|
copy(argsForCache, args)
|
||||||
|
argsForCache = append(session.Statement.Params, argsForCache...)
|
||||||
|
} else {
|
||||||
|
// !oinume! sqlStrForCache and argsForCache is needed to behave as executing "DELETE FROM ..." for cache.
|
||||||
|
sqlStrForCache = fmt.Sprintf("DELETE FROM %v WHERE %v",
|
||||||
|
session.Engine.Quote(session.Statement.TableName()), condition)
|
||||||
|
copy(argsForCache, args)
|
||||||
|
argsForCache = append(session.Statement.Params, argsForCache...)
|
||||||
|
|
||||||
|
softDeleteCol := table.SoftDeleteColumn()
|
||||||
|
sqlStr = fmt.Sprintf("UPDATE %v SET %v = ? WHERE %v",
|
||||||
|
session.Engine.Quote(session.Statement.TableName()),
|
||||||
|
session.Engine.Quote(softDeleteCol.Name),
|
||||||
|
condition)
|
||||||
|
|
||||||
|
// !oinume! Insert NowTime to the head of session.Statement.Params
|
||||||
|
session.Statement.Params = append(session.Statement.Params, "")
|
||||||
|
paramsLen := len(session.Statement.Params)
|
||||||
|
copy(session.Statement.Params[1:paramsLen], session.Statement.Params[0:paramsLen-1])
|
||||||
|
session.Statement.Params[0] = session.Engine.NowTime(softDeleteCol.SQLType.Name)
|
||||||
|
}
|
||||||
|
|
||||||
args = append(session.Statement.Params, args...)
|
args = append(session.Statement.Params, args...)
|
||||||
|
|
||||||
if cacher := session.Engine.getCacher2(session.Statement.RefTable); cacher != nil && session.Statement.UseCache {
|
if cacher := session.Engine.getCacher2(session.Statement.RefTable); cacher != nil && session.Statement.UseCache {
|
||||||
session.cacheDelete(sqlStr, args...)
|
session.cacheDelete(sqlStrForCache, argsForCache...)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := session.exec(sqlStr, args...)
|
res, err := session.exec(sqlStr, args...)
|
||||||
|
@ -3651,6 +3677,10 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if col.IsSoftDelete {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if session.Statement.ColumnStr != "" {
|
if session.Statement.ColumnStr != "" {
|
||||||
if _, ok := session.Statement.columnMap[lColName]; !ok {
|
if _, ok := session.Statement.columnMap[lColName]; !ok {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -286,6 +286,9 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
||||||
if !includeAutoIncr && col.IsAutoIncrement {
|
if !includeAutoIncr && col.IsAutoIncrement {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if col.IsSoftDelete {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
|
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
|
||||||
continue
|
continue
|
||||||
|
@ -490,6 +493,10 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if col.IsSoftDelete && !engine.unscoped { // softdelete enabled
|
||||||
|
colNames = append(colNames, fmt.Sprintf("%v IS NULL", engine.Quote(col.Name)))
|
||||||
|
}
|
||||||
|
|
||||||
fieldValue := *fieldValuePtr
|
fieldValue := *fieldValuePtr
|
||||||
if fieldValue.Interface() == nil {
|
if fieldValue.Interface() == nil {
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue