Fix deleted tag attribute zeroTime is not DatabaseTZ (#2299)
Co-authored-by: CyJay <cyjay@MacBook-Pro.lan> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/2299 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: CyJaySong <CyJaySong@gmail.com> Co-committed-by: CyJaySong <CyJaySong@gmail.com>
This commit is contained in:
parent
c622cdaf89
commit
94882e39df
|
@ -28,14 +28,19 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
return &dt, nil
|
return &dt, nil
|
||||||
} else if len(s) == 20 && s[10] == 'T' && s[19] == 'Z' {
|
} else if len(s) == 20 && s[10] == 'T' && s[19] == 'Z' {
|
||||||
|
if strings.HasPrefix(s, "0000-00-00T00:00:00") || strings.HasPrefix(s, "0001-01-01T00:00:00") {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
dt, err := time.ParseInLocation("2006-01-02T15:04:05", s[:19], originalLocation)
|
dt, err := time.ParseInLocation("2006-01-02T15:04:05", s[:19], originalLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
dt.IsZero()
|
|
||||||
return &dt, nil
|
return &dt, nil
|
||||||
} else if len(s) == 25 && s[10] == 'T' && s[19] == '+' && s[22] == ':' {
|
} else if len(s) == 25 && s[10] == 'T' && s[19] == '+' && s[22] == ':' {
|
||||||
|
if strings.HasPrefix(s, "0000-00-00T00:00:00") || strings.HasPrefix(s, "0001-01-01T00:00:00") {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
dt, err := time.Parse(time.RFC3339, s)
|
dt, err := time.Parse(time.RFC3339, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -43,6 +48,10 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
return &dt, nil
|
return &dt, nil
|
||||||
} else if len(s) >= 21 && s[10] == 'T' && s[19] == '.' {
|
} else if len(s) >= 21 && s[10] == 'T' && s[19] == '.' {
|
||||||
|
if strings.HasPrefix(s, "0000-00-00T00:00:00."+strings.Repeat("0", len(s)-20)) ||
|
||||||
|
strings.HasPrefix(s, "0001-01-01T00:00:00."+strings.Repeat("0", len(s)-20)) {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
dt, err := time.Parse(time.RFC3339Nano, s)
|
dt, err := time.Parse(time.RFC3339Nano, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -50,6 +59,10 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
return &dt, nil
|
return &dt, nil
|
||||||
} else if len(s) >= 21 && s[19] == '.' {
|
} else if len(s) >= 21 && s[19] == '.' {
|
||||||
|
if strings.HasPrefix(s, "0000-00-00T00:00:00."+strings.Repeat("0", len(s)-20)) ||
|
||||||
|
strings.HasPrefix(s, "0001-01-01T00:00:00."+strings.Repeat("0", len(s)-20)) {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
layout := "2006-01-02 15:04:05." + strings.Repeat("0", len(s)-20)
|
layout := "2006-01-02 15:04:05." + strings.Repeat("0", len(s)-20)
|
||||||
dt, err := time.ParseInLocation(layout, s, originalLocation)
|
dt, err := time.ParseInLocation(layout, s, originalLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -68,11 +81,11 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
return &dt, nil
|
return &dt, nil
|
||||||
} else if len(s) == 8 && s[2] == ':' && s[5] == ':' {
|
} else if len(s) == 8 && s[2] == ':' && s[5] == ':' {
|
||||||
currentDate := time.Now()
|
|
||||||
dt, err := time.ParseInLocation("15:04:05", s, originalLocation)
|
dt, err := time.ParseInLocation("15:04:05", s, originalLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
currentDate := time.Now()
|
||||||
// add current date for correct time locations
|
// add current date for correct time locations
|
||||||
dt = dt.AddDate(currentDate.Year(), int(currentDate.Month()), currentDate.Day())
|
dt = dt.AddDate(currentDate.Year(), int(currentDate.Month()), currentDate.Day())
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
|
@ -82,6 +95,9 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
|
||||||
} else {
|
} else {
|
||||||
i, err := strconv.ParseInt(s, 10, 64)
|
i, err := strconv.ParseInt(s, 10, 64)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
if i == 0 {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
tm := time.Unix(i, 0).In(convertedLocation)
|
tm := time.Unix(i, 0).In(convertedLocation)
|
||||||
return &tm, nil
|
return &tm, nil
|
||||||
}
|
}
|
||||||
|
@ -108,6 +124,9 @@ func AsTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
|
||||||
if !t.Valid {
|
if !t.Valid {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
if utils.IsTimeZero(t.Time) {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
z, _ := t.Time.Zone()
|
z, _ := t.Time.Zone()
|
||||||
if len(z) == 0 || t.Time.Year() == 0 || t.Time.Location().String() != dbLoc.String() {
|
if len(z) == 0 || t.Time.Year() == 0 || t.Time.Location().String() != dbLoc.String() {
|
||||||
tm := time.Date(t.Time.Year(), t.Time.Month(), t.Time.Day(), t.Time.Hour(),
|
tm := time.Date(t.Time.Year(), t.Time.Month(), t.Time.Day(), t.Time.Hour(),
|
||||||
|
@ -117,6 +136,9 @@ func AsTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
|
||||||
tm := t.Time.In(uiLoc)
|
tm := t.Time.In(uiLoc)
|
||||||
return &tm, nil
|
return &tm, nil
|
||||||
case *time.Time:
|
case *time.Time:
|
||||||
|
if utils.IsTimeZero(*t) {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
z, _ := t.Zone()
|
z, _ := t.Zone()
|
||||||
if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbLoc.String() {
|
if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbLoc.String() {
|
||||||
tm := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
|
tm := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
|
||||||
|
@ -126,6 +148,9 @@ func AsTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
|
||||||
tm := t.In(uiLoc)
|
tm := t.In(uiLoc)
|
||||||
return &tm, nil
|
return &tm, nil
|
||||||
case time.Time:
|
case time.Time:
|
||||||
|
if utils.IsTimeZero(t) {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
z, _ := t.Zone()
|
z, _ := t.Zone()
|
||||||
if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbLoc.String() {
|
if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbLoc.String() {
|
||||||
tm := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
|
tm := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
|
||||||
|
@ -135,12 +160,21 @@ func AsTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
|
||||||
tm := t.In(uiLoc)
|
tm := t.In(uiLoc)
|
||||||
return &tm, nil
|
return &tm, nil
|
||||||
case int:
|
case int:
|
||||||
|
if t == 0 {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
tm := time.Unix(int64(t), 0).In(uiLoc)
|
tm := time.Unix(int64(t), 0).In(uiLoc)
|
||||||
return &tm, nil
|
return &tm, nil
|
||||||
case int64:
|
case int64:
|
||||||
|
if t == 0 {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
tm := time.Unix(t, 0).In(uiLoc)
|
tm := time.Unix(t, 0).In(uiLoc)
|
||||||
return &tm, nil
|
return &tm, nil
|
||||||
case *sql.NullInt64:
|
case *sql.NullInt64:
|
||||||
|
if t.Int64 == 0 {
|
||||||
|
return &time.Time{}, nil
|
||||||
|
}
|
||||||
tm := time.Unix(t.Int64, 0).In(uiLoc)
|
tm := time.Unix(t.Int64, 0).In(uiLoc)
|
||||||
return &tm, nil
|
return &tm, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -320,11 +320,7 @@ func (db *mssql) SQLType(c *schemas.Column) string {
|
||||||
res += "(MAX)"
|
res += "(MAX)"
|
||||||
}
|
}
|
||||||
case schemas.TimeStamp, schemas.DateTime:
|
case schemas.TimeStamp, schemas.DateTime:
|
||||||
if c.Length > 3 {
|
return "DATETIME2"
|
||||||
res = "DATETIME2"
|
|
||||||
} else {
|
|
||||||
return schemas.DateTime
|
|
||||||
}
|
|
||||||
case schemas.TimeStampz:
|
case schemas.TimeStampz:
|
||||||
res = "DATETIMEOFFSET"
|
res = "DATETIMEOFFSET"
|
||||||
c.Length = 7
|
c.Length = 7
|
||||||
|
|
|
@ -690,11 +690,8 @@ func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
||||||
if col.SQLType.IsNumeric() {
|
if col.SQLType.IsNumeric() {
|
||||||
cond = builder.Eq{colName: 0}
|
cond = builder.Eq{colName: 0}
|
||||||
} else {
|
} else {
|
||||||
// FIXME: mssql: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
|
|
||||||
if statement.dialect.URI().DBType != schemas.MSSQL {
|
|
||||||
cond = builder.Eq{colName: utils.ZeroTime1}
|
cond = builder.Eq{colName: utils.ZeroTime1}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if col.Nullable {
|
if col.Nullable {
|
||||||
cond = cond.Or(builder.IsNull{colName})
|
cond = cond.Or(builder.IsNull{colName})
|
||||||
|
|
|
@ -471,7 +471,8 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.IsDeleted {
|
if col.IsDeleted {
|
||||||
arg, err := dialects.FormatColumnTime(session.engine.dialect, session.engine.DatabaseTZ, col, time.Time{})
|
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, session.engine.DatabaseTZ)
|
||||||
|
arg, err := dialects.FormatColumnTime(session.engine.dialect, session.engine.DatabaseTZ, col, zeroTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1209,3 +1209,80 @@ func TestInsertMultipleMap(t *testing.T) {
|
||||||
Name: "xiaolunwen",
|
Name: "xiaolunwen",
|
||||||
}, res[1])
|
}, res[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInsertNotDeleted(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, testEngine.GetTZDatabase())
|
||||||
|
type TestInsertNotDeletedStructNotRight struct {
|
||||||
|
ID uint64 `xorm:"'ID' pk autoincr"`
|
||||||
|
DeletedAt time.Time `xorm:"'DELETED_AT' deleted notnull"`
|
||||||
|
}
|
||||||
|
// notnull tag will be ignored
|
||||||
|
err := testEngine.Sync(new(TestInsertNotDeletedStructNotRight))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
type TestInsertNotDeletedStruct struct {
|
||||||
|
ID uint64 `xorm:"'ID' pk autoincr"`
|
||||||
|
DeletedAt time.Time `xorm:"'DELETED_AT' deleted"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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.In(testEngine.GetTZDatabase()).Format("2006-01-02 15:04:05"), zeroTime.Format("2006-01-02 15:04:05"))
|
||||||
|
|
||||||
|
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.In(testEngine.GetTZDatabase()).Format("2006-01-02 15:04:05"), zeroTime.Format("2006-01-02 15:04:05"))
|
||||||
|
}
|
||||||
|
|
||||||
|
type MyAutoTimeFields1 struct {
|
||||||
|
Id int64
|
||||||
|
Dt time.Time `xorm:"created DATETIME"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (MyAutoTimeFields1) TableName() string {
|
||||||
|
return "my_auto_time_fields"
|
||||||
|
}
|
||||||
|
|
||||||
|
type MyAutoTimeFields2 struct {
|
||||||
|
Id int64
|
||||||
|
Dt time.Time `xorm:"created"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (MyAutoTimeFields2) TableName() string {
|
||||||
|
return "my_auto_time_fields"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoTimeFields(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
|
||||||
|
assertSync(t, new(MyAutoTimeFields1))
|
||||||
|
|
||||||
|
_, err := testEngine.Insert(&MyAutoTimeFields1{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var res []MyAutoTimeFields2
|
||||||
|
assert.NoError(t, testEngine.Find(&res))
|
||||||
|
assert.EqualValues(t, 1, len(res))
|
||||||
|
|
||||||
|
_, err = testEngine.Insert(&MyAutoTimeFields2{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
res = []MyAutoTimeFields2{}
|
||||||
|
assert.NoError(t, testEngine.Find(&res))
|
||||||
|
assert.EqualValues(t, 2, len(res))
|
||||||
|
}
|
||||||
|
|
|
@ -7,11 +7,11 @@ package tests
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"xorm.io/xorm/convert"
|
||||||
"xorm.io/xorm/internal/utils"
|
"xorm.io/xorm/internal/utils"
|
||||||
"xorm.io/xorm/names"
|
"xorm.io/xorm/names"
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
|
@ -1201,8 +1201,10 @@ func TestTagTime(t *testing.T) {
|
||||||
has, err = testEngine.Table("tag_u_t_c_struct").Cols("created").Get(&tm)
|
has, err = testEngine.Table("tag_u_t_c_struct").Cols("created").Get(&tm)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.True(t, has)
|
assert.True(t, has)
|
||||||
assert.EqualValues(t, s.Created.UTC().Format("2006-01-02 15:04:05"),
|
|
||||||
strings.ReplaceAll(strings.ReplaceAll(tm, "T", " "), "Z", ""))
|
tmTime, err := convert.String2Time(tm, time.UTC, time.UTC)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, s.Created.UTC().Format("2006-01-02 15:04:05"), tmTime.Format("2006-01-02 15:04:05"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTagAutoIncr(t *testing.T) {
|
func TestTagAutoIncr(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue