|
|
@@ -6,7 +6,6 @@ import (
|
|
|
"git.sxidc.com/service-supports/ds-sdk/sdk/raw_sql_tpl"
|
|
|
"git.sxidc.com/service-supports/ds-sdk/sdk/tag"
|
|
|
"reflect"
|
|
|
- "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
@@ -17,7 +16,6 @@ type SqlExecutor interface {
|
|
|
}
|
|
|
|
|
|
const (
|
|
|
- timeWriteFormat = time.DateTime + ".000000 +08:00"
|
|
|
createdTimeFieldName = "CreatedTime"
|
|
|
lastUpdatedTimeFieldName = "LastUpdatedTime"
|
|
|
)
|
|
|
@@ -80,14 +78,9 @@ func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
value = now
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(value)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- executeParams.TableRows = append(executeParams.TableRows, raw_sql_tpl.TableRow{
|
|
|
+ executeParams.TableRows = append(executeParams.TableRows, &raw_sql_tpl.TableRow{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: value,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -128,7 +121,7 @@ func Delete[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
|
|
|
executeParams := raw_sql_tpl.DeleteExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
}
|
|
|
|
|
|
for _, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
@@ -143,15 +136,10 @@ func Delete[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
value = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(value)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: "=",
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: value,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -188,7 +176,7 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
|
|
|
executeParams := raw_sql_tpl.UpdateExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
}
|
|
|
|
|
|
now := time.Now()
|
|
|
@@ -233,23 +221,18 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(value)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
if !sqlMappingColumn.IsKey {
|
|
|
- executeParams.TableRows = append(executeParams.TableRows, raw_sql_tpl.TableRow{
|
|
|
+ executeParams.TableRows = append(executeParams.TableRows, &raw_sql_tpl.TableRow{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: value,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
if sqlMappingColumn.IsKey {
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: "=",
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: value,
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -294,14 +277,14 @@ func Query[T any](executor SqlExecutor, tableName string, e T, pageNo int, pageS
|
|
|
|
|
|
executeParams := raw_sql_tpl.QueryExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
Limit: limit,
|
|
|
Offset: offset,
|
|
|
}
|
|
|
|
|
|
countParams := raw_sql_tpl.CountExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
}
|
|
|
|
|
|
for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
@@ -342,21 +325,16 @@ func Query[T any](executor SqlExecutor, tableName string, e T, pageNo int, pageS
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(conditionValue)
|
|
|
- if err != nil {
|
|
|
- return nil, 0, err
|
|
|
- }
|
|
|
-
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: conditionOp,
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: conditionValue,
|
|
|
})
|
|
|
|
|
|
- countParams.Conditions = append(countParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ countParams.Conditions = append(countParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: conditionOp,
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: conditionValue,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -403,7 +381,7 @@ func QueryByKeys[T any](executor SqlExecutor, tableName string, e T) (map[string
|
|
|
|
|
|
executeParams := raw_sql_tpl.QueryExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
Limit: 0,
|
|
|
Offset: 0,
|
|
|
}
|
|
|
@@ -420,15 +398,10 @@ func QueryByKeys[T any](executor SqlExecutor, tableName string, e T) (map[string
|
|
|
conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(conditionValue)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: "=",
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: conditionValue,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -469,7 +442,7 @@ func Count[T any](executor SqlExecutor, tableName string, e T, callback Conditio
|
|
|
|
|
|
executeParams := raw_sql_tpl.CountExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
}
|
|
|
|
|
|
for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
@@ -501,15 +474,10 @@ func Count[T any](executor SqlExecutor, tableName string, e T, callback Conditio
|
|
|
conditionOp = retConditionOp
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(conditionValue)
|
|
|
- if err != nil {
|
|
|
- return 0, err
|
|
|
- }
|
|
|
-
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: conditionOp,
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: conditionValue,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -546,7 +514,7 @@ func CheckExist[T any](executor SqlExecutor, tableName string, e T, callback Con
|
|
|
|
|
|
executeParams := raw_sql_tpl.CountExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
}
|
|
|
|
|
|
for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
@@ -578,15 +546,10 @@ func CheckExist[T any](executor SqlExecutor, tableName string, e T, callback Con
|
|
|
conditionOp = retConditionOp
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(conditionValue)
|
|
|
- if err != nil {
|
|
|
- return false, err
|
|
|
- }
|
|
|
-
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: conditionOp,
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: conditionValue,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -623,7 +586,7 @@ func CheckExistByKey[T any](executor SqlExecutor, tableName string, e T) (bool,
|
|
|
|
|
|
executeParams := raw_sql_tpl.CountExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
}
|
|
|
|
|
|
for _, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
@@ -638,15 +601,10 @@ func CheckExistByKey[T any](executor SqlExecutor, tableName string, e T) (bool,
|
|
|
conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(conditionValue)
|
|
|
- if err != nil {
|
|
|
- return false, err
|
|
|
- }
|
|
|
-
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: "=",
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: conditionValue,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -683,7 +641,7 @@ func CheckHasOnlyOne[T any](executor SqlExecutor, tableName string, e T, callbac
|
|
|
|
|
|
executeParams := raw_sql_tpl.CountExecuteParams{
|
|
|
TableName: tableName,
|
|
|
- Conditions: make([]raw_sql_tpl.Condition, 0),
|
|
|
+ Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
}
|
|
|
|
|
|
for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
@@ -715,15 +673,10 @@ func CheckHasOnlyOne[T any](executor SqlExecutor, tableName string, e T, callbac
|
|
|
conditionOp = retConditionOp
|
|
|
}
|
|
|
|
|
|
- tableRowValue, err := parseValue(conditionValue)
|
|
|
- if err != nil {
|
|
|
- return false, err
|
|
|
- }
|
|
|
-
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, raw_sql_tpl.Condition{
|
|
|
+ executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
Column: sqlMappingColumn.Name,
|
|
|
Operator: conditionOp,
|
|
|
- Value: tableRowValue,
|
|
|
+ Value: conditionValue,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -773,36 +726,3 @@ func ExecuteSql(executor SqlExecutor, name string, executeParams map[string]any)
|
|
|
|
|
|
return tableRows, nil
|
|
|
}
|
|
|
-
|
|
|
-func parseValue(value any) (string, error) {
|
|
|
- switch v := value.(type) {
|
|
|
- case string:
|
|
|
- return "'" + v + "'", nil
|
|
|
- case bool:
|
|
|
- return strconv.FormatBool(v), nil
|
|
|
- case time.Time:
|
|
|
- return "'" + v.Format(timeWriteFormat) + "'", nil
|
|
|
- case int:
|
|
|
- return strconv.Itoa(v), nil
|
|
|
- case int8:
|
|
|
- return strconv.FormatInt(int64(v), 10), nil
|
|
|
- case int16:
|
|
|
- return strconv.FormatInt(int64(v), 10), nil
|
|
|
- case int32:
|
|
|
- return strconv.FormatInt(int64(v), 10), nil
|
|
|
- case int64:
|
|
|
- return strconv.FormatInt(v, 10), nil
|
|
|
- case uint:
|
|
|
- return strconv.FormatUint(uint64(v), 10), nil
|
|
|
- case uint8:
|
|
|
- return strconv.FormatUint(uint64(v), 10), nil
|
|
|
- case uint16:
|
|
|
- return strconv.FormatUint(uint64(v), 10), nil
|
|
|
- case uint32:
|
|
|
- return strconv.FormatUint(uint64(v), 10), nil
|
|
|
- case uint64:
|
|
|
- return strconv.FormatUint(v, 10), nil
|
|
|
- default:
|
|
|
- return "", errors.New("不支持的类型")
|
|
|
- }
|
|
|
-}
|