mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
本次PR主要针对GoFrame ORM中的软删除`SoftTime`功能进行了优化 - 新增`SoftTimeFieldType`枚举类型,用于区分创建、更新、删除三种不同的软时间字段 - 替代之前使用的魔数方式,提高类型安全性 - 将原有的6个方法精简为4个方法, 合并了三个几乎相同的`GetFieldNameAndTypeFor*`方法为统一的方法 This pull request refactors and simplifies the "soft time" (created/updated/deleted timestamp) handling logic in the database layer, making the codebase more maintainable and extensible. The changes consolidate multiple similar methods into general-purpose ones, improve cache key generation, and clarify the logic for generating and applying soft time field values and conditions. Key changes include: **Soft Time API Refactoring and Simplification** - Consolidated multiple methods (`GetFieldNameAndTypeForCreate`, `GetFieldNameAndTypeForUpdate`, `GetFieldNameAndTypeForDelete`) into a single, parameterized method `GetFieldInfo`, reducing code duplication and making it easier to support new soft time field types. The interface and implementation for soft time maintenance (`iSoftTimeMaintainer`, `softTimeMaintainer`) have been updated accordingly. [[1]](diffhunk://#diff-6c1d606032d981a7b8aecd3a7167823f76b69407a29eb9a244175a82f59965d8L46-R66) [[2]](diffhunk://#diff-6c1d606032d981a7b8aecd3a7167823f76b69407a29eb9a244175a82f59965d8L105-R180) - Combined and renamed methods for generating soft time field values and delete conditions, such as merging `GetValueByFieldTypeForCreateOrUpdate` into `GetFieldValue`, and `GetWhereConditionForDelete` into `GetDeleteCondition`. Related usages throughout the codebase have been updated to use the new methods. [[1]](diffhunk://#diff-6c1d606032d981a7b8aecd3a7167823f76b69407a29eb9a244175a82f59965d8L255-R206) [[2]](diffhunk://#diff-97beb485550e4381182a04bbb857a25b7f4ecd4a594dff8ac884cfaae38f3046L34-R35) [[3]](diffhunk://#diff-97beb485550e4381182a04bbb857a25b7f4ecd4a594dff8ac884cfaae38f3046L55-R55) [[4]](diffhunk://#diff-88304ddb7791aedbd83dafb68374aecab286d1356a7f2f149a8e57ac1a7f40b4L265-R267) [[5]](diffhunk://#diff-88304ddb7791aedbd83dafb68374aecab286d1356a7f2f149a8e57ac1a7f40b4L298-R311) [[6]](diffhunk://#diff-d4f6e0370e049dea52f3db9a13c64e2cfb2f7ef012433186e21179149b626d0fL944-R944) **Soft Delete Logic Improvements** - Refactored the logic for building soft delete WHERE conditions and generating update data, with clearer and more robust handling of field types and prefixes. Introduced helper methods like `buildDeleteCondition`, `GetDeleteData`, and improved error logging for invalid field types. [[1]](diffhunk://#diff-6c1d606032d981a7b8aecd3a7167823f76b69407a29eb9a244175a82f59965d8L287-R234) [[2]](diffhunk://#diff-6c1d606032d981a7b8aecd3a7167823f76b69407a29eb9a244175a82f59965d8L313-R395) **Cache Key Generation Enhancements** - Added dedicated helper functions for generating cache keys for table names, table fields, select queries, and soft time field/type lookups, improving cache consistency and code readability. [[1]](diffhunk://#diff-d57d57e6f9b342ba6fa30c4bb413e2f4f3514a8cd5ad36949eef126e5f8b7ac9R969) [[2]](diffhunk://#diff-d57d57e6f9b342ba6fa30c4bb413e2f4f3514a8cd5ad36949eef126e5f8b7ac9R980) [[3]](diffhunk://#diff-d57d57e6f9b342ba6fa30c4bb413e2f4f3514a8cd5ad36949eef126e5f8b7ac9R993-R1002) [[4]](diffhunk://#diff-b1bbe5e3995261813e4e0ac6ffee8a37c236eaa2759f2bd82e211711695a70bcL790-R790) **General Code Cleanup** - Removed redundant code, clarified comments, and improved naming throughout the affected files, making the code easier to follow and maintain. [[1]](diffhunk://#diff-6c1d606032d981a7b8aecd3a7167823f76b69407a29eb9a244175a82f59965d8L105-R180) [[2]](diffhunk://#diff-6c1d606032d981a7b8aecd3a7167823f76b69407a29eb9a244175a82f59965d8L255-R206) Let me know if you'd like a walkthrough of any specific part of the refactored soft time logic!
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// 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"
|
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
)
|
|
|
|
// Delete does "DELETE FROM ... " statement for the model.
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function,
|
|
// see Model.Where.
|
|
func (m *Model) Delete(where ...any) (result sql.Result, err error) {
|
|
var ctx = m.GetCtx()
|
|
if len(where) > 0 {
|
|
return m.Where(where[0], where[1:]...).Delete()
|
|
}
|
|
defer func() {
|
|
if err == nil {
|
|
m.checkAndRemoveSelectCache(ctx)
|
|
}
|
|
}()
|
|
var (
|
|
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(ctx, false, false)
|
|
conditionStr = conditionWhere + conditionExtra
|
|
fieldNameDelete, fieldTypeDelete = m.softTimeMaintainer().GetFieldInfo(ctx, "", m.tablesInit, SoftTimeFieldDelete)
|
|
)
|
|
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 fieldNameDelete != "" {
|
|
dataHolder, dataValue := m.softTimeMaintainer().GetDeleteData(
|
|
ctx, "", fieldNameDelete, fieldTypeDelete,
|
|
)
|
|
in := &HookUpdateInput{
|
|
internalParamHookUpdate: internalParamHookUpdate{
|
|
internalParamHook: internalParamHook{
|
|
link: m.getLink(true),
|
|
},
|
|
handler: m.hookHandler.Update,
|
|
},
|
|
Model: m,
|
|
Table: m.tables,
|
|
Schema: m.schema,
|
|
Data: dataHolder,
|
|
Condition: conditionStr,
|
|
Args: append([]any{dataValue}, conditionArgs...),
|
|
}
|
|
return in.Next(ctx)
|
|
}
|
|
|
|
in := &HookDeleteInput{
|
|
internalParamHookDelete: internalParamHookDelete{
|
|
internalParamHook: internalParamHook{
|
|
link: m.getLink(true),
|
|
},
|
|
handler: m.hookHandler.Delete,
|
|
},
|
|
Model: m,
|
|
Table: m.tables,
|
|
Schema: m.schema,
|
|
Condition: conditionStr,
|
|
Args: conditionArgs,
|
|
}
|
|
return in.Next(ctx)
|
|
}
|