|
|
@@ -0,0 +1,294 @@
|
|
|
+package db_operations
|
|
|
+
|
|
|
+import (
|
|
|
+ "reflect"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+type TableRow struct {
|
|
|
+ row map[string]any
|
|
|
+}
|
|
|
+
|
|
|
+func NewTableRow() *TableRow {
|
|
|
+ return &TableRow{row: make(map[string]any)}
|
|
|
+}
|
|
|
+
|
|
|
+func NewTableRowFromMap(m map[string]any) *TableRow {
|
|
|
+ tableRow := NewTableRow()
|
|
|
+
|
|
|
+ for key, value := range m {
|
|
|
+ v := value
|
|
|
+
|
|
|
+ valueType := reflect.TypeOf(value)
|
|
|
+ if valueType.Kind() == reflect.Ptr {
|
|
|
+ v = reflect.ValueOf(value).Elem().Interface()
|
|
|
+ }
|
|
|
+
|
|
|
+ switch typedValue := v.(type) {
|
|
|
+ case string:
|
|
|
+ tableRow.row[key] = typedValue
|
|
|
+ case int:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case uint:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case int8:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case uint8:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case int16:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case uint16:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case int32:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case uint32:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case int64:
|
|
|
+ tableRow.row[key] = uint64(typedValue)
|
|
|
+ case uint64:
|
|
|
+ tableRow.row[key] = typedValue
|
|
|
+ case float32:
|
|
|
+ tableRow.row[key] = float64(typedValue)
|
|
|
+ case float64:
|
|
|
+ tableRow.row[key] = typedValue
|
|
|
+ case bool:
|
|
|
+ tableRow.row[key] = typedValue
|
|
|
+ case []byte:
|
|
|
+ tableRow.row[key] = typedValue
|
|
|
+ case time.Time:
|
|
|
+ tableRow.row[key] = typedValue
|
|
|
+ default:
|
|
|
+ panic("未支持的数据类型")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ToMap() map[string]any {
|
|
|
+ return tableRow.row
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueTime(columnName string, value time.Time) *TableRow {
|
|
|
+ tableRow.row[columnName] = value
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueBool(columnName string, value bool) *TableRow {
|
|
|
+ tableRow.row[columnName] = value
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueString(columnName string, value string) *TableRow {
|
|
|
+ tableRow.row[columnName] = value
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueBytes(columnName string, value []byte) *TableRow {
|
|
|
+ tableRow.row[columnName] = value
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueInt(columnName string, value int) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueInt8(columnName string, value int8) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueInt16(columnName string, value int16) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueInt32(columnName string, value int32) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueInt64(columnName string, value int64) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueUint(columnName string, value uint) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueUint8(columnName string, value uint8) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueUint16(columnName string, value uint16) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueUint32(columnName string, value uint32) *TableRow {
|
|
|
+ tableRow.row[columnName] = uint64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueUint64(columnName string, value uint64) *TableRow {
|
|
|
+ tableRow.row[columnName] = value
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueFloat32(columnName string, value float32) *TableRow {
|
|
|
+ tableRow.row[columnName] = float64(value)
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) AddColumnValueFloat64(columnName string, value float64) *TableRow {
|
|
|
+ tableRow.row[columnName] = value
|
|
|
+ return tableRow
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueTime(columnName string) time.Time {
|
|
|
+ value, ok := tableRow.row[columnName].(time.Time)
|
|
|
+ if !ok {
|
|
|
+ return time.Time{}
|
|
|
+ }
|
|
|
+
|
|
|
+ return value
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueBool(columnName string) bool {
|
|
|
+ value, ok := tableRow.row[columnName].(bool)
|
|
|
+ if !ok {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return value
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueString(columnName string) string {
|
|
|
+ value, ok := tableRow.row[columnName].(string)
|
|
|
+ if !ok {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+
|
|
|
+ return value
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueBytes(columnName string) []byte {
|
|
|
+ value, ok := tableRow.row[columnName].([]byte)
|
|
|
+ if !ok {
|
|
|
+ return make([]byte, 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ return value
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueInt(columnName string) int {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return int(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueInt8(columnName string) int8 {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return int8(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueInt16(columnName string) int16 {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return int16(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueInt32(columnName string) int32 {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return int32(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueInt64(columnName string) int64 {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return int64(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueUint(columnName string) uint {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return uint(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueUint8(columnName string) uint8 {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return uint8(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueUint16(columnName string) uint16 {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return uint16(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueUint32(columnName string) uint32 {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return uint32(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueUint64(columnName string) uint64 {
|
|
|
+ value, ok := tableRow.row[columnName].(uint64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return value
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueFloat32(columnName string) float32 {
|
|
|
+ value, ok := tableRow.row[columnName].(float64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return float32(value)
|
|
|
+}
|
|
|
+
|
|
|
+func (tableRow *TableRow) ColumnValueFloat64(columnName string) float64 {
|
|
|
+ value, ok := tableRow.row[columnName].(float64)
|
|
|
+ if !ok {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return value
|
|
|
+}
|