Source File
convert.go
Belonging Package
database/sql
package sql
import (
)
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
func ( *driver.NamedValue) string {
if len(.Name) == 0 {
return fmt.Sprintf("$%d", .Ordinal)
}
return fmt.Sprintf("with name %q", .Name)
}
func ( string) error {
if len() == 0 {
return nil
}
, := utf8.DecodeRuneInString()
if unicode.IsLetter() {
return nil
}
return fmt.Errorf("name %q does not begin with a letter", )
}
func ( *driver.NamedValue) ( error) {
.Value, = driver.DefaultParameterConverter.ConvertValue(.Value)
return
}
func ( driver.Conn, *driverStmt, []interface{}) ([]driver.NamedValue, error) {
:= make([]driver.NamedValue, len())
, := .(driver.NamedValueChecker)
if ! {
, = .(driver.NamedValueChecker)
}
, := .(driver.ColumnConverter)
if {
.cci =
}
:= defaultCheckNamedValue
:= false
switch {
case != nil:
= != nil
= .CheckNamedValue
case != nil:
= .CheckNamedValue
}
:
= ()
switch {
case nil:
++
continue
case driver.ErrRemoveArgument:
= [:len()-1]
continue
case driver.ErrSkip:
if {
= false
= .CheckNamedValue
} else {
= defaultCheckNamedValue
}
goto
default:
return nil, fmt.Errorf("sql: converting argument %s type: %v", describeNamedValue(), )
}
}
func (, interface{}) error {
return convertAssignRows(, , nil)
}
switch s := .(type) {
case string:
switch d := .(type) {
case *string:
if == nil {
return errNilPtr
}
* =
return nil
case *[]byte:
if == nil {
return errNilPtr
}
* = []byte()
return nil
case *RawBytes:
if == nil {
return errNilPtr
}
* = append((*)[:0], ...)
return nil
}
case []byte:
switch d := .(type) {
case *string:
if == nil {
return errNilPtr
}
* = string()
return nil
case *interface{}:
if == nil {
return errNilPtr
}
* = cloneBytes()
return nil
case *[]byte:
if == nil {
return errNilPtr
}
* = cloneBytes()
return nil
case *RawBytes:
if == nil {
return errNilPtr
}
* =
return nil
}
case time.Time:
switch d := .(type) {
case *time.Time:
* =
return nil
case *string:
* = .Format(time.RFC3339Nano)
return nil
case *[]byte:
if == nil {
return errNilPtr
}
* = []byte(.Format(time.RFC3339Nano))
return nil
case *RawBytes:
if == nil {
return errNilPtr
}
* = .AppendFormat((*)[:0], time.RFC3339Nano)
return nil
}
case decimalDecompose:
switch d := .(type) {
case decimalCompose:
return .Compose(.Decompose(nil))
}
case nil:
switch d := .(type) {
case *interface{}:
if == nil {
return errNilPtr
}
* = nil
return nil
case *[]byte:
if == nil {
return errNilPtr
}
* = nil
return nil
case *RawBytes:
if == nil {
return errNilPtr
}
* = nil
return nil
:= .cancel
.close(.lasterr)
if != nil {
()
}
}
.closemu.Unlock()
return nil
}
}
var reflect.Value
switch d := .(type) {
case *string:
= reflect.ValueOf()
switch .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:
* = asString()
return nil
}
case *[]byte:
= reflect.ValueOf()
if , := asBytes(nil, ); {
* =
return nil
}
case *RawBytes:
= reflect.ValueOf()
if , := asBytes([]byte(*)[:0], ); {
* = RawBytes()
return nil
}
case *bool:
, := driver.Bool.ConvertValue()
if == nil {
* = .(bool)
}
return
case *interface{}:
* =
return nil
}
if , := .(Scanner); {
return .Scan()
}
:= reflect.ValueOf()
if .Kind() != reflect.Ptr {
return errors.New("destination not a pointer")
}
if .IsNil() {
return errNilPtr
}
if !.IsValid() {
= reflect.ValueOf()
}
:= reflect.Indirect()
if .IsValid() && .Type().AssignableTo(.Type()) {
switch b := .(type) {
case []byte:
.Set(reflect.ValueOf(cloneBytes()))
default:
.Set()
}
return nil
}
if .Kind() == .Kind() && .Type().ConvertibleTo(.Type()) {
.Set(.Convert(.Type()))
return nil
}
switch .Kind() {
case reflect.Ptr:
if == nil {
.Set(reflect.Zero(.Type()))
return nil
}
.Set(reflect.New(.Type().Elem()))
return (.Interface(), , )
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if == nil {
return fmt.Errorf("converting NULL to %s is unsupported", .Kind())
}
:= asString()
, := strconv.ParseInt(, 10, .Type().Bits())
if != nil {
= strconvErr()
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", , , .Kind(), )
}
.SetInt()
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if == nil {
return fmt.Errorf("converting NULL to %s is unsupported", .Kind())
}
:= asString()
, := strconv.ParseUint(, 10, .Type().Bits())
if != nil {
= strconvErr()
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", , , .Kind(), )
}
.SetUint()
return nil
case reflect.Float32, reflect.Float64:
if == nil {
return fmt.Errorf("converting NULL to %s is unsupported", .Kind())
}
:= asString()
, := strconv.ParseFloat(, .Type().Bits())
if != nil {
= strconvErr()
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", , , .Kind(), )
}
.SetFloat()
return nil
case reflect.String:
if == nil {
return fmt.Errorf("converting NULL to %s is unsupported", .Kind())
}
switch v := .(type) {
case string:
.SetString()
return nil
case []byte:
.SetString(string())
return nil
}
}
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", , )
}
func ( error) error {
if , := .(*strconv.NumError); {
return .Err
}
return
}
func ( []byte) []byte {
if == nil {
return nil
}
:= make([]byte, len())
copy(, )
return
}
func ( interface{}) string {
switch v := .(type) {
case string:
return
case []byte:
return string()
}
:= reflect.ValueOf()
switch .Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(.Uint(), 10)
case reflect.Float64:
return strconv.FormatFloat(.Float(), 'g', -1, 64)
case reflect.Float32:
return strconv.FormatFloat(.Float(), 'g', -1, 32)
case reflect.Bool:
return strconv.FormatBool(.Bool())
}
return fmt.Sprintf("%v", )
}
func ( []byte, reflect.Value) ( []byte, bool) {
switch .Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.AppendInt(, .Int(), 10), true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.AppendUint(, .Uint(), 10), true
case reflect.Float32:
return strconv.AppendFloat(, .Float(), 'g', -1, 32), true
case reflect.Float64:
return strconv.AppendFloat(, .Float(), 'g', -1, 64), true
case reflect.Bool:
return strconv.AppendBool(, .Bool()), true
case reflect.String:
:= .String()
return append(, ...), true
}
return
}
var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
type decimal interface {
decimalDecompose
decimalCompose
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |