mirror of
https://github.com/golang/go
synced 2024-11-19 23:04:40 -07:00
database/sql: convert SQL null values to []byte as nil.
Also allow string values to scan into []byte. Fixes #2788. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/5577054
This commit is contained in:
parent
01afb79c59
commit
2a22f35598
@ -40,6 +40,9 @@ func convertAssign(dest, src interface{}) error {
|
|||||||
case *string:
|
case *string:
|
||||||
*d = s
|
*d = s
|
||||||
return nil
|
return nil
|
||||||
|
case *[]byte:
|
||||||
|
*d = []byte(s)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
case []byte:
|
case []byte:
|
||||||
switch d := dest.(type) {
|
switch d := dest.(type) {
|
||||||
@ -50,6 +53,12 @@ func convertAssign(dest, src interface{}) error {
|
|||||||
*d = s
|
*d = s
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
case nil:
|
||||||
|
switch d := dest.(type) {
|
||||||
|
case *[]byte:
|
||||||
|
*d = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sv reflect.Value
|
var sv reflect.Value
|
||||||
|
@ -904,6 +904,12 @@ func (rs *Rows) Scan(dest ...interface{}) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
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 {
|
if _, ok = dp.(*RawBytes); ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -945,17 +951,10 @@ func (r *Row) Scan(dest ...interface{}) error {
|
|||||||
if r.err != nil {
|
if r.err != nil {
|
||||||
return r.err
|
return r.err
|
||||||
}
|
}
|
||||||
defer r.rows.Close()
|
|
||||||
if !r.rows.Next() {
|
|
||||||
return ErrNoRows
|
|
||||||
}
|
|
||||||
err := r.rows.Scan(dest...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(bradfitz): for now we need to defensively clone all
|
// TODO(bradfitz): for now we need to defensively clone all
|
||||||
// []byte that the driver returned, since we're about to close
|
// []byte that the driver returned (not permitting
|
||||||
|
// *RawBytes in Rows.Scan), since we're about to close
|
||||||
// the Rows in our defer, when we return from this function.
|
// the Rows in our defer, when we return from this function.
|
||||||
// the contract with the driver.Next(...) interface is that it
|
// the contract with the driver.Next(...) interface is that it
|
||||||
// can return slices into read-only temporary memory that's
|
// can return slices into read-only temporary memory that's
|
||||||
@ -970,14 +969,17 @@ func (r *Row) Scan(dest ...interface{}) error {
|
|||||||
if _, ok := dp.(*RawBytes); ok {
|
if _, ok := dp.(*RawBytes); ok {
|
||||||
return errors.New("sql: RawBytes isn't allowed on Row.Scan")
|
return errors.New("sql: RawBytes isn't allowed on Row.Scan")
|
||||||
}
|
}
|
||||||
b, ok := dp.(*[]byte)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
clone := make([]byte, len(*b))
|
|
||||||
copy(clone, *b)
|
|
||||||
*b = clone
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer r.rows.Close()
|
||||||
|
if !r.rows.Next() {
|
||||||
|
return ErrNoRows
|
||||||
|
}
|
||||||
|
err := r.rows.Scan(dest...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -358,6 +358,34 @@ func TestIssue2542Deadlock(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests fix for issue 2788, that we bind nil to a []byte if the
|
||||||
|
// value in the column is sql null
|
||||||
|
func TestNullByteSlice(t *testing.T) {
|
||||||
|
db := newTestDB(t, "")
|
||||||
|
defer closeDB(t, db)
|
||||||
|
exec(t, db, "CREATE|t|id=int32,name=nullstring")
|
||||||
|
exec(t, db, "INSERT|t|id=10,name=?", nil)
|
||||||
|
|
||||||
|
var name []byte
|
||||||
|
|
||||||
|
err := db.QueryRow("SELECT|t|name|id=?", 10).Scan(&name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if name != nil {
|
||||||
|
t.Fatalf("name []byte should be nil for null column value, got: %#v", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
exec(t, db, "INSERT|t|id=11,name=?", "bob")
|
||||||
|
err = db.QueryRow("SELECT|t|name|id=?", 11).Scan(&name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(name) != "bob" {
|
||||||
|
t.Fatalf("name []byte should be bob, got: %q", string(name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestQueryRowClosingStmt(t *testing.T) {
|
func TestQueryRowClosingStmt(t *testing.T) {
|
||||||
db := newTestDB(t, "people")
|
db := newTestDB(t, "people")
|
||||||
defer closeDB(t, db)
|
defer closeDB(t, db)
|
||||||
|
Loading…
Reference in New Issue
Block a user