Fix the issue of incorrect insertion of data in non UTC time zone zero for numeric types
This commit is contained in:
parent
620074d751
commit
24540c9e23
|
@ -7,17 +7,17 @@ package dialects
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"xorm.io/xorm/internal/utils"
|
||||||
|
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FormatColumnTime format column time
|
// FormatColumnTime format column time
|
||||||
func FormatColumnTime(dialect Dialect, dbLocation *time.Location, col *schemas.Column, t time.Time) (interface{}, error) {
|
func FormatColumnTime(dialect Dialect, dbLocation *time.Location, col *schemas.Column, t time.Time) (interface{}, error) {
|
||||||
if t.IsZero() {
|
if utils.IsTimeZero(t) {
|
||||||
if col.Nullable {
|
if col.Nullable {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.SQLType.IsNumeric() {
|
if col.SQLType.IsNumeric() {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,6 +146,6 @@ const (
|
||||||
|
|
||||||
// IsTimeZero return true if a time is zero
|
// IsTimeZero return true if a time is zero
|
||||||
func IsTimeZero(t time.Time) bool {
|
func IsTimeZero(t time.Time) bool {
|
||||||
return t.IsZero() || t.Format("2006-01-02 15:04:05") == ZeroTime0 ||
|
return t.IsZero() || t.Format("2006-01-02 15:04:05.999999999") == ZeroTime0 ||
|
||||||
t.Format("2006-01-02 15:04:05") == ZeroTime1
|
t.Format("2006-01-02 15:04:05.999999999") == ZeroTime1
|
||||||
}
|
}
|
||||||
|
|
|
@ -1249,6 +1249,44 @@ func TestInsertNotDeleted(t *testing.T) {
|
||||||
assert.Equal(t, v4.DeletedAt.In(testEngine.GetTZDatabase()).Format("2006-01-02 15:04:05"), zeroTime.Format("2006-01-02 15:04:05"))
|
assert.Equal(t, v4.DeletedAt.In(testEngine.GetTZDatabase()).Format("2006-01-02 15:04:05"), zeroTime.Format("2006-01-02 15:04:05"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInsertNotDeletedNum(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
type TestInsertNotDeletedStructNotRight struct {
|
||||||
|
ID uint64 `xorm:"'ID' pk autoincr"`
|
||||||
|
DeletedAt int64 `xorm:"'DELETED_AT' deleted notnull INT(11)"`
|
||||||
|
}
|
||||||
|
// notnull tag will be ignored
|
||||||
|
err := testEngine.Sync(new(TestInsertNotDeletedStructNotRight))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
type TestInsertNotDeletedStruct struct {
|
||||||
|
ID uint64 `xorm:"'ID' pk autoincr"`
|
||||||
|
DeletedAt int64 `xorm:"'DELETED_AT' deleted INT(11)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, testEngine.Sync(new(TestInsertNotDeletedStruct)))
|
||||||
|
|
||||||
|
var v1 TestInsertNotDeletedStructNotRight
|
||||||
|
_, err = testEngine.Insert(&v1)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var v2 TestInsertNotDeletedStructNotRight
|
||||||
|
has, err := testEngine.Get(&v2)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.Equal(t, v2.DeletedAt, int64(0))
|
||||||
|
|
||||||
|
var v3 TestInsertNotDeletedStruct
|
||||||
|
_, err = testEngine.Insert(&v3)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var v4 TestInsertNotDeletedStruct
|
||||||
|
has, err = testEngine.Get(&v4)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.Equal(t, v4.DeletedAt, int64(0))
|
||||||
|
}
|
||||||
|
|
||||||
type MyAutoTimeFields1 struct {
|
type MyAutoTimeFields1 struct {
|
||||||
Id int64
|
Id int64
|
||||||
Dt time.Time `xorm:"created DATETIME"`
|
Dt time.Time `xorm:"created DATETIME"`
|
||||||
|
|
Loading…
Reference in New Issue