|
@@ -33,41 +33,16 @@ func InsertEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
return errors.New("没有传递实体")
|
|
return errors.New("没有传递实体")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- sqlMapping, err := ParseSqlMapping(e)
|
|
|
|
|
|
|
+ tableRows := sql_tpl.NewTableRows()
|
|
|
|
|
+
|
|
|
|
|
+ err := formInsertTableRow(e, tableRows)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- tableRow := sql_tpl.NewTableRows()
|
|
|
|
|
-
|
|
|
|
|
- now := time.Now()
|
|
|
|
|
-
|
|
|
|
|
- for fieldName, sqlColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- fieldType := sqlColumn.FieldTypeElem
|
|
|
|
|
-
|
|
|
|
|
- // 有值取值,没有值构造零值
|
|
|
|
|
- value := reflect.Zero(fieldType).Interface()
|
|
|
|
|
- if !sqlColumn.FieldValueElem.IsZero() {
|
|
|
|
|
- value = sqlColumn.FieldValueElem.Interface()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 自动添加创建时间和更新时间
|
|
|
|
|
- if (fieldName == createdTimeFieldName || fieldName == lastUpdatedTimeFieldName) &&
|
|
|
|
|
- fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
|
|
|
- value = now
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var opts []sql_tpl.AfterParsedStrValueOption
|
|
|
|
|
- if strutils.IsStringNotEmpty(sqlColumn.AESKey) {
|
|
|
|
|
- opts = append(opts, sql_tpl.WithAESKey(sqlColumn.AESKey))
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- tableRow.Add(sqlColumn.Name, value, opts...)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
executeParamsMap, err := sql_tpl.InsertExecuteParams{
|
|
executeParamsMap, err := sql_tpl.InsertExecuteParams{
|
|
|
TableName: tableName,
|
|
TableName: tableName,
|
|
|
- TableRows: tableRow,
|
|
|
|
|
|
|
+ TableRows: tableRows,
|
|
|
}.Map()
|
|
}.Map()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
@@ -85,6 +60,50 @@ func InsertEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func formInsertTableRow(e any, tableRows *sql_tpl.TableRows) error {
|
|
|
|
|
+ sqlMapping, err := ParseSqlMapping(e)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ now := time.Now()
|
|
|
|
|
+
|
|
|
|
|
+ for fieldName, mappingElement := range sqlMapping.MappingElement {
|
|
|
|
|
+ switch element := mappingElement.(type) {
|
|
|
|
|
+ case *MappingStruct:
|
|
|
|
|
+ err := formInsertTableRow(element.FieldValueElem.Addr().Interface(), tableRows)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ case *MappingColumn:
|
|
|
|
|
+ fieldType := element.FieldTypeElem
|
|
|
|
|
+
|
|
|
|
|
+ // 有值取值,没有值构造零值
|
|
|
|
|
+ value := reflect.Zero(fieldType).Interface()
|
|
|
|
|
+ if !element.FieldValueElem.IsZero() {
|
|
|
|
|
+ value = element.FieldValueElem.Interface()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 自动添加创建时间和更新时间
|
|
|
|
|
+ if (fieldName == createdTimeFieldName || fieldName == lastUpdatedTimeFieldName) &&
|
|
|
|
|
+ fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
|
|
|
+ value = now
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var opts []sql_tpl.AfterParsedStrValueOption
|
|
|
|
|
+ if strutils.IsStringNotEmpty(element.AESKey) {
|
|
|
|
|
+ opts = append(opts, sql_tpl.WithAESKey(element.AESKey))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ tableRows.Add(element.Name, value, opts...)
|
|
|
|
|
+ default:
|
|
|
|
|
+ return errors.New("不支持的元素类型")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func DeleteEntity[T any](executor Executor, tableName string, e T) error {
|
|
func DeleteEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return errors.New("没有传递执行器")
|
|
return errors.New("没有传递执行器")
|
|
@@ -98,30 +117,11 @@ func DeleteEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
return errors.New("没有传递实体")
|
|
return errors.New("没有传递实体")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- sqlMapping, err := ParseSqlMapping(e)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
conditions := sql_tpl.NewConditions()
|
|
conditions := sql_tpl.NewConditions()
|
|
|
|
|
|
|
|
- for _, sqlColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- // 不是键,字段跳过
|
|
|
|
|
- if !sqlColumn.IsKey {
|
|
|
|
|
- continue
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 键字段没有赋值
|
|
|
|
|
- if sqlColumn.FieldValueElem.IsZero() {
|
|
|
|
|
- return errors.New("键字段没有传值")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var opts []sql_tpl.AfterParsedStrValueOption
|
|
|
|
|
- if strutils.IsStringNotEmpty(sqlColumn.AESKey) {
|
|
|
|
|
- opts = append(opts, sql_tpl.WithAESKey(sqlColumn.AESKey))
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- conditions.Equal(sqlColumn.Name, sqlColumn.FieldValueElem.Interface(), opts...)
|
|
|
|
|
|
|
+ err := formDeleteConditions(e, conditions)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
executeParamsMap, err := sql_tpl.DeleteExecuteParams{
|
|
executeParamsMap, err := sql_tpl.DeleteExecuteParams{
|
|
@@ -140,6 +140,44 @@ func DeleteEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func formDeleteConditions(e any, conditions *sql_tpl.Conditions) error {
|
|
|
|
|
+ sqlMapping, err := ParseSqlMapping(e)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for _, mappingElement := range sqlMapping.MappingElement {
|
|
|
|
|
+ switch element := mappingElement.(type) {
|
|
|
|
|
+ case *MappingStruct:
|
|
|
|
|
+ err := formDeleteConditions(element.FieldValueElem.Addr().Interface(), conditions)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ case *MappingColumn:
|
|
|
|
|
+ // 不是键,字段跳过
|
|
|
|
|
+ if !element.IsKey {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 键字段没有赋值
|
|
|
|
|
+ if element.FieldValueElem.IsZero() {
|
|
|
|
|
+ return errors.New("键字段没有传值")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var opts []sql_tpl.AfterParsedStrValueOption
|
|
|
|
|
+ if strutils.IsStringNotEmpty(element.AESKey) {
|
|
|
|
|
+ opts = append(opts, sql_tpl.WithAESKey(element.AESKey))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ conditions.Equal(element.Name, element.FieldValueElem.Interface(), opts...)
|
|
|
|
|
+ default:
|
|
|
|
|
+ return errors.New("不支持的元素类型")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func UpdateEntity[T any](executor Executor, tableName string, e T) error {
|
|
func UpdateEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return errors.New("没有传递执行器")
|
|
return errors.New("没有传递执行器")
|
|
@@ -153,52 +191,12 @@ func UpdateEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
return errors.New("没有传递实体")
|
|
return errors.New("没有传递实体")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- sqlMapping, err := ParseSqlMapping(e)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
tableRows := sql_tpl.NewTableRows()
|
|
tableRows := sql_tpl.NewTableRows()
|
|
|
conditions := sql_tpl.NewConditions()
|
|
conditions := sql_tpl.NewConditions()
|
|
|
|
|
|
|
|
- now := time.Now()
|
|
|
|
|
-
|
|
|
|
|
- for fieldName, sqlColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- if sqlColumn.IsKey {
|
|
|
|
|
- // 键字段但是没有赋值
|
|
|
|
|
- if sqlColumn.FieldValueElem.IsZero() {
|
|
|
|
|
- return errors.New("键字段没有传值")
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- // 不是键字段
|
|
|
|
|
- // 不更新的字段或者字段为空且不能清空,跳过
|
|
|
|
|
- if !sqlColumn.CanUpdate || (sqlColumn.FieldValueElem.IsZero() && !sqlColumn.CanUpdateClear) {
|
|
|
|
|
- continue
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- fieldType := sqlColumn.FieldTypeElem
|
|
|
|
|
-
|
|
|
|
|
- value := reflect.Zero(fieldType).Interface()
|
|
|
|
|
- if !sqlColumn.FieldValueElem.IsZero() {
|
|
|
|
|
- value = sqlColumn.FieldValueElem.Interface()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if fieldName == lastUpdatedTimeFieldName &&
|
|
|
|
|
- fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
|
|
|
- value = now
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var opts []sql_tpl.AfterParsedStrValueOption
|
|
|
|
|
- if strutils.IsStringNotEmpty(sqlColumn.AESKey) {
|
|
|
|
|
- opts = append(opts, sql_tpl.WithAESKey(sqlColumn.AESKey))
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if sqlColumn.IsKey {
|
|
|
|
|
- conditions.Equal(sqlColumn.Name, value, opts...)
|
|
|
|
|
- } else {
|
|
|
|
|
- tableRows.Add(sqlColumn.Name, value, opts...)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ err := formUpdateTableRowsAndConditions(e, tableRows, conditions)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
executeParamsMap, err := sql_tpl.UpdateExecuteParams{
|
|
executeParamsMap, err := sql_tpl.UpdateExecuteParams{
|
|
@@ -218,6 +216,65 @@ func UpdateEntity[T any](executor Executor, tableName string, e T) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func formUpdateTableRowsAndConditions(e any, tableRows *sql_tpl.TableRows, conditions *sql_tpl.Conditions) error {
|
|
|
|
|
+ sqlMapping, err := ParseSqlMapping(e)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ now := time.Now()
|
|
|
|
|
+
|
|
|
|
|
+ for fieldName, mappingElement := range sqlMapping.MappingElement {
|
|
|
|
|
+ switch element := mappingElement.(type) {
|
|
|
|
|
+ case *MappingStruct:
|
|
|
|
|
+ err := formUpdateTableRowsAndConditions(element.FieldValueElem.Addr().Interface(), tableRows, conditions)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ case *MappingColumn:
|
|
|
|
|
+ if element.IsKey {
|
|
|
|
|
+ // 键字段但是没有赋值
|
|
|
|
|
+ if element.FieldValueElem.IsZero() {
|
|
|
|
|
+ return errors.New("键字段没有传值")
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 不是键字段
|
|
|
|
|
+ // 不更新的字段或者字段为空且不能清空,跳过
|
|
|
|
|
+ if !element.CanUpdate || (element.FieldValueElem.IsZero() && !element.CanUpdateClear) {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fieldType := element.FieldTypeElem
|
|
|
|
|
+
|
|
|
|
|
+ value := reflect.Zero(fieldType).Interface()
|
|
|
|
|
+ if !element.FieldValueElem.IsZero() {
|
|
|
|
|
+ value = element.FieldValueElem.Interface()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if fieldName == lastUpdatedTimeFieldName &&
|
|
|
|
|
+ fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
|
|
|
+ value = now
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var opts []sql_tpl.AfterParsedStrValueOption
|
|
|
|
|
+ if strutils.IsStringNotEmpty(element.AESKey) {
|
|
|
|
|
+ opts = append(opts, sql_tpl.WithAESKey(element.AESKey))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if element.IsKey {
|
|
|
|
|
+ conditions.Equal(element.Name, value, opts...)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ tableRows.Add(element.Name, value, opts...)
|
|
|
|
|
+ }
|
|
|
|
|
+ default:
|
|
|
|
|
+ return errors.New("不支持的元素类型")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func Insert(executor Executor, executeParams *sql_tpl.InsertExecuteParams) error {
|
|
func Insert(executor Executor, executeParams *sql_tpl.InsertExecuteParams) error {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return errors.New("没有传递执行器")
|
|
return errors.New("没有传递执行器")
|