2
0

Unregistered OIDs are handled the same as unknown OIDs

This improves handling of unregistered types. In general, they should
"just work". But there are performance benefits gained and some edge
cases avoided by registering types. Updated documentation to mention
this.

https://github.com/jackc/pgx/issues/1445
This commit is contained in:
Jack Christensen
2022-12-23 13:11:28 -06:00
parent d737852654
commit 456a242f5c
5 changed files with 121 additions and 34 deletions
+41
View File
@@ -43,6 +43,10 @@ type _float32Slice []float32
type _float64Slice []float64
type _byteSlice []byte
// unregisteredOID represents a actual type that is not registered. Cannot use 0 because that represents that the type
// is not known (e.g. when using the simple protocol).
const unregisteredOID = uint32(1)
func mustParseCIDR(t testing.TB, s string) *net.IPNet {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
@@ -127,6 +131,13 @@ func (f sqlScannerFunc) Scan(src any) error {
return f(src)
}
// driverValuerFunc lets an arbitrary function be used as a driver.Valuer.
type driverValuerFunc func() (driver.Value, error)
func (f driverValuerFunc) Value() (driver.Value, error) {
return f()
}
func TestMapScanNilIsNoOp(t *testing.T) {
m := pgtype.NewMap()
@@ -324,6 +335,36 @@ func TestMapEncodeBinaryFormatDatabaseValuerThatReturnsString(t *testing.T) {
require.Equal(t, []byte{0, 0, 0, 42}, buf)
}
// https://github.com/jackc/pgx/issues/1445
func TestMapEncodeDatabaseValuerThatReturnsStringIntoUnregisteredTypeTextFormat(t *testing.T) {
m := pgtype.NewMap()
buf, err := m.Encode(unregisteredOID, pgtype.TextFormatCode, driverValuerFunc(func() (driver.Value, error) { return "foo", nil }), nil)
require.NoError(t, err)
require.Equal(t, []byte("foo"), buf)
}
// https://github.com/jackc/pgx/issues/1445
func TestMapEncodeDatabaseValuerThatReturnsByteSliceIntoUnregisteredTypeTextFormat(t *testing.T) {
m := pgtype.NewMap()
buf, err := m.Encode(unregisteredOID, pgtype.TextFormatCode, driverValuerFunc(func() (driver.Value, error) { return []byte{0, 1, 2, 3}, nil }), nil)
require.NoError(t, err)
require.Equal(t, []byte(`\x00010203`), buf)
}
func TestMapEncodeStringIntoUnregisteredTypeTextFormat(t *testing.T) {
m := pgtype.NewMap()
buf, err := m.Encode(unregisteredOID, pgtype.TextFormatCode, "foo", nil)
require.NoError(t, err)
require.Equal(t, []byte("foo"), buf)
}
func TestMapEncodeByteSliceIntoUnregisteredTypeTextFormat(t *testing.T) {
m := pgtype.NewMap()
buf, err := m.Encode(unregisteredOID, pgtype.TextFormatCode, []byte{0, 1, 2, 3}, nil)
require.NoError(t, err)
require.Equal(t, []byte(`\x00010203`), buf)
}
// https://github.com/jackc/pgx/issues/1326
func TestMapScanPointerToRenamedType(t *testing.T) {
srcBuf := []byte("foo")