2017-01-03 03:20:28 -05:00
|
|
|
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
"xorm.io/builder"
|
|
|
|
"xorm.io/core"
|
2017-01-03 03:20:28 -05:00
|
|
|
)
|
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
func (session *Session) cacheUpdate(table *core.Table, tableName, sqlStr string, args ...interface{}) error {
|
|
|
|
if table == nil ||
|
2017-08-22 07:39:52 -04:00
|
|
|
session.tx != nil {
|
2017-01-03 03:20:28 -05:00
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
oldhead, newsql := session.statement.convertUpdateSQL(sqlStr)
|
2017-01-03 03:20:28 -05:00
|
|
|
if newsql == "" {
|
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
for _, filter := range session.engine.dialect.Filters() {
|
2017-09-13 01:18:22 -04:00
|
|
|
newsql = filter.Do(newsql, session.engine.dialect, table)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheUpdate] new sql", oldhead, newsql)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
|
|
|
var nStart int
|
|
|
|
if len(args) > 0 {
|
|
|
|
if strings.Index(sqlStr, "?") > -1 {
|
|
|
|
nStart = strings.Count(oldhead, "?")
|
|
|
|
} else {
|
|
|
|
// only for pq, TODO: if any other databse?
|
|
|
|
nStart = strings.Count(oldhead, "$")
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 01:18:22 -04:00
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
cacher := session.engine.getCacher(tableName)
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheUpdate] get cache sql", newsql, args[nStart:])
|
2017-01-03 03:20:28 -05:00
|
|
|
ids, err := core.GetCacheSql(cacher, tableName, newsql, args[nStart:])
|
|
|
|
if err != nil {
|
2017-09-13 01:18:22 -04:00
|
|
|
rows, err := session.NoCache().queryRows(newsql, args[nStart:]...)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
ids = make([]core.PK, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var res = make([]string, len(table.PrimaryKeys))
|
|
|
|
err = rows.ScanSlice(&res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
|
|
|
|
for i, col := range table.PKColumns() {
|
|
|
|
if col.SQLType.IsNumeric() {
|
|
|
|
n, err := strconv.ParseInt(res[i], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pk[i] = n
|
|
|
|
} else if col.SQLType.IsText() {
|
|
|
|
pk[i] = res[i]
|
|
|
|
} else {
|
|
|
|
return errors.New("not supported")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ids = append(ids, pk)
|
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheUpdate] find updated id", ids)
|
2017-01-03 03:20:28 -05:00
|
|
|
} /*else {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.LogDebug("[xorm:cacheUpdate] del cached sql:", tableName, newsql, args)
|
2017-01-03 03:20:28 -05:00
|
|
|
cacher.DelIds(tableName, genSqlKey(newsql, args))
|
|
|
|
}*/
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
sid, err := id.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if bean := cacher.GetBean(tableName, sid); bean != nil {
|
|
|
|
sqls := splitNNoCase(sqlStr, "where", 2)
|
|
|
|
if len(sqls) == 0 || len(sqls) > 2 {
|
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
sqls = splitNNoCase(sqls[0], "set", 2)
|
|
|
|
if len(sqls) != 2 {
|
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
kvs := strings.Split(strings.TrimSpace(sqls[1]), ",")
|
2019-08-26 22:17:23 -04:00
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
for idx, kv := range kvs {
|
|
|
|
sps := strings.SplitN(kv, "=", 2)
|
|
|
|
sps2 := strings.Split(sps[0], ".")
|
|
|
|
colName := sps2[len(sps2)-1]
|
2019-08-26 22:17:23 -04:00
|
|
|
// treat quote prefix, suffix and '`' as quotes
|
|
|
|
quotes := append(strings.Split(session.engine.Quote(""), ""), "`")
|
|
|
|
if strings.ContainsAny(colName, strings.Join(quotes, "")) {
|
|
|
|
colName = strings.TrimSpace(eraseAny(colName, quotes...))
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheUpdate] cannot find column", tableName, colName)
|
2017-01-03 03:20:28 -05:00
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
if col := table.GetColumn(colName); col != nil {
|
|
|
|
fieldValue, err := col.ValueOf(bean)
|
|
|
|
if err != nil {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Error(err)
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheUpdate] set bean field", bean, colName, fieldValue.Interface())
|
|
|
|
if col.IsVersion && session.statement.checkVersion {
|
2018-12-11 20:01:41 -05:00
|
|
|
session.incrVersionFieldValue(fieldValue)
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
|
|
|
fieldValue.Set(reflect.ValueOf(args[idx]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Errorf("[cacheUpdate] ERROR: column %v is not table %v's",
|
2017-01-03 03:20:28 -05:00
|
|
|
colName, table.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheUpdate] update cache", tableName, id, bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
cacher.PutBean(tableName, sid, bean)
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheUpdate] clear cached table sql:", tableName)
|
2017-01-03 03:20:28 -05:00
|
|
|
cacher.ClearIds(tableName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update records, bean's non-empty fields are updated contents,
|
|
|
|
// condiBean' non-empty filds are conditions
|
|
|
|
// CAUTION:
|
|
|
|
// 1.bool will defaultly be updated content nor conditions
|
|
|
|
// You should call UseBool if you have bool to use.
|
|
|
|
// 2.float32 & float64 may be not inexact as conditions
|
|
|
|
func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int64, error) {
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.isAutoClose {
|
2017-01-03 03:20:28 -05:00
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
if session.statement.lastError != nil {
|
|
|
|
return 0, session.statement.lastError
|
|
|
|
}
|
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
v := rValue(bean)
|
|
|
|
t := v.Type()
|
|
|
|
|
|
|
|
var colNames []string
|
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
// handle before update processors
|
|
|
|
for _, closure := range session.beforeClosures {
|
|
|
|
closure(bean)
|
|
|
|
}
|
|
|
|
cleanupProcessorsClosures(&session.beforeClosures) // cleanup after used
|
|
|
|
if processor, ok := interface{}(bean).(BeforeUpdateProcessor); ok {
|
|
|
|
processor.BeforeUpdate()
|
|
|
|
}
|
|
|
|
// --
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var isMap = t.Kind() == reflect.Map
|
|
|
|
var isStruct = t.Kind() == reflect.Struct
|
|
|
|
if isStruct {
|
2018-07-19 22:10:17 -04:00
|
|
|
if err := session.statement.setRefBean(bean); err != nil {
|
2017-05-01 20:50:33 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2017-01-03 03:20:28 -05:00
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.statement.ColumnStr == "" {
|
2018-07-19 22:10:17 -04:00
|
|
|
colNames, args = session.statement.buildUpdates(bean, false, false,
|
|
|
|
false, false, true)
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
2018-07-19 22:10:17 -04:00
|
|
|
colNames, args, err = session.genUpdateColumns(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if isMap {
|
|
|
|
colNames = make([]string, 0)
|
|
|
|
args = make([]interface{}, 0)
|
|
|
|
bValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
|
|
|
|
for _, v := range bValue.MapKeys() {
|
2017-08-22 07:39:52 -04:00
|
|
|
colNames = append(colNames, session.engine.Quote(v.String())+" = ?")
|
2017-01-03 03:20:28 -05:00
|
|
|
args = append(args, bValue.MapIndex(v).Interface())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0, ErrParamsType
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
table := session.statement.RefTable
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.statement.UseAutoTime && table != nil && table.Updated != "" {
|
2018-07-19 22:10:17 -04:00
|
|
|
if !session.statement.columnMap.contain(table.Updated) &&
|
|
|
|
!session.statement.omitColumnMap.contain(table.Updated) {
|
2017-09-14 23:23:48 -04:00
|
|
|
colNames = append(colNames, session.engine.Quote(table.Updated)+" = ?")
|
|
|
|
col := table.UpdatedColumn()
|
2017-10-01 12:52:35 -04:00
|
|
|
val, t := session.engine.nowTime(col)
|
2017-09-14 23:23:48 -04:00
|
|
|
args = append(args, val)
|
|
|
|
|
|
|
|
var colName = col.Name
|
|
|
|
if isStruct {
|
|
|
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
|
|
|
col := table.GetColumn(colName)
|
|
|
|
setColumnTime(bean, col, t)
|
|
|
|
})
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 22:17:23 -04:00
|
|
|
// for update action to like "column = column + ?"
|
2019-09-24 09:22:39 -04:00
|
|
|
incColumns := session.statement.incrColumns
|
|
|
|
for i, colName := range incColumns.colNames {
|
|
|
|
colNames = append(colNames, session.engine.Quote(colName)+" = "+session.engine.Quote(colName)+" + ?")
|
|
|
|
args = append(args, incColumns.args[i])
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2019-08-26 22:17:23 -04:00
|
|
|
// for update action to like "column = column - ?"
|
2019-09-24 09:22:39 -04:00
|
|
|
decColumns := session.statement.decrColumns
|
|
|
|
for i, colName := range decColumns.colNames {
|
|
|
|
colNames = append(colNames, session.engine.Quote(colName)+" = "+session.engine.Quote(colName)+" - ?")
|
|
|
|
args = append(args, decColumns.args[i])
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2019-08-26 22:17:23 -04:00
|
|
|
// for update action to like "column = expression"
|
2019-09-24 09:22:39 -04:00
|
|
|
exprColumns := session.statement.exprColumns
|
|
|
|
for i, colName := range exprColumns.colNames {
|
|
|
|
switch tp := exprColumns.args[i].(type) {
|
|
|
|
case string:
|
2020-01-20 10:45:14 -05:00
|
|
|
if len(tp) == 0 {
|
|
|
|
tp = "''"
|
|
|
|
}
|
|
|
|
colNames = append(colNames, session.engine.Quote(colName)+"="+tp)
|
2019-09-24 09:22:39 -04:00
|
|
|
case *builder.Builder:
|
|
|
|
subQuery, subArgs, err := builder.ToSQL(tp)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-01-20 10:45:14 -05:00
|
|
|
colNames = append(colNames, session.engine.Quote(colName)+"=("+subQuery+")")
|
2019-09-24 09:22:39 -04:00
|
|
|
args = append(args, subArgs...)
|
2020-01-20 10:45:14 -05:00
|
|
|
default:
|
|
|
|
colNames = append(colNames, session.engine.Quote(colName)+"=?")
|
|
|
|
args = append(args, exprColumns.args[i])
|
2019-09-24 09:22:39 -04:00
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
if err = session.statement.processIDParam(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
|
|
|
var autoCond builder.Cond
|
2019-06-23 11:22:43 -04:00
|
|
|
if !session.statement.noAutoCondition {
|
|
|
|
condBeanIsStruct := false
|
|
|
|
if len(condiBean) > 0 {
|
|
|
|
if c, ok := condiBean[0].(map[string]interface{}); ok {
|
|
|
|
autoCond = builder.Eq(c)
|
|
|
|
} else {
|
|
|
|
ct := reflect.TypeOf(condiBean[0])
|
|
|
|
k := ct.Kind()
|
|
|
|
if k == reflect.Ptr {
|
|
|
|
k = ct.Elem().Kind()
|
|
|
|
}
|
|
|
|
if k == reflect.Struct {
|
|
|
|
var err error
|
|
|
|
autoCond, err = session.statement.buildConds(session.statement.RefTable, condiBean[0], true, true, false, true, false)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
condBeanIsStruct = true
|
|
|
|
} else {
|
|
|
|
return 0, ErrConditionType
|
|
|
|
}
|
2018-01-27 10:20:59 -05:00
|
|
|
}
|
2019-06-23 11:22:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !condBeanIsStruct && table != nil {
|
|
|
|
if col := table.DeletedColumn(); col != nil && !session.statement.unscoped { // tag "deleted" is enabled
|
|
|
|
autoCond1 := session.engine.CondDeleted(session.engine.Quote(col.Name))
|
|
|
|
|
|
|
|
if autoCond == nil {
|
|
|
|
autoCond = autoCond1
|
|
|
|
} else {
|
|
|
|
autoCond = autoCond.And(autoCond1)
|
2018-01-27 10:20:59 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
st := &session.statement
|
2017-01-03 03:20:28 -05:00
|
|
|
|
|
|
|
var sqlStr string
|
|
|
|
var condArgs []interface{}
|
|
|
|
var condSQL string
|
2017-08-22 07:39:52 -04:00
|
|
|
cond := session.statement.cond.And(autoCond)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
var doIncVer = (table != nil && table.Version != "" && session.statement.checkVersion)
|
2017-01-03 03:20:28 -05:00
|
|
|
var verValue *reflect.Value
|
2017-04-06 21:47:25 -04:00
|
|
|
if doIncVer {
|
2017-01-03 03:20:28 -05:00
|
|
|
verValue, err = table.VersionColumn().ValueOf(bean)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
cond = cond.And(builder.Eq{session.engine.Quote(table.Version): verValue.Interface()})
|
|
|
|
colNames = append(colNames, session.engine.Quote(table.Version)+" = "+session.engine.Quote(table.Version)+" + 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
condSQL, condArgs, err = builder.ToSQL(cond)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2017-04-06 21:47:25 -04:00
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-04-06 21:47:25 -04:00
|
|
|
if len(condSQL) > 0 {
|
|
|
|
condSQL = "WHERE " + condSQL
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-04-06 21:47:25 -04:00
|
|
|
if st.OrderStr != "" {
|
|
|
|
condSQL = condSQL + fmt.Sprintf(" ORDER BY %v", st.OrderStr)
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
var tableName = session.statement.TableName()
|
2017-04-06 21:47:25 -04:00
|
|
|
// TODO: Oracle support needed
|
|
|
|
var top string
|
|
|
|
if st.LimitN > 0 {
|
|
|
|
if st.Engine.dialect.DBType() == core.MYSQL {
|
2017-01-03 03:20:28 -05:00
|
|
|
condSQL = condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN)
|
2017-04-06 21:47:25 -04:00
|
|
|
} else if st.Engine.dialect.DBType() == core.SQLITE {
|
|
|
|
tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN)
|
|
|
|
cond = cond.And(builder.Expr(fmt.Sprintf("rowid IN (SELECT rowid FROM %v %v)",
|
2017-09-13 01:18:22 -04:00
|
|
|
session.engine.Quote(tableName), tempCondSQL), condArgs...))
|
2017-08-22 07:39:52 -04:00
|
|
|
condSQL, condArgs, err = builder.ToSQL(cond)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-04-06 21:47:25 -04:00
|
|
|
if len(condSQL) > 0 {
|
|
|
|
condSQL = "WHERE " + condSQL
|
|
|
|
}
|
|
|
|
} else if st.Engine.dialect.DBType() == core.POSTGRES {
|
|
|
|
tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN)
|
|
|
|
cond = cond.And(builder.Expr(fmt.Sprintf("CTID IN (SELECT CTID FROM %v %v)",
|
2017-09-13 01:18:22 -04:00
|
|
|
session.engine.Quote(tableName), tempCondSQL), condArgs...))
|
2017-08-22 07:39:52 -04:00
|
|
|
condSQL, condArgs, err = builder.ToSQL(cond)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:47:25 -04:00
|
|
|
if len(condSQL) > 0 {
|
|
|
|
condSQL = "WHERE " + condSQL
|
|
|
|
}
|
|
|
|
} else if st.Engine.dialect.DBType() == core.MSSQL {
|
2017-08-22 07:39:52 -04:00
|
|
|
if st.OrderStr != "" && st.Engine.dialect.DBType() == core.MSSQL &&
|
|
|
|
table != nil && len(table.PrimaryKeys) == 1 {
|
|
|
|
cond = builder.Expr(fmt.Sprintf("%s IN (SELECT TOP (%d) %s FROM %v%v)",
|
|
|
|
table.PrimaryKeys[0], st.LimitN, table.PrimaryKeys[0],
|
2017-09-13 01:18:22 -04:00
|
|
|
session.engine.Quote(tableName), condSQL), condArgs...)
|
2017-08-22 07:39:52 -04:00
|
|
|
|
|
|
|
condSQL, condArgs, err = builder.ToSQL(cond)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if len(condSQL) > 0 {
|
|
|
|
condSQL = "WHERE " + condSQL
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
top = fmt.Sprintf("TOP (%d) ", st.LimitN)
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
if len(colNames) <= 0 {
|
|
|
|
return 0, errors.New("No content found to be updated")
|
|
|
|
}
|
|
|
|
|
2019-12-18 08:07:36 -05:00
|
|
|
var tableAlias = session.engine.Quote(tableName)
|
|
|
|
var fromSQL string
|
|
|
|
if session.statement.TableAlias != "" {
|
|
|
|
switch session.engine.dialect.DBType() {
|
|
|
|
case core.MSSQL:
|
|
|
|
fromSQL = fmt.Sprintf("FROM %s %s ", tableAlias, session.statement.TableAlias)
|
|
|
|
tableAlias = session.statement.TableAlias
|
|
|
|
default:
|
|
|
|
tableAlias = fmt.Sprintf("%s AS %s", tableAlias, session.statement.TableAlias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlStr = fmt.Sprintf("UPDATE %v%v SET %v %v%v",
|
2017-04-06 21:47:25 -04:00
|
|
|
top,
|
2019-12-18 08:07:36 -05:00
|
|
|
tableAlias,
|
2017-04-06 21:47:25 -04:00
|
|
|
strings.Join(colNames, ", "),
|
2019-12-18 08:07:36 -05:00
|
|
|
fromSQL,
|
2017-04-06 21:47:25 -04:00
|
|
|
condSQL)
|
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
res, err := session.exec(sqlStr, append(args, condArgs...)...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if doIncVer {
|
|
|
|
if verValue != nil && verValue.IsValid() && verValue.CanSet() {
|
2018-12-11 20:01:41 -05:00
|
|
|
session.incrVersionFieldValue(verValue)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
if cacher := session.engine.getCacher(tableName); cacher != nil && session.statement.UseCache {
|
2019-08-26 22:17:23 -04:00
|
|
|
// session.cacheUpdate(table, tableName, sqlStr, args...)
|
2018-07-19 22:10:17 -04:00
|
|
|
session.engine.logger.Debug("[cacheUpdate] clear table ", tableName)
|
|
|
|
cacher.ClearIds(tableName)
|
|
|
|
cacher.ClearBeans(tableName)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle after update processors
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.isAutoCommit {
|
2017-01-03 03:20:28 -05:00
|
|
|
for _, closure := range session.afterClosures {
|
|
|
|
closure(bean)
|
|
|
|
}
|
|
|
|
if processor, ok := interface{}(bean).(AfterUpdateProcessor); ok {
|
2017-09-13 01:18:22 -04:00
|
|
|
session.engine.logger.Debug("[event]", tableName, " has after update processor")
|
2017-01-03 03:20:28 -05:00
|
|
|
processor.AfterUpdate()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lenAfterClosures := len(session.afterClosures)
|
|
|
|
if lenAfterClosures > 0 {
|
|
|
|
if value, has := session.afterUpdateBeans[bean]; has && value != nil {
|
|
|
|
*value = append(*value, session.afterClosures...)
|
|
|
|
} else {
|
|
|
|
afterClosures := make([]func(interface{}), lenAfterClosures)
|
|
|
|
copy(afterClosures, session.afterClosures)
|
|
|
|
// FIXME: if bean is a map type, it will panic because map cannot be as map key
|
|
|
|
session.afterUpdateBeans[bean] = &afterClosures
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-01-25 09:54:52 -05:00
|
|
|
if _, ok := interface{}(bean).(AfterUpdateProcessor); ok {
|
2017-01-03 03:20:28 -05:00
|
|
|
session.afterUpdateBeans[bean] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanupProcessorsClosures(&session.afterClosures) // cleanup after used
|
|
|
|
// --
|
|
|
|
|
|
|
|
return res.RowsAffected()
|
|
|
|
}
|
2018-07-19 22:10:17 -04:00
|
|
|
|
|
|
|
func (session *Session) genUpdateColumns(bean interface{}) ([]string, []interface{}, error) {
|
|
|
|
table := session.statement.RefTable
|
|
|
|
colNames := make([]string, 0, len(table.ColumnsSeq()))
|
|
|
|
args := make([]interface{}, 0, len(table.ColumnsSeq()))
|
|
|
|
|
|
|
|
for _, col := range table.Columns() {
|
|
|
|
if !col.IsVersion && !col.IsCreated && !col.IsUpdated {
|
|
|
|
if session.statement.omitColumnMap.contain(col.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if col.MapType == core.ONLYFROMDB {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldValuePtr, err := col.ValueOf(bean)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
fieldValue := *fieldValuePtr
|
|
|
|
|
|
|
|
if col.IsAutoIncrement {
|
|
|
|
switch fieldValue.Type().Kind() {
|
|
|
|
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
|
|
|
|
if fieldValue.Int() == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
|
|
|
|
if fieldValue.Uint() == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
if len(fieldValue.String()) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
if fieldValue.Pointer() == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:01:41 -05:00
|
|
|
if (col.IsDeleted && !session.statement.unscoped) || col.IsCreated {
|
2018-07-19 22:10:17 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-09-24 09:22:39 -04:00
|
|
|
// if only update specify columns
|
|
|
|
if len(session.statement.columnMap) > 0 && !session.statement.columnMap.contain(col.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.statement.incrColumns.isColExist(col.Name) {
|
|
|
|
continue
|
|
|
|
} else if session.statement.decrColumns.isColExist(col.Name) {
|
|
|
|
continue
|
|
|
|
} else if session.statement.exprColumns.isColExist(col.Name) {
|
|
|
|
continue
|
2018-07-19 22:10:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
|
|
|
|
if _, ok := getFlagForColumn(session.statement.nullableMap, col); ok {
|
|
|
|
if col.Nullable && isZero(fieldValue.Interface()) {
|
|
|
|
var nilValue *int
|
|
|
|
fieldValue = reflect.ValueOf(nilValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if col.IsUpdated && session.statement.UseAutoTime /*&& isZero(fieldValue.Interface())*/ {
|
|
|
|
// if time is non-empty, then set to auto time
|
|
|
|
val, t := session.engine.nowTime(col)
|
|
|
|
args = append(args, val)
|
|
|
|
|
|
|
|
var colName = col.Name
|
|
|
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
|
|
|
col := table.GetColumn(colName)
|
|
|
|
setColumnTime(bean, col, t)
|
|
|
|
})
|
|
|
|
} else if col.IsVersion && session.statement.checkVersion {
|
|
|
|
args = append(args, 1)
|
|
|
|
} else {
|
|
|
|
arg, err := session.value2Interface(col, fieldValue)
|
|
|
|
if err != nil {
|
|
|
|
return colNames, args, err
|
|
|
|
}
|
|
|
|
args = append(args, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
colNames = append(colNames, session.engine.Quote(col.Name)+" = ?")
|
|
|
|
}
|
|
|
|
return colNames, args, nil
|
|
|
|
}
|