sql_result.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package sdk
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-tools/utils/reflectutils"
  5. "strings"
  6. "time"
  7. )
  8. const (
  9. sqlResultTimeMicroFormat = "2006-01-02T15:04:05.000000+08:00"
  10. sqlResultTimeMilliFormat = "2006-01-02T15:04:05.000+08:00"
  11. sqlResultTimeSecFormat = "2006-01-02T15:04:05+08:00"
  12. )
  13. func ParseSqlResultTimeStr(timeStr string) (time.Time, error) {
  14. layout := sqlResultTimeSecFormat
  15. timeZoneIndex := strings.LastIndex(timeStr, "+08:")
  16. if timeZoneIndex != -1 {
  17. timeStr = timeStr[:timeZoneIndex]
  18. afterSecondIndex := strings.LastIndex(timeStr, ".")
  19. if afterSecondIndex != -1 {
  20. if len(timeStr)-afterSecondIndex-1 == 6 {
  21. layout = sqlResultTimeMicroFormat
  22. } else if len(timeStr)-afterSecondIndex-1 == 3 {
  23. layout = sqlResultTimeMilliFormat
  24. }
  25. }
  26. }
  27. return time.ParseInLocation(layout, timeStr, time.Local)
  28. }
  29. type SqlResult map[string]any
  30. func (result SqlResult) ColumnValueStringAsTime(columnName string) time.Time {
  31. value, ok := result[columnName].(string)
  32. if !ok {
  33. return time.Time{}
  34. }
  35. t, err := ParseSqlResultTimeStr(value)
  36. if err != nil {
  37. return time.Time{}
  38. }
  39. return t
  40. }
  41. func (result SqlResult) ColumnValueBool(columnName string) bool {
  42. value, err := reflectutils.ToBool(result[columnName])
  43. if err != nil {
  44. fmt.Println(err)
  45. return false
  46. }
  47. return value
  48. }
  49. func (result SqlResult) ColumnValueString(columnName string) string {
  50. value, err := reflectutils.ToString(result[columnName])
  51. if err != nil {
  52. fmt.Println(err)
  53. return ""
  54. }
  55. return value
  56. }
  57. func (result SqlResult) ColumnValueInt(columnName string) int {
  58. value, err := reflectutils.ToInt64(result[columnName])
  59. if err != nil {
  60. fmt.Println(err)
  61. return 0
  62. }
  63. return int(value)
  64. }
  65. func (result SqlResult) ColumnValueInt8(columnName string) int8 {
  66. value, err := reflectutils.ToInt64(result[columnName])
  67. if err != nil {
  68. fmt.Println(err)
  69. return 0
  70. }
  71. return int8(value)
  72. }
  73. func (result SqlResult) ColumnValueInt16(columnName string) int16 {
  74. value, err := reflectutils.ToInt64(result[columnName])
  75. if err != nil {
  76. fmt.Println(err)
  77. return 0
  78. }
  79. return int16(value)
  80. }
  81. func (result SqlResult) ColumnValueInt32(columnName string) int32 {
  82. value, err := reflectutils.ToInt64(result[columnName])
  83. if err != nil {
  84. fmt.Println(err)
  85. return 0
  86. }
  87. return int32(value)
  88. }
  89. func (result SqlResult) ColumnValueInt64(columnName string) int64 {
  90. value, err := reflectutils.ToInt64(result[columnName])
  91. if err != nil {
  92. fmt.Println(err)
  93. return 0
  94. }
  95. return value
  96. }
  97. func (result SqlResult) ColumnValueUint(columnName string) uint {
  98. value, err := reflectutils.ToUint64(result[columnName])
  99. if err != nil {
  100. fmt.Println(err)
  101. return 0
  102. }
  103. return uint(value)
  104. }
  105. func (result SqlResult) ColumnValueUint8(columnName string) uint8 {
  106. value, err := reflectutils.ToUint64(result[columnName])
  107. if err != nil {
  108. fmt.Println(err)
  109. return 0
  110. }
  111. return uint8(value)
  112. }
  113. func (result SqlResult) ColumnValueUint16(columnName string) uint16 {
  114. value, err := reflectutils.ToUint64(result[columnName])
  115. if err != nil {
  116. fmt.Println(err)
  117. return 0
  118. }
  119. return uint16(value)
  120. }
  121. func (result SqlResult) ColumnValueUint32(columnName string) uint32 {
  122. value, err := reflectutils.ToUint64(result[columnName])
  123. if err != nil {
  124. fmt.Println(err)
  125. return 0
  126. }
  127. return uint32(value)
  128. }
  129. func (result SqlResult) ColumnValueUint64(columnName string) uint64 {
  130. value, err := reflectutils.ToUint64(result[columnName])
  131. if err != nil {
  132. fmt.Println(err)
  133. return 0
  134. }
  135. return value
  136. }
  137. func (result SqlResult) ColumnValueFloat32(columnName string) float32 {
  138. value, err := reflectutils.ToFloat64(result[columnName])
  139. if err != nil {
  140. fmt.Println(err)
  141. return 0
  142. }
  143. return float32(value)
  144. }
  145. func (result SqlResult) ColumnValueFloat64(columnName string) float64 {
  146. value, err := reflectutils.ToFloat64(result[columnName])
  147. if err != nil {
  148. fmt.Println(err)
  149. return 0
  150. }
  151. return value
  152. }