diff options
| author | James P. Cooper <jamespcooper@gmail.com> | 2012-01-26 15:12:48 -0800 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2012-01-26 15:12:48 -0800 |
| commit | 2a22f35598bba353f13d4808b4c4d710fa125f43 (patch) | |
| tree | ba4b61a52b8e0e9c01847f8dc1b5d91a0029e65a /src/pkg/database/sql/sql.go | |
| parent | 01afb79c5960c746238c21b1eadc222af85d7c19 (diff) | |
| download | go-2a22f35598bba353f13d4808b4c4d710fa125f43.tar.xz | |
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
Diffstat (limited to 'src/pkg/database/sql/sql.go')
| -rw-r--r-- | src/pkg/database/sql/sql.go | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/pkg/database/sql/sql.go b/src/pkg/database/sql/sql.go index 7e226b17dc..34a7652105 100644 --- a/src/pkg/database/sql/sql.go +++ b/src/pkg/database/sql/sql.go @@ -904,6 +904,12 @@ func (rs *Rows) Scan(dest ...interface{}) error { 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 } @@ -945,17 +951,10 @@ func (r *Row) Scan(dest ...interface{}) error { if r.err != nil { 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 - // []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 contract with the driver.Next(...) interface is that it // 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 { 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 } |
