From aedf8032fbca0c6c318005f8f363b5c76b804e6c Mon Sep 17 00:00:00 2001 From: chendy Date: Tue, 18 Aug 2020 17:12:11 +0800 Subject: [PATCH] fix bug when update Time type for Oracle --- engine.go | 9 +++++++++ session_insert.go | 16 ++++++++++++---- session_update.go | 16 ++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/engine.go b/engine.go index 7399f41a..2bdf2121 100644 --- a/engine.go +++ b/engine.go @@ -1342,3 +1342,12 @@ func (engine *Engine) Transaction(f func(*Session) (interface{}, error)) (interf 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) +} diff --git a/session_insert.go b/session_insert.go index 5f968151..1d99167b 100644 --- a/session_insert.go +++ b/session_insert.go @@ -11,6 +11,7 @@ import ( "sort" "strconv" "strings" + "time" "xorm.io/xorm/internal/utils" "xorm.io/xorm/schemas" @@ -549,11 +550,18 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac } else if col.IsVersion && session.statement.CheckVersion { args = append(args, 1) } else { - arg, err := session.statement.Value2Interface(col, fieldValue) - if err != nil { - return colNames, args, err + fieldType := fieldValue.Type() + if fieldType.ConvertibleTo(schemas.TimeType) && session.engine.dialect.URI().DBType == schemas.ORACLE { + 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) diff --git a/session_update.go b/session_update.go index 7df8c752..6249f208 100644 --- a/session_update.go +++ b/session_update.go @@ -10,6 +10,7 @@ import ( "reflect" "strconv" "strings" + "time" "xorm.io/builder" "xorm.io/xorm/caches" @@ -519,11 +520,18 @@ func (session *Session) genUpdateColumns(bean interface{}) ([]string, []interfac } else if col.IsVersion && session.statement.CheckVersion { args = append(args, 1) } else { - arg, err := session.statement.Value2Interface(col, fieldValue) - if err != nil { - return colNames, args, err + fieldType := fieldValue.Type() + if fieldType.ConvertibleTo(schemas.TimeType) && session.engine.dialect.URI().DBType == schemas.ORACLE { + 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)+" = ?")