Fix NULL being lost when scanning unknown OID into sql.Scanner
https://github.com/jackc/pgx/issues/1078
This commit is contained in:
@@ -588,7 +588,11 @@ type scanPlanSQLScanner struct{}
|
|||||||
|
|
||||||
func (scanPlanSQLScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
func (scanPlanSQLScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||||
scanner := dst.(sql.Scanner)
|
scanner := dst.(sql.Scanner)
|
||||||
if formatCode == BinaryFormatCode {
|
if src == nil {
|
||||||
|
// This is necessary because interface value []byte:nil does not equal nil:nil for the binary format path and the
|
||||||
|
// text format path would be converted to empty string.
|
||||||
|
return scanner.Scan(nil)
|
||||||
|
} else if formatCode == BinaryFormatCode {
|
||||||
return scanner.Scan(src)
|
return scanner.Scan(src)
|
||||||
} else {
|
} else {
|
||||||
return scanner.Scan(string(src))
|
return scanner.Scan(string(src))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package pgtype_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -211,6 +212,16 @@ func TestConnInfoScanUnknownOIDTextFormat(t *testing.T) {
|
|||||||
assert.EqualValues(t, 123, n)
|
assert.EqualValues(t, 123, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnInfoScanUnknownOIDIntoSQLScanner(t *testing.T) {
|
||||||
|
ci := pgtype.NewConnInfo()
|
||||||
|
|
||||||
|
var s sql.NullString
|
||||||
|
err := ci.Scan(0, pgx.TextFormatCode, []byte(nil), &s)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "", s.String)
|
||||||
|
assert.False(t, s.Valid)
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkConnInfoScanInt4IntoBinaryDecoder(b *testing.B) {
|
func BenchmarkConnInfoScanInt4IntoBinaryDecoder(b *testing.B) {
|
||||||
ci := pgtype.NewConnInfo()
|
ci := pgtype.NewConnInfo()
|
||||||
src := []byte{0, 0, 0, 42}
|
src := []byte{0, 0, 0, 42}
|
||||||
|
|||||||
Reference in New Issue
Block a user