Add sql type check on deleted cond

This commit is contained in:
Lunny Xiao 2020-02-21 17:52:35 +08:00
parent d6e31ce715
commit 92bb9f5e4d
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
4 changed files with 15 additions and 8 deletions

View File

@ -91,12 +91,19 @@ func (engine *Engine) BufferSize(size int) *Session {
} }
// CondDeleted returns the conditions whether a record is soft deleted. // CondDeleted returns the conditions whether a record is soft deleted.
func (engine *Engine) CondDeleted(colName string) builder.Cond { func (engine *Engine) CondDeleted(col *core.Column) builder.Cond {
cond := builder.IsNull{colName}.Or(builder.Eq{colName: 0}) var cond builder.Cond
if engine.dialect.DBType() == core.MSSQL { if col.SQLType.IsNumeric() {
return cond cond = builder.Eq{col.Name: 0}
} else {
cond = builder.Eq{col.Name: zeroTime1}
} }
return cond.Or(builder.Eq{colName: zeroTime1})
if col.Nullable {
cond = cond.Or(builder.IsNull{col.Name})
}
return cond
} }
// ShowSQL show SQL statement or not on logger if log level is great than INFO // ShowSQL show SQL statement or not on logger if log level is great than INFO

View File

@ -58,7 +58,7 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{},
} }
if col.IsDeleted && !unscoped { // tag "deleted" is enabled if col.IsDeleted && !unscoped { // tag "deleted" is enabled
conds = append(conds, engine.CondDeleted(colName)) conds = append(conds, engine.CondDeleted(col))
} }
fieldValue := *fieldValuePtr fieldValue := *fieldValuePtr

View File

@ -121,7 +121,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
colName = session.engine.Quote(nm) + "." + colName colName = session.engine.Quote(nm) + "." + colName
} }
autoCond = session.engine.CondDeleted(colName) autoCond = session.engine.CondDeleted(col)
} }
} }
} }

View File

@ -287,7 +287,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
if !condBeanIsStruct && table != nil { if !condBeanIsStruct && table != nil {
if col := table.DeletedColumn(); col != nil && !session.statement.unscoped { // tag "deleted" is enabled if col := table.DeletedColumn(); col != nil && !session.statement.unscoped { // tag "deleted" is enabled
autoCond1 := session.engine.CondDeleted(session.engine.Quote(col.Name)) autoCond1 := session.engine.CondDeleted(col)
if autoCond == nil { if autoCond == nil {
autoCond = autoCond1 autoCond = autoCond1