|
|
@@ -10,6 +10,10 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+type RawSqlExecutor interface {
|
|
|
+ ExecuteRawSql(sql string, executeParams map[string]any) ([]map[string]any, error)
|
|
|
+}
|
|
|
+
|
|
|
const (
|
|
|
timeWriteFormat = time.DateTime + ".000000 +08:00"
|
|
|
createdTimeFieldName = "CreatedTime"
|
|
|
@@ -18,9 +22,9 @@ const (
|
|
|
|
|
|
type InsertCallback[T any] func(e T, fieldName string, value any) (retValue any, err error)
|
|
|
|
|
|
-func Insert[T any](sdk *SDK, tableName string, e T, callback InsertCallback[T]) error {
|
|
|
- if sdk == nil {
|
|
|
- return errors.New("没有传递sdk")
|
|
|
+func Insert[T any](executor RawSqlExecutor, tableName string, e T, callback InsertCallback[T]) error {
|
|
|
+ if executor == nil {
|
|
|
+ return errors.New("没有传递执行器")
|
|
|
}
|
|
|
|
|
|
if strutils.IsStringEmpty(tableName) {
|
|
|
@@ -89,7 +93,66 @@ func Insert[T any](sdk *SDK, tableName string, e T, callback InsertCallback[T])
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- _, err = sdk.ExecuteRawSql(raw_sql_tpl.InsertTpl, executeParamsMap)
|
|
|
+ _, err = executor.ExecuteRawSql(raw_sql_tpl.InsertTpl, executeParamsMap)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func Delete[T any](executor RawSqlExecutor, tableName string, e T) error {
|
|
|
+ if executor == nil {
|
|
|
+ return errors.New("没有传递执行器")
|
|
|
+ }
|
|
|
+
|
|
|
+ if strutils.IsStringEmpty(tableName) {
|
|
|
+ return errors.New("没有传递表名")
|
|
|
+ }
|
|
|
+
|
|
|
+ if reflect.TypeOf(e) == nil {
|
|
|
+ return errors.New("没有传递实体")
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlMapping, err := tag.ParseSqlMapping(e)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ executeParams := raw_sql_tpl.DeleteExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
+ if !sqlMappingColumn.IsKey {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ fieldType := sqlMappingColumn.FieldType
|
|
|
+
|
|
|
+ value := reflect.Zero(fieldType).Interface()
|
|
|
+ if !sqlMappingColumn.FieldValue.IsZero() {
|
|
|
+ value = sqlMappingColumn.FieldValue.Interface()
|
|
|
+ }
|
|
|
+
|
|
|
+ tableRowValue, err := parseValue(value)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ Column: sqlMappingColumn.Name,
|
|
|
+ Operator: "=",
|
|
|
+ Value: tableRowValue,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ executeParamsMap, err := executeParams.Map()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = executor.ExecuteRawSql(raw_sql_tpl.DeleteTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|