table_row.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package operations
  2. import (
  3. "reflect"
  4. "time"
  5. )
  6. type TableRow struct {
  7. row map[string]any
  8. }
  9. func NewTableRow() *TableRow {
  10. return &TableRow{row: make(map[string]any)}
  11. }
  12. func NewTableRowFromMap(m map[string]any) *TableRow {
  13. tableRow := NewTableRow()
  14. for key, value := range m {
  15. v := value
  16. valueType := reflect.TypeOf(value)
  17. if valueType.Kind() == reflect.Ptr {
  18. v = reflect.ValueOf(value).Elem().Interface()
  19. }
  20. switch typedValue := v.(type) {
  21. case string:
  22. tableRow.row[key] = typedValue
  23. case int:
  24. tableRow.row[key] = uint64(typedValue)
  25. case uint:
  26. tableRow.row[key] = uint64(typedValue)
  27. case int8:
  28. tableRow.row[key] = uint64(typedValue)
  29. case uint8:
  30. tableRow.row[key] = uint64(typedValue)
  31. case int16:
  32. tableRow.row[key] = uint64(typedValue)
  33. case uint16:
  34. tableRow.row[key] = uint64(typedValue)
  35. case int32:
  36. tableRow.row[key] = uint64(typedValue)
  37. case uint32:
  38. tableRow.row[key] = uint64(typedValue)
  39. case int64:
  40. tableRow.row[key] = uint64(typedValue)
  41. case uint64:
  42. tableRow.row[key] = typedValue
  43. case float32:
  44. tableRow.row[key] = float64(typedValue)
  45. case float64:
  46. tableRow.row[key] = typedValue
  47. case bool:
  48. tableRow.row[key] = typedValue
  49. case []byte:
  50. tableRow.row[key] = typedValue
  51. case time.Time:
  52. tableRow.row[key] = typedValue
  53. default:
  54. panic("未支持的数据类型")
  55. }
  56. }
  57. return tableRow
  58. }
  59. func (tableRow *TableRow) ToMap() map[string]any {
  60. return tableRow.row
  61. }
  62. func (tableRow *TableRow) Len() int {
  63. return len(tableRow.row)
  64. }
  65. func (tableRow *TableRow) Empty() bool {
  66. return len(tableRow.row) == 0
  67. }
  68. func (tableRow *TableRow) AddColumnValueTime(columnName string, value time.Time) *TableRow {
  69. tableRow.row[columnName] = value
  70. return tableRow
  71. }
  72. func (tableRow *TableRow) AddColumnValueBool(columnName string, value bool) *TableRow {
  73. tableRow.row[columnName] = value
  74. return tableRow
  75. }
  76. func (tableRow *TableRow) AddColumnValueString(columnName string, value string) *TableRow {
  77. tableRow.row[columnName] = value
  78. return tableRow
  79. }
  80. func (tableRow *TableRow) AddColumnValueBytes(columnName string, value []byte) *TableRow {
  81. tableRow.row[columnName] = value
  82. return tableRow
  83. }
  84. func (tableRow *TableRow) AddColumnValueInt(columnName string, value int) *TableRow {
  85. tableRow.row[columnName] = uint64(value)
  86. return tableRow
  87. }
  88. func (tableRow *TableRow) AddColumnValueInt8(columnName string, value int8) *TableRow {
  89. tableRow.row[columnName] = uint64(value)
  90. return tableRow
  91. }
  92. func (tableRow *TableRow) AddColumnValueInt16(columnName string, value int16) *TableRow {
  93. tableRow.row[columnName] = uint64(value)
  94. return tableRow
  95. }
  96. func (tableRow *TableRow) AddColumnValueInt32(columnName string, value int32) *TableRow {
  97. tableRow.row[columnName] = uint64(value)
  98. return tableRow
  99. }
  100. func (tableRow *TableRow) AddColumnValueInt64(columnName string, value int64) *TableRow {
  101. tableRow.row[columnName] = uint64(value)
  102. return tableRow
  103. }
  104. func (tableRow *TableRow) AddColumnValueUint(columnName string, value uint) *TableRow {
  105. tableRow.row[columnName] = uint64(value)
  106. return tableRow
  107. }
  108. func (tableRow *TableRow) AddColumnValueUint8(columnName string, value uint8) *TableRow {
  109. tableRow.row[columnName] = uint64(value)
  110. return tableRow
  111. }
  112. func (tableRow *TableRow) AddColumnValueUint16(columnName string, value uint16) *TableRow {
  113. tableRow.row[columnName] = uint64(value)
  114. return tableRow
  115. }
  116. func (tableRow *TableRow) AddColumnValueUint32(columnName string, value uint32) *TableRow {
  117. tableRow.row[columnName] = uint64(value)
  118. return tableRow
  119. }
  120. func (tableRow *TableRow) AddColumnValueUint64(columnName string, value uint64) *TableRow {
  121. tableRow.row[columnName] = value
  122. return tableRow
  123. }
  124. func (tableRow *TableRow) AddColumnValueFloat32(columnName string, value float32) *TableRow {
  125. tableRow.row[columnName] = float64(value)
  126. return tableRow
  127. }
  128. func (tableRow *TableRow) AddColumnValueFloat64(columnName string, value float64) *TableRow {
  129. tableRow.row[columnName] = value
  130. return tableRow
  131. }
  132. func (tableRow *TableRow) ColumnValueTime(columnName string) time.Time {
  133. value, ok := tableRow.row[columnName].(time.Time)
  134. if !ok {
  135. return time.Time{}
  136. }
  137. return value
  138. }
  139. func (tableRow *TableRow) ColumnValueBool(columnName string) bool {
  140. value, ok := tableRow.row[columnName].(bool)
  141. if !ok {
  142. return false
  143. }
  144. return value
  145. }
  146. func (tableRow *TableRow) ColumnValueString(columnName string) string {
  147. value, ok := tableRow.row[columnName].(string)
  148. if !ok {
  149. return ""
  150. }
  151. return value
  152. }
  153. func (tableRow *TableRow) ColumnValueBytes(columnName string) []byte {
  154. value, ok := tableRow.row[columnName].([]byte)
  155. if !ok {
  156. return make([]byte, 0)
  157. }
  158. return value
  159. }
  160. func (tableRow *TableRow) ColumnValueInt(columnName string) int {
  161. value, ok := tableRow.row[columnName].(uint64)
  162. if !ok {
  163. return 0
  164. }
  165. return int(value)
  166. }
  167. func (tableRow *TableRow) ColumnValueInt8(columnName string) int8 {
  168. value, ok := tableRow.row[columnName].(uint64)
  169. if !ok {
  170. return 0
  171. }
  172. return int8(value)
  173. }
  174. func (tableRow *TableRow) ColumnValueInt16(columnName string) int16 {
  175. value, ok := tableRow.row[columnName].(uint64)
  176. if !ok {
  177. return 0
  178. }
  179. return int16(value)
  180. }
  181. func (tableRow *TableRow) ColumnValueInt32(columnName string) int32 {
  182. value, ok := tableRow.row[columnName].(uint64)
  183. if !ok {
  184. return 0
  185. }
  186. return int32(value)
  187. }
  188. func (tableRow *TableRow) ColumnValueInt64(columnName string) int64 {
  189. value, ok := tableRow.row[columnName].(uint64)
  190. if !ok {
  191. return 0
  192. }
  193. return int64(value)
  194. }
  195. func (tableRow *TableRow) ColumnValueUint(columnName string) uint {
  196. value, ok := tableRow.row[columnName].(uint64)
  197. if !ok {
  198. return 0
  199. }
  200. return uint(value)
  201. }
  202. func (tableRow *TableRow) ColumnValueUint8(columnName string) uint8 {
  203. value, ok := tableRow.row[columnName].(uint64)
  204. if !ok {
  205. return 0
  206. }
  207. return uint8(value)
  208. }
  209. func (tableRow *TableRow) ColumnValueUint16(columnName string) uint16 {
  210. value, ok := tableRow.row[columnName].(uint64)
  211. if !ok {
  212. return 0
  213. }
  214. return uint16(value)
  215. }
  216. func (tableRow *TableRow) ColumnValueUint32(columnName string) uint32 {
  217. value, ok := tableRow.row[columnName].(uint64)
  218. if !ok {
  219. return 0
  220. }
  221. return uint32(value)
  222. }
  223. func (tableRow *TableRow) ColumnValueUint64(columnName string) uint64 {
  224. value, ok := tableRow.row[columnName].(uint64)
  225. if !ok {
  226. return 0
  227. }
  228. return value
  229. }
  230. func (tableRow *TableRow) ColumnValueFloat32(columnName string) float32 {
  231. value, ok := tableRow.row[columnName].(float64)
  232. if !ok {
  233. return 0
  234. }
  235. return float32(value)
  236. }
  237. func (tableRow *TableRow) ColumnValueFloat64(columnName string) float64 {
  238. value, ok := tableRow.row[columnName].(float64)
  239. if !ok {
  240. return 0
  241. }
  242. return value
  243. }