This commit is contained in:
John Guo
2023-02-15 09:45:40 +08:00
committed by GitHub
parent b69e0ff9f7
commit ac6b0c0980
11 changed files with 81 additions and 145 deletions

View File

@ -9,11 +9,12 @@ package gdb
import (
"database/sql"
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/os/gtime"
)
// Delete does "DELETE FROM ... " statement for the model.
@ -32,9 +33,25 @@ func (m *Model) Delete(where ...interface{}) (result sql.Result, err error) {
var (
fieldNameDelete = m.getSoftFieldNameDeleted("", m.tablesInit)
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(ctx, false, false)
conditionStr = conditionWhere + conditionExtra
)
if m.unscoped {
fieldNameDelete = ""
}
if !gstr.ContainsI(conditionStr, " WHERE ") || (fieldNameDelete != "" && !gstr.ContainsI(conditionStr, " AND ")) {
intlog.Printf(
ctx,
`sql condition string "%s" has no WHERE for DELETE operation, fieldNameDelete: %s`,
conditionStr, fieldNameDelete,
)
return nil, gerror.NewCode(
gcode.CodeMissingParameter,
"there should be WHERE condition statement for DELETE operation",
)
}
// Soft deleting.
if !m.unscoped && fieldNameDelete != "" {
if fieldNameDelete != "" {
in := &HookUpdateInput{
internalParamHookUpdate: internalParamHookUpdate{
internalParamHook: internalParamHook{
@ -45,18 +62,11 @@ func (m *Model) Delete(where ...interface{}) (result sql.Result, err error) {
Model: m,
Table: m.tables,
Data: fmt.Sprintf(`%s=?`, m.db.GetCore().QuoteString(fieldNameDelete)),
Condition: conditionWhere + conditionExtra,
Condition: conditionStr,
Args: append([]interface{}{gtime.Now().String()}, conditionArgs...),
}
return in.Next(ctx)
}
conditionStr := conditionWhere + conditionExtra
if !gstr.ContainsI(conditionStr, " WHERE ") {
return nil, gerror.NewCode(
gcode.CodeMissingParameter,
"there should be WHERE condition statement for DELETE operation",
)
}
in := &HookDeleteInput{
internalParamHookDelete: internalParamHookDelete{

View File

@ -617,7 +617,9 @@ func (m *Model) getFieldsFiltered() string {
// Note that this function does not change any attribute value of the `m`.
//
// The parameter `limit1` specifies whether limits querying only one record if m.limit is not set.
func (m *Model) formatCondition(ctx context.Context, limit1 bool, isCountStatement bool) (conditionWhere string, conditionExtra string, conditionArgs []interface{}) {
func (m *Model) formatCondition(
ctx context.Context, limit1 bool, isCountStatement bool,
) (conditionWhere string, conditionExtra string, conditionArgs []interface{}) {
var autoPrefix = m.getAutoPrefix()
// GROUP BY.
if m.groupBy != "" {

View File

@ -9,6 +9,7 @@ package gdb
import (
"database/sql"
"fmt"
"github.com/gogf/gf/v2/internal/intlog"
"reflect"
"github.com/gogf/gf/v2/errors/gcode"
@ -48,7 +49,12 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
reflectInfo = reflection.OriginTypeAndKind(updateData)
fieldNameUpdate = m.getSoftFieldNameUpdated("", m.tablesInit)
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(ctx, false, false)
conditionStr = conditionWhere + conditionExtra
)
if m.unscoped {
fieldNameUpdate = ""
}
switch reflectInfo.OriginKind {
case reflect.Map, reflect.Struct:
var dataMap map[string]interface{}
@ -57,7 +63,7 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
return nil, err
}
// Automatically update the record updating time.
if !m.unscoped && fieldNameUpdate != "" {
if fieldNameUpdate != "" {
dataMap[fieldNameUpdate] = gtime.Now().String()
}
updateData = dataMap
@ -65,7 +71,7 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
default:
updates := gconv.String(m.data)
// Automatically update the record updating time.
if !m.unscoped && fieldNameUpdate != "" {
if fieldNameUpdate != "" {
if fieldNameUpdate != "" && !gstr.Contains(updates, fieldNameUpdate) {
updates += fmt.Sprintf(`,%s='%s'`, fieldNameUpdate, gtime.Now().String())
}
@ -76,9 +82,17 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
if err != nil {
return nil, err
}
conditionStr := conditionWhere + conditionExtra
if !gstr.ContainsI(conditionStr, " WHERE ") {
return nil, gerror.NewCode(gcode.CodeMissingParameter, "there should be WHERE condition statement for UPDATE operation")
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",
)
}
in := &HookUpdateInput{