mirror of
https://github.com/golang/go
synced 2024-11-12 05:40:22 -07:00
database/sql: optimized []byte copy + []byte(nil) -> *interface fix
Make the copy directly in the convert switch instead of an extra loop. Also stops converting nil-[]byte to zero-[]byte when assigning to *interface R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/7962044
This commit is contained in:
parent
4529c047b8
commit
5e74f5029b
@ -112,15 +112,13 @@ func convertAssign(dest, src interface{}) error {
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
bcopy := make([]byte, len(s))
|
||||
copy(bcopy, s)
|
||||
*d = bcopy
|
||||
*d = cloneBytes(s)
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = s
|
||||
*d = cloneBytes(s)
|
||||
return nil
|
||||
case *RawBytes:
|
||||
if d == nil {
|
||||
@ -131,6 +129,12 @@ func convertAssign(dest, src interface{}) error {
|
||||
}
|
||||
case nil:
|
||||
switch d := dest.(type) {
|
||||
case *interface{}:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = nil
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
@ -250,6 +254,16 @@ func convertAssign(dest, src interface{}) error {
|
||||
return fmt.Errorf("unsupported driver -> Scan pair: %T -> %T", src, dest)
|
||||
}
|
||||
|
||||
func cloneBytes(b []byte) []byte {
|
||||
if b == nil {
|
||||
return nil
|
||||
} else {
|
||||
c := make([]byte, len(b))
|
||||
copy(c, b)
|
||||
return c
|
||||
}
|
||||
}
|
||||
|
||||
func asString(src interface{}) string {
|
||||
switch v := src.(type) {
|
||||
case string:
|
||||
|
@ -143,6 +143,7 @@ var conversionTests = []conversionTest{
|
||||
{s: []byte("byteslice"), d: &scaniface, wantiface: []byte("byteslice")},
|
||||
{s: true, d: &scaniface, wantiface: true},
|
||||
{s: nil, d: &scaniface},
|
||||
{s: []byte(nil), d: &scaniface, wantiface: []byte(nil)},
|
||||
}
|
||||
|
||||
func intPtrValue(intptr interface{}) interface{} {
|
||||
@ -221,7 +222,7 @@ func TestConversions(t *testing.T) {
|
||||
}
|
||||
if srcBytes, ok := ct.s.([]byte); ok {
|
||||
dstBytes := (*ifptr).([]byte)
|
||||
if &dstBytes[0] == &srcBytes[0] {
|
||||
if len(srcBytes) > 0 && &dstBytes[0] == &srcBytes[0] {
|
||||
errf("copy into interface{} didn't copy []byte data")
|
||||
}
|
||||
}
|
||||
|
@ -1301,24 +1301,6 @@ func (rs *Rows) Scan(dest ...interface{}) error {
|
||||
return fmt.Errorf("sql: Scan error on column index %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
for _, dp := range dest {
|
||||
b, ok := dp.(*[]byte)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if *b == nil {
|
||||
// If the []byte is now nil (for a NULL value),
|
||||
// don't fall through to below which would
|
||||
// turn it into a non-nil 0-length byte slice
|
||||
continue
|
||||
}
|
||||
if _, ok = dp.(*RawBytes); ok {
|
||||
continue
|
||||
}
|
||||
clone := make([]byte, len(*b))
|
||||
copy(clone, *b)
|
||||
*b = clone
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user