|
@@ -21,10 +21,7 @@ const (
|
|
|
lastUpdatedTimeFieldName = "LastUpdatedTime"
|
|
lastUpdatedTimeFieldName = "LastUpdatedTime"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-type ValueCallback[T any] func(e T, fieldName string, value any) (retValue any, err error)
|
|
|
|
|
-type ConditionCallback[T any] func(e T, fieldName string, columnName string, value any) (retConditionOp string, retConditionValue any, err error)
|
|
|
|
|
-
|
|
|
|
|
-func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCallback[T]) error {
|
|
|
|
|
|
|
+func InsertEntity[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return errors.New("没有传递执行器")
|
|
return errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
@@ -42,9 +39,7 @@ func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParams := raw_sql_tpl.InsertExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ tableRow := raw_sql_tpl.NewTableRows()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
now := time.Now()
|
|
|
|
|
|
|
@@ -56,36 +51,21 @@ func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
value = sqlMappingColumn.ValueFieldValue.Interface()
|
|
value = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if sqlMappingColumn.InsertCallback {
|
|
|
|
|
- if callback == nil {
|
|
|
|
|
- return errors.New("需要使用回调函数但是没有传递回调函数")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retValue, err := callback(e, fieldName, value)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retValueType := reflect.TypeOf(retValue)
|
|
|
|
|
- if retValueType == nil || retValueType.Kind() == reflect.Ptr {
|
|
|
|
|
- return errors.New("返回应当为值类型")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- value = retValue
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
if (fieldName == createdTimeFieldName || fieldName == lastUpdatedTimeFieldName) &&
|
|
if (fieldName == createdTimeFieldName || fieldName == lastUpdatedTimeFieldName) &&
|
|
|
fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
|
value = now
|
|
value = now
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParams.TableRows = append(executeParams.TableRows, &raw_sql_tpl.TableRow{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Value: value,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ err := tableRow.Add(sqlMappingColumn.Name, value).Err
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParamsMap, err := executeParams.Map()
|
|
|
|
|
|
|
+ executeParamsMap, err := raw_sql_tpl.InsertExecuteParams{
|
|
|
|
|
+ TableName: tableName,
|
|
|
|
|
+ TableRows: tableRow,
|
|
|
|
|
+ }.Map()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -102,7 +82,7 @@ func Insert[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func Delete[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
|
|
|
|
+func DeleteEntity[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return errors.New("没有传递执行器")
|
|
return errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
@@ -120,10 +100,7 @@ func Delete[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParams := raw_sql_tpl.DeleteExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ conditions := raw_sql_tpl.NewConditions()
|
|
|
|
|
|
|
|
for _, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
for _, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
if !sqlMappingColumn.IsKey {
|
|
if !sqlMappingColumn.IsKey {
|
|
@@ -137,14 +114,16 @@ func Delete[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
value = sqlMappingColumn.ValueFieldValue.Interface()
|
|
value = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: "=",
|
|
|
|
|
- Value: value,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ err := conditions.Equal(sqlMappingColumn.Name, value).Err
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParamsMap, err := executeParams.Map()
|
|
|
|
|
|
|
+ executeParamsMap, err := raw_sql_tpl.DeleteExecuteParams{
|
|
|
|
|
+ TableName: tableName,
|
|
|
|
|
+ Conditions: conditions,
|
|
|
|
|
+ }.Map()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -157,7 +136,7 @@ func Delete[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCallback[T]) error {
|
|
|
|
|
|
|
+func UpdateEntity[T any](executor SqlExecutor, tableName string, e T) error {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return errors.New("没有传递执行器")
|
|
return errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
@@ -175,10 +154,8 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParams := raw_sql_tpl.UpdateExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ tableRows := raw_sql_tpl.NewTableRows()
|
|
|
|
|
+ conditions := raw_sql_tpl.NewConditions()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
now := time.Now()
|
|
|
|
|
|
|
@@ -194,51 +171,36 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
value = sqlMappingColumn.ValueFieldValue.Interface()
|
|
value = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if sqlMappingColumn.UpdateCallback {
|
|
|
|
|
- if callback == nil {
|
|
|
|
|
- return errors.New("需要使用回调函数但是没有传递回调函数")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retValue, err := callback(e, fieldName, value)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retValueType := reflect.TypeOf(retValue)
|
|
|
|
|
- if retValueType == nil || retValueType.Kind() == reflect.Ptr {
|
|
|
|
|
- return errors.New("返回应当为值类型")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- value = retValue
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
if fieldName == lastUpdatedTimeFieldName &&
|
|
if fieldName == lastUpdatedTimeFieldName &&
|
|
|
fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
fieldType.String() == "time.Time" && value.(time.Time).IsZero() {
|
|
|
value = now
|
|
value = now
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 字段为空不更新
|
|
|
|
|
|
|
+ // 字段为空且不能清空,不更新
|
|
|
if reflect.ValueOf(value).IsZero() && !sqlMappingColumn.CanUpdateClear {
|
|
if reflect.ValueOf(value).IsZero() && !sqlMappingColumn.CanUpdateClear {
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if !sqlMappingColumn.IsKey {
|
|
if !sqlMappingColumn.IsKey {
|
|
|
- executeParams.TableRows = append(executeParams.TableRows, &raw_sql_tpl.TableRow{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Value: value,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ err := tableRows.Add(sqlMappingColumn.Name, value).Err
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if sqlMappingColumn.IsKey {
|
|
if sqlMappingColumn.IsKey {
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: "=",
|
|
|
|
|
- Value: value,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ err := conditions.Equal(sqlMappingColumn.Name, value).Err
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParamsMap, err := executeParams.Map()
|
|
|
|
|
|
|
+ executeParamsMap, err := raw_sql_tpl.UpdateExecuteParams{
|
|
|
|
|
+ TableName: tableName,
|
|
|
|
|
+ TableRows: tableRows,
|
|
|
|
|
+ Conditions: conditions,
|
|
|
|
|
+ }.Map()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -251,110 +213,100 @@ func Update[T any](executor SqlExecutor, tableName string, e T, callback ValueCa
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func Query[T any](executor SqlExecutor, tableName string, e T, pageNo int, pageSize int, callback ConditionCallback[T]) ([]map[string]any, int64, error) {
|
|
|
|
|
|
|
+func Insert(executor SqlExecutor, executeParams *raw_sql_tpl.InsertExecuteParams) error {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
- return nil, 0, errors.New("没有传递执行器")
|
|
|
|
|
|
|
+ return errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if strutils.IsStringEmpty(tableName) {
|
|
|
|
|
- return nil, 0, errors.New("没有传递表名")
|
|
|
|
|
|
|
+ if executeParams == nil {
|
|
|
|
|
+ return errors.New("没有传递执行参数")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if reflect.TypeOf(e) == nil {
|
|
|
|
|
- return nil, 0, errors.New("没有传递实体")
|
|
|
|
|
|
|
+ executeParamsMap, err := executeParams.Map()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- sqlMapping, err := tag.ParseSqlMapping(e)
|
|
|
|
|
|
|
+ _, err = executor.ExecuteRawSql(raw_sql_tpl.InsertTpl, executeParamsMap)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return nil, 0, err
|
|
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var offset int
|
|
|
|
|
- var limit int
|
|
|
|
|
- if pageNo != 0 && pageSize != 0 {
|
|
|
|
|
- offset = (pageNo - 1) * pageSize
|
|
|
|
|
- limit = pageSize
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- executeParams := raw_sql_tpl.QueryExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
- Limit: limit,
|
|
|
|
|
- Offset: offset,
|
|
|
|
|
|
|
+func Delete(executor SqlExecutor, executeParams *raw_sql_tpl.DeleteExecuteParams) error {
|
|
|
|
|
+ if executor == nil {
|
|
|
|
|
+ return errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- countParams := raw_sql_tpl.CountExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
|
|
+ if executeParams == nil {
|
|
|
|
|
+ return errors.New("没有传递执行参数")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- if !sqlMappingColumn.CanQuery {
|
|
|
|
|
- continue
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- fieldType := sqlMappingColumn.ValueFieldType
|
|
|
|
|
|
|
+ executeParamsMap, err := executeParams.Map()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- conditionValue := reflect.Zero(fieldType).Interface()
|
|
|
|
|
- if !sqlMappingColumn.ValueFieldValue.IsZero() {
|
|
|
|
|
- conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ _, err = executor.ExecuteRawSql(raw_sql_tpl.DeleteTpl, executeParamsMap)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- conditionOp := "="
|
|
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if sqlMappingColumn.QueryCallback {
|
|
|
|
|
- if callback == nil {
|
|
|
|
|
- return nil, 0, errors.New("需要使用回调函数但是没有传递回调函数")
|
|
|
|
|
- }
|
|
|
|
|
|
|
+func Update(executor SqlExecutor, executeParams *raw_sql_tpl.UpdateExecuteParams) error {
|
|
|
|
|
+ if executor == nil {
|
|
|
|
|
+ return errors.New("没有传递执行器")
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, 0, err
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if executeParams == nil {
|
|
|
|
|
+ return errors.New("没有传递执行参数")
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- retValueType := reflect.TypeOf(retConditionValue)
|
|
|
|
|
- if retValueType == nil || retValueType.Kind() == reflect.Ptr {
|
|
|
|
|
- return nil, 0, errors.New("返回应当为值类型")
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ executeParamsMap, err := executeParams.Map()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- conditionValue = retConditionValue
|
|
|
|
|
- conditionOp = retConditionOp
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ _, err = executor.ExecuteRawSql(raw_sql_tpl.UpdateTpl, executeParamsMap)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 字段为空不更新
|
|
|
|
|
- if reflect.ValueOf(conditionValue).IsZero() {
|
|
|
|
|
- continue
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: conditionOp,
|
|
|
|
|
- Value: conditionValue,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+func Query(executor SqlExecutor, executeParams *raw_sql_tpl.QueryExecuteParams) ([]map[string]any, int64, error) {
|
|
|
|
|
+ if executor == nil {
|
|
|
|
|
+ return nil, 0, errors.New("没有传递执行器")
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- countParams.Conditions = append(countParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: conditionOp,
|
|
|
|
|
- Value: conditionValue,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ if executeParams == nil {
|
|
|
|
|
+ return nil, 0, errors.New("没有传递执行参数")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- executeParamsMap, err := executeParams.Map()
|
|
|
|
|
|
|
+ queryExecuteParamsMap, err := executeParams.Map()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, 0, err
|
|
return nil, 0, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- countParamsMap, err := countParams.Map()
|
|
|
|
|
|
|
+ countExecuteParamsMap, err := raw_sql_tpl.ConditionsExecuteParams{
|
|
|
|
|
+ TableName: executeParams.TableName,
|
|
|
|
|
+ Conditions: executeParams.Conditions,
|
|
|
|
|
+ }.Map()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, 0, err
|
|
return nil, 0, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- tableRows, err := executor.ExecuteRawSql(raw_sql_tpl.QueryTpl, executeParamsMap)
|
|
|
|
|
|
|
+ tableRows, err := executor.ExecuteRawSql(raw_sql_tpl.QueryTpl, queryExecuteParamsMap)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, 0, err
|
|
return nil, 0, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- countTableRow, err := executor.ExecuteRawSql(raw_sql_tpl.CountTpl, countParamsMap)
|
|
|
|
|
|
|
+ countTableRow, err := executor.ExecuteRawSql(raw_sql_tpl.CountTpl, countExecuteParamsMap)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, 0, err
|
|
return nil, 0, err
|
|
|
}
|
|
}
|
|
@@ -362,48 +314,13 @@ func Query[T any](executor SqlExecutor, tableName string, e T, pageNo int, pageS
|
|
|
return tableRows, int64(countTableRow[0]["count"].(float64)), nil
|
|
return tableRows, int64(countTableRow[0]["count"].(float64)), nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func QueryByKeys[T any](executor SqlExecutor, tableName string, e T) (map[string]any, error) {
|
|
|
|
|
|
|
+func QueryOne(executor SqlExecutor, executeParams *raw_sql_tpl.QueryExecuteParams) (map[string]any, error) {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return nil, errors.New("没有传递执行器")
|
|
return nil, errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if strutils.IsStringEmpty(tableName) {
|
|
|
|
|
- return nil, errors.New("没有传递表名")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if reflect.TypeOf(e) == nil {
|
|
|
|
|
- return nil, errors.New("没有传递实体")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- sqlMapping, err := tag.ParseSqlMapping(e)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams := raw_sql_tpl.QueryExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
- Limit: 0,
|
|
|
|
|
- Offset: 0,
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for _, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- if !sqlMappingColumn.IsKey {
|
|
|
|
|
- continue
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- fieldType := sqlMappingColumn.ValueFieldType
|
|
|
|
|
-
|
|
|
|
|
- conditionValue := reflect.Zero(fieldType).Interface()
|
|
|
|
|
- if !sqlMappingColumn.ValueFieldValue.IsZero() {
|
|
|
|
|
- conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: "=",
|
|
|
|
|
- Value: conditionValue,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ if executeParams == nil {
|
|
|
|
|
+ return nil, errors.New("没有传递执行参数")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
executeParamsMap, err := executeParams.Map()
|
|
executeParamsMap, err := executeParams.Map()
|
|
@@ -423,63 +340,13 @@ func QueryByKeys[T any](executor SqlExecutor, tableName string, e T) (map[string
|
|
|
return tableRows[0], nil
|
|
return tableRows[0], nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func Count[T any](executor SqlExecutor, tableName string, e T, callback ConditionCallback[T]) (int64, error) {
|
|
|
|
|
|
|
+func Count(executor SqlExecutor, executeParams *raw_sql_tpl.ConditionsExecuteParams) (int64, error) {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return 0, errors.New("没有传递执行器")
|
|
return 0, errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if strutils.IsStringEmpty(tableName) {
|
|
|
|
|
- return 0, errors.New("没有传递表名")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if reflect.TypeOf(e) == nil {
|
|
|
|
|
- return 0, errors.New("没有传递实体")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- sqlMapping, err := tag.ParseSqlMapping(e)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return 0, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams := raw_sql_tpl.CountExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- fieldType := sqlMappingColumn.ValueFieldType
|
|
|
|
|
-
|
|
|
|
|
- conditionValue := reflect.Zero(fieldType).Interface()
|
|
|
|
|
- if !sqlMappingColumn.ValueFieldValue.IsZero() {
|
|
|
|
|
- conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- conditionOp := "="
|
|
|
|
|
-
|
|
|
|
|
- if sqlMappingColumn.CountCallback {
|
|
|
|
|
- if callback == nil {
|
|
|
|
|
- return 0, errors.New("需要使用回调函数但是没有传递回调函数")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return 0, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retValueType := reflect.TypeOf(retConditionValue)
|
|
|
|
|
- if retValueType == nil || retValueType.Kind() == reflect.Ptr {
|
|
|
|
|
- return 0, errors.New("返回应当为值类型")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- conditionValue = retConditionValue
|
|
|
|
|
- conditionOp = retConditionOp
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: conditionOp,
|
|
|
|
|
- Value: conditionValue,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ if executeParams == nil {
|
|
|
|
|
+ return 0, errors.New("没有传递执行参数")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
executeParamsMap, err := executeParams.Map()
|
|
executeParamsMap, err := executeParams.Map()
|
|
@@ -495,63 +362,13 @@ func Count[T any](executor SqlExecutor, tableName string, e T, callback Conditio
|
|
|
return int64(tableRows[0]["count"].(float64)), nil
|
|
return int64(tableRows[0]["count"].(float64)), nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func CheckExist[T any](executor SqlExecutor, tableName string, e T, callback ConditionCallback[T]) (bool, error) {
|
|
|
|
|
|
|
+func CheckExist(executor SqlExecutor, executeParams *raw_sql_tpl.ConditionsExecuteParams) (bool, error) {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return false, errors.New("没有传递执行器")
|
|
return false, errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if strutils.IsStringEmpty(tableName) {
|
|
|
|
|
- return false, errors.New("没有传递表名")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if reflect.TypeOf(e) == nil {
|
|
|
|
|
- return false, errors.New("没有传递实体")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- sqlMapping, err := tag.ParseSqlMapping(e)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return false, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams := raw_sql_tpl.CountExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- fieldType := sqlMappingColumn.ValueFieldType
|
|
|
|
|
-
|
|
|
|
|
- conditionValue := reflect.Zero(fieldType).Interface()
|
|
|
|
|
- if !sqlMappingColumn.ValueFieldValue.IsZero() {
|
|
|
|
|
- conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- conditionOp := "="
|
|
|
|
|
-
|
|
|
|
|
- if sqlMappingColumn.CheckExistCallback {
|
|
|
|
|
- if callback == nil {
|
|
|
|
|
- return false, errors.New("需要使用回调函数但是没有传递回调函数")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return false, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retValueType := reflect.TypeOf(retConditionValue)
|
|
|
|
|
- if retValueType == nil || retValueType.Kind() == reflect.Ptr {
|
|
|
|
|
- return false, errors.New("返回应当为值类型")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- conditionValue = retConditionValue
|
|
|
|
|
- conditionOp = retConditionOp
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: conditionOp,
|
|
|
|
|
- Value: conditionValue,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ if executeParams == nil {
|
|
|
|
|
+ return false, errors.New("没有传递执行参数")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
executeParamsMap, err := executeParams.Map()
|
|
executeParamsMap, err := executeParams.Map()
|
|
@@ -567,118 +384,13 @@ func CheckExist[T any](executor SqlExecutor, tableName string, e T, callback Con
|
|
|
return int64(tableRows[0]["count"].(float64)) > 0, nil
|
|
return int64(tableRows[0]["count"].(float64)) > 0, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func CheckExistByKey[T any](executor SqlExecutor, tableName string, e T) (bool, error) {
|
|
|
|
|
|
|
+func CheckHasOnlyOne(executor SqlExecutor, executeParams *raw_sql_tpl.ConditionsExecuteParams) (bool, error) {
|
|
|
if executor == nil {
|
|
if executor == nil {
|
|
|
return false, errors.New("没有传递执行器")
|
|
return false, errors.New("没有传递执行器")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if strutils.IsStringEmpty(tableName) {
|
|
|
|
|
- return false, errors.New("没有传递表名")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if reflect.TypeOf(e) == nil {
|
|
|
|
|
- return false, errors.New("没有传递实体")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- sqlMapping, err := tag.ParseSqlMapping(e)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return false, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams := raw_sql_tpl.CountExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for _, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- if !sqlMappingColumn.IsKey {
|
|
|
|
|
- continue
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- fieldType := sqlMappingColumn.ValueFieldType
|
|
|
|
|
-
|
|
|
|
|
- conditionValue := reflect.Zero(fieldType).Interface()
|
|
|
|
|
- if !sqlMappingColumn.ValueFieldValue.IsZero() {
|
|
|
|
|
- conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: "=",
|
|
|
|
|
- Value: conditionValue,
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParamsMap, err := executeParams.Map()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return false, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- tableRows, err := executor.ExecuteRawSql(raw_sql_tpl.CountTpl, executeParamsMap)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return false, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return int64(tableRows[0]["count"].(float64)) > 0, nil
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func CheckHasOnlyOne[T any](executor SqlExecutor, tableName string, e T, callback ConditionCallback[T]) (bool, error) {
|
|
|
|
|
- if executor == nil {
|
|
|
|
|
- return false, errors.New("没有传递执行器")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if strutils.IsStringEmpty(tableName) {
|
|
|
|
|
- return false, errors.New("没有传递表名")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if reflect.TypeOf(e) == nil {
|
|
|
|
|
- return false, errors.New("没有传递实体")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- sqlMapping, err := tag.ParseSqlMapping(e)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return false, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams := raw_sql_tpl.CountExecuteParams{
|
|
|
|
|
- TableName: tableName,
|
|
|
|
|
- Conditions: make([]*raw_sql_tpl.Condition, 0),
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for fieldName, sqlMappingColumn := range sqlMapping.ColumnMap {
|
|
|
|
|
- fieldType := sqlMappingColumn.ValueFieldType
|
|
|
|
|
-
|
|
|
|
|
- conditionValue := reflect.Zero(fieldType).Interface()
|
|
|
|
|
- if !sqlMappingColumn.ValueFieldValue.IsZero() {
|
|
|
|
|
- conditionValue = sqlMappingColumn.ValueFieldValue.Interface()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- conditionOp := "="
|
|
|
|
|
-
|
|
|
|
|
- if sqlMappingColumn.QueryCallback {
|
|
|
|
|
- if callback == nil {
|
|
|
|
|
- return false, errors.New("需要使用回调函数但是没有传递回调函数")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retConditionOp, retConditionValue, err := callback(e, fieldName, sqlMappingColumn.Name, conditionValue)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return false, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- retValueType := reflect.TypeOf(retConditionValue)
|
|
|
|
|
- if retValueType == nil || retValueType.Kind() == reflect.Ptr {
|
|
|
|
|
- return false, errors.New("返回应当为值类型")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- conditionValue = retConditionValue
|
|
|
|
|
- conditionOp = retConditionOp
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- executeParams.Conditions = append(executeParams.Conditions, &raw_sql_tpl.Condition{
|
|
|
|
|
- Column: sqlMappingColumn.Name,
|
|
|
|
|
- Operator: conditionOp,
|
|
|
|
|
- Value: conditionValue,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ if executeParams == nil {
|
|
|
|
|
+ return false, errors.New("没有传递执行参数")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
executeParamsMap, err := executeParams.Map()
|
|
executeParamsMap, err := executeParams.Map()
|
|
@@ -732,7 +444,7 @@ const (
|
|
|
sqlResultTimeFormat = "2006-01-02T15:04:05.000000+08:00"
|
|
sqlResultTimeFormat = "2006-01-02T15:04:05.000000+08:00"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-func parseSqlResults(results any, e any) error {
|
|
|
|
|
|
|
+func ParseSqlResults(results any, e any) error {
|
|
|
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
DecodeHook: mapstructure.StringToTimeHookFunc(sqlResultTimeFormat),
|
|
DecodeHook: mapstructure.StringToTimeHookFunc(sqlResultTimeFormat),
|
|
|
Result: e,
|
|
Result: e,
|