Skip to content

Commit eabfb48

Browse files
committed
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
1 parent 35b2813 commit eabfb48

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

engine.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,22 @@ func (engine *Engine) BufferSize(size int) *Session {
9191
}
9292

9393
// CondDeleted returns the conditions whether a record is soft deleted.
94-
func (engine *Engine) CondDeleted(colName string) builder.Cond {
95-
if engine.dialect.DBType() == core.MSSQL {
96-
return builder.IsNull{colName}
94+
func (engine *Engine) CondDeleted(col *core.Column) builder.Cond {
95+
var cond = builder.NewCond()
96+
if col.SQLType.IsNumeric() {
97+
cond = builder.Eq{col.Name: 0}
98+
} else {
99+
// FIXME: mssql: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
100+
if engine.dialect.DBType() != core.MSSQL {
101+
cond = builder.Eq{col.Name: zeroTime1}
102+
}
97103
}
98-
return builder.IsNull{colName}.Or(builder.Eq{colName: zeroTime1})
104+
105+
if col.Nullable {
106+
cond = cond.Or(builder.IsNull{col.Name})
107+
}
108+
109+
return cond
99110
}
100111

101112
// ShowSQL show SQL statement or not on logger if log level is great than INFO

engine_cond.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{},
5858
}
5959

6060
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
61-
conds = append(conds, engine.CondDeleted(colName))
61+
conds = append(conds, engine.CondDeleted(col))
6262
}
6363

6464
fieldValue := *fieldValuePtr

session_find.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
121121
colName = session.engine.Quote(nm) + "." + colName
122122
}
123123

124-
autoCond = session.engine.CondDeleted(colName)
124+
autoCond = session.engine.CondDeleted(col)
125125
}
126126
}
127127
}

session_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
287287

288288
if !condBeanIsStruct && table != nil {
289289
if col := table.DeletedColumn(); col != nil && !session.statement.unscoped { // tag "deleted" is enabled
290-
autoCond1 := session.engine.CondDeleted(session.engine.Quote(col.Name))
290+
autoCond1 := session.engine.CondDeleted(col)
291291

292292
if autoCond == nil {
293293
autoCond = autoCond1

time_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,40 @@ func TestCustomTimeUserDeletedDiffLoc(t *testing.T) {
477477
assert.EqualValues(t, formatTime(time.Time(user3.DeletedAt)), formatTime(time.Time(user4.DeletedAt)))
478478
fmt.Println("user3", user3.DeletedAt, user4.DeletedAt)
479479
}
480+
481+
func TestDeletedInt64(t *testing.T) {
482+
assert.NoError(t, prepareEngine())
483+
484+
type DeletedInt64Struct struct {
485+
Id int64
486+
Deleted int64 `xorm:"deleted default(0) notnull"` // timestamp
487+
}
488+
489+
assertSync(t, new(DeletedInt64Struct))
490+
491+
var d1 DeletedInt64Struct
492+
cnt, err := testEngine.Insert(&d1)
493+
assert.NoError(t, err)
494+
assert.EqualValues(t, 1, cnt)
495+
496+
var d2 DeletedInt64Struct
497+
has, err := testEngine.ID(d1.Id).Get(&d2)
498+
assert.NoError(t, err)
499+
assert.True(t, has)
500+
assert.EqualValues(t, d1, d2)
501+
502+
cnt, err = testEngine.ID(d1.Id).NoAutoCondition().Delete(&d1)
503+
assert.NoError(t, err)
504+
assert.EqualValues(t, 1, cnt)
505+
506+
var d3 DeletedInt64Struct
507+
has, err = testEngine.ID(d1.Id).Get(&d3)
508+
assert.NoError(t, err)
509+
assert.False(t, has)
510+
511+
var d4 DeletedInt64Struct
512+
has, err = testEngine.ID(d1.Id).Unscoped().Get(&d4)
513+
assert.NoError(t, err)
514+
assert.True(t, has)
515+
assert.EqualValues(t, d1, d4)
516+
}

0 commit comments

Comments
 (0)