This commit is contained in:
Lunny Xiao 2021-06-12 09:51:31 +08:00
parent 1139445b2e
commit 9b1e746c6c
3 changed files with 21 additions and 5 deletions

View File

@ -10,8 +10,8 @@ import (
"strconv"
)
// ConvertAssignString converts an interface to string
func ConvertAssignString(v interface{}) (string, error) {
// Interface2String converts an interface to string
func Interface2String(v interface{}) (string, error) {
if v == nil {
return "", nil
}

View File

@ -82,7 +82,12 @@ func newEngine(driverName, dataSourceName string, dialect dialects.Dialect, db *
dataSourceName: dataSourceName,
db: db,
logSessionID: false,
DatabaseTZ: time.Local,
}
if dialect.URI().DBType == schemas.SQLITE {
engine.DatabaseTZ = time.UTC
} else {
engine.DatabaseTZ = time.Local
}
logger := log.NewSimpleLogger(os.Stdout)
@ -469,8 +474,6 @@ func formatColumnValue(dbLocation *time.Location, dstDialect dialects.Dialect, d
return "NULL"
}
fmt.Printf("%#v------%v\n", d, col.Name)
if dq, ok := d.(bool); ok && (dstDialect.URI().DBType == schemas.SQLITE ||
dstDialect.URI().DBType == schemas.MSSQL) {
if dq {
@ -1297,6 +1300,14 @@ func (engine *Engine) Import(r io.Reader) ([]sql.Result, error) {
return session.Import(r)
}
func (engine *Engine) columnTime(col *schemas.Column, t *time.Time) interface{} {
var tz = engine.DatabaseTZ
if !col.DisableTimeZone && col.TimeZone != nil {
tz = col.TimeZone
}
return t.In(tz)
}
// nowTime return current time
func (engine *Engine) nowTime(col *schemas.Column) (interface{}, time.Time) {
t := time.Now()

View File

@ -562,6 +562,11 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
if err != nil {
return colNames, args, err
}
if t, ok := arg.(time.Time); ok {
arg = session.engine.columnTime(col, &t)
} else if t, ok := arg.(*time.Time); ok {
arg = session.engine.columnTime(col, t)
}
args = append(args, arg)
}