Signed-off-by: 商讯在线 <swhbox@foxmail.com>
This commit is contained in:
商讯在线 2014-04-15 23:27:08 +08:00
parent 9682b65488
commit aa9b62f275
5 changed files with 57 additions and 29 deletions

View File

@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"sync"
"time"
)
const (
@ -71,6 +72,7 @@ type Engine struct {
Logger io.Writer
Cacher Cacher
UseCache bool
TimeZone string
}
func (engine *Engine) SetMapper(mapper IMapper) {
@ -1071,3 +1073,41 @@ func (engine *Engine) Import(ddlPath string) ([]sql.Result, error) {
}
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 {
args = append(args, time.Now())
args = append(args, session.Engine.NowTime(col.SQLType.Name))
} else {
arg, err := session.value2Interface(col, fieldValue)
if err != nil {
@ -1835,7 +1835,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
}
}
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
args = append(args, time.Now())
args = append(args, session.Engine.NowTime(col.SQLType.Name))
} else {
arg, err := session.value2Interface(col, fieldValue)
if err != nil {
@ -2401,20 +2401,13 @@ func (session *Session) value2Interface(col *Column, fieldValue reflect.Value) (
return nil, nil
}
}
if col.SQLType.Name == Time {
//s := fieldValue.Interface().(time.Time).Format("2006-01-02 15:04:05 -0700")
s := fieldValue.Interface().(time.Time).Format(time.RFC3339)
return s[11:19], nil
} else if col.SQLType.Name == Date {
return fieldValue.Interface().(time.Time).Format("2006-01-02"), 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
switch fieldValue.Interface().(type) {
case time.Time:
tf := session.Engine.FormatTime(col.SQLType.Name,fieldValue.Interface().(time.Time))
return tf, nil
default:
return fieldValue.Interface(), nil
}
return fieldValue.Interface(), nil
}
if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok {
if len(fieldTable.PrimaryKeys) == 1 {
@ -2878,7 +2871,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
if session.Statement.UseAutoTime && 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

View File

@ -332,16 +332,7 @@ func buildConditions(engine *Engine, table *Table, bean interface{},
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) {
continue
}
var str string
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
}
val = engine.FormatTime(col.SQLType.Name, t)
} else {
engine.autoMapType(fieldValue)
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 {
args = append(args, time.Now())
args = append(args, session.Engine.NowTime(col.SQLType.Name))
} else if col.IsVersion && session.Statement.checkVersion {
args = append(args, 1)
} else {

View File

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