Fix NULL being lost when scanning unknown OID into sql.Scanner

https://github.com/jackc/pgx/issues/1078
non-blocking
Jack Christensen 2021-09-11 10:59:26 -05:00
parent 90af821478
commit 693c7c7f7d
2 changed files with 16 additions and 1 deletions

View File

@ -588,7 +588,11 @@ type scanPlanSQLScanner struct{}
func (scanPlanSQLScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
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)
} else {
return scanner.Scan(string(src))

View File

@ -2,6 +2,7 @@ package pgtype_test
import (
"bytes"
"database/sql"
"errors"
"net"
"testing"
@ -211,6 +212,16 @@ func TestConnInfoScanUnknownOIDTextFormat(t *testing.T) {
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) {
ci := pgtype.NewConnInfo()
src := []byte{0, 0, 0, 42}