Skip to content

Commit f7fe1ab

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

18 files changed

Lines changed: 767 additions & 158 deletions

backends/clickhouse/column.go

Lines changed: 46 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,26 @@ 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()))
316304
}
317305

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

375363
// ConvertFromString implementation of STristateColumn for IColumnSpec
376364
func (c *SFloatColumn) ConvertFromString(str string) interface{} {
377-
ctype := c.SBaseColumn.ColType()
378-
val, _ := strconv.ParseFloat(str, 64)
379-
switch ctype {
365+
floatVal := sqlchemy.ConvertValueToFloat(str)
366+
switch c.ColType() {
380367
case "Float32":
381-
return float32(val)
368+
return float32(floatVal)
382369
case "Float64":
383-
return val
384-
default:
385-
panic(fmt.Sprintf("unsupported type %s", ctype))
370+
return floatVal
386371
}
372+
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
387373
}
388374

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

443429
// ConvertFromString implementation of STristateColumn for IColumnSpec
444430
func (c *SDecimalColumn) ConvertFromString(str string) interface{} {
445-
val, _ := strconv.ParseFloat(str, 64)
446-
return val
431+
return sqlchemy.ConvertValueToFloat(str)
447432
}
448433

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

565550
// ConvertFromString implementation of STristateColumn for IColumnSpec
566551
func (c *STimeTypeColumn) ConvertFromString(str string) interface{} {
567-
tm, _ := timeutils.ParseTimeStr(str)
568-
return tm
552+
return sqlchemy.ConvertValueToTime(str)
553+
}
554+
555+
// ConvertFromValue implementation of STimeTypeColumn for IColumnSpec
556+
func (c *STimeTypeColumn) ConvertFromValue(val interface{}) interface{} {
557+
return sqlchemy.ConvertValueToTime(val)
569558
}
570559

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

backends/clickhouse/column_test.go

Lines changed: 6 additions & 0 deletions
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"
@@ -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

backends/dameng/column.go

Lines changed: 41 additions & 29 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"
@@ -85,8 +83,8 @@ func (c *SBooleanColumn) DefinitionString() string {
8583

8684
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
8785
func (c *SBooleanColumn) ConvertFromString(str string) interface{} {
88-
switch strings.ToLower(str) {
89-
case "true", "yes", "on", "ok", "1":
86+
switch sqlchemy.ConvertValueToBool(str) {
87+
case true:
9088
return 1
9189
default:
9290
return 0
@@ -95,16 +93,12 @@ func (c *SBooleanColumn) ConvertFromString(str string) interface{} {
9593

9694
// ConvertFromValue implementation of STristateColumn for IColumnSpec
9795
func (c *SBooleanColumn) ConvertFromValue(val interface{}) interface{} {
98-
var bVal bool
99-
if c.IsPointer() {
100-
bVal = *val.(*bool)
101-
} else {
102-
bVal = val.(bool)
103-
}
104-
if bVal {
96+
switch sqlchemy.ConvertValueToBool(val) {
97+
case true:
10598
return 1
99+
default:
100+
return 0
106101
}
107-
return 0
108102
}
109103

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

141135
// ConvertFromString implementation of STristateColumn for IColumnSpec
142136
func (c *STristateColumn) ConvertFromString(str string) interface{} {
143-
switch strings.ToLower(str) {
144-
case "true", "yes", "on", "ok", "1":
137+
switch sqlchemy.ConvertValueToTriState(str) {
138+
case tristate.True:
145139
return 1
146-
case "none", "null", "unknown":
147-
return sql.NullInt32{}
148-
default:
140+
case tristate.False:
149141
return 0
142+
default:
143+
return sql.NullInt32{}
150144
}
151145
}
152146

153147
// ConvertFromValue implementation of STristateColumn for IColumnSpec
154148
func (c *STristateColumn) ConvertFromValue(val interface{}) interface{} {
155-
bVal := val.(tristate.TriState)
156-
if bVal == tristate.True {
149+
switch sqlchemy.ConvertValueToTriState(val) {
150+
case tristate.True:
157151
return 1
158-
} else if bVal == tristate.False {
152+
case tristate.False:
159153
return 0
160-
} else {
154+
default:
161155
return sql.NullInt32{}
162156
}
163157
}
@@ -234,8 +228,18 @@ func (c *SIntegerColumn) IsZero(val interface{}) bool {
234228

235229
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
236230
func (c *SIntegerColumn) ConvertFromString(str string) interface{} {
237-
val, _ := strconv.ParseInt(str, 10, 64)
238-
return val
231+
intval := sqlchemy.ConvertValueToInteger(str)
232+
switch c.ColType() {
233+
case "TINYINT":
234+
return int8(intval)
235+
case "SMALLINT":
236+
return int16(intval)
237+
case "INT":
238+
return int(intval)
239+
case "BIGINT":
240+
return int64(intval)
241+
}
242+
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
239243
}
240244

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

336340
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
337341
func (c *SFloatColumn) ConvertFromString(str string) interface{} {
338-
val, _ := strconv.ParseFloat(str, 64)
339-
return val
342+
floatVal := sqlchemy.ConvertValueToFloat(str)
343+
switch c.ColType() {
344+
case "FLOAT", "REAL":
345+
return float32(floatVal)
346+
case "DOUBLE":
347+
return floatVal
348+
}
349+
panic(fmt.Sprintf("unsupported type %s", c.ColType()))
340350
}
341351

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

393403
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
394404
func (c *SDecimalColumn) ConvertFromString(str string) interface{} {
395-
val, _ := strconv.ParseFloat(str, 64)
396-
return val
405+
return sqlchemy.ConvertValueToFloat(str)
397406
}
398407

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

499508
// ConvertFromString implementation of SBooleanColumn for IColumnSpec
500509
func (c *STimeTypeColumn) ConvertFromString(str string) interface{} {
501-
tm, _ := timeutils.ParseTimeStr(str)
502-
return tm
510+
return sqlchemy.ConvertValueToTime(str)
511+
}
512+
513+
func (c *STimeTypeColumn) ConvertFromValue(val interface{}) interface{} {
514+
return sqlchemy.ConvertValueToTime(val)
503515
}
504516

505517
// NewTimeTypeColumn return an instance of STimeTypeColumn

backends/dameng/column_test.go

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

2122
"yunion.io/x/jsonutils"
2223
"yunion.io/x/pkg/tristate"
@@ -166,6 +167,11 @@ func TestConvertValue(t *testing.T) {
166167
want: `{}`,
167168
col: &compCol,
168169
},
170+
{
171+
in: "2025-03-27 12:00:00",
172+
want: time.Date(2025, 3, 27, 12, 0, 0, 0, time.UTC),
173+
col: &dateCol,
174+
},
169175
}
170176
for _, c := range cases {
171177
got := c.col.ConvertFromValue(c.in)
@@ -207,12 +213,12 @@ func TestConvertString(t *testing.T) {
207213
},
208214
{
209215
in: "23",
210-
want: int64(23),
216+
want: 23,
211217
col: &intCol,
212218
},
213219
{
214220
in: "0.01",
215-
want: 0.01,
221+
want: float32(0.01),
216222
col: &floatCol,
217223
},
218224
}

backends/dameng/dameng.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (dameng *SDamengBackend) GetColumnSpecByFieldType(table *sqlchemy.STableSpe
257257
col := NewDecimalColumn(fieldname, tagmap, isPointer)
258258
return &col
259259
}
260-
colType := "REAL"
260+
colType := "FLOAT"
261261
if fieldType == gotypes.Float64Type {
262262
colType = "DOUBLE"
263263
}

0 commit comments

Comments
 (0)