fix bug when insert/update Time type

This commit is contained in:
chendy 2020-08-18 16:46:53 +08:00
parent 53a5e6c79d
commit a45b3fb4cb
3 changed files with 33 additions and 8 deletions

View File

@ -1272,3 +1272,12 @@ func (engine *Engine) Transaction(f func(*Session) (interface{}, error)) (interf
return result, nil return result, nil
} }
// convertTime convert time
func (engine *Engine) convertTime(col *schemas.Column, t time.Time) (interface{}, time.Time) {
var tz = engine.DatabaseTZ
if !col.DisableTimeZone && col.TimeZone != nil {
tz = col.TimeZone
}
return dialects.FormatTime(engine.dialect, col.SQLType.Name, t.In(tz)), t.In(engine.TZLocation)
}

View File

@ -11,6 +11,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"time"
"xorm.io/xorm/internal/utils" "xorm.io/xorm/internal/utils"
"xorm.io/xorm/schemas" "xorm.io/xorm/schemas"
@ -549,11 +550,18 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
} else if col.IsVersion && session.statement.CheckVersion { } else if col.IsVersion && session.statement.CheckVersion {
args = append(args, 1) args = append(args, 1)
} else { } else {
arg, err := session.statement.Value2Interface(col, fieldValue) fieldType := fieldValue.Type()
if err != nil { if fieldType.ConvertibleTo(schemas.TimeType) && session.engine.dialect.URI().DBType == schemas.ORACLE {
return colNames, args, err t := fieldValue.Convert(schemas.TimeType).Interface().(time.Time)
_, t = session.engine.convertTime(col, t)
args = append(args, t)
} else {
arg, err := session.statement.Value2Interface(col, fieldValue)
if err != nil {
return colNames, args, err
}
args = append(args, arg)
} }
args = append(args, arg)
} }
colNames = append(colNames, col.Name) colNames = append(colNames, col.Name)

View File

@ -10,6 +10,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"time"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm/caches" "xorm.io/xorm/caches"
@ -519,11 +520,18 @@ func (session *Session) genUpdateColumns(bean interface{}) ([]string, []interfac
} else if col.IsVersion && session.statement.CheckVersion { } else if col.IsVersion && session.statement.CheckVersion {
args = append(args, 1) args = append(args, 1)
} else { } else {
arg, err := session.statement.Value2Interface(col, fieldValue) fieldType := fieldValue.Type()
if err != nil { if fieldType.ConvertibleTo(schemas.TimeType) && session.engine.dialect.URI().DBType == schemas.ORACLE {
return colNames, args, err t := fieldValue.Convert(schemas.TimeType).Interface().(time.Time)
_, t = session.engine.convertTime(col, t)
args = append(args, t)
} else {
arg, err := session.statement.Value2Interface(col, fieldValue)
if err != nil {
return colNames, args, err
}
args = append(args, arg)
} }
args = append(args, arg)
} }
colNames = append(colNames, session.engine.Quote(col.Name)+" = ?") colNames = append(colNames, session.engine.Quote(col.Name)+" = ?")