table_row.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package db_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) AddColumnValueTime(columnName string, value time.Time) *TableRow {
  66. tableRow.row[columnName] = value
  67. return tableRow
  68. }
  69. func (tableRow *TableRow) AddColumnValueBool(columnName string, value bool) *TableRow {
  70. tableRow.row[columnName] = value
  71. return tableRow
  72. }
  73. func (tableRow *TableRow) AddColumnValueString(columnName string, value string) *TableRow {
  74. tableRow.row[columnName] = value
  75. return tableRow
  76. }
  77. func (tableRow *TableRow) AddColumnValueBytes(columnName string, value []byte) *TableRow {
  78. tableRow.row[columnName] = value
  79. return tableRow
  80. }
  81. func (tableRow *TableRow) AddColumnValueInt(columnName string, value int) *TableRow {
  82. tableRow.row[columnName] = uint64(value)
  83. return tableRow
  84. }
  85. func (tableRow *TableRow) AddColumnValueInt8(columnName string, value int8) *TableRow {
  86. tableRow.row[columnName] = uint64(value)
  87. return tableRow
  88. }
  89. func (tableRow *TableRow) AddColumnValueInt16(columnName string, value int16) *TableRow {
  90. tableRow.row[columnName] = uint64(value)
  91. return tableRow
  92. }
  93. func (tableRow *TableRow) AddColumnValueInt32(columnName string, value int32) *TableRow {
  94. tableRow.row[columnName] = uint64(value)
  95. return tableRow
  96. }
  97. func (tableRow *TableRow) AddColumnValueInt64(columnName string, value int64) *TableRow {
  98. tableRow.row[columnName] = uint64(value)
  99. return tableRow
  100. }
  101. func (tableRow *TableRow) AddColumnValueUint(columnName string, value uint) *TableRow {
  102. tableRow.row[columnName] = uint64(value)
  103. return tableRow
  104. }
  105. func (tableRow *TableRow) AddColumnValueUint8(columnName string, value uint8) *TableRow {
  106. tableRow.row[columnName] = uint64(value)
  107. return tableRow
  108. }
  109. func (tableRow *TableRow) AddColumnValueUint16(columnName string, value uint16) *TableRow {
  110. tableRow.row[columnName] = uint64(value)
  111. return tableRow
  112. }
  113. func (tableRow *TableRow) AddColumnValueUint32(columnName string, value uint32) *TableRow {
  114. tableRow.row[columnName] = uint64(value)
  115. return tableRow
  116. }
  117. func (tableRow *TableRow) AddColumnValueUint64(columnName string, value uint64) *TableRow {
  118. tableRow.row[columnName] = value
  119. return tableRow
  120. }
  121. func (tableRow *TableRow) AddColumnValueFloat32(columnName string, value float32) *TableRow {
  122. tableRow.row[columnName] = float64(value)
  123. return tableRow
  124. }
  125. func (tableRow *TableRow) AddColumnValueFloat64(columnName string, value float64) *TableRow {
  126. tableRow.row[columnName] = value
  127. return tableRow
  128. }
  129. func (tableRow *TableRow) ColumnValueTime(columnName string) time.Time {
  130. value, ok := tableRow.row[columnName].(time.Time)
  131. if !ok {
  132. return time.Time{}
  133. }
  134. return value
  135. }
  136. func (tableRow *TableRow) ColumnValueBool(columnName string) bool {
  137. value, ok := tableRow.row[columnName].(bool)
  138. if !ok {
  139. return false
  140. }
  141. return value
  142. }
  143. func (tableRow *TableRow) ColumnValueString(columnName string) string {
  144. value, ok := tableRow.row[columnName].(string)
  145. if !ok {
  146. return ""
  147. }
  148. return value
  149. }
  150. func (tableRow *TableRow) ColumnValueBytes(columnName string) []byte {
  151. value, ok := tableRow.row[columnName].([]byte)
  152. if !ok {
  153. return make([]byte, 0)
  154. }
  155. return value
  156. }
  157. func (tableRow *TableRow) ColumnValueInt(columnName string) int {
  158. value, ok := tableRow.row[columnName].(uint64)
  159. if !ok {
  160. return 0
  161. }
  162. return int(value)
  163. }
  164. func (tableRow *TableRow) ColumnValueInt8(columnName string) int8 {
  165. value, ok := tableRow.row[columnName].(uint64)
  166. if !ok {
  167. return 0
  168. }
  169. return int8(value)
  170. }
  171. func (tableRow *TableRow) ColumnValueInt16(columnName string) int16 {
  172. value, ok := tableRow.row[columnName].(uint64)
  173. if !ok {
  174. return 0
  175. }
  176. return int16(value)
  177. }
  178. func (tableRow *TableRow) ColumnValueInt32(columnName string) int32 {
  179. value, ok := tableRow.row[columnName].(uint64)
  180. if !ok {
  181. return 0
  182. }
  183. return int32(value)
  184. }
  185. func (tableRow *TableRow) ColumnValueInt64(columnName string) int64 {
  186. value, ok := tableRow.row[columnName].(uint64)
  187. if !ok {
  188. return 0
  189. }
  190. return int64(value)
  191. }
  192. func (tableRow *TableRow) ColumnValueUint(columnName string) uint {
  193. value, ok := tableRow.row[columnName].(uint64)
  194. if !ok {
  195. return 0
  196. }
  197. return uint(value)
  198. }
  199. func (tableRow *TableRow) ColumnValueUint8(columnName string) uint8 {
  200. value, ok := tableRow.row[columnName].(uint64)
  201. if !ok {
  202. return 0
  203. }
  204. return uint8(value)
  205. }
  206. func (tableRow *TableRow) ColumnValueUint16(columnName string) uint16 {
  207. value, ok := tableRow.row[columnName].(uint64)
  208. if !ok {
  209. return 0
  210. }
  211. return uint16(value)
  212. }
  213. func (tableRow *TableRow) ColumnValueUint32(columnName string) uint32 {
  214. value, ok := tableRow.row[columnName].(uint64)
  215. if !ok {
  216. return 0
  217. }
  218. return uint32(value)
  219. }
  220. func (tableRow *TableRow) ColumnValueUint64(columnName string) uint64 {
  221. value, ok := tableRow.row[columnName].(uint64)
  222. if !ok {
  223. return 0
  224. }
  225. return value
  226. }
  227. func (tableRow *TableRow) ColumnValueFloat32(columnName string) float32 {
  228. value, ok := tableRow.row[columnName].(float64)
  229. if !ok {
  230. return 0
  231. }
  232. return float32(value)
  233. }
  234. func (tableRow *TableRow) ColumnValueFloat64(columnName string) float64 {
  235. value, ok := tableRow.row[columnName].(float64)
  236. if !ok {
  237. return 0
  238. }
  239. return value
  240. }