Merge 5c8daa6983
into 7a9249de33
This commit is contained in:
commit
38025a78b6
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue