|
|
@@ -17,116 +17,14 @@ const (
|
|
|
resultTimeSecFormat = "2006-01-02T15:04:05+08:00"
|
|
|
)
|
|
|
|
|
|
-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 output == nil {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- typeCheckErr := errors.New("可以接受的输出类型为指针或者slice的指针")
|
|
|
- outputType := reflect.TypeOf(output)
|
|
|
-
|
|
|
- if outputType.Kind() != reflect.Pointer {
|
|
|
- return typeCheckErr
|
|
|
- }
|
|
|
-
|
|
|
- outputElemType := reflectutils.PointerTypeElem(outputType)
|
|
|
-
|
|
|
- // 输出不是slice,直接用result赋值即可
|
|
|
- if outputElemType.Kind() != reflect.Slice {
|
|
|
- result, ok := input.(Result)
|
|
|
- if !ok {
|
|
|
- return errors.New("输出不是slice,输入需要是sql.Result")
|
|
|
- }
|
|
|
-
|
|
|
- return parseSqlSingle(result, output, columnName)
|
|
|
- }
|
|
|
-
|
|
|
- // 输出是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
|
|
|
-
|
|
|
- // 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)
|
|
|
- }
|
|
|
-
|
|
|
- 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 {
|
|
|
- value, ok := result[columnName]
|
|
|
- if !ok {
|
|
|
- return errors.New("列不存在")
|
|
|
- }
|
|
|
-
|
|
|
- oneResultValue = value
|
|
|
- }
|
|
|
-
|
|
|
- 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("不支持的类型")
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
+// Result 查询结果
|
|
|
type Result map[string]any
|
|
|
|
|
|
+// ColumnValueStringAsTime 将string类型的列值转化为time.Time
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - 转换为time.Time的列值
|
|
|
func (result Result) ColumnValueStringAsTime(columnName string) time.Time {
|
|
|
value, ok := result[columnName].(string)
|
|
|
if !ok {
|
|
|
@@ -157,6 +55,11 @@ func (result Result) ColumnValueStringAsTime(columnName string) time.Time {
|
|
|
return t
|
|
|
}
|
|
|
|
|
|
+// ColumnValueBool 以bool类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - bool类型的列值
|
|
|
func (result Result) ColumnValueBool(columnName string) bool {
|
|
|
value, err := reflectutils.ToBool(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -167,6 +70,11 @@ func (result Result) ColumnValueBool(columnName string) bool {
|
|
|
return value
|
|
|
}
|
|
|
|
|
|
+// ColumnValueString 以string类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - string类型的列值
|
|
|
func (result Result) ColumnValueString(columnName string) string {
|
|
|
value, err := reflectutils.ToString(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -177,6 +85,11 @@ func (result Result) ColumnValueString(columnName string) string {
|
|
|
return value
|
|
|
}
|
|
|
|
|
|
+// ColumnValueInt 以int类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - int类型的列值
|
|
|
func (result Result) ColumnValueInt(columnName string) int {
|
|
|
value, err := reflectutils.ToInt64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -187,6 +100,11 @@ func (result Result) ColumnValueInt(columnName string) int {
|
|
|
return int(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueInt8 以int8类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - int8类型的列值
|
|
|
func (result Result) ColumnValueInt8(columnName string) int8 {
|
|
|
value, err := reflectutils.ToInt64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -197,6 +115,11 @@ func (result Result) ColumnValueInt8(columnName string) int8 {
|
|
|
return int8(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueInt16 以int16类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - int16类型的列值
|
|
|
func (result Result) ColumnValueInt16(columnName string) int16 {
|
|
|
value, err := reflectutils.ToInt64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -207,6 +130,11 @@ func (result Result) ColumnValueInt16(columnName string) int16 {
|
|
|
return int16(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueInt32 以int32类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - int32类型的列值
|
|
|
func (result Result) ColumnValueInt32(columnName string) int32 {
|
|
|
value, err := reflectutils.ToInt64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -217,6 +145,11 @@ func (result Result) ColumnValueInt32(columnName string) int32 {
|
|
|
return int32(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueInt64 以int64类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - int64类型的列值
|
|
|
func (result Result) ColumnValueInt64(columnName string) int64 {
|
|
|
value, err := reflectutils.ToInt64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -227,6 +160,11 @@ func (result Result) ColumnValueInt64(columnName string) int64 {
|
|
|
return value
|
|
|
}
|
|
|
|
|
|
+// ColumnValueUint 以uint类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - uint类型的列值
|
|
|
func (result Result) ColumnValueUint(columnName string) uint {
|
|
|
value, err := reflectutils.ToUint64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -237,6 +175,11 @@ func (result Result) ColumnValueUint(columnName string) uint {
|
|
|
return uint(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueUint8 以uint8类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - uint8类型的列值
|
|
|
func (result Result) ColumnValueUint8(columnName string) uint8 {
|
|
|
value, err := reflectutils.ToUint64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -247,6 +190,11 @@ func (result Result) ColumnValueUint8(columnName string) uint8 {
|
|
|
return uint8(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueUint16 以uint16类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - uint16类型的列值
|
|
|
func (result Result) ColumnValueUint16(columnName string) uint16 {
|
|
|
value, err := reflectutils.ToUint64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -257,6 +205,11 @@ func (result Result) ColumnValueUint16(columnName string) uint16 {
|
|
|
return uint16(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueUint32 以uint32类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - uint32类型的列值
|
|
|
func (result Result) ColumnValueUint32(columnName string) uint32 {
|
|
|
value, err := reflectutils.ToUint64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -267,6 +220,11 @@ func (result Result) ColumnValueUint32(columnName string) uint32 {
|
|
|
return uint32(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueUint64 以uint64类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - uint64类型的列值
|
|
|
func (result Result) ColumnValueUint64(columnName string) uint64 {
|
|
|
value, err := reflectutils.ToUint64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -277,6 +235,11 @@ func (result Result) ColumnValueUint64(columnName string) uint64 {
|
|
|
return value
|
|
|
}
|
|
|
|
|
|
+// ColumnValueFloat32 以float32类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - float32类型的列值
|
|
|
func (result Result) ColumnValueFloat32(columnName string) float32 {
|
|
|
value, err := reflectutils.ToFloat64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -287,6 +250,11 @@ func (result Result) ColumnValueFloat32(columnName string) float32 {
|
|
|
return float32(value)
|
|
|
}
|
|
|
|
|
|
+// ColumnValueFloat64 以float64类型获取列值
|
|
|
+// 参数:
|
|
|
+// - columnName: 列名
|
|
|
+// 返回值:
|
|
|
+// - float64类型的列值
|
|
|
func (result Result) ColumnValueFloat64(columnName string) float64 {
|
|
|
value, err := reflectutils.ToFloat64(result[columnName])
|
|
|
if err != nil {
|
|
|
@@ -296,3 +264,124 @@ func (result Result) ColumnValueFloat64(columnName string) float64 {
|
|
|
|
|
|
return value
|
|
|
}
|
|
|
+
|
|
|
+// ParseSqlResult 解析查询结果
|
|
|
+// 参数:
|
|
|
+// - input: sql.Result或者[]sql.Result类型的查询结果
|
|
|
+// - output: 接收查询结果的指针,如果是结构,需要使用sqlresult tag标注字段
|
|
|
+// 返回值:
|
|
|
+// - 错误
|
|
|
+func ParseSqlResult(input any, output any) error {
|
|
|
+ return ParseSqlResultWithColumn(input, output, "")
|
|
|
+}
|
|
|
+
|
|
|
+// ParseSqlResultWithColumn 取查询结果列解析结果
|
|
|
+// 参数:
|
|
|
+// - input: sql.Result或者[]sql.Result类型的查询结果
|
|
|
+// - output: 接收查询结果的指针,如果是结构,需要使用sqlresult tag标注字段
|
|
|
+// - columnName: 获取的列名
|
|
|
+// 返回值:
|
|
|
+// - 错误
|
|
|
+func ParseSqlResultWithColumn(input any, output any, columnName string) error {
|
|
|
+ if input == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if output == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ typeCheckErr := errors.New("可以接受的输出类型为指针或者slice的指针")
|
|
|
+ outputType := reflect.TypeOf(output)
|
|
|
+
|
|
|
+ if outputType.Kind() != reflect.Pointer {
|
|
|
+ return typeCheckErr
|
|
|
+ }
|
|
|
+
|
|
|
+ outputElemType := reflectutils.PointerTypeElem(outputType)
|
|
|
+
|
|
|
+ // 输出不是slice,直接用result赋值即可
|
|
|
+ if outputElemType.Kind() != reflect.Slice {
|
|
|
+ result, ok := input.(Result)
|
|
|
+ if !ok {
|
|
|
+ return errors.New("输出不是slice,输入需要是sql.Result")
|
|
|
+ }
|
|
|
+
|
|
|
+ return parseSqlSingle(result, output, columnName)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 输出是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
|
|
|
+
|
|
|
+ // 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ value, ok := result[columnName]
|
|
|
+ if !ok {
|
|
|
+ return errors.New("列不存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ oneResultValue = value
|
|
|
+ }
|
|
|
+
|
|
|
+ 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("不支持的类型")
|
|
|
+ }
|
|
|
+}
|