-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.go
More file actions
64 lines (60 loc) · 1.57 KB
/
Copy pathvalue.go
File metadata and controls
64 lines (60 loc) · 1.57 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package quackdriver
import (
"database/sql/driver"
"math"
"math/big"
"time"
)
// CheckNamedValue accepts any Go scalar plus a few common stdlib types. It is
// used by database/sql to validate that a value is convertible to driver.Value
// before the driver attempts to format it.
func (c *Conn) CheckNamedValue(nv *driver.NamedValue) error {
v, err := convertValue(nv.Value)
if err != nil {
return err
}
nv.Value = v
return nil
}
var _ driver.NamedValueChecker = (*Conn)(nil)
func convertValue(v any) (driver.Value, error) {
switch x := v.(type) {
case nil:
return nil, nil
case bool, int64, float64, string, []byte, time.Time:
return x, nil
case int:
return int64(x), nil
case int8:
return int64(x), nil
case int16:
return int64(x), nil
case int32:
return int64(x), nil
case uint:
// uint is 64-bit on most platforms; values above MaxInt64 would
// silently wrap to a negative int64. Pass through as uint64 so the
// sqlfmt layer renders the actual magnitude.
if uint64(x) > math.MaxInt64 {
return uint64(x), nil
}
return int64(x), nil
case uint8:
return int64(x), nil
case uint16:
return int64(x), nil
case uint32:
return int64(x), nil
case uint64:
// Pass through as-is so the sqlfmt layer can render it without
// truncating to int64.
return x, nil
case float32:
return float64(x), nil
case *big.Int, big.Int, *big.Rat:
return x, nil
}
// Fall back to database/sql's default conversion, which understands the
// driver.Valuer interface and a handful of additional types.
return driver.DefaultParameterConverter.ConvertValue(v)
}