add new time type timestampz for datetime with time zone

This commit is contained in:
Lunny Xiao 2013-11-28 21:24:38 +08:00
parent afedb683c6
commit 20bfc62f13
6 changed files with 33 additions and 11 deletions

View File

@ -1632,6 +1632,7 @@ func testBool(engine *Engine, t *testing.T) {
type TTime struct { type TTime struct {
Id int64 Id int64
T time.Time T time.Time
Tz time.Time `xorm:"timestampz"`
} }
func testTime(engine *Engine, t *testing.T) { func testTime(engine *Engine, t *testing.T) {
@ -1660,12 +1661,20 @@ func testTime(engine *Engine, t *testing.T) {
panic(err) panic(err)
} }
tt3 := &TTime{T: time.Now()} tt3 := &TTime{T: time.Now(), Tz: time.Now()}
_, err = engine.Insert(tt3) _, err = engine.Insert(tt3)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
panic(err) panic(err)
} }
tt4s := make([]TTime, 0)
err = engine.Find(&tt4s)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println("=======\n", tt4s, "=======\n")
} }
func testPrefixTableName(engine *Engine, t *testing.T) { func testPrefixTableName(engine *Engine, t *testing.T) {

View File

@ -90,6 +90,9 @@ func (db *mysql) SqlType(c *Column) string {
res = BigInt res = BigInt
case Bytea: case Bytea:
res = Blob res = Blob
case TimeStampz:
res = Char
c.Length = 64
default: default:
res = t res = t
} }

View File

@ -72,6 +72,8 @@ func (db *postgres) SqlType(c *Column) string {
return Bytea return Bytea
case DateTime: case DateTime:
res = TimeStamp res = TimeStamp
case TimeStampz:
return "timestamp with time zone"
case Float: case Float:
res = Real res = Real
case TinyText, MediumText, LongText: case TinyText, MediumText, LongText:
@ -180,6 +182,8 @@ func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column,
col.SQLType = SQLType{Varchar, 0, 0} col.SQLType = SQLType{Varchar, 0, 0}
case "timestamp without time zone": case "timestamp without time zone":
col.SQLType = SQLType{DateTime, 0, 0} col.SQLType = SQLType{DateTime, 0, 0}
case "timestamp with time zone":
col.SQLType = SQLType{TimeStampz, 0, 0}
case "double precision": case "double precision":
col.SQLType = SQLType{Double, 0, 0} col.SQLType = SQLType{Double, 0, 0}
case "boolean": case "boolean":

View File

@ -1622,6 +1622,8 @@ func (session *Session) value2Interface(col *Column, fieldValue reflect.Value) (
return s[11:19], nil return s[11:19], nil
} else if col.SQLType.Name == Date { } else if col.SQLType.Name == Date {
return fieldValue.Interface().(time.Time).Format("2006-01-02"), nil return fieldValue.Interface().(time.Time).Format("2006-01-02"), nil
} else if col.SQLType.Name == TimeStampz {
return fieldValue.Interface().(time.Time).Format(time.RFC3339Nano), nil
} }
return fieldValue.Interface(), nil return fieldValue.Interface(), nil
} }

View File

@ -18,6 +18,8 @@ func (db *sqlite3) SqlType(c *Column) string {
switch t := c.SQLType.Name; t { switch t := c.SQLType.Name; t {
case Date, DateTime, TimeStamp, Time: case Date, DateTime, TimeStamp, Time:
return Numeric return Numeric
case TimeStampz:
return Text
case Char, Varchar, TinyText, Text, MediumText, LongText: case Char, Varchar, TinyText, Text, MediumText, LongText:
return Text return Text
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, BigInt, Bool: case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, BigInt, Bool:

View File

@ -46,6 +46,7 @@ var (
DateTime = "DATETIME" DateTime = "DATETIME"
Time = "TIME" Time = "TIME"
TimeStamp = "TIMESTAMP" TimeStamp = "TIMESTAMP"
TimeStampz = "TIMESTAMPZ"
Decimal = "DECIMAL" Decimal = "DECIMAL"
Numeric = "NUMERIC" Numeric = "NUMERIC"
@ -87,6 +88,7 @@ var (
DateTime: true, DateTime: true,
Time: true, Time: true,
TimeStamp: true, TimeStamp: true,
TimeStampz: true,
Decimal: true, Decimal: true,
Numeric: true, Numeric: true,
@ -122,7 +124,7 @@ func Type2SQLType(t reflect.Type) (st SQLType) {
st = SQLType{Double, 0, 0} st = SQLType{Double, 0, 0}
case reflect.Complex64, reflect.Complex128: case reflect.Complex64, reflect.Complex128:
st = SQLType{Varchar, 64, 0} st = SQLType{Varchar, 64, 0}
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice, reflect.Map:
if t.Elem() == reflect.TypeOf(b) { if t.Elem() == reflect.TypeOf(b) {
st = SQLType{Blob, 0, 0} st = SQLType{Blob, 0, 0}
} else { } else {
@ -162,7 +164,7 @@ func SQLType2Type(st SQLType) reflect.Type {
return reflect.TypeOf([]byte{}) return reflect.TypeOf([]byte{})
case Bool: case Bool:
return reflect.TypeOf(true) return reflect.TypeOf(true)
case DateTime, Date, Time, TimeStamp: case DateTime, Date, Time, TimeStamp, TimeStampz:
return reflect.TypeOf(tm) return reflect.TypeOf(tm)
case Decimal, Numeric: case Decimal, Numeric:
return reflect.TypeOf("") return reflect.TypeOf("")