Merge pull request #98 from admpub/master

Repair Time problem
This commit is contained in:
S.W.H 2014-04-15 23:45:26 +08:00
commit 69701b973d
5 changed files with 57 additions and 29 deletions

View File

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
) )
const ( const (
@ -71,6 +72,7 @@ type Engine struct {
Logger io.Writer Logger io.Writer
Cacher Cacher Cacher Cacher
UseCache bool UseCache bool
TimeZone string
} }
func (engine *Engine) SetMapper(mapper IMapper) { func (engine *Engine) SetMapper(mapper IMapper) {
@ -1071,3 +1073,41 @@ func (engine *Engine) Import(ddlPath string) ([]sql.Result, error) {
} }
return results, lastError return results, lastError
} }
func (engine *Engine) TZTime(t time.Time) (r time.Time) {
switch engine.TimeZone {
case "Local", "L":
r = t.Local()
case "UTC", "U":
fallthrough
default:
r = t.UTC()
}
return
}
func (engine *Engine) NowTime(SQLTypeName string) interface{} {
t := time.Now()
return engine.FormatTime(SQLTypeName, t)
}
func (engine *Engine) FormatTime(SQLTypeName string, t time.Time) (v interface{}) {
switch SQLTypeName {
case Time:
s := engine.TZTime(t).Format("2006-01-02 15:04:05") //time.RFC3339
v = s[11:19]
case Date:
v = engine.TZTime(t).Format("2006-01-02")
case DateTime, TimeStamp:
v = engine.TZTime(t).Format("2006-01-02 15:04:05")
case TimeStampz:
if engine.dialect.DBType() == MSSQL {
v = engine.TZTime(t).Format("2006-01-02T15:04:05.9999999Z07:00")
} else {
v = engine.TZTime(t).Format(time.RFC3339Nano)
}
default:
v = engine.TZTime(t)
}
return
}

View File

@ -1807,7 +1807,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
} }
} }
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime { if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
args = append(args, time.Now()) args = append(args, session.Engine.NowTime(col.SQLType.Name))
} else { } else {
arg, err := session.value2Interface(col, fieldValue) arg, err := session.value2Interface(col, fieldValue)
if err != nil { if err != nil {
@ -1835,7 +1835,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
} }
} }
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime { if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
args = append(args, time.Now()) args = append(args, session.Engine.NowTime(col.SQLType.Name))
} else { } else {
arg, err := session.value2Interface(col, fieldValue) arg, err := session.value2Interface(col, fieldValue)
if err != nil { if err != nil {
@ -2401,20 +2401,13 @@ func (session *Session) value2Interface(col *Column, fieldValue reflect.Value) (
return nil, nil return nil, nil
} }
} }
if col.SQLType.Name == Time { switch fieldValue.Interface().(type) {
//s := fieldValue.Interface().(time.Time).Format("2006-01-02 15:04:05 -0700") case time.Time:
s := fieldValue.Interface().(time.Time).Format(time.RFC3339) tf := session.Engine.FormatTime(col.SQLType.Name,fieldValue.Interface().(time.Time))
return s[11:19], nil return tf, nil
} else if col.SQLType.Name == Date { default:
return fieldValue.Interface().(time.Time).Format("2006-01-02"), nil return fieldValue.Interface(), nil
} else if col.SQLType.Name == TimeStampz {
if session.Engine.dialect.DBType() == MSSQL {
tf := t.Format("2006-01-02T15:04:05.9999999Z07:00")
return tf, nil
}
return fieldValue.Interface().(time.Time).Format(time.RFC3339Nano), nil
} }
return fieldValue.Interface(), nil
} }
if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok { if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok {
if len(fieldTable.PrimaryKeys) == 1 { if len(fieldTable.PrimaryKeys) == 1 {
@ -2878,7 +2871,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
if session.Statement.UseAutoTime && table.Updated != "" { if session.Statement.UseAutoTime && table.Updated != "" {
colNames = append(colNames, session.Engine.Quote(table.Updated)+" = ?") colNames = append(colNames, session.Engine.Quote(table.Updated)+" = ?")
args = append(args, time.Now()) args = append(args, session.Engine.NowTime(table.Columns[table.Updated].SQLType.Name))
} }
var condiColNames []string var condiColNames []string

View File

@ -332,16 +332,7 @@ func buildConditions(engine *Engine, table *Table, bean interface{},
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) { if !requiredField && (t.IsZero() || !fieldValue.IsValid()) {
continue continue
} }
var str string val = engine.FormatTime(col.SQLType.Name, t)
if col.SQLType.Name == Time {
s := t.UTC().Format("2006-01-02 15:04:05")
val = s[11:19]
} else if col.SQLType.Name == Date {
str = t.Format("2006-01-02")
val = str
} else {
val = t
}
} else { } else {
engine.autoMapType(fieldValue) engine.autoMapType(fieldValue)
if table, ok := engine.Tables[fieldValue.Type()]; ok { if table, ok := engine.Tables[fieldValue.Type()]; ok {

View File

@ -456,7 +456,7 @@ func (table *Table) genCols(session *Session, bean interface{}, useCol bool, inc
} }
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime { if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
args = append(args, time.Now()) args = append(args, session.Engine.NowTime(col.SQLType.Name))
} else if col.IsVersion && session.Statement.checkVersion { } else if col.IsVersion && session.Statement.checkVersion {
args = append(args, 1) args = append(args, 1)
} else { } else {

View File

@ -20,8 +20,12 @@ func close(engine *Engine) {
// new a db manager according to the parameter. Currently support four // new a db manager according to the parameter. Currently support four
// drivers // drivers
func NewEngine(driverName string, dataSourceName string) (*Engine, error) { func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
engine := &Engine{DriverName: driverName, engine := &Engine{
DataSourceName: dataSourceName, Filters: make([]Filter, 0)} DriverName: driverName,
DataSourceName: dataSourceName,
Filters: make([]Filter, 0),
TimeZone: "UTC",
}
engine.SetMapper(SnakeMapper{}) engine.SetMapper(SnakeMapper{})
if driverName == SQLITE { if driverName == SQLITE {