Source File
types.go
Belonging Package
database/sql/driver
package driver
import (
)
var Bool boolType
type boolType struct{}
var _ ValueConverter = boolType{}
func (boolType) () string { return "Bool" }
func (boolType) ( interface{}) (Value, error) {
switch s := .(type) {
case bool:
return , nil
case string:
, := strconv.ParseBool()
if != nil {
return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", )
}
return , nil
case []byte:
, := strconv.ParseBool(string())
if != nil {
return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", )
}
return , nil
}
:= reflect.ValueOf()
switch .Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
:= .Int()
if == 1 || == 0 {
return == 1, nil
}
return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", )
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
:= .Uint()
if == 1 || == 0 {
return == 1, nil
}
return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", )
}
return nil, fmt.Errorf("sql/driver: couldn't convert %v (%T) into type bool", , )
}
var Int32 int32Type
type int32Type struct{}
var _ ValueConverter = int32Type{}
func (int32Type) ( interface{}) (Value, error) {
:= reflect.ValueOf()
switch .Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
:= .Int()
if > (1<<31)-1 || < -(1<<31) {
return nil, fmt.Errorf("sql/driver: value %d overflows int32", )
}
return , nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
:= .Uint()
if > (1<<31)-1 {
return nil, fmt.Errorf("sql/driver: value %d overflows int32", )
}
return int64(), nil
case reflect.String:
, := strconv.Atoi(.String())
if != nil {
return nil, fmt.Errorf("sql/driver: value %q can't be converted to int32", )
}
return int64(), nil
}
return nil, fmt.Errorf("sql/driver: unsupported value %v (type %T) converting to int32", , )
}
var String stringType
type stringType struct{}
func (stringType) ( interface{}) (Value, error) {
switch .(type) {
case string, []byte:
return , nil
}
return fmt.Sprintf("%v", ), nil
}
var DefaultParameterConverter defaultConverter
type defaultConverter struct{}
var _ ValueConverter = defaultConverter{}
var valuerReflectType = reflect.TypeOf((*Valuer)(nil)).Elem()
func ( Valuer) ( Value, error) {
if := reflect.ValueOf(); .Kind() == reflect.Ptr &&
.IsNil() &&
.Type().Elem().Implements(valuerReflectType) {
return nil, nil
}
return .Value()
}
func (defaultConverter) ( interface{}) (Value, error) {
if IsValue() {
return , nil
}
switch vr := .(type) {
case Valuer:
, := callValuerValue()
if != nil {
return nil,
}
if !IsValue() {
return nil, fmt.Errorf("non-Value type %T returned from Value", )
}
return , nil
case decimalDecompose:
return , nil
}
:= reflect.ValueOf()
switch .Kind() {
if .IsNil() {
return nil, nil
} else {
return defaultConverter{}.(.Elem().Interface())
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return .Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
return int64(.Uint()), nil
case reflect.Uint64:
:= .Uint()
if >= 1<<63 {
return nil, fmt.Errorf("uint64 values with high bit set are not supported")
}
return int64(), nil
case reflect.Float32, reflect.Float64:
return .Float(), nil
case reflect.Bool:
return .Bool(), nil
case reflect.Slice:
:= .Type().Elem().Kind()
if == reflect.Uint8 {
return .Bytes(), nil
}
return nil, fmt.Errorf("unsupported type %T, a slice of %s", , )
case reflect.String:
return .String(), nil
}
return nil, fmt.Errorf("unsupported type %T, a %s", , .Kind())
}
![]() |
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. |