|
|
@@ -11,8 +11,6 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-// TODO Key字段校验零值
|
|
|
-
|
|
|
type Executor interface {
|
|
|
ExecuteRawSql(sql string, executeParams map[string]any) ([]map[string]any, error)
|
|
|
ExecuteSql(name string, executeParams map[string]any) ([]map[string]any, error)
|
|
|
@@ -48,11 +46,13 @@ func InsertEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
for fieldName, sqlColumn := range sqlMapping.ColumnMap {
|
|
|
fieldType := sqlColumn.ValueFieldType
|
|
|
|
|
|
+ // 有值取值,没有值构造零值
|
|
|
value := reflect.Zero(fieldType).Interface()
|
|
|
if !sqlColumn.ValueFieldValue.IsZero() {
|
|
|
value = sqlColumn.ValueFieldValue.Interface()
|
|
|
}
|
|
|
|
|
|
+ // 自动添加创建时间和更新时间
|
|
|
if (fieldName == createdTimeFieldName || fieldName == lastUpdatedTimeFieldName) &&
|
|
|
fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
|
value = now
|
|
|
@@ -102,18 +102,17 @@ func DeleteEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
conditions := sql_tpl.NewConditions()
|
|
|
|
|
|
for _, sqlColumn := range sqlMapping.ColumnMap {
|
|
|
+ // 不是键,字段跳过
|
|
|
if !sqlColumn.IsKey {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- fieldType := sqlColumn.ValueFieldType
|
|
|
-
|
|
|
- value := reflect.Zero(fieldType).Interface()
|
|
|
- if !sqlColumn.ValueFieldValue.IsZero() {
|
|
|
- value = sqlColumn.ValueFieldValue.Interface()
|
|
|
+ // 键字段没有赋值
|
|
|
+ if sqlColumn.ValueFieldValue.IsZero() {
|
|
|
+ return errors.New("键字段没有传值")
|
|
|
}
|
|
|
|
|
|
- conditions.Equal(sqlColumn.Name, value)
|
|
|
+ conditions.Equal(sqlColumn.Name, sqlColumn.ValueFieldValue.Interface())
|
|
|
}
|
|
|
|
|
|
executeParamsMap, err := sql_tpl.DeleteExecuteParams{
|
|
|
@@ -156,8 +155,17 @@ func UpdateEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
now := time.Now()
|
|
|
|
|
|
for fieldName, sqlColumn := range sqlMapping.ColumnMap {
|
|
|
- if !sqlColumn.IsKey && !sqlColumn.CanUpdate {
|
|
|
- continue
|
|
|
+ if sqlColumn.IsKey {
|
|
|
+ // 键字段但是没有赋值
|
|
|
+ if sqlColumn.ValueFieldValue.IsZero() {
|
|
|
+ return errors.New("键字段没有传值")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 不是键字段
|
|
|
+ // 不更新的字段或者字段为空且不能清空,跳过
|
|
|
+ if !sqlColumn.CanUpdate || (sqlColumn.ValueFieldValue.IsZero() && !sqlColumn.CanUpdateClear) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
fieldType := sqlColumn.ValueFieldType
|
|
|
@@ -172,17 +180,10 @@ func UpdateEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
value = now
|
|
|
}
|
|
|
|
|
|
- // 字段为空且不能清空,不更新
|
|
|
- if reflect.ValueOf(value).IsZero() && !sqlColumn.CanUpdateClear {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- if !sqlColumn.IsKey {
|
|
|
- tableRows.Add(sqlColumn.Name, value)
|
|
|
- }
|
|
|
-
|
|
|
if sqlColumn.IsKey {
|
|
|
conditions.Equal(sqlColumn.Name, value)
|
|
|
+ } else {
|
|
|
+ tableRows.Add(sqlColumn.Name, value)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -436,11 +437,10 @@ const (
|
|
|
sqlResultTimeSecFormat = "2006-01-02T15:04:05+08:00"
|
|
|
)
|
|
|
|
|
|
-// TODO 添加DecodeHook
|
|
|
-
|
|
|
func ParseSqlResults(results any, e any) error {
|
|
|
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
DecodeHook: func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
|
|
|
+
|
|
|
if f.Kind() != reflect.String {
|
|
|
return data, nil
|
|
|
}
|