Skip to content

Commit 9e253ff

Browse files
author
Qiu Jian
committed
fix: IQueryField support ConvertFromValue
1 parent 2f8e0af commit 9e253ff

19 files changed

Lines changed: 885 additions & 160 deletions

backends/clickhouse/column.go

Lines changed: 92 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ import (
2020
"fmt"
2121
"reflect"
2222
"strconv"
23-
"strings"
2423
"time"
2524

2625
"yunion.io/x/log"
2726
"yunion.io/x/pkg/gotypes"
2827
"yunion.io/x/pkg/tristate"
29-
"yunion.io/x/pkg/util/timeutils"
3028
"yunion.io/x/pkg/utils"
3129

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

158156
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
159157
func (c *SBooleanColumn) ConvertFromString(str string) interface{} {
160-
switch strings.ToLower(str) {
161-
case "true", "yes", "on", "ok", "1":
158+
switch sqlchemy.ConvertValueToBool(str) {
159+
case true:
162160
return uint8(1)
163161
default:
164162
return uint8(0)
@@ -167,11 +165,12 @@ func (c *SBooleanColumn) ConvertFromString(str string) interface{} {
167165

168166
// ConvertFromValue implementation of STristateColumn for IColumnSpec
169167
func (c *SBooleanColumn) ConvertFromValue(val interface{}) interface{} {
170-
bVal := val.(bool)
171-
if bVal {
168+
switch sqlchemy.ConvertValueToBool(val) {
169+
case true:
172170
return uint8(1)
171+
default:
172+
return uint8(0)
173173
}
174-
return uint8(0)
175174
}
176175

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

208207
// ConvertFromString implementation of STristateColumn for IColumnSpec
209208
func (c *STristateColumn) ConvertFromString(str string) interface{} {
210-
switch strings.ToLower(str) {
211-
case "true", "yes", "on", "ok", "1":
209+
switch sqlchemy.ConvertValueToTriState(str) {
210+
case tristate.True:
212211
return uint8(1)
213-
case "none", "null", "unknown":
214-
return sql.NullInt32{}
215-
default:
212+
case tristate.False:
216213
return uint8(0)
214+
default:
215+
return sql.NullInt32{}
217216
}
218217
}
219218

220219
// ConvertFromValue implementation of STristateColumn for IColumnSpec
221220
func (c *STristateColumn) ConvertFromValue(val interface{}) interface{} {
222-
bVal := val.(tristate.TriState)
223-
if bVal == tristate.True {
221+
switch sqlchemy.ConvertValueToTriState(val) {
222+
case tristate.True:
224223
return uint8(1)
225-
} else if bVal == tristate.False {
224+
case tristate.False:
226225
return uint8(0)
227-
} else {
226+
default:
228227
return sql.NullInt32{}
229228
}
230229
}
@@ -282,37 +281,50 @@ func (c *SIntegerColumn) IsZero(val interface{}) bool {
282281

283282
// ConvertFromString implementation of STristateColumn for IColumnSpec
284283
func (c *SIntegerColumn) ConvertFromString(str string) interface{} {
285-
ctype := c.SBaseColumn.ColType()
286-
if ctype[0] == 'U' {
287-
// unsigned
288-
val, _ := strconv.ParseUint(str, 10, 64)
289-
switch ctype {
290-
case "UInt8":
291-
return uint8(val)
292-
case "UInt16":
293-
return uint16(val)
294-
case "UInt32":
295-
return uint32(val)
296-
case "UInt64":
297-
return val
298-
default:
299-
panic(fmt.Sprintf("unsupported type %s", ctype))
300-
}
301-
} else {
302-
val, _ := strconv.ParseInt(str, 10, 64)
303-
switch ctype {
304-
case "Int8":
305-
return int8(val)
306-
case "Int16":
307-
return int16(val)
308-
case "Int32":
309-
return int32(val)
310-
case "Int64":
311-
return val
312-
default:
313-
panic(fmt.Sprintf("unsupported type %s", ctype))
314-
}
284+
val := sqlchemy.ConvertValueToInteger(str)
285+
switch c.ColType() {
286+
case "UInt8":
287+
return uint8(val)
288+
case "UInt16":
289+
return uint16(val)
290+
case "UInt32":
291+
return uint32(val)
292+
case "UInt64":
293+
return val
294+
case "Int8":
295+
return int8(val)
296+
case "Int16":
297+
return int16(val)
298+
case "Int32":
299+
return int32(val)
300+
case "Int64":
301+
return val
315302
}
303+
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
304+
}
305+
306+
// ConvertFromValue implementation of SIntegerColumn for IColumnSpec
307+
func (c *SIntegerColumn) ConvertFromValue(val interface{}) interface{} {
308+
intval := sqlchemy.ConvertValueToInteger(val)
309+
switch c.ColType() {
310+
case "UInt8":
311+
return uint8(intval)
312+
case "UInt16":
313+
return uint16(intval)
314+
case "UInt32":
315+
return uint32(intval)
316+
case "UInt64":
317+
return intval
318+
case "Int8":
319+
return int8(intval)
320+
case "Int16":
321+
return int16(intval)
322+
case "Int32":
323+
return int32(intval)
324+
case "Int64":
325+
return intval
326+
}
327+
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
316328
}
317329

318330
// IsAutoVersion implements IsAutoVersion for IColumnSpec
@@ -374,16 +386,26 @@ func (c *SFloatColumn) IsZero(val interface{}) bool {
374386

375387
// ConvertFromString implementation of STristateColumn for IColumnSpec
376388
func (c *SFloatColumn) ConvertFromString(str string) interface{} {
377-
ctype := c.SBaseColumn.ColType()
378-
val, _ := strconv.ParseFloat(str, 64)
379-
switch ctype {
389+
floatVal := sqlchemy.ConvertValueToFloat(str)
390+
switch c.ColType() {
380391
case "Float32":
381-
return float32(val)
392+
return float32(floatVal)
382393
case "Float64":
383-
return val
384-
default:
385-
panic(fmt.Sprintf("unsupported type %s", ctype))
394+
return floatVal
395+
}
396+
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
397+
}
398+
399+
// ConvertFromValue implementation of SFloatColumn for IColumnSpec
400+
func (c *SFloatColumn) ConvertFromValue(val interface{}) interface{} {
401+
floatVal := sqlchemy.ConvertValueToFloat(val)
402+
switch c.ColType() {
403+
case "Float32":
404+
return float32(floatVal)
405+
case "Float64":
406+
return floatVal
386407
}
408+
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
387409
}
388410

389411
// NewFloatColumn returns an instance of SFloatColumn
@@ -442,8 +464,12 @@ func (c *SDecimalColumn) IsZero(val interface{}) bool {
442464

443465
// ConvertFromString implementation of STristateColumn for IColumnSpec
444466
func (c *SDecimalColumn) ConvertFromString(str string) interface{} {
445-
val, _ := strconv.ParseFloat(str, 64)
446-
return val
467+
return sqlchemy.ConvertValueToFloat(str)
468+
}
469+
470+
// ConvertFromValue implementation of SDecimalColumn for IColumnSpec
471+
func (c *SDecimalColumn) ConvertFromValue(val interface{}) interface{} {
472+
return sqlchemy.ConvertValueToFloat(val)
447473
}
448474

449475
// NewDecimalColumn returns an instance of SDecimalColumn
@@ -527,6 +553,11 @@ func (c *STextColumn) ConvertFromString(str string) interface{} {
527553
return str
528554
}
529555

556+
// ConvertFromValue implementation of STextColumn for IColumnSpec
557+
func (c *STextColumn) ConvertFromValue(val interface{}) interface{} {
558+
return sqlchemy.ConvertValueToString(val)
559+
}
560+
530561
// NewTextColumn return an instance of STextColumn
531562
func NewTextColumn(name string, sqlType string, tagmap map[string]string, isPointer bool) STextColumn {
532563
return STextColumn{
@@ -564,8 +595,12 @@ func (c *STimeTypeColumn) IsZero(val interface{}) bool {
564595

565596
// ConvertFromString implementation of STristateColumn for IColumnSpec
566597
func (c *STimeTypeColumn) ConvertFromString(str string) interface{} {
567-
tm, _ := timeutils.ParseTimeStr(str)
568-
return tm
598+
return sqlchemy.ConvertValueToTime(str)
599+
}
600+
601+
// ConvertFromValue implementation of STimeTypeColumn for IColumnSpec
602+
func (c *STimeTypeColumn) ConvertFromValue(val interface{}) interface{} {
603+
return sqlchemy.ConvertValueToTime(val)
569604
}
570605

571606
func (c *STimeTypeColumn) GetTTL() (int, string) {

backends/clickhouse/column_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package clickhouse
1717
import (
1818
"database/sql"
1919
"testing"
20+
"time"
2021

2122
"yunion.io/x/jsonutils"
2223
"yunion.io/x/pkg/tristate"
@@ -211,7 +212,7 @@ func TestConvertValue(t *testing.T) {
211212
},
212213
{
213214
in: 23,
214-
want: 23,
215+
want: int8(23),
215216
col: &intCol,
216217
},
217218
{
@@ -268,6 +269,11 @@ func TestConvertString(t *testing.T) {
268269
want: float32(0.01),
269270
col: &float32Col,
270271
},
272+
{
273+
in: "2025-03-27 12:00:00",
274+
want: time.Date(2025, 3, 27, 12, 0, 0, 0, time.UTC),
275+
col: &dateCol,
276+
},
271277
}
272278
for _, c := range cases {
273279
got := c.col.ConvertFromString(c.in)

backends/clickhouse/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (clickhouse *SClickhouseBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpe
164164
if oldTtlSpec != newTtlSpec {
165165
if oldTtlSpec.Count > 0 && newTtlSpec.Count == 0 {
166166
// remove
167-
sql := fmt.Sprintf("REMOVE TTL")
167+
sql := "REMOVE TTL"
168168
alters = append(alters, sql)
169169
} else {
170170
// alter

0 commit comments

Comments
 (0)