1
2
3
4
5 package driver
6
7 import (
8 "fmt"
9 "reflect"
10 "strconv"
11 "time"
12 )
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 type ValueConverter interface {
31
32 ConvertValue(v any) (Value, error)
33 }
34
35
36
37
38
39 type Valuer interface {
40
41
42 Value() (Value, error)
43 }
44
45
46
47
48
49
50
51
52
53
54
55 var Bool boolType
56
57 type boolType struct{}
58
59 var _ ValueConverter = boolType{}
60
61 func (boolType) String() string { return "Bool" }
62
63 func (boolType) ConvertValue(src any) (Value, error) {
64 switch s := src.(type) {
65 case bool:
66 return s, nil
67 case string:
68 b, err := strconv.ParseBool(s)
69 if err != nil {
70 return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
71 }
72 return b, nil
73 case []byte:
74 b, err := strconv.ParseBool(string(s))
75 if err != nil {
76 return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
77 }
78 return b, nil
79 }
80
81 sv := reflect.ValueOf(src)
82 switch sv.Kind() {
83 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
84 iv := sv.Int()
85 if iv == 1 || iv == 0 {
86 return iv == 1, nil
87 }
88 return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", iv)
89 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
90 uv := sv.Uint()
91 if uv == 1 || uv == 0 {
92 return uv == 1, nil
93 }
94 return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", uv)
95 }
96
97 return nil, fmt.Errorf("sql/driver: couldn't convert %v (%T) into type bool", src, src)
98 }
99
100
101
102 var Int32 int32Type
103
104 type int32Type struct{}
105
106 var _ ValueConverter = int32Type{}
107
108 func (int32Type) ConvertValue(v any) (Value, error) {
109 rv := reflect.ValueOf(v)
110 switch rv.Kind() {
111 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
112 i64 := rv.Int()
113 if i64 > (1<<31)-1 || i64 < -(1<<31) {
114 return nil, fmt.Errorf("sql/driver: value %d overflows int32", v)
115 }
116 return i64, nil
117 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
118 u64 := rv.Uint()
119 if u64 > (1<<31)-1 {
120 return nil, fmt.Errorf("sql/driver: value %d overflows int32", v)
121 }
122 return int64(u64), nil
123 case reflect.String:
124 i, err := strconv.Atoi(rv.String())
125 if err != nil {
126 return nil, fmt.Errorf("sql/driver: value %q can't be converted to int32", v)
127 }
128 return int64(i), nil
129 }
130 return nil, fmt.Errorf("sql/driver: unsupported value %v (type %T) converting to int32", v, v)
131 }
132
133
134
135
136
137 var String stringType
138
139 type stringType struct{}
140
141 func (stringType) ConvertValue(v any) (Value, error) {
142 switch v.(type) {
143 case string, []byte:
144 return v, nil
145 }
146 return fmt.Sprintf("%v", v), nil
147 }
148
149
150
151 type Null struct {
152 Converter ValueConverter
153 }
154
155 func (n Null) ConvertValue(v any) (Value, error) {
156 if v == nil {
157 return nil, nil
158 }
159 return n.Converter.ConvertValue(v)
160 }
161
162
163
164 type NotNull struct {
165 Converter ValueConverter
166 }
167
168 func (n NotNull) ConvertValue(v any) (Value, error) {
169 if v == nil {
170 return nil, fmt.Errorf("nil value not allowed")
171 }
172 return n.Converter.ConvertValue(v)
173 }
174
175
176 func IsValue(v any) bool {
177 if v == nil {
178 return true
179 }
180 switch v.(type) {
181 case []byte, bool, float64, int64, string, time.Time:
182 return true
183 case decimalDecompose:
184 return true
185 }
186 return false
187 }
188
189
190
191 func IsScanValue(v any) bool {
192 return IsValue(v)
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 var DefaultParameterConverter defaultConverter
209
210 type defaultConverter struct{}
211
212 var _ ValueConverter = defaultConverter{}
213
214 var valuerReflectType = reflect.TypeOf((*Valuer)(nil)).Elem()
215
216
217
218
219
220
221
222
223
224
225
226
227 func callValuerValue(vr Valuer) (v Value, err error) {
228 if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Pointer &&
229 rv.IsNil() &&
230 rv.Type().Elem().Implements(valuerReflectType) {
231 return nil, nil
232 }
233 return vr.Value()
234 }
235
236 func (defaultConverter) ConvertValue(v any) (Value, error) {
237 if IsValue(v) {
238 return v, nil
239 }
240
241 switch vr := v.(type) {
242 case Valuer:
243 sv, err := callValuerValue(vr)
244 if err != nil {
245 return nil, err
246 }
247 if !IsValue(sv) {
248 return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
249 }
250 return sv, nil
251
252
253 case decimalDecompose:
254 return vr, nil
255 }
256
257 rv := reflect.ValueOf(v)
258 switch rv.Kind() {
259 case reflect.Pointer:
260
261 if rv.IsNil() {
262 return nil, nil
263 } else {
264 return defaultConverter{}.ConvertValue(rv.Elem().Interface())
265 }
266 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
267 return rv.Int(), nil
268 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
269 return int64(rv.Uint()), nil
270 case reflect.Uint64:
271 u64 := rv.Uint()
272 if u64 >= 1<<63 {
273 return nil, fmt.Errorf("uint64 values with high bit set are not supported")
274 }
275 return int64(u64), nil
276 case reflect.Float32, reflect.Float64:
277 return rv.Float(), nil
278 case reflect.Bool:
279 return rv.Bool(), nil
280 case reflect.Slice:
281 ek := rv.Type().Elem().Kind()
282 if ek == reflect.Uint8 {
283 return rv.Bytes(), nil
284 }
285 return nil, fmt.Errorf("unsupported type %T, a slice of %s", v, ek)
286 case reflect.String:
287 return rv.String(), nil
288 }
289 return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
290 }
291
292 type decimalDecompose interface {
293
294
295
296 Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32)
297 }
298
View as plain text