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.
5 // Type conversions for Scan.
17 var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
19 // driverArgs converts arguments from callers of Stmt.Exec and
20 // Stmt.Query into driver Values.
22 // The statement ds may be nil, if no statement is available.
23 func driverArgs(ds *driverStmt, args []interface{}) ([]driver.Value, error) {
24 dargs := make([]driver.Value, len(args))
29 cc, ok := si.(driver.ColumnConverter)
31 // Normal path, for a driver.Stmt that is not a ColumnConverter.
33 for n, arg := range args {
35 dargs[n], err = driver.DefaultParameterConverter.ConvertValue(arg)
37 return nil, fmt.Errorf("sql: converting Exec argument #%d's type: %v", n, err)
43 // Let the Stmt convert its own arguments.
44 for n, arg := range args {
45 // First, see if the value itself knows how to convert
46 // itself to a driver type. For example, a NullString
47 // struct changing into a string or nil.
48 if svi, ok := arg.(driver.Valuer); ok {
49 sv, err := svi.Value()
51 return nil, fmt.Errorf("sql: argument index %d from Value: %v", n, err)
53 if !driver.IsValue(sv) {
54 return nil, fmt.Errorf("sql: argument index %d: non-subset type %T returned from Value", n, sv)
59 // Second, ask the column to sanity check itself. For
60 // example, drivers might use this to make sure that
61 // an int64 values being inserted into a 16-bit
62 // integer field is in range (before getting
63 // truncated), or that a nil can't go into a NOT NULL
64 // column before going across the network to get the
68 dargs[n], err = cc.ColumnConverter(n).ConvertValue(arg)
71 return nil, fmt.Errorf("sql: converting argument #%d's type: %v", n, err)
73 if !driver.IsValue(dargs[n]) {
74 return nil, fmt.Errorf("sql: driver ColumnConverter error converted %T to unsupported type %T",
82 // convertAssign copies to dest the value in src, converting it if possible.
83 // An error is returned if the copy would result in loss of information.
84 // dest should be a pointer type.
85 func convertAssign(dest, src interface{}) error {
86 // Common cases, without reflect.
87 switch s := src.(type) {
89 switch d := dest.(type) {
104 switch d := dest.(type) {
131 switch d := dest.(type) {
155 switch d := dest.(type) {
157 sv = reflect.ValueOf(src)
160 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
161 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
162 reflect.Float32, reflect.Float64:
163 *d = fmt.Sprintf("%v", src)
167 sv = reflect.ValueOf(src)
170 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
171 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
172 reflect.Float32, reflect.Float64:
173 *d = []byte(fmt.Sprintf("%v", src))
177 sv = reflect.ValueOf(src)
180 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
181 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
182 reflect.Float32, reflect.Float64:
183 *d = RawBytes(fmt.Sprintf("%v", src))
187 bv, err := driver.Bool.ConvertValue(src)
197 if scanner, ok := dest.(Scanner); ok {
198 return scanner.Scan(src)
201 dpv := reflect.ValueOf(dest)
202 if dpv.Kind() != reflect.Ptr {
203 return errors.New("destination not a pointer")
210 sv = reflect.ValueOf(src)
213 dv := reflect.Indirect(dpv)
214 if dv.Kind() == sv.Kind() {
222 dv.Set(reflect.Zero(dv.Type()))
225 dv.Set(reflect.New(dv.Type().Elem()))
226 return convertAssign(dv.Interface(), src)
228 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
230 i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
232 return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
236 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
238 u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
240 return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
244 case reflect.Float32, reflect.Float64:
246 f64, err := strconv.ParseFloat(s, dv.Type().Bits())
248 return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
254 return fmt.Errorf("unsupported driver -> Scan pair: %T -> %T", src, dest)
257 func cloneBytes(b []byte) []byte {
261 c := make([]byte, len(b))
267 func asString(src interface{}) string {
268 switch v := src.(type) {
274 return fmt.Sprintf("%v", src)