Fix int time deleted bug (#1539)
Fix panic Fix test Fix test for mssql time Add sql type check on deleted cond Fix int time deleted bug Reviewed-on: https://gitea.com/xorm/xorm/pulls/1539
This commit is contained in:
parent
35b2813796
commit
eabfb48b5d
19
engine.go
19
engine.go
|
@ -91,11 +91,22 @@ 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 {
|
||||
if engine.dialect.DBType() == core.MSSQL {
|
||||
return builder.IsNull{colName}
|
||||
func (engine *Engine) CondDeleted(col *core.Column) builder.Cond {
|
||||
var cond = builder.NewCond()
|
||||
if col.SQLType.IsNumeric() {
|
||||
cond = builder.Eq{col.Name: 0}
|
||||
} else {
|
||||
// FIXME: mssql: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
|
||||
if engine.dialect.DBType() != core.MSSQL {
|
||||
cond = builder.Eq{col.Name: zeroTime1}
|
||||
}
|
||||
return builder.IsNull{colName}.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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
37
time_test.go
37
time_test.go
|
@ -477,3 +477,40 @@ func TestCustomTimeUserDeletedDiffLoc(t *testing.T) {
|
|||
assert.EqualValues(t, formatTime(time.Time(user3.DeletedAt)), formatTime(time.Time(user4.DeletedAt)))
|
||||
fmt.Println("user3", user3.DeletedAt, user4.DeletedAt)
|
||||
}
|
||||
|
||||
func TestDeletedInt64(t *testing.T) {
|
||||
assert.NoError(t, prepareEngine())
|
||||
|
||||
type DeletedInt64Struct struct {
|
||||
Id int64
|
||||
Deleted int64 `xorm:"deleted default(0) notnull"` // timestamp
|
||||
}
|
||||
|
||||
assertSync(t, new(DeletedInt64Struct))
|
||||
|
||||
var d1 DeletedInt64Struct
|
||||
cnt, err := testEngine.Insert(&d1)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, cnt)
|
||||
|
||||
var d2 DeletedInt64Struct
|
||||
has, err := testEngine.ID(d1.Id).Get(&d2)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, has)
|
||||
assert.EqualValues(t, d1, d2)
|
||||
|
||||
cnt, err = testEngine.ID(d1.Id).NoAutoCondition().Delete(&d1)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, cnt)
|
||||
|
||||
var d3 DeletedInt64Struct
|
||||
has, err = testEngine.ID(d1.Id).Get(&d3)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, has)
|
||||
|
||||
var d4 DeletedInt64Struct
|
||||
has, err = testEngine.ID(d1.Id).Unscoped().Get(&d4)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, has)
|
||||
assert.EqualValues(t, d1, d4)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue