if the param is type of time.Time, it will be automatic shifted to Engine.DatabaseTZ timezone
This commit is contained in:
parent
7a9249de33
commit
5c8daa6983
|
@ -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
|
||||||
|
@ -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()
|
incColumns := session.statement.getInc()
|
||||||
for _, v := range incColumns {
|
for _, v := range incColumns {
|
||||||
colNames = append(colNames, session.engine.Quote(v.colName)+" = "+session.engine.Quote(v.colName)+" + ?")
|
colNames = append(colNames, session.engine.Quote(v.colName)+" = "+session.engine.Quote(v.colName)+" + ?")
|
||||||
args = append(args, v.arg)
|
args = append(args, v.arg)
|
||||||
}
|
}
|
||||||
//for update action to like "column = column - ?"
|
// for update action to like "column = column - ?"
|
||||||
decColumns := session.statement.getDec()
|
decColumns := session.statement.getDec()
|
||||||
for _, v := range decColumns {
|
for _, v := range decColumns {
|
||||||
colNames = append(colNames, session.engine.Quote(v.colName)+" = "+session.engine.Quote(v.colName)+" - ?")
|
colNames = append(colNames, session.engine.Quote(v.colName)+" = "+session.engine.Quote(v.colName)+" - ?")
|
||||||
args = append(args, v.arg)
|
args = append(args, v.arg)
|
||||||
}
|
}
|
||||||
//for update action to like "column = expression"
|
// for update action to like "column = expression"
|
||||||
exprColumns := session.statement.getExpr()
|
exprColumns := session.statement.getExpr()
|
||||||
for _, v := range exprColumns {
|
for _, v := range exprColumns {
|
||||||
colNames = append(colNames, session.engine.Quote(v.colName)+" = "+v.expr)
|
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 {
|
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)
|
session.engine.logger.Debug("[cacheUpdate] clear table ", tableName)
|
||||||
cacher.ClearIds(tableName)
|
cacher.ClearIds(tableName)
|
||||||
cacher.ClearBeans(tableName)
|
cacher.ClearBeans(tableName)
|
||||||
|
|
|
@ -19,9 +19,10 @@ func TestUpdateMap(t *testing.T) {
|
||||||
assert.NoError(t, prepareEngine())
|
assert.NoError(t, prepareEngine())
|
||||||
|
|
||||||
type UpdateTable struct {
|
type UpdateTable struct {
|
||||||
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) {
|
||||||
|
|
Loading…
Reference in New Issue