123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- package 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) Len() int {
- return len(tableRow.row)
- }
- func (tableRow *TableRow) Empty() bool {
- return len(tableRow.row) == 0
- }
- 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
- }
|