From 5c8daa69834bac29a056c4bdd785fd464ffa56ba Mon Sep 17 00:00:00 2001 From: zhanghelong Date: Fri, 28 Sep 2018 19:20:03 +0800 Subject: [PATCH] if the param is type of time.Time, it will be automatic shifted to Engine.DatabaseTZ timezone --- session_update.go | 28 +++++++++++++++++++++++----- session_update_test.go | 22 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/session_update.go b/session_update.go index 42dfaacd..7ef68319 100644 --- a/session_update.go +++ b/session_update.go @@ -10,6 +10,7 @@ import ( "reflect" "strconv" "strings" + "time" "github.com/go-xorm/builder" "github.com/go-xorm/core" @@ -191,7 +192,24 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 for _, v := range bValue.MapKeys() { colNames = append(colNames, session.engine.Quote(v.String())+" = ?") - args = append(args, bValue.MapIndex(v).Interface()) + + val := bValue.MapIndex(v).Interface() + fType := reflect.TypeOf(val) + if fType.Kind() == reflect.Struct { + if fType.ConvertibleTo(core.TimeType) { + t := val.(time.Time) + // if the param is in specific timezone + if t.Location() != session.statement.Engine.DatabaseTZ && + t.Location() != session.statement.Engine.TZLocation { + val = t + } else { + // shift to Engine.DatabaseTZ timezone + val = t.In(session.statement.Engine.DatabaseTZ) + } + } + } + + args = append(args, val) } } else { return 0, ErrParamsType @@ -217,19 +235,19 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 } } - //for update action to like "column = column + ?" + // for update action to like "column = column + ?" incColumns := session.statement.getInc() for _, v := range incColumns { colNames = append(colNames, session.engine.Quote(v.colName)+" = "+session.engine.Quote(v.colName)+" + ?") args = append(args, v.arg) } - //for update action to like "column = column - ?" + // for update action to like "column = column - ?" decColumns := session.statement.getDec() for _, v := range decColumns { colNames = append(colNames, session.engine.Quote(v.colName)+" = "+session.engine.Quote(v.colName)+" - ?") args = append(args, v.arg) } - //for update action to like "column = expression" + // for update action to like "column = expression" exprColumns := session.statement.getExpr() for _, v := range exprColumns { colNames = append(colNames, session.engine.Quote(v.colName)+" = "+v.expr) @@ -362,7 +380,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 } if cacher := session.engine.getCacher(tableName); cacher != nil && session.statement.UseCache { - //session.cacheUpdate(table, tableName, sqlStr, args...) + // session.cacheUpdate(table, tableName, sqlStr, args...) session.engine.logger.Debug("[cacheUpdate] clear table ", tableName) cacher.ClearIds(tableName) cacher.ClearBeans(tableName) diff --git a/session_update_test.go b/session_update_test.go index 2a7005ee..43a6ef14 100644 --- a/session_update_test.go +++ b/session_update_test.go @@ -19,9 +19,10 @@ func TestUpdateMap(t *testing.T) { assert.NoError(t, prepareEngine()) type UpdateTable struct { - Id int64 - Name string - Age int + Id int64 + Name string + Age int + UpdateAt time.Time } assert.NoError(t, testEngine.Sync2(new(UpdateTable))) @@ -38,6 +39,21 @@ func TestUpdateMap(t *testing.T) { }) assert.NoError(t, err) assert.EqualValues(t, 1, cnt) + + now := time.Now() + cnt, err = testEngine.Table("update_table").Where("id = ?", tb.Id).Update(map[string]interface{}{ + "name": "test2", + "age": 36, + "update_at": now, + }) + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + var data UpdateTable + has, err := testEngine.Table("update_table").Where("id = ?", tb.Id).Get(&data) + assert.NoError(t, err) + assert.EqualValues(t, true, has) + assert.EqualValues(t, now.Unix(), data.UpdateAt.Unix()) } func TestUpdateLimit(t *testing.T) {