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
|
|
|
|
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"
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2020-03-13 17:21:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Delete does "DELETE FROM ... " statement for the model.
|
2021-02-08 17:57:21 +08:00
|
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function,
|
2020-03-13 17:21:30 +08:00
|
|
|
// see Model.Where.
|
|
|
|
func (m *Model) Delete(where ...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(where) > 0 {
|
|
|
|
return m.Where(where[0], where[1:]...).Delete()
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
2022-03-31 15:42:12 +08:00
|
|
|
m.checkAndRemoveSelectCache(ctx)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
}()
|
2020-04-08 21:26:14 +08:00
|
|
|
var (
|
2020-10-20 21:01:39 +08:00
|
|
|
fieldNameDelete = m.getSoftFieldNameDeleted()
|
2022-03-24 17:51:49 +08:00
|
|
|
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(ctx, false, false)
|
2020-04-08 21:26:14 +08:00
|
|
|
)
|
|
|
|
// Soft deleting.
|
2020-04-09 22:48:21 +08:00
|
|
|
if !m.unscoped && fieldNameDelete != "" {
|
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: fmt.Sprintf(`%s=?`, m.db.GetCore().QuoteString(fieldNameDelete)),
|
|
|
|
Condition: conditionWhere + conditionExtra,
|
|
|
|
Args: append([]interface{}{gtime.Now().String()}, conditionArgs...),
|
|
|
|
}
|
2022-03-24 17:51:49 +08:00
|
|
|
return in.Next(ctx)
|
2020-04-08 21:26:14 +08:00
|
|
|
}
|
2020-11-06 00:00:41 +08:00
|
|
|
conditionStr := conditionWhere + conditionExtra
|
|
|
|
if !gstr.ContainsI(conditionStr, " WHERE ") {
|
2022-03-14 23:47:55 +08:00
|
|
|
return nil, gerror.NewCode(
|
|
|
|
gcode.CodeMissingParameter,
|
|
|
|
"there should be WHERE condition statement for DELETE operation",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
in := &HookDeleteInput{
|
|
|
|
internalParamHookDelete: internalParamHookDelete{
|
|
|
|
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.Delete,
|
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,
|
|
|
|
Condition: conditionStr,
|
|
|
|
Args: conditionArgs,
|
2020-11-06 00:00:41 +08:00
|
|
|
}
|
2022-03-24 17:51:49 +08:00
|
|
|
return in.Next(ctx)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|