Retro68/gcc/libgo/go/database/sql/convert.go

394 lines
9.6 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Type conversions for Scan.
package sql
import (
"database/sql/driver"
"errors"
"fmt"
"reflect"
"strconv"
2017-04-10 11:32:00 +00:00
"time"
"unicode"
"unicode/utf8"
2012-03-27 23:13:14 +00:00
)
2014-09-21 17:33:12 +00:00
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
func describeNamedValue(nv *driver.NamedValue) string {
if len(nv.Name) == 0 {
return fmt.Sprintf("$%d", nv.Ordinal)
}
return fmt.Sprintf("with name %q", nv.Name)
}
func validateNamedValueName(name string) error {
if len(name) == 0 {
return nil
}
r, _ := utf8.DecodeRuneInString(name)
if unicode.IsLetter(r) {
return nil
}
return fmt.Errorf("name %q does not begin with a letter", name)
}
2014-09-21 17:33:12 +00:00
// driverArgs converts arguments from callers of Stmt.Exec and
// Stmt.Query into driver Values.
//
// The statement ds may be nil, if no statement is available.
func driverArgs(ds *driverStmt, args []interface{}) ([]driver.NamedValue, error) {
nvargs := make([]driver.NamedValue, len(args))
2014-09-21 17:33:12 +00:00
var si driver.Stmt
if ds != nil {
si = ds.si
}
cc, ok := si.(driver.ColumnConverter)
// Normal path, for a driver.Stmt that is not a ColumnConverter.
if !ok {
for n, arg := range args {
var err error
nv := &nvargs[n]
nv.Ordinal = n + 1
if np, ok := arg.(NamedArg); ok {
if err := validateNamedValueName(np.Name); err != nil {
return nil, err
}
arg = np.Value
nvargs[n].Name = np.Name
}
nv.Value, err = driver.DefaultParameterConverter.ConvertValue(arg)
2014-09-21 17:33:12 +00:00
if err != nil {
return nil, fmt.Errorf("sql: converting Exec argument %s type: %v", describeNamedValue(nv), err)
2014-09-21 17:33:12 +00:00
}
}
return nvargs, nil
2014-09-21 17:33:12 +00:00
}
// Let the Stmt convert its own arguments.
2012-03-27 23:13:14 +00:00
for n, arg := range args {
nv := &nvargs[n]
nv.Ordinal = n + 1
if np, ok := arg.(NamedArg); ok {
if err := validateNamedValueName(np.Name); err != nil {
return nil, err
}
arg = np.Value
nv.Name = np.Name
}
2014-09-21 17:33:12 +00:00
// First, see if the value itself knows how to convert
// itself to a driver type. For example, a NullString
2014-09-21 17:33:12 +00:00
// struct changing into a string or nil.
if vr, ok := arg.(driver.Valuer); ok {
sv, err := callValuerValue(vr)
2014-09-21 17:33:12 +00:00
if err != nil {
return nil, fmt.Errorf("sql: argument %s from Value: %v", describeNamedValue(nv), err)
2014-09-21 17:33:12 +00:00
}
if !driver.IsValue(sv) {
return nil, fmt.Errorf("sql: argument %s: non-subset type %T returned from Value", describeNamedValue(nv), sv)
2014-09-21 17:33:12 +00:00
}
arg = sv
}
// Second, ask the column to sanity check itself. For
// example, drivers might use this to make sure that
// an int64 values being inserted into a 16-bit
// integer field is in range (before getting
// truncated), or that a nil can't go into a NOT NULL
// column before going across the network to get the
// same error.
2012-03-27 23:13:14 +00:00
var err error
2014-09-21 17:33:12 +00:00
ds.Lock()
nv.Value, err = cc.ColumnConverter(n).ConvertValue(arg)
2014-09-21 17:33:12 +00:00
ds.Unlock()
2012-03-27 23:13:14 +00:00
if err != nil {
return nil, fmt.Errorf("sql: converting argument %s type: %v", describeNamedValue(nv), err)
2014-09-21 17:33:12 +00:00
}
if !driver.IsValue(nv.Value) {
return nil, fmt.Errorf("sql: for argument %s, driver ColumnConverter error converted %T to unsupported type %T",
describeNamedValue(nv), arg, nv.Value)
2012-03-27 23:13:14 +00:00
}
}
2014-09-21 17:33:12 +00:00
return nvargs, nil
2012-03-27 23:13:14 +00:00
}
// convertAssign copies to dest the value in src, converting it if possible.
// An error is returned if the copy would result in loss of information.
// dest should be a pointer type.
func convertAssign(dest, src interface{}) error {
2014-09-21 17:33:12 +00:00
// Common cases, without reflect.
2012-03-27 23:13:14 +00:00
switch s := src.(type) {
case string:
switch d := dest.(type) {
case *string:
2014-09-21 17:33:12 +00:00
if d == nil {
return errNilPtr
}
2012-03-27 23:13:14 +00:00
*d = s
return nil
case *[]byte:
2014-09-21 17:33:12 +00:00
if d == nil {
return errNilPtr
}
2012-03-27 23:13:14 +00:00
*d = []byte(s)
return nil
}
case []byte:
switch d := dest.(type) {
case *string:
2014-09-21 17:33:12 +00:00
if d == nil {
return errNilPtr
}
2012-03-27 23:13:14 +00:00
*d = string(s)
return nil
case *interface{}:
2014-09-21 17:33:12 +00:00
if d == nil {
return errNilPtr
}
*d = cloneBytes(s)
2012-03-27 23:13:14 +00:00
return nil
case *[]byte:
2014-09-21 17:33:12 +00:00
if d == nil {
return errNilPtr
}
*d = cloneBytes(s)
return nil
case *RawBytes:
if d == nil {
return errNilPtr
}
2012-03-27 23:13:14 +00:00
*d = s
return nil
}
2017-04-10 11:32:00 +00:00
case time.Time:
switch d := dest.(type) {
case *string:
*d = s.Format(time.RFC3339Nano)
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = []byte(s.Format(time.RFC3339Nano))
return nil
}
2012-03-27 23:13:14 +00:00
case nil:
switch d := dest.(type) {
2014-09-21 17:33:12 +00:00
case *interface{}:
if d == nil {
return errNilPtr
}
*d = nil
return nil
2012-03-27 23:13:14 +00:00
case *[]byte:
2014-09-21 17:33:12 +00:00
if d == nil {
return errNilPtr
}
*d = nil
return nil
case *RawBytes:
if d == nil {
return errNilPtr
}
2012-03-27 23:13:14 +00:00
*d = nil
return nil
}
}
var sv reflect.Value
switch d := dest.(type) {
case *string:
sv = reflect.ValueOf(src)
switch sv.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
2015-08-28 15:33:40 +00:00
*d = asString(src)
2012-03-27 23:13:14 +00:00
return nil
}
2014-09-21 17:33:12 +00:00
case *[]byte:
sv = reflect.ValueOf(src)
2015-08-28 15:33:40 +00:00
if b, ok := asBytes(nil, sv); ok {
*d = b
2014-09-21 17:33:12 +00:00
return nil
}
case *RawBytes:
sv = reflect.ValueOf(src)
2015-08-28 15:33:40 +00:00
if b, ok := asBytes([]byte(*d)[:0], sv); ok {
*d = RawBytes(b)
2014-09-21 17:33:12 +00:00
return nil
}
2012-03-27 23:13:14 +00:00
case *bool:
bv, err := driver.Bool.ConvertValue(src)
if err == nil {
*d = bv.(bool)
}
return err
case *interface{}:
*d = src
return nil
}
if scanner, ok := dest.(Scanner); ok {
return scanner.Scan(src)
}
dpv := reflect.ValueOf(dest)
if dpv.Kind() != reflect.Ptr {
return errors.New("destination not a pointer")
}
2014-09-21 17:33:12 +00:00
if dpv.IsNil() {
return errNilPtr
}
2012-03-27 23:13:14 +00:00
if !sv.IsValid() {
sv = reflect.ValueOf(src)
}
dv := reflect.Indirect(dpv)
2017-04-10 11:32:00 +00:00
if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
switch b := src.(type) {
case []byte:
dv.Set(reflect.ValueOf(cloneBytes(b)))
default:
dv.Set(sv)
}
2012-03-27 23:13:14 +00:00
return nil
}
2017-04-10 11:32:00 +00:00
if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
dv.Set(sv.Convert(dv.Type()))
return nil
}
2012-03-27 23:13:14 +00:00
switch dv.Kind() {
case reflect.Ptr:
if src == nil {
dv.Set(reflect.Zero(dv.Type()))
return nil
} else {
dv.Set(reflect.New(dv.Type().Elem()))
return convertAssign(dv.Interface(), src)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := asString(src)
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
if err != nil {
2017-04-10 11:32:00 +00:00
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
2012-03-27 23:13:14 +00:00
}
dv.SetInt(i64)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
s := asString(src)
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
if err != nil {
2017-04-10 11:32:00 +00:00
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
2012-03-27 23:13:14 +00:00
}
dv.SetUint(u64)
return nil
case reflect.Float32, reflect.Float64:
s := asString(src)
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
if err != nil {
2017-04-10 11:32:00 +00:00
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
2012-03-27 23:13:14 +00:00
}
dv.SetFloat(f64)
return nil
}
2017-04-10 11:32:00 +00:00
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
}
func strconvErr(err error) error {
if ne, ok := err.(*strconv.NumError); ok {
return ne.Err
}
return err
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
func cloneBytes(b []byte) []byte {
if b == nil {
return nil
} else {
c := make([]byte, len(b))
copy(c, b)
return c
}
}
2012-03-27 23:13:14 +00:00
func asString(src interface{}) string {
switch v := src.(type) {
case string:
return v
case []byte:
return string(v)
}
2015-08-28 15:33:40 +00:00
rv := reflect.ValueOf(src)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(rv.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(rv.Uint(), 10)
case reflect.Float64:
return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
case reflect.Float32:
return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
case reflect.Bool:
return strconv.FormatBool(rv.Bool())
}
2012-03-27 23:13:14 +00:00
return fmt.Sprintf("%v", src)
}
2015-08-28 15:33:40 +00:00
func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.AppendInt(buf, rv.Int(), 10), true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.AppendUint(buf, rv.Uint(), 10), true
case reflect.Float32:
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true
case reflect.Float64:
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true
case reflect.Bool:
return strconv.AppendBool(buf, rv.Bool()), true
case reflect.String:
s := rv.String()
return append(buf, s...), true
}
return
}
var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
// callValuerValue returns vr.Value(), with one exception:
// If vr.Value is an auto-generated method on a pointer type and the
// pointer is nil, it would panic at runtime in the panicwrap
// method. Treat it like nil instead.
// Issue 8415.
//
// This is so people can implement driver.Value on value types and
// still use nil pointers to those types to mean nil/NULL, just like
// string/*string.
//
// This function is mirrored in the database/sql/driver package.
func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Ptr &&
rv.IsNil() &&
rv.Type().Elem().Implements(valuerReflectType) {
return nil, nil
}
return vr.Value()
}