result.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package sql
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
  4. "git.sxidc.com/go-framework/baize/framework/core/tag/sql/sql_result"
  5. "git.sxidc.com/go-tools/utils/reflectutils"
  6. "git.sxidc.com/go-tools/utils/strutils"
  7. "github.com/pkg/errors"
  8. "reflect"
  9. "strings"
  10. "time"
  11. )
  12. const (
  13. resultTimeMicroFormat = "2006-01-02T15:04:05.000000+08:00"
  14. resultTimeMilliFormat = "2006-01-02T15:04:05.000+08:00"
  15. resultTimeSecFormat = "2006-01-02T15:04:05+08:00"
  16. )
  17. // Result 查询结果
  18. type Result map[string]any
  19. // ColumnValueTime 以time.Time类型获取列值
  20. // 参数:
  21. // - columnName: 列名
  22. // 返回值:
  23. // - time.Time类型的列值
  24. func (result Result) ColumnValueTime(columnName string) time.Time {
  25. value, ok := result[columnName].(string)
  26. if !ok {
  27. value, ok := result[columnName].(time.Time)
  28. if !ok {
  29. logger.GetInstance().Error(errors.New("无法转换为时间类型"))
  30. return time.Time{}
  31. }
  32. return value
  33. }
  34. layout := resultTimeSecFormat
  35. parseValue := value
  36. timeZoneIndex := strings.LastIndex(value, "+08:")
  37. if timeZoneIndex != -1 {
  38. parseValue = parseValue[:timeZoneIndex]
  39. afterSecondIndex := strings.LastIndex(parseValue, ".")
  40. if afterSecondIndex != -1 {
  41. if len(value)-afterSecondIndex-1 == 6 {
  42. layout = resultTimeMicroFormat
  43. } else if len(value)-afterSecondIndex-1 == 3 {
  44. layout = resultTimeMilliFormat
  45. }
  46. }
  47. }
  48. t, err := time.ParseInLocation(layout, value, time.Local)
  49. if err != nil {
  50. return time.Time{}
  51. }
  52. return t
  53. }
  54. // ColumnValueBool 以bool类型获取列值
  55. // 参数:
  56. // - columnName: 列名
  57. // 返回值:
  58. // - bool类型的列值
  59. func (result Result) ColumnValueBool(columnName string) bool {
  60. value, err := reflectutils.ToBool(result[columnName])
  61. if err != nil {
  62. logger.GetInstance().Error(err)
  63. return false
  64. }
  65. return value
  66. }
  67. // ColumnValueString 以string类型获取列值
  68. // 参数:
  69. // - columnName: 列名
  70. // 返回值:
  71. // - string类型的列值
  72. func (result Result) ColumnValueString(columnName string) string {
  73. value, err := reflectutils.ToString(result[columnName])
  74. if err != nil {
  75. logger.GetInstance().Error(err)
  76. return ""
  77. }
  78. return value
  79. }
  80. // ColumnValueInt 以int类型获取列值
  81. // 参数:
  82. // - columnName: 列名
  83. // 返回值:
  84. // - int类型的列值
  85. func (result Result) ColumnValueInt(columnName string) int {
  86. value, err := reflectutils.ToInt64(result[columnName])
  87. if err != nil {
  88. logger.GetInstance().Error(err)
  89. return 0
  90. }
  91. return int(value)
  92. }
  93. // ColumnValueInt8 以int8类型获取列值
  94. // 参数:
  95. // - columnName: 列名
  96. // 返回值:
  97. // - int8类型的列值
  98. func (result Result) ColumnValueInt8(columnName string) int8 {
  99. value, err := reflectutils.ToInt64(result[columnName])
  100. if err != nil {
  101. logger.GetInstance().Error(err)
  102. return 0
  103. }
  104. return int8(value)
  105. }
  106. // ColumnValueInt16 以int16类型获取列值
  107. // 参数:
  108. // - columnName: 列名
  109. // 返回值:
  110. // - int16类型的列值
  111. func (result Result) ColumnValueInt16(columnName string) int16 {
  112. value, err := reflectutils.ToInt64(result[columnName])
  113. if err != nil {
  114. logger.GetInstance().Error(err)
  115. return 0
  116. }
  117. return int16(value)
  118. }
  119. // ColumnValueInt32 以int32类型获取列值
  120. // 参数:
  121. // - columnName: 列名
  122. // 返回值:
  123. // - int32类型的列值
  124. func (result Result) ColumnValueInt32(columnName string) int32 {
  125. value, err := reflectutils.ToInt64(result[columnName])
  126. if err != nil {
  127. logger.GetInstance().Error(err)
  128. return 0
  129. }
  130. return int32(value)
  131. }
  132. // ColumnValueInt64 以int64类型获取列值
  133. // 参数:
  134. // - columnName: 列名
  135. // 返回值:
  136. // - int64类型的列值
  137. func (result Result) ColumnValueInt64(columnName string) int64 {
  138. value, err := reflectutils.ToInt64(result[columnName])
  139. if err != nil {
  140. logger.GetInstance().Error(err)
  141. return 0
  142. }
  143. return value
  144. }
  145. // ColumnValueUint 以uint类型获取列值
  146. // 参数:
  147. // - columnName: 列名
  148. // 返回值:
  149. // - uint类型的列值
  150. func (result Result) ColumnValueUint(columnName string) uint {
  151. value, err := reflectutils.ToUint64(result[columnName])
  152. if err != nil {
  153. logger.GetInstance().Error(err)
  154. return 0
  155. }
  156. return uint(value)
  157. }
  158. // ColumnValueUint8 以uint8类型获取列值
  159. // 参数:
  160. // - columnName: 列名
  161. // 返回值:
  162. // - uint8类型的列值
  163. func (result Result) ColumnValueUint8(columnName string) uint8 {
  164. value, err := reflectutils.ToUint64(result[columnName])
  165. if err != nil {
  166. logger.GetInstance().Error(err)
  167. return 0
  168. }
  169. return uint8(value)
  170. }
  171. // ColumnValueUint16 以uint16类型获取列值
  172. // 参数:
  173. // - columnName: 列名
  174. // 返回值:
  175. // - uint16类型的列值
  176. func (result Result) ColumnValueUint16(columnName string) uint16 {
  177. value, err := reflectutils.ToUint64(result[columnName])
  178. if err != nil {
  179. logger.GetInstance().Error(err)
  180. return 0
  181. }
  182. return uint16(value)
  183. }
  184. // ColumnValueUint32 以uint32类型获取列值
  185. // 参数:
  186. // - columnName: 列名
  187. // 返回值:
  188. // - uint32类型的列值
  189. func (result Result) ColumnValueUint32(columnName string) uint32 {
  190. value, err := reflectutils.ToUint64(result[columnName])
  191. if err != nil {
  192. logger.GetInstance().Error(err)
  193. return 0
  194. }
  195. return uint32(value)
  196. }
  197. // ColumnValueUint64 以uint64类型获取列值
  198. // 参数:
  199. // - columnName: 列名
  200. // 返回值:
  201. // - uint64类型的列值
  202. func (result Result) ColumnValueUint64(columnName string) uint64 {
  203. value, err := reflectutils.ToUint64(result[columnName])
  204. if err != nil {
  205. logger.GetInstance().Error(err)
  206. return 0
  207. }
  208. return value
  209. }
  210. // ColumnValueFloat32 以float32类型获取列值
  211. // 参数:
  212. // - columnName: 列名
  213. // 返回值:
  214. // - float32类型的列值
  215. func (result Result) ColumnValueFloat32(columnName string) float32 {
  216. value, err := reflectutils.ToFloat64(result[columnName])
  217. if err != nil {
  218. logger.GetInstance().Error(err)
  219. return 0
  220. }
  221. return float32(value)
  222. }
  223. // ColumnValueFloat64 以float64类型获取列值
  224. // 参数:
  225. // - columnName: 列名
  226. // 返回值:
  227. // - float64类型的列值
  228. func (result Result) ColumnValueFloat64(columnName string) float64 {
  229. value, err := reflectutils.ToFloat64(result[columnName])
  230. if err != nil {
  231. logger.GetInstance().Error(err)
  232. return 0
  233. }
  234. return value
  235. }
  236. // ParseSqlResult 解析查询结果
  237. // 参数:
  238. // - input: sql.Result或者[]sql.Result类型的查询结果
  239. // - output: 接收查询结果的指针,如果是结构,需要使用sqlresult tag标注字段
  240. // 返回值:
  241. // - 错误
  242. func ParseSqlResult(input any, output any) error {
  243. return ParseSqlResultWithColumn(input, output, "")
  244. }
  245. // ParseSqlResultWithColumn 取查询结果列解析结果
  246. // 参数:
  247. // - input: sql.Result或者[]sql.Result类型的查询结果
  248. // - output: 接收查询结果的指针,如果是结构,需要使用sqlresult tag标注字段
  249. // - columnName: 获取的列名
  250. // 返回值:
  251. // - 错误
  252. func ParseSqlResultWithColumn(input any, output any, columnName string) error {
  253. if input == nil {
  254. return nil
  255. }
  256. if output == nil {
  257. return nil
  258. }
  259. typeCheckErr := errors.New("可以接受的输出类型为指针或者slice的指针")
  260. outputType := reflect.TypeOf(output)
  261. if outputType.Kind() != reflect.Pointer {
  262. return typeCheckErr
  263. }
  264. outputElemType := reflectutils.PointerTypeElem(outputType)
  265. // 输出不是slice,直接用result赋值即可
  266. if outputElemType.Kind() != reflect.Slice {
  267. result, ok := input.(Result)
  268. if !ok {
  269. return errors.New("输出不是slice,输入需要是sql.Result")
  270. }
  271. return parseSqlSingle(result, output, columnName)
  272. }
  273. // 输出是slice,需要遍历处理
  274. results, ok := input.([]Result)
  275. if !ok {
  276. return errors.New("输出是slice,输入需要是[]sql.Result")
  277. }
  278. outputEntities := reflect.MakeSlice(outputElemType, 0, 0)
  279. for _, result := range results {
  280. var outputEntityValue reflect.Value
  281. // slice子类型判断
  282. if outputElemType.Elem().Kind() == reflect.Pointer {
  283. outputEntityValue = reflect.New(outputElemType.Elem().Elem())
  284. err := parseSqlSingle(result, outputEntityValue.Interface(), columnName)
  285. if err != nil {
  286. return err
  287. }
  288. } else {
  289. outputEntityValue = reflect.New(outputElemType.Elem()).Elem()
  290. err := parseSqlSingle(result, outputEntityValue.Addr().Interface(), columnName)
  291. if err != nil {
  292. return err
  293. }
  294. }
  295. outputEntities = reflect.Append(outputEntities, outputEntityValue)
  296. }
  297. outputElemValue := reflectutils.PointerValueElem(reflect.ValueOf(output))
  298. outputElemValue.Set(outputEntities)
  299. return nil
  300. }
  301. func parseSqlSingle(result Result, output any, columnName string) error {
  302. outputValue := reflectutils.PointerValueElem(reflect.ValueOf(output))
  303. outputKind := reflectutils.GroupValueKind(outputValue)
  304. var oneResultValue any
  305. if strutils.IsStringEmpty(columnName) {
  306. for _, value := range result {
  307. oneResultValue = value
  308. break
  309. }
  310. } else {
  311. value, ok := result[columnName]
  312. if !ok {
  313. return errors.New("列不存在")
  314. }
  315. oneResultValue = value
  316. }
  317. switch outputKind {
  318. case reflect.String:
  319. return reflectutils.AssignStringValue(oneResultValue, outputValue)
  320. case reflect.Bool:
  321. return reflectutils.AssignBoolValue(oneResultValue, outputValue)
  322. case reflect.Int64:
  323. return reflectutils.AssignInt64Value(oneResultValue, outputValue)
  324. case reflect.Uint64:
  325. return reflectutils.AssignUint64Value(oneResultValue, outputValue)
  326. case reflect.Float64:
  327. return reflectutils.AssignFloat64Value(oneResultValue, outputValue)
  328. case reflect.Struct:
  329. if outputValue.Type().Name() == "time.Time" {
  330. return errors.New("不支持转换到time.Time,请使用Result.ColumnValueStringAsTime")
  331. }
  332. return sql_result.DefaultUsage(result, output, columnName)
  333. default:
  334. return errors.New("不支持的类型")
  335. }
  336. }