-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathvalue_converter.go
More file actions
46 lines (38 loc) · 885 Bytes
/
value_converter.go
File metadata and controls
46 lines (38 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package clickhouse
import (
"database/sql/driver"
"reflect"
"strconv"
)
func (stmt *stmt) ColumnConverter(idx int) driver.ValueConverter {
return converter{}
}
func (c *conn) CheckNamedValue(nv *driver.NamedValue) (err error) {
nv.Value, err = converter{}.ConvertValue(nv.Value)
return
}
type converter struct{}
const maxAllowedUInt64 = 1<<63 - 1
func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
if driver.IsValue(v) {
return v, nil
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr:
// indirect pointers
if rv.IsNil() {
return nil, nil
}
return c.ConvertValue(rv.Elem().Interface())
case reflect.Uint64:
u64 := rv.Uint()
if u64 > maxAllowedUInt64 {
s := strconv.FormatUint(u64, 10)
bytes := []byte(s)
return bytes, nil
}
return u64, nil
}
return driver.DefaultParameterConverter.ConvertValue(v)
}