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"
|
2024-06-12 21:41:44 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/empty"
|
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
2022-03-11 10:24:42 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/reflection"
|
2021-10-11 21:41:56 +08:00
|
|
|
"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) {
|
2022-03-24 17:51:49 +08:00
|
|
|
var ctx = m.GetCtx()
|
2020-03-13 17:21:30 +08:00
|
|
|
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 {
|
2022-03-31 15:42:12 +08:00
|
|
|
m.checkAndRemoveSelectCache(ctx)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
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 (
|
2024-06-12 21:41:44 +08:00
|
|
|
newData interface{}
|
2024-02-06 10:21:23 +08:00
|
|
|
stm = m.softTimeMaintainer()
|
2024-06-12 21:41:44 +08:00
|
|
|
reflectInfo = reflection.OriginTypeAndKind(m.data)
|
2022-03-24 17:51:49 +08:00
|
|
|
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(ctx, false, false)
|
2023-02-15 09:45:40 +08:00
|
|
|
conditionStr = conditionWhere + conditionExtra
|
2024-02-06 10:21:23 +08:00
|
|
|
fieldNameUpdate, fieldTypeUpdate = stm.GetFieldNameAndTypeForUpdate(
|
|
|
|
ctx, "", m.tablesInit,
|
|
|
|
)
|
2020-04-08 21:26:14 +08:00
|
|
|
)
|
2023-02-15 09:45:40 +08:00
|
|
|
if m.unscoped {
|
|
|
|
fieldNameUpdate = ""
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:44 +08:00
|
|
|
newData, err = m.filterDataForInsertOrUpdate(m.data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-19 23:38:57 +08:00
|
|
|
switch reflectInfo.OriginKind {
|
|
|
|
case reflect.Map, reflect.Struct:
|
2024-06-12 21:41:44 +08:00
|
|
|
var dataMap = anyValueToMapBeforeToRecord(newData)
|
2022-03-19 23:38:57 +08:00
|
|
|
// Automatically update the record updating time.
|
2024-06-12 21:41:44 +08:00
|
|
|
if fieldNameUpdate != "" && empty.IsNil(dataMap[fieldNameUpdate]) {
|
2024-02-06 10:21:23 +08:00
|
|
|
dataValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeUpdate, false)
|
|
|
|
dataMap[fieldNameUpdate] = dataValue
|
2022-03-19 23:38:57 +08:00
|
|
|
}
|
2024-06-12 21:41:44 +08:00
|
|
|
newData = dataMap
|
2021-12-22 17:42:26 +08:00
|
|
|
|
2022-03-19 23:38:57 +08:00
|
|
|
default:
|
2024-06-12 21:41:44 +08:00
|
|
|
var updateStr = gconv.String(newData)
|
2022-03-19 23:38:57 +08:00
|
|
|
// Automatically update the record updating time.
|
2024-06-12 21:41:44 +08:00
|
|
|
if fieldNameUpdate != "" && !gstr.Contains(updateStr, fieldNameUpdate) {
|
2024-02-06 10:21:23 +08:00
|
|
|
dataValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeUpdate, false)
|
2024-06-12 21:41:44 +08:00
|
|
|
updateStr += fmt.Sprintf(`,%s=?`, fieldNameUpdate)
|
|
|
|
conditionArgs = append([]interface{}{dataValue}, conditionArgs...)
|
2020-04-08 21:26:14 +08:00
|
|
|
}
|
2024-06-12 21:41:44 +08:00
|
|
|
newData = updateStr
|
2020-10-17 18:16:13 +08:00
|
|
|
}
|
2023-02-15 09:45:40 +08:00
|
|
|
|
2020-11-06 00:00:41 +08:00
|
|
|
if !gstr.ContainsI(conditionStr, " WHERE ") {
|
2023-02-15 09:45:40 +08:00
|
|
|
intlog.Printf(
|
|
|
|
ctx,
|
|
|
|
`sql condition string "%s" has no WHERE for UPDATE operation, fieldNameUpdate: %s`,
|
|
|
|
conditionStr, fieldNameUpdate,
|
|
|
|
)
|
|
|
|
return nil, gerror.NewCode(
|
|
|
|
gcode.CodeMissingParameter,
|
|
|
|
"there should be WHERE condition statement for UPDATE operation",
|
|
|
|
)
|
2020-11-06 00:00:41 +08:00
|
|
|
}
|
2022-03-14 23:47:55 +08:00
|
|
|
|
|
|
|
in := &HookUpdateInput{
|
|
|
|
internalParamHookUpdate: internalParamHookUpdate{
|
|
|
|
internalParamHook: internalParamHook{
|
2022-05-09 14:22:28 +08:00
|
|
|
link: m.getLink(true),
|
2022-03-14 23:47:55 +08:00
|
|
|
},
|
2022-03-21 14:26:56 +08:00
|
|
|
handler: m.hookHandler.Update,
|
2022-03-14 23:47:55 +08:00
|
|
|
},
|
2022-05-09 14:22:28 +08:00
|
|
|
Model: m,
|
2022-03-14 23:47:55 +08:00
|
|
|
Table: m.tables,
|
|
|
|
Data: newData,
|
|
|
|
Condition: conditionStr,
|
|
|
|
Args: m.mergeArguments(conditionArgs),
|
|
|
|
}
|
2022-03-24 17:51:49 +08:00
|
|
|
return in.Next(ctx)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
2021-05-02 12:17:06 +08:00
|
|
|
|
2022-04-08 17:22:07 +08:00
|
|
|
// UpdateAndGetAffected performs update statement and returns the affected rows number.
|
|
|
|
func (m *Model) UpdateAndGetAffected(dataAndWhere ...interface{}) (affected int64, err error) {
|
|
|
|
result, err := m.Update(dataAndWhere...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return result.RowsAffected()
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|