Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 46 additions & 57 deletions backends/clickhouse/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import (
"fmt"
"reflect"
"strconv"
"strings"
"time"

"yunion.io/x/log"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/timeutils"
"yunion.io/x/pkg/utils"

"yunion.io/x/sqlchemy"
Expand Down Expand Up @@ -157,8 +155,8 @@ func (c *SBooleanColumn) DefinitionString() string {

// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SBooleanColumn) ConvertFromString(str string) interface{} {
switch strings.ToLower(str) {
case "true", "yes", "on", "ok", "1":
switch sqlchemy.ConvertValueToBool(str) {
case true:
return uint8(1)
default:
return uint8(0)
Expand All @@ -167,11 +165,12 @@ func (c *SBooleanColumn) ConvertFromString(str string) interface{} {

// ConvertFromValue implementation of STristateColumn for IColumnSpec
func (c *SBooleanColumn) ConvertFromValue(val interface{}) interface{} {
bVal := val.(bool)
if bVal {
switch sqlchemy.ConvertValueToBool(val) {
case true:
return uint8(1)
default:
return uint8(0)
}
return uint8(0)
}

// IsZero implementation of SBooleanColumn for IColumnSpec
Expand Down Expand Up @@ -207,24 +206,24 @@ func (c *STristateColumn) DefinitionString() string {

// ConvertFromString implementation of STristateColumn for IColumnSpec
func (c *STristateColumn) ConvertFromString(str string) interface{} {
switch strings.ToLower(str) {
case "true", "yes", "on", "ok", "1":
switch sqlchemy.ConvertValueToTriState(str) {
case tristate.True:
return uint8(1)
case "none", "null", "unknown":
return sql.NullInt32{}
default:
case tristate.False:
return uint8(0)
default:
return sql.NullInt32{}
}
}

// ConvertFromValue implementation of STristateColumn for IColumnSpec
func (c *STristateColumn) ConvertFromValue(val interface{}) interface{} {
bVal := val.(tristate.TriState)
if bVal == tristate.True {
switch sqlchemy.ConvertValueToTriState(val) {
case tristate.True:
return uint8(1)
} else if bVal == tristate.False {
case tristate.False:
return uint8(0)
} else {
default:
return sql.NullInt32{}
}
}
Expand Down Expand Up @@ -282,37 +281,26 @@ func (c *SIntegerColumn) IsZero(val interface{}) bool {

// ConvertFromString implementation of STristateColumn for IColumnSpec
func (c *SIntegerColumn) ConvertFromString(str string) interface{} {
ctype := c.SBaseColumn.ColType()
if ctype[0] == 'U' {
// unsigned
val, _ := strconv.ParseUint(str, 10, 64)
switch ctype {
case "UInt8":
return uint8(val)
case "UInt16":
return uint16(val)
case "UInt32":
return uint32(val)
case "UInt64":
return val
default:
panic(fmt.Sprintf("unsupported type %s", ctype))
}
} else {
val, _ := strconv.ParseInt(str, 10, 64)
switch ctype {
case "Int8":
return int8(val)
case "Int16":
return int16(val)
case "Int32":
return int32(val)
case "Int64":
return val
default:
panic(fmt.Sprintf("unsupported type %s", ctype))
}
val := sqlchemy.ConvertValueToInteger(str)
switch c.ColType() {
case "UInt8":
return uint8(val)
case "UInt16":
return uint16(val)
case "UInt32":
return uint32(val)
case "UInt64":
return val
case "Int8":
return int8(val)
case "Int16":
return int16(val)
case "Int32":
return int32(val)
case "Int64":
return val
}
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
}

// IsAutoVersion implements IsAutoVersion for IColumnSpec
Expand Down Expand Up @@ -374,16 +362,14 @@ func (c *SFloatColumn) IsZero(val interface{}) bool {

// ConvertFromString implementation of STristateColumn for IColumnSpec
func (c *SFloatColumn) ConvertFromString(str string) interface{} {
ctype := c.SBaseColumn.ColType()
val, _ := strconv.ParseFloat(str, 64)
switch ctype {
floatVal := sqlchemy.ConvertValueToFloat(str)
switch c.ColType() {
case "Float32":
return float32(val)
return float32(floatVal)
case "Float64":
return val
default:
panic(fmt.Sprintf("unsupported type %s", ctype))
return floatVal
}
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
}

// NewFloatColumn returns an instance of SFloatColumn
Expand Down Expand Up @@ -442,8 +428,7 @@ func (c *SDecimalColumn) IsZero(val interface{}) bool {

// ConvertFromString implementation of STristateColumn for IColumnSpec
func (c *SDecimalColumn) ConvertFromString(str string) interface{} {
val, _ := strconv.ParseFloat(str, 64)
return val
return sqlchemy.ConvertValueToFloat(str)
}

// NewDecimalColumn returns an instance of SDecimalColumn
Expand Down Expand Up @@ -564,8 +549,12 @@ func (c *STimeTypeColumn) IsZero(val interface{}) bool {

// ConvertFromString implementation of STristateColumn for IColumnSpec
func (c *STimeTypeColumn) ConvertFromString(str string) interface{} {
tm, _ := timeutils.ParseTimeStr(str)
return tm
return sqlchemy.ConvertValueToTime(str)
}

// ConvertFromValue implementation of STimeTypeColumn for IColumnSpec
func (c *STimeTypeColumn) ConvertFromValue(val interface{}) interface{} {
return sqlchemy.ConvertValueToTime(val)
}

func (c *STimeTypeColumn) GetTTL() (int, string) {
Expand Down
6 changes: 6 additions & 0 deletions backends/clickhouse/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package clickhouse
import (
"database/sql"
"testing"
"time"

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/tristate"
Expand Down Expand Up @@ -268,6 +269,11 @@ func TestConvertString(t *testing.T) {
want: float32(0.01),
col: &float32Col,
},
{
in: "2025-03-27 12:00:00",
want: time.Date(2025, 3, 27, 12, 0, 0, 0, time.UTC),
col: &dateCol,
},
}
for _, c := range cases {
got := c.col.ConvertFromString(c.in)
Expand Down
2 changes: 1 addition & 1 deletion backends/clickhouse/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (clickhouse *SClickhouseBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpe
if oldTtlSpec != newTtlSpec {
if oldTtlSpec.Count > 0 && newTtlSpec.Count == 0 {
// remove
sql := fmt.Sprintf("REMOVE TTL")
sql := "REMOVE TTL"
alters = append(alters, sql)
} else {
// alter
Expand Down
70 changes: 41 additions & 29 deletions backends/dameng/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import (
"fmt"
"reflect"
"strconv"
"strings"
"time"

"yunion.io/x/log"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/timeutils"
"yunion.io/x/pkg/utils"

"yunion.io/x/sqlchemy"
Expand Down Expand Up @@ -85,8 +83,8 @@ func (c *SBooleanColumn) DefinitionString() string {

// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SBooleanColumn) ConvertFromString(str string) interface{} {
switch strings.ToLower(str) {
case "true", "yes", "on", "ok", "1":
switch sqlchemy.ConvertValueToBool(str) {
case true:
return 1
default:
return 0
Expand All @@ -95,16 +93,12 @@ func (c *SBooleanColumn) ConvertFromString(str string) interface{} {

// ConvertFromValue implementation of STristateColumn for IColumnSpec
func (c *SBooleanColumn) ConvertFromValue(val interface{}) interface{} {
var bVal bool
if c.IsPointer() {
bVal = *val.(*bool)
} else {
bVal = val.(bool)
}
if bVal {
switch sqlchemy.ConvertValueToBool(val) {
case true:
return 1
default:
return 0
}
return 0
}

// IsZero implementation of SBooleanColumn for IColumnSpec
Expand Down Expand Up @@ -140,24 +134,24 @@ func (c *STristateColumn) DefinitionString() string {

// ConvertFromString implementation of STristateColumn for IColumnSpec
func (c *STristateColumn) ConvertFromString(str string) interface{} {
switch strings.ToLower(str) {
case "true", "yes", "on", "ok", "1":
switch sqlchemy.ConvertValueToTriState(str) {
case tristate.True:
return 1
case "none", "null", "unknown":
return sql.NullInt32{}
default:
case tristate.False:
return 0
default:
return sql.NullInt32{}
}
}

// ConvertFromValue implementation of STristateColumn for IColumnSpec
func (c *STristateColumn) ConvertFromValue(val interface{}) interface{} {
bVal := val.(tristate.TriState)
if bVal == tristate.True {
switch sqlchemy.ConvertValueToTriState(val) {
case tristate.True:
return 1
} else if bVal == tristate.False {
case tristate.False:
return 0
} else {
default:
return sql.NullInt32{}
}
}
Expand Down Expand Up @@ -234,8 +228,18 @@ func (c *SIntegerColumn) IsZero(val interface{}) bool {

// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SIntegerColumn) ConvertFromString(str string) interface{} {
val, _ := strconv.ParseInt(str, 10, 64)
return val
intval := sqlchemy.ConvertValueToInteger(str)
switch c.ColType() {
case "TINYINT":
return int8(intval)
case "SMALLINT":
return int16(intval)
case "INT":
return int(intval)
case "BIGINT":
return int64(intval)
}
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
}

func (c *SIntegerColumn) IsAutoVersion() bool {
Expand Down Expand Up @@ -335,8 +339,14 @@ func (c *SFloatColumn) IsZero(val interface{}) bool {

// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SFloatColumn) ConvertFromString(str string) interface{} {
val, _ := strconv.ParseFloat(str, 64)
return val
floatVal := sqlchemy.ConvertValueToFloat(str)
switch c.ColType() {
case "FLOAT", "REAL":
return float32(floatVal)
case "DOUBLE":
return floatVal
}
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
}

// NewFloatColumn returns an instance of SFloatColumn
Expand Down Expand Up @@ -392,8 +402,7 @@ func (c *SDecimalColumn) IsZero(val interface{}) bool {

// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *SDecimalColumn) ConvertFromString(str string) interface{} {
val, _ := strconv.ParseFloat(str, 64)
return val
return sqlchemy.ConvertValueToFloat(str)
}

// NewDecimalColumn returns an instance of SDecimalColumn
Expand Down Expand Up @@ -498,8 +507,11 @@ func (c *STimeTypeColumn) ColType() string {

// ConvertFromString implementation of SBooleanColumn for IColumnSpec
func (c *STimeTypeColumn) ConvertFromString(str string) interface{} {
tm, _ := timeutils.ParseTimeStr(str)
return tm
return sqlchemy.ConvertValueToTime(str)
}

func (c *STimeTypeColumn) ConvertFromValue(val interface{}) interface{} {
return sqlchemy.ConvertValueToTime(val)
}

// NewTimeTypeColumn return an instance of STimeTypeColumn
Expand Down
10 changes: 8 additions & 2 deletions backends/dameng/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package dameng
import (
"database/sql"
"testing"
"time"

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/tristate"
Expand Down Expand Up @@ -166,6 +167,11 @@ func TestConvertValue(t *testing.T) {
want: `{}`,
col: &compCol,
},
{
in: "2025-03-27 12:00:00",
want: time.Date(2025, 3, 27, 12, 0, 0, 0, time.UTC),
col: &dateCol,
},
}
for _, c := range cases {
got := c.col.ConvertFromValue(c.in)
Expand Down Expand Up @@ -207,12 +213,12 @@ func TestConvertString(t *testing.T) {
},
{
in: "23",
want: int64(23),
want: 23,
col: &intCol,
},
{
in: "0.01",
want: 0.01,
want: float32(0.01),
col: &floatCol,
},
}
Expand Down
2 changes: 1 addition & 1 deletion backends/dameng/dameng.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (dameng *SDamengBackend) GetColumnSpecByFieldType(table *sqlchemy.STableSpe
col := NewDecimalColumn(fieldname, tagmap, isPointer)
return &col
}
colType := "REAL"
colType := "FLOAT"
if fieldType == gotypes.Float64Type {
colType = "DOUBLE"
}
Expand Down
Loading