2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-04-08 21:26:14 +08:00
|
|
|
"fmt"
|
2021-05-21 13:25:53 +08:00
|
|
|
"reflect"
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2020-03-13 17:21:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Update does "UPDATE ... " statement for the model.
|
|
|
|
//
|
2021-02-08 17:57:21 +08:00
|
|
|
// If the optional parameter `dataAndWhere` is given, the dataAndWhere[0] is the updated data field,
|
2020-03-13 17:21:30 +08:00
|
|
|
// and dataAndWhere[1:] is treated as where condition fields.
|
|
|
|
// Also see Model.Data and Model.Where functions.
|
|
|
|
func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err error) {
|
|
|
|
if len(dataAndWhere) > 0 {
|
|
|
|
if len(dataAndWhere) > 2 {
|
|
|
|
return m.Data(dataAndWhere[0]).Where(dataAndWhere[1], dataAndWhere[2:]...).Update()
|
|
|
|
} else if len(dataAndWhere) == 2 {
|
|
|
|
return m.Data(dataAndWhere[0]).Where(dataAndWhere[1]).Update()
|
|
|
|
} else {
|
|
|
|
return m.Data(dataAndWhere[0]).Update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
m.checkAndRemoveCache()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if m.data == nil {
|
2021-08-24 21:18:59 +08:00
|
|
|
return nil, gerror.NewCode(gcode.CodeMissingParameter, "updating table with empty data")
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
2020-04-08 21:26:14 +08:00
|
|
|
var (
|
|
|
|
updateData = m.data
|
2020-10-20 21:01:39 +08:00
|
|
|
fieldNameUpdate = m.getSoftFieldNameUpdated()
|
2020-11-10 10:37:42 +08:00
|
|
|
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(false, false)
|
2020-04-08 21:26:14 +08:00
|
|
|
)
|
|
|
|
// Automatically update the record updating time.
|
2020-04-09 22:48:21 +08:00
|
|
|
if !m.unscoped && fieldNameUpdate != "" {
|
2021-11-16 17:21:13 +08:00
|
|
|
reflectInfo := utils.OriginTypeAndKind(m.data)
|
2021-10-28 23:18:23 +08:00
|
|
|
switch reflectInfo.OriginKind {
|
2020-04-08 21:26:14 +08:00
|
|
|
case reflect.Map, reflect.Struct:
|
2020-10-10 17:29:38 +08:00
|
|
|
dataMap := ConvertDataForTableRecord(m.data)
|
2020-04-15 12:56:41 +08:00
|
|
|
if fieldNameUpdate != "" {
|
2020-04-08 21:26:14 +08:00
|
|
|
dataMap[fieldNameUpdate] = gtime.Now().String()
|
|
|
|
}
|
|
|
|
updateData = dataMap
|
|
|
|
default:
|
|
|
|
updates := gconv.String(m.data)
|
|
|
|
if fieldNameUpdate != "" && !gstr.Contains(updates, fieldNameUpdate) {
|
|
|
|
updates += fmt.Sprintf(`,%s='%s'`, fieldNameUpdate, gtime.Now().String())
|
|
|
|
}
|
|
|
|
updateData = updates
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 18:16:13 +08:00
|
|
|
newData, err := m.filterDataForInsertOrUpdate(updateData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-06 00:00:41 +08:00
|
|
|
conditionStr := conditionWhere + conditionExtra
|
|
|
|
if !gstr.ContainsI(conditionStr, " WHERE ") {
|
2021-08-24 21:18:59 +08:00
|
|
|
return nil, gerror.NewCode(gcode.CodeMissingParameter, "there should be WHERE condition statement for UPDATE operation")
|
2020-11-06 00:00:41 +08:00
|
|
|
}
|
2021-06-04 09:54:19 +08:00
|
|
|
return m.db.DoUpdate(
|
2021-05-21 13:25:53 +08:00
|
|
|
m.GetCtx(),
|
2020-03-13 17:21:30 +08:00
|
|
|
m.getLink(true),
|
|
|
|
m.tables,
|
2020-10-17 18:16:13 +08:00
|
|
|
newData,
|
2020-11-06 00:00:41 +08:00
|
|
|
conditionStr,
|
2020-03-13 17:21:30 +08:00
|
|
|
m.mergeArguments(conditionArgs)...,
|
|
|
|
)
|
|
|
|
}
|
2021-05-02 12:17:06 +08:00
|
|
|
|
|
|
|
// Increment increments a column's value by a given amount.
|
2021-06-23 09:34:53 +08:00
|
|
|
// The parameter `amount` can be type of float or integer.
|
|
|
|
func (m *Model) Increment(column string, amount interface{}) (sql.Result, error) {
|
2021-05-02 12:17:06 +08:00
|
|
|
return m.getModel().Data(column, &Counter{
|
|
|
|
Field: column,
|
2021-06-23 09:34:53 +08:00
|
|
|
Value: gconv.Float64(amount),
|
2021-05-02 12:17:06 +08:00
|
|
|
}).Update()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement decrements a column's value by a given amount.
|
2021-06-23 09:34:53 +08:00
|
|
|
// The parameter `amount` can be type of float or integer.
|
|
|
|
func (m *Model) Decrement(column string, amount interface{}) (sql.Result, error) {
|
2021-05-02 12:17:06 +08:00
|
|
|
return m.getModel().Data(column, &Counter{
|
|
|
|
Field: column,
|
2021-06-23 09:34:53 +08:00
|
|
|
Value: -gconv.Float64(amount),
|
2021-05-02 12:17:06 +08:00
|
|
|
}).Update()
|
|
|
|
}
|