Fix mssql timestampz

This commit is contained in:
Lunny Xiao 2021-08-03 23:23:44 +08:00
parent ef81f61ada
commit 8401f51374
2 changed files with 45 additions and 17 deletions

View File

@ -320,7 +320,7 @@ func (db *mssql) SQLType(c *schemas.Column) string {
res = schemas.DateTime res = schemas.DateTime
} }
case schemas.TimeStampz: case schemas.TimeStampz:
res = "DATETIMEOFFSET" res = "DATETIME2"
c.Length = 7 c.Length = 7
case schemas.MediumInt, schemas.TinyInt, schemas.SmallInt, schemas.UnsignedMediumInt, schemas.UnsignedTinyInt, schemas.UnsignedSmallInt: case schemas.MediumInt, schemas.TinyInt, schemas.SmallInt, schemas.UnsignedMediumInt, schemas.UnsignedTinyInt, schemas.UnsignedSmallInt:
res = schemas.Int res = schemas.Int
@ -502,6 +502,8 @@ func (db *mssql) GetColumns(queryer core.Queryer, ctx context.Context, tableName
col.Length /= 2 col.Length /= 2
col.Length2 /= 2 col.Length2 /= 2
} }
case "DATETIME2":
col.SQLType = schemas.SQLType{Name: schemas.DateTime, DefaultLength: 7, DefaultLength2: 0}
case "IMAGE": case "IMAGE":
col.SQLType = schemas.SQLType{Name: schemas.VarBinary, DefaultLength: 0, DefaultLength2: 0} col.SQLType = schemas.SQLType{Name: schemas.VarBinary, DefaultLength: 0, DefaultLength2: 0}
case "NCHAR": case "NCHAR":

View File

@ -571,6 +571,7 @@ func TestDeletedInt64(t *testing.T) {
} }
func TestTimestamp(t *testing.T) { func TestTimestamp(t *testing.T) {
{
assert.NoError(t, PrepareEngine()) assert.NoError(t, PrepareEngine())
type TimestampStruct struct { type TimestampStruct struct {
@ -593,3 +594,28 @@ func TestTimestamp(t *testing.T) {
assert.True(t, has) assert.True(t, has)
assert.EqualValues(t, formatTime(d1.InsertTime, 6), formatTime(d2.InsertTime, 6)) assert.EqualValues(t, formatTime(d1.InsertTime, 6), formatTime(d2.InsertTime, 6))
} }
{
assert.NoError(t, PrepareEngine())
type TimestampzStruct struct {
Id int64
InsertTime time.Time `xorm:"TIMESTAMPZ"`
}
assertSync(t, new(TimestampzStruct))
var d3 = TimestampzStruct{
InsertTime: time.Now(),
}
cnt, err := testEngine.Insert(&d3)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var d4 TimestampzStruct
has, err := testEngine.ID(d3.Id).Get(&d4)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, formatTime(d3.InsertTime, 6), formatTime(d4.InsertTime, 6))
}
}