|
|
@@ -4,6 +4,7 @@ import (
|
|
|
"fmt"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/tag/sql/sql_result"
|
|
|
"git.sxidc.com/go-tools/utils/reflectutils"
|
|
|
+ "git.sxidc.com/go-tools/utils/strutils"
|
|
|
"github.com/pkg/errors"
|
|
|
"reflect"
|
|
|
"strings"
|
|
|
@@ -16,79 +17,112 @@ const (
|
|
|
resultTimeSecFormat = "2006-01-02T15:04:05+08:00"
|
|
|
)
|
|
|
|
|
|
-func ParseSqlResult(input any, e any) error {
|
|
|
+func ParseSqlResult(input any, output any) error {
|
|
|
+ return ParseSqlResultWithColumn(input, output, "")
|
|
|
+}
|
|
|
+
|
|
|
+func ParseSqlResultWithColumn(input any, output any, columnName string) error {
|
|
|
if input == nil {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- if e == nil {
|
|
|
+ if output == nil {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- results, ok := input.([]Result)
|
|
|
- if !ok {
|
|
|
- tableRow, ok := input.(Result)
|
|
|
- if !ok {
|
|
|
- return errors.New("输入数据应该为sdk.SqlResult或[]sdk.SqlResult")
|
|
|
- }
|
|
|
+ typeCheckErr := errors.New("可以接受的输出类型为指针或者slice的指针")
|
|
|
+ outputType := reflect.TypeOf(output)
|
|
|
|
|
|
- results = []Result{tableRow}
|
|
|
+ if outputType.Kind() != reflect.Pointer {
|
|
|
+ return typeCheckErr
|
|
|
}
|
|
|
|
|
|
- typeCheckErr := errors.New("可以接受的输出类型为结构指针或者结构slice的指针")
|
|
|
- outputValue := reflect.ValueOf(e)
|
|
|
-
|
|
|
- if outputValue.Kind() != reflect.Pointer {
|
|
|
- return typeCheckErr
|
|
|
- } else {
|
|
|
- outputElemValue := reflectutils.PointerValueElem(outputValue)
|
|
|
+ outputElemType := reflectutils.PointerTypeElem(outputType)
|
|
|
|
|
|
- if outputElemValue.Kind() != reflect.Struct && outputElemValue.Kind() != reflect.Slice {
|
|
|
- return typeCheckErr
|
|
|
+ // 输出不是slice,直接用result赋值即可
|
|
|
+ if outputElemType.Kind() != reflect.Slice {
|
|
|
+ result, ok := input.(Result)
|
|
|
+ if !ok {
|
|
|
+ return errors.New("输出不是slice,输入需要是sql.Result")
|
|
|
}
|
|
|
|
|
|
- if outputElemValue.Kind() == reflect.Slice && !reflectutils.IsSliceValueOf(outputElemValue, reflect.Struct) {
|
|
|
- return typeCheckErr
|
|
|
- }
|
|
|
+ return parseSqlSingle(result, output, columnName)
|
|
|
}
|
|
|
|
|
|
- outputElemValue := reflectutils.PointerValueElem(outputValue)
|
|
|
-
|
|
|
- // 构造输出实体slice
|
|
|
- var outputEntities reflect.Value
|
|
|
- if outputElemValue.Kind() == reflect.Struct {
|
|
|
- outputEntities = reflect.MakeSlice(reflect.SliceOf(outputElemValue.Type()), 0, 0)
|
|
|
- } else {
|
|
|
- outputEntities = reflect.MakeSlice(outputElemValue.Type(), 0, 0)
|
|
|
+ // 输出是slice,需要遍历处理
|
|
|
+ results, ok := input.([]Result)
|
|
|
+ if !ok {
|
|
|
+ return errors.New("输出是slice,输入需要是[]sql.Result")
|
|
|
}
|
|
|
|
|
|
+ outputEntities := reflect.MakeSlice(outputElemType, 0, 0)
|
|
|
+
|
|
|
for _, result := range results {
|
|
|
var outputEntityValue reflect.Value
|
|
|
- if outputElemValue.Kind() == reflect.Struct {
|
|
|
- outputEntityValue = reflect.New(outputElemValue.Type()).Elem()
|
|
|
- } else {
|
|
|
- outputEntityValue = reflect.New(outputElemValue.Type().Elem()).Elem()
|
|
|
- }
|
|
|
-
|
|
|
- outputEntity := outputEntityValue.Addr().Interface()
|
|
|
|
|
|
- err := sql_result.DefaultUsage(result, outputEntity)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
+ // slice子类型判断
|
|
|
+ if outputElemType.Elem().Kind() == reflect.Pointer {
|
|
|
+ outputEntityValue = reflect.New(outputElemType.Elem().Elem())
|
|
|
+ err := parseSqlSingle(result, outputEntityValue.Interface(), columnName)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ outputEntityValue = reflect.New(outputElemType.Elem()).Elem()
|
|
|
+ err := parseSqlSingle(result, outputEntityValue.Addr().Interface(), columnName)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 保存输出实体
|
|
|
outputEntities = reflect.Append(outputEntities, outputEntityValue)
|
|
|
}
|
|
|
|
|
|
- // 将输出实体赋值给输出指针变量
|
|
|
- if outputElemValue.Kind() == reflect.Slice {
|
|
|
- outputElemValue.Set(outputEntities)
|
|
|
+ outputElemValue := reflectutils.PointerValueElem(reflect.ValueOf(output))
|
|
|
+ outputElemValue.Set(outputEntities)
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func parseSqlSingle(result Result, output any, columnName string) error {
|
|
|
+ outputValue := reflectutils.PointerValueElem(reflect.ValueOf(output))
|
|
|
+ outputKind := reflectutils.GroupValueKind(outputValue)
|
|
|
+
|
|
|
+ var oneResultValue any
|
|
|
+ if strutils.IsStringEmpty(columnName) {
|
|
|
+ for _, value := range result {
|
|
|
+ oneResultValue = value
|
|
|
+ break
|
|
|
+ }
|
|
|
} else {
|
|
|
- outputElemValue.Set(outputEntities.Index(0))
|
|
|
+ value, ok := result[columnName]
|
|
|
+ if !ok {
|
|
|
+ return errors.New("列不存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ oneResultValue = value
|
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
+ switch outputKind {
|
|
|
+ case reflect.String:
|
|
|
+ return reflectutils.AssignStringValue(oneResultValue, outputValue)
|
|
|
+ case reflect.Bool:
|
|
|
+ return reflectutils.AssignBoolValue(oneResultValue, outputValue)
|
|
|
+ case reflect.Int64:
|
|
|
+ return reflectutils.AssignInt64Value(oneResultValue, outputValue)
|
|
|
+ case reflect.Uint64:
|
|
|
+ return reflectutils.AssignUint64Value(oneResultValue, outputValue)
|
|
|
+ case reflect.Float64:
|
|
|
+ return reflectutils.AssignFloat64Value(oneResultValue, outputValue)
|
|
|
+ case reflect.Struct:
|
|
|
+ if outputValue.Type().Name() == "time.Time" {
|
|
|
+ return errors.New("不支持转换到time.Time,请使用Result.ColumnValueStringAsTime")
|
|
|
+ }
|
|
|
+
|
|
|
+ return sql_result.DefaultUsage(result, output, columnName)
|
|
|
+ default:
|
|
|
+ return errors.New("不支持的类型")
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
type Result map[string]any
|