2
0

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

https://github.com/jackc/pgx/issues/1078
This commit is contained in:
Jack Christensen
2021-09-11 10:59:26 -05:00
parent 90af821478
commit 693c7c7f7d
2 changed files with 16 additions and 1 deletions
+5 -1
View File
@@ -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))
+11
View File
@@ -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}