Debian Codegraph @

src/database/sql/convert.go

raw
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Type conversions for Scan.
6
7 package sql
8
9 import (
10 "bytes"
11 "database/sql/driver"
12 "errors"
13 "fmt"
14 "reflect"
15 "strconv"
16 "time"
17 "unicode"
18 "unicode/utf8"
19 _ "unsafe" // for linkname
20 )
21
22 var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
23
24 func describeNamedValue(nv *driver.NamedValue) string {
25 if len(nv.Name) == 0 {
26 return fmt.Sprintf("$%d", nv.Ordinal)
27 }
28 return fmt.Sprintf("with name %q", nv.Name)
29 }
30
31 func validateNamedValueName(name string) error {
32 if len(name) == 0 {
33 return nil
34 }
35 r, _ := utf8.DecodeRuneInString(name)
36 if unicode.IsLetter(r) {
37 return nil
38 }
39 return fmt.Errorf("name %q does not begin with a letter", name)
40 }
41
42 // ccChecker wraps the driver.ColumnConverter and allows it to be used
43 // as if it were a NamedValueChecker. If the driver ColumnConverter
44 // is not present then the NamedValueChecker will return driver.ErrSkip.
45 type ccChecker struct {
46 cci driver.ColumnConverter
47 want int
48 }
49
50 func (c ccChecker) CheckNamedValue(nv *driver.NamedValue) error {
51 if c.cci == nil {
52 return driver.ErrSkip
53 }
54 // The column converter shouldn't be called on any index
55 // it isn't expecting. The final error will be thrown
56 // in the argument converter loop.
57 index := nv.Ordinal - 1
58 if c.want <= index {
59 return nil
60 }
61
62 // First, see if the value itself knows how to convert
63 // itself to a driver type. For example, a NullString
64 // struct changing into a string or nil.
65 if vr, ok := nv.Value.(driver.Valuer); ok {
66 sv, err := callValuerValue(vr)
67 if err != nil {
68 return err
69 }
70 if !driver.IsValue(sv) {
71 return fmt.Errorf("non-subset type %T returned from Value", sv)
72 }
73 nv.Value = sv
74 }
75
76 // Second, ask the column to sanity check itself. For
77 // example, drivers might use this to make sure that
78 // an int64 values being inserted into a 16-bit
79 // integer field is in range (before getting
80 // truncated), or that a nil can't go into a NOT NULL
81 // column before going across the network to get the
82 // same error.
83 var err error
84 arg := nv.Value
85 nv.Value, err = c.cci.ColumnConverter(index).ConvertValue(arg)
86 if err != nil {
87 return err
88 }
89 if !driver.IsValue(nv.Value) {
90 return fmt.Errorf("driver ColumnConverter error converted %T to unsupported type %T", arg, nv.Value)
91 }
92 return nil
93 }
94
95 // defaultCheckNamedValue wraps the default ColumnConverter to have the same
96 // function signature as the CheckNamedValue in the driver.NamedValueChecker
97 // interface.
98 func defaultCheckNamedValue(nv *driver.NamedValue) (err error) {
99 nv.Value, err = driver.DefaultParameterConverter.ConvertValue(nv.Value)
100 return err
101 }
102
103 // driverArgsConnLocked converts arguments from callers of Stmt.Exec and
104 // Stmt.Query into driver Values.
105 //
106 // The statement ds may be nil, if no statement is available.
107 //
108 // ci must be locked.
109 func driverArgsConnLocked(ci driver.Conn, ds *driverStmt, args []any) ([]driver.NamedValue, error) {
110 nvargs := make([]driver.NamedValue, len(args))
111
112 // -1 means the driver doesn't know how to count the number of
113 // placeholders, so we won't sanity check input here and instead let the
114 // driver deal with errors.
115 want := -1
116
117 var si driver.Stmt
118 var cc ccChecker
119 if ds != nil {
120 si = ds.si
121 want = ds.si.NumInput()
122 cc.want = want
123 }
124
125 // Check all types of interfaces from the start.
126 // Drivers may opt to use the NamedValueChecker for special
127 // argument types, then return driver.ErrSkip to pass it along
128 // to the column converter.
129 nvc, ok := si.(driver.NamedValueChecker)
130 if !ok {
131 nvc, _ = ci.(driver.NamedValueChecker)
132 }
133 cci, ok := si.(driver.ColumnConverter)
134 if ok {
135 cc.cci = cci
136 }
137
138 // Loop through all the arguments, checking each one.
139 // If no error is returned simply increment the index
140 // and continue. However, if driver.ErrRemoveArgument
141 // is returned the argument is not included in the query
142 // argument list.
143 var err error
144 var n int
145 for _, arg := range args {
146 nv := &nvargs[n]
147 if np, ok := arg.(NamedArg); ok {
148 if err = validateNamedValueName(np.Name); err != nil {
149 return nil, err
150 }
151 arg = np.Value
152 nv.Name = np.Name
153 }
154 nv.Ordinal = n + 1
155 nv.Value = arg
156
157 // Checking sequence has four routes:
158 // A: 1. Default
159 // B: 1. NamedValueChecker 2. Column Converter 3. Default
160 // C: 1. NamedValueChecker 3. Default
161 // D: 1. Column Converter 2. Default
162 //
163 // The only time a Column Converter is called is first
164 // or after NamedValueConverter. If first it is handled before
165 // the nextCheck label. Thus for repeats tries only when the
166 // NamedValueConverter is selected should the Column Converter
167 // be used in the retry.
168 checker := defaultCheckNamedValue
169 nextCC := false
170 switch {
171 case nvc != nil:
172 nextCC = cci != nil
173 checker = nvc.CheckNamedValue
174 case cci != nil:
175 checker = cc.CheckNamedValue
176 }
177
178 nextCheck:
179 err = checker(nv)
180 switch err {
181 case nil:
182 n++
183 continue
184 case driver.ErrRemoveArgument:
185 nvargs = nvargs[:len(nvargs)-1]
186 continue
187 case driver.ErrSkip:
188 if nextCC {
189 nextCC = false
190 checker = cc.CheckNamedValue
191 } else {
192 checker = defaultCheckNamedValue
193 }
194 goto nextCheck
195 default:
196 return nil, fmt.Errorf("sql: converting argument %s type: %w", describeNamedValue(nv), err)
197 }
198 }
199
200 // Check the length of arguments after conversion to allow for omitted
201 // arguments.
202 if want != -1 && len(nvargs) != want {
203 return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(nvargs))
204 }
205
206 return nvargs, nil
207 }
208
209 // convertAssign is the same as convertAssignRows, but without the optional
210 // rows argument.
211 //
212 // convertAssign should be an internal detail,
213 // but widely used packages access it using linkname.
214 // Notable members of the hall of shame include:
215 // - ariga.io/entcache
216 //
217 // Do not remove or change the type signature.
218 // See go.dev/issue/67401.
219 //
220 //go:linkname convertAssign
221 func convertAssign(dest, src any) error {
222 return convertAssignRows(dest, src, nil)
223 }
224
225 // convertAssignRows copies to dest the value in src, converting it if possible.
226 // An error is returned if the copy would result in loss of information.
227 // dest should be a pointer type. If rows is passed in, the rows will
228 // be used as the parent for any cursor values converted from a
229 // driver.Rows to a *Rows.
230 func convertAssignRows(dest, src any, rows *Rows) error {
231 // Common cases, without reflect.
232 switch s := src.(type) {
233 case string:
234 switch d := dest.(type) {
235 case *string:
236 if d == nil {
237 return errNilPtr
238 }
239 *d = s
240 return nil
241 case *[]byte:
242 if d == nil {
243 return errNilPtr
244 }
245 *d = []byte(s)
246 return nil
247 case *RawBytes:
248 if d == nil {
249 return errNilPtr
250 }
251 *d = rows.setrawbuf(append(rows.rawbuf(), s...))
252 return nil
253 }
254 case []byte:
255 switch d := dest.(type) {
256 case *string:
257 if d == nil {
258 return errNilPtr
259 }
260 *d = string(s)
261 return nil
262 case *any:
263 if d == nil {
264 return errNilPtr
265 }
266 *d = bytes.Clone(s)
267 return nil
268 case *[]byte:
269 if d == nil {
270 return errNilPtr
271 }
272 *d = bytes.Clone(s)
273 return nil
274 case *RawBytes:
275 if d == nil {
276 return errNilPtr
277 }
278 *d = s
279 return nil
280 }
281 case time.Time:
282 switch d := dest.(type) {
283 case *time.Time:
284 *d = s
285 return nil
286 case *string:
287 *d = s.Format(time.RFC3339Nano)
288 return nil
289 case *[]byte:
290 if d == nil {
291 return errNilPtr
292 }
293 *d = s.AppendFormat(make([]byte, 0, len(time.RFC3339Nano)), time.RFC3339Nano)
294 return nil
295 case *RawBytes:
296 if d == nil {
297 return errNilPtr
298 }
299 *d = rows.setrawbuf(s.AppendFormat(rows.rawbuf(), time.RFC3339Nano))
300 return nil
301 }
302 case decimalDecompose:
303 switch d := dest.(type) {
304 case decimalCompose:
305 return d.Compose(s.Decompose(nil))
306 }
307 case nil:
308 switch d := dest.(type) {
309 case *any:
310 if d == nil {
311 return errNilPtr
312 }
313 *d = nil
314 return nil
315 case *[]byte:
316 if d == nil {
317 return errNilPtr
318 }
319 *d = nil
320 return nil
321 case *RawBytes:
322 if d == nil {
323 return errNilPtr
324 }
325 *d = nil
326 return nil
327 }
328 // The driver is returning a cursor the client may iterate over.
329 case driver.Rows:
330 switch d := dest.(type) {
331 case *Rows:
332 if d == nil {
333 return errNilPtr
334 }
335 if rows == nil {
336 return errors.New("invalid context to convert cursor rows, missing parent *Rows")
337 }
338 *d = Rows{
339 dc: rows.dc,
340 releaseConn: func(error) {},
341 rowsi: s,
342 }
343 // Chain the cancel function.
344 parentCancel := rows.cancel
345 rows.cancel = func() {
346 // When Rows.cancel is called, the closemu will be locked as well.
347 // So we can access rs.lasterr.
348 d.close(rows.lasterr)
349 if parentCancel != nil {
350 parentCancel()
351 }
352 }
353 return nil
354 }
355 }
356
357 var sv reflect.Value
358
359 switch d := dest.(type) {
360 case *string:
361 sv = reflect.ValueOf(src)
362 switch sv.Kind() {
363 case reflect.Bool,
364 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
365 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
366 reflect.Float32, reflect.Float64:
367 *d = asString(src)
368 return nil
369 }
370 case *[]byte:
371 sv = reflect.ValueOf(src)
372 if b, ok := asBytes(nil, sv); ok {
373 *d = b
374 return nil
375 }
376 case *RawBytes:
377 sv = reflect.ValueOf(src)
378 if b, ok := asBytes(rows.rawbuf(), sv); ok {
379 *d = rows.setrawbuf(b)
380 return nil
381 }
382 case *bool:
383 bv, err := driver.Bool.ConvertValue(src)
384 if err == nil {
385 *d = bv.(bool)
386 }
387 return err
388 case *any:
389 *d = src
390 return nil
391 }
392
393 if scanner, ok := dest.(Scanner); ok {
394 return scanner.Scan(src)
395 }
396
397 dpv := reflect.ValueOf(dest)
398 if dpv.Kind() != reflect.Pointer {
399 return errors.New("destination not a pointer")
400 }
401 if dpv.IsNil() {
402 return errNilPtr
403 }
404
405 if !sv.IsValid() {
406 sv = reflect.ValueOf(src)
407 }
408
409 dv := reflect.Indirect(dpv)
410 if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
411 switch b := src.(type) {
412 case []byte:
413 dv.Set(reflect.ValueOf(bytes.Clone(b)))
414 default:
415 dv.Set(sv)
416 }
417 return nil
418 }
419
420 if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
421 dv.Set(sv.Convert(dv.Type()))
422 return nil
423 }
424
425 // The following conversions use a string value as an intermediate representation
426 // to convert between various numeric types.
427 //
428 // This also allows scanning into user defined types such as "type Int int64".
429 // For symmetry, also check for string destination types.
430 switch dv.Kind() {
431 case reflect.Pointer:
432 if src == nil {
433 dv.SetZero()
434 return nil
435 }
436 dv.Set(reflect.New(dv.Type().Elem()))
437 return convertAssignRows(dv.Interface(), src, rows)
438 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
439 if src == nil {
440 return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
441 }
442 s := asString(src)
443 i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
444 if err != nil {
445 err = strconvErr(err)
446 return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
447 }
448 dv.SetInt(i64)
449 return nil
450 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
451 if src == nil {
452 return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
453 }
454 s := asString(src)
455 u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
456 if err != nil {
457 err = strconvErr(err)
458 return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
459 }
460 dv.SetUint(u64)
461 return nil
462 case reflect.Float32, reflect.Float64:
463 if src == nil {
464 return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
465 }
466 s := asString(src)
467 f64, err := strconv.ParseFloat(s, dv.Type().Bits())
468 if err != nil {
469 err = strconvErr(err)
470 return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
471 }
472 dv.SetFloat(f64)
473 return nil
474 case reflect.String:
475 if src == nil {
476 return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
477 }
478 switch v := src.(type) {
479 case string:
480 dv.SetString(v)
481 return nil
482 case []byte:
483 dv.SetString(string(v))
484 return nil
485 }
486 }
487
488 return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
489 }
490
491 func strconvErr(err error) error {
492 if ne, ok := err.(*strconv.NumError); ok {
493 return ne.Err
494 }
495 return err
496 }
497
498 func asString(src any) string {
499 switch v := src.(type) {
500 case string:
501 return v
502 case []byte:
503 return string(v)
504 }
505 rv := reflect.ValueOf(src)
506 switch rv.Kind() {
507 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
508 return strconv.FormatInt(rv.Int(), 10)
509 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
510 return strconv.FormatUint(rv.Uint(), 10)
511 case reflect.Float64:
512 return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
513 case reflect.Float32:
514 return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
515 case reflect.Bool:
516 return strconv.FormatBool(rv.Bool())
517 }
518 return fmt.Sprintf("%v", src)
519 }
520
521 func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
522 switch rv.Kind() {
523 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
524 return strconv.AppendInt(buf, rv.Int(), 10), true
525 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
526 return strconv.AppendUint(buf, rv.Uint(), 10), true
527 case reflect.Float32:
528 return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true
529 case reflect.Float64:
530 return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true
531 case reflect.Bool:
532 return strconv.AppendBool(buf, rv.Bool()), true
533 case reflect.String:
534 s := rv.String()
535 return append(buf, s...), true
536 }
537 return
538 }
539
540 var valuerReflectType = reflect.TypeFor[driver.Valuer]()
541
542 // callValuerValue returns vr.Value(), with one exception:
543 // If vr.Value is an auto-generated method on a pointer type and the
544 // pointer is nil, it would panic at runtime in the panicwrap
545 // method. Treat it like nil instead.
546 // Issue 8415.
547 //
548 // This is so people can implement driver.Value on value types and
549 // still use nil pointers to those types to mean nil/NULL, just like
550 // string/*string.
551 //
552 // This function is mirrored in the database/sql/driver package.
553 func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
554 if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Pointer &&
555 rv.IsNil() &&
556 rv.Type().Elem().Implements(valuerReflectType) {
557 return nil, nil
558 }
559 return vr.Value()
560 }
561
562 // decimal composes or decomposes a decimal value to and from individual parts.
563 // There are four parts: a boolean negative flag, a form byte with three possible states
564 // (finite=0, infinite=1, NaN=2), a base-2 big-endian integer
565 // coefficient (also known as a significand) as a []byte, and an int32 exponent.
566 // These are composed into a final value as "decimal = (neg) (form=finite) coefficient * 10 ^ exponent".
567 // A zero length coefficient is a zero value.
568 // The big-endian integer coefficient stores the most significant byte first (at coefficient[0]).
569 // If the form is not finite the coefficient and exponent should be ignored.
570 // The negative parameter may be set to true for any form, although implementations are not required
571 // to respect the negative parameter in the non-finite form.
572 //
573 // Implementations may choose to set the negative parameter to true on a zero or NaN value,
574 // but implementations that do not differentiate between negative and positive
575 // zero or NaN values should ignore the negative parameter without error.
576 // If an implementation does not support Infinity it may be converted into a NaN without error.
577 // If a value is set that is larger than what is supported by an implementation,
578 // an error must be returned.
579 // Implementations must return an error if a NaN or Infinity is attempted to be set while neither
580 // are supported.
581 //
582 // NOTE(kardianos): This is an experimental interface. See https://golang.org/issue/30870
583 type decimal interface {
584 decimalDecompose
585 decimalCompose
586 }
587
588 type decimalDecompose interface {
589 // Decompose returns the internal decimal state in parts.
590 // If the provided buf has sufficient capacity, buf may be returned as the coefficient with
591 // the value set and length set as appropriate.
592 Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32)
593 }
594
595 type decimalCompose interface {
596 // Compose sets the internal decimal value from parts. If the value cannot be
597 // represented then an error should be returned.
598 Compose(form byte, negative bool, coefficient []byte, exponent int32) error
599 }