refactor
This commit is contained in:
parent
2d5f13a854
commit
a0042a7117
37
convert.go
37
convert.go
|
@ -247,3 +247,40 @@ func convertAssign(dest, src interface{}) error {
|
||||||
|
|
||||||
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
|
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func asKind(vv reflect.Value, tp reflect.Type) (interface{}, error) {
|
||||||
|
switch tp.Kind() {
|
||||||
|
case reflect.Int64:
|
||||||
|
return vv.Int(), nil
|
||||||
|
case reflect.Int:
|
||||||
|
return int(vv.Int()), nil
|
||||||
|
case reflect.Int32:
|
||||||
|
return int32(vv.Int()), nil
|
||||||
|
case reflect.Int16:
|
||||||
|
return int16(vv.Int()), nil
|
||||||
|
case reflect.Int8:
|
||||||
|
return int8(vv.Int()), nil
|
||||||
|
case reflect.Uint64:
|
||||||
|
return vv.Uint(), nil
|
||||||
|
case reflect.Uint:
|
||||||
|
return uint(vv.Uint()), nil
|
||||||
|
case reflect.Uint32:
|
||||||
|
return uint32(vv.Uint()), nil
|
||||||
|
case reflect.Uint16:
|
||||||
|
return uint16(vv.Uint()), nil
|
||||||
|
case reflect.Uint8:
|
||||||
|
return uint8(vv.Uint()), nil
|
||||||
|
case reflect.String:
|
||||||
|
return vv.String(), nil
|
||||||
|
case reflect.Slice:
|
||||||
|
if tp.Elem().Kind() == reflect.Uint8 {
|
||||||
|
v, err := strconv.ParseInt(string(vv.Interface().([]byte)), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unsupported primary key type: %v, %v", tp, vv)
|
||||||
|
}
|
||||||
|
|
33
session.go
33
session.go
|
@ -11,7 +11,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -614,34 +613,10 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i
|
||||||
panic("unsupported non or composited primary key cascade")
|
panic("unsupported non or composited primary key cascade")
|
||||||
}
|
}
|
||||||
var pk = make(core.PK, len(table.PrimaryKeys))
|
var pk = make(core.PK, len(table.PrimaryKeys))
|
||||||
|
var err error
|
||||||
switch rawValueType.Kind() {
|
pk[0], err = asKind(vv, rawValueType)
|
||||||
case reflect.Int64:
|
if err != nil {
|
||||||
pk[0] = vv.Int()
|
return nil, err
|
||||||
case reflect.Int:
|
|
||||||
pk[0] = int(vv.Int())
|
|
||||||
case reflect.Int32:
|
|
||||||
pk[0] = int32(vv.Int())
|
|
||||||
case reflect.Int16:
|
|
||||||
pk[0] = int16(vv.Int())
|
|
||||||
case reflect.Int8:
|
|
||||||
pk[0] = int8(vv.Int())
|
|
||||||
case reflect.Uint64:
|
|
||||||
pk[0] = vv.Uint()
|
|
||||||
case reflect.Uint:
|
|
||||||
pk[0] = uint(vv.Uint())
|
|
||||||
case reflect.Uint32:
|
|
||||||
pk[0] = uint32(vv.Uint())
|
|
||||||
case reflect.Uint16:
|
|
||||||
pk[0] = uint16(vv.Uint())
|
|
||||||
case reflect.Uint8:
|
|
||||||
pk[0] = uint8(vv.Uint())
|
|
||||||
case reflect.String:
|
|
||||||
pk[0] = vv.String()
|
|
||||||
case reflect.Slice:
|
|
||||||
pk[0], _ = strconv.ParseInt(string(rawValue.Interface().([]byte)), 10, 64)
|
|
||||||
default:
|
|
||||||
panic(fmt.Sprintf("unsupported primary key type: %v, %v", rawValueType, fieldValue))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isPKZero(pk) {
|
if !isPKZero(pk) {
|
||||||
|
|
Loading…
Reference in New Issue