Fix hstore NULL versus empty

When running queries with the hstore type registered, and with simple
mode queries, the scan implementation does not correctly distinguish
between NULL and empty. Fix the implementation and add a test to
verify this.
This commit is contained in:
Evan Jones 2023-05-10 22:33:34 -04:00 committed by Jack Christensen
parent f59e8bf555
commit 2a86501e86
2 changed files with 25 additions and 2 deletions

View File

@ -174,7 +174,7 @@ func (scanPlanBinaryHstoreToHstoreScanner) Scan(src []byte, dst any) error {
scanner := (dst).(HstoreScanner)
if src == nil {
return scanner.ScanHstore(Hstore{})
return scanner.ScanHstore(Hstore(nil))
}
rp := 0
@ -234,7 +234,7 @@ func (scanPlanTextAnyToHstoreScanner) Scan(src []byte, dst any) error {
scanner := (dst).(HstoreScanner)
if src == nil {
return scanner.ScanHstore(Hstore{})
return scanner.ScanHstore(Hstore(nil))
}
keys, values, err := parseHstore(string(src))

View File

@ -2,6 +2,7 @@ package pgtype_test
import (
"context"
"reflect"
"testing"
"github.com/jackc/pgx/v5"
@ -179,4 +180,26 @@ func TestHstoreCodec(t *testing.T) {
}
pgxtest.RunValueRoundTripTests(context.Background(), t, ctr, pgxtest.KnownOIDQueryExecModes, "hstore", tests)
// scan empty and NULL: should be different
pgxtest.RunWithQueryExecModes(context.Background(), t, ctr, pgxtest.AllQueryExecModes, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
h := pgtype.Hstore{"should_be_erased": nil}
err := conn.QueryRow(ctx, `select cast(null as hstore)`).Scan(&h)
if err != nil {
t.Fatal(err)
}
expectedNil := pgtype.Hstore(nil)
if !reflect.DeepEqual(h, expectedNil) {
t.Errorf("plain conn.Scan failed expectedNil=%#v actual=%#v", expectedNil, h)
}
err = conn.QueryRow(ctx, `select cast('' as hstore)`).Scan(&h)
if err != nil {
t.Fatal(err)
}
expectedEmpty := pgtype.Hstore{}
if !reflect.DeepEqual(h, expectedEmpty) {
t.Errorf("plain conn.Scan failed expectedEmpty=%#v actual=%#v", expectedEmpty, h)
}
})
}