|
|
@@ -1,15 +1,16 @@
|
|
|
package database
|
|
|
|
|
|
import (
|
|
|
+ "reflect"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/operations"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/tag/sql/sql_mapping"
|
|
|
"git.sxidc.com/go-tools/utils/reflectutils"
|
|
|
"git.sxidc.com/go-tools/utils/strutils"
|
|
|
"github.com/pkg/errors"
|
|
|
- "reflect"
|
|
|
- "strings"
|
|
|
- "time"
|
|
|
)
|
|
|
|
|
|
// Executor 数据库基础设施接口
|
|
|
@@ -243,6 +244,22 @@ func formInsertTableRow(fields []sql_mapping.Field, createTime time.Time) *sql.T
|
|
|
// 返回值:
|
|
|
// - 错误
|
|
|
func DeleteEntity(executor Executor, tableName string, e any) error {
|
|
|
+ return deleteEntityCommon(executor, tableName, e)
|
|
|
+}
|
|
|
+
|
|
|
+// DeleteEntityWithScope 通过结构删除数据,可以添加范围条件,如租户ID
|
|
|
+// 参数:
|
|
|
+// - executor: 数据库基础设施接口
|
|
|
+// - tableName: 表名
|
|
|
+// - e: 结构,结构字段需要使用sqlmapping标注
|
|
|
+// - scopeFields: 范围字段名
|
|
|
+// 返回值:
|
|
|
+// - 错误
|
|
|
+func DeleteEntityWithScope(executor Executor, tableName string, e any, scopeFields ...string) error {
|
|
|
+ return deleteEntityCommon(executor, tableName, e, scopeFields...)
|
|
|
+}
|
|
|
+
|
|
|
+func deleteEntityCommon(executor Executor, tableName string, e any, scopeFields ...string) error {
|
|
|
if executor == nil {
|
|
|
return errors.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -267,12 +284,19 @@ func DeleteEntity(executor Executor, tableName string, e any) error {
|
|
|
|
|
|
conditions := sql.NewConditions()
|
|
|
for _, field := range fields {
|
|
|
- // 不是键,字段跳过
|
|
|
- if !field.IsKey {
|
|
|
- continue
|
|
|
+ if scopeFields != nil && len(scopeFields) != 0 {
|
|
|
+ for _, scopeField := range scopeFields {
|
|
|
+ if scopeField != field.FieldName {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ conditions.Equal(field.ColumnName, field.Value)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- conditions.Equal(field.ColumnName, field.Value)
|
|
|
+ if field.IsKey {
|
|
|
+ conditions.Equal(field.ColumnName, field.Value)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
executeParams := sql.DeleteExecuteParams{
|
|
|
@@ -301,7 +325,24 @@ func DeleteEntity(executor Executor, tableName string, e any) error {
|
|
|
// 返回值:
|
|
|
// - 错误
|
|
|
func UpdateEntity(executor Executor, tableName string, e any) error {
|
|
|
- _, err := UpdateEntityWithRowsAffected(executor, tableName, e)
|
|
|
+ _, err := updateEntityCommon(executor, tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateEntityWithScope 通过结构更新数据,可以添加范围条件,如租户ID
|
|
|
+// 参数:
|
|
|
+// - executor: 数据库基础设施接口
|
|
|
+// - tableName: 表名
|
|
|
+// - e: 结构,结构字段需要使用sqlmapping标注
|
|
|
+// - scopeFields: 范围字段名
|
|
|
+// 返回值:
|
|
|
+// - 错误
|
|
|
+func UpdateEntityWithScope(executor Executor, tableName string, e any, scopeFields ...string) error {
|
|
|
+ _, err := updateEntityCommon(executor, tableName, e, scopeFields...)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -318,6 +359,23 @@ func UpdateEntity(executor Executor, tableName string, e any) error {
|
|
|
// - 影响行数
|
|
|
// - 错误
|
|
|
func UpdateEntityWithRowsAffected(executor Executor, tableName string, e any) (int64, error) {
|
|
|
+ return updateEntityCommon(executor, tableName, e)
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateEntityWithRowsAffectedAndScope 通过结构更新数据,可以添加范围条件,如租户ID,返回影响行数
|
|
|
+// 参数:
|
|
|
+// - executor: 数据库基础设施接口
|
|
|
+// - tableName: 表名
|
|
|
+// - e: 结构,结构字段需要使用sqlmapping标注
|
|
|
+// - scopeFields: 范围字段名
|
|
|
+// 返回值:
|
|
|
+// - 影响行数
|
|
|
+// - 错误
|
|
|
+func UpdateEntityWithRowsAffectedAndScope(executor Executor, tableName string, e any, scopeFields ...string) (int64, error) {
|
|
|
+ return updateEntityCommon(executor, tableName, e, scopeFields...)
|
|
|
+}
|
|
|
+
|
|
|
+func updateEntityCommon(executor Executor, tableName string, e any, scopeFields ...string) (int64, error) {
|
|
|
if executor == nil {
|
|
|
return 0, errors.New("没有传递执行器")
|
|
|
}
|
|
|
@@ -345,6 +403,16 @@ func UpdateEntityWithRowsAffected(executor Executor, tableName string, e any) (i
|
|
|
conditions := sql.NewConditions()
|
|
|
|
|
|
for _, field := range fields {
|
|
|
+ if scopeFields != nil && len(scopeFields) != 0 {
|
|
|
+ for _, scopeField := range scopeFields {
|
|
|
+ if scopeField != field.FieldName {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ conditions.Equal(field.ColumnName, field.Value)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 不是键字段
|
|
|
// 不是更新时间字段
|
|
|
// 不更新的字段或者字段为零值且不能清空,跳过
|