table_row.go 6.4 KB

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