From 92bb9f5e4d72212f5e1522d393aba9ffbf33d178 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 21 Feb 2020 17:52:35 +0800 Subject: [PATCH] Add sql type check on deleted cond --- engine.go | 17 ++++++++++++----- engine_cond.go | 2 +- session_find.go | 2 +- session_update.go | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/engine.go b/engine.go index e95d97b1..c23b1999 100644 --- a/engine.go +++ b/engine.go @@ -91,12 +91,19 @@ func (engine *Engine) BufferSize(size int) *Session { } // CondDeleted returns the conditions whether a record is soft deleted. -func (engine *Engine) CondDeleted(colName string) builder.Cond { - cond := builder.IsNull{colName}.Or(builder.Eq{colName: 0}) - if engine.dialect.DBType() == core.MSSQL { - return cond +func (engine *Engine) CondDeleted(col *core.Column) builder.Cond { + var cond builder.Cond + if col.SQLType.IsNumeric() { + 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 diff --git a/engine_cond.go b/engine_cond.go index 702ac804..17f50bd7 100644 --- a/engine_cond.go +++ b/engine_cond.go @@ -58,7 +58,7 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{}, } if col.IsDeleted && !unscoped { // tag "deleted" is enabled - conds = append(conds, engine.CondDeleted(colName)) + conds = append(conds, engine.CondDeleted(col)) } fieldValue := *fieldValuePtr diff --git a/session_find.go b/session_find.go index f9dd24ca..c7043ea6 100644 --- a/session_find.go +++ b/session_find.go @@ -121,7 +121,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) colName = session.engine.Quote(nm) + "." + colName } - autoCond = session.engine.CondDeleted(colName) + autoCond = session.engine.CondDeleted(col) } } } diff --git a/session_update.go b/session_update.go index 18425ec3..ceaade46 100644 --- a/session_update.go +++ b/session_update.go @@ -287,7 +287,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 if !condBeanIsStruct && table != nil { 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 { autoCond = autoCond1