This commit is contained in:
BetaCat 2018-09-28 11:42:10 +00:00 committed by GitHub
commit 38025a78b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/go-xorm/builder" "github.com/go-xorm/builder"
"github.com/go-xorm/core" "github.com/go-xorm/core"
@ -191,7 +192,24 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
for _, v := range bValue.MapKeys() { for _, v := range bValue.MapKeys() {
colNames = append(colNames, session.engine.Quote(v.String())+" = ?") 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 { } else {
return 0, ErrParamsType return 0, ErrParamsType

View File

@ -22,6 +22,7 @@ func TestUpdateMap(t *testing.T) {
Id int64 Id int64
Name string Name string
Age int Age int
UpdateAt time.Time
} }
assert.NoError(t, testEngine.Sync2(new(UpdateTable))) assert.NoError(t, testEngine.Sync2(new(UpdateTable)))
@ -38,6 +39,21 @@ func TestUpdateMap(t *testing.T) {
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 1, cnt) 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) { func TestUpdateLimit(t *testing.T) {