Add Rows.Values support for unknown types

pull/668/head
Jack Christensen 2020-01-23 20:50:21 -06:00
parent f6feb39655
commit fd4e692323
2 changed files with 45 additions and 1 deletions

View File

@ -197,6 +197,33 @@ func TestConnQueryValuesWhenUnableToDecode(t *testing.T) {
require.Equal(t, "({1},)", values[0])
}
func TestConnQueryValuesWithUnknownOID(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
defer closeConn(t, conn)
tx, err := conn.Begin(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx)
_, err = tx.Exec(ctx, "create type fruit as enum('orange', 'apple', 'pear')")
require.NoError(t, err)
rows, err := conn.Query(context.Background(), "select 'orange'::fruit")
require.NoError(t, err)
defer rows.Close()
require.True(t, rows.Next())
values, err := rows.Values()
require.NoError(t, err)
require.Equal(t, "orange", values[0])
}
// https://github.com/jackc/pgx/issues/478
func TestConnQueryReadRowMultipleTimes(t *testing.T) {
t.Parallel()

19
rows.go
View File

@ -237,7 +237,24 @@ func (rows *connRows) Values() ([]interface{}, error) {
rows.fatal(errors.New("Unknown format code"))
}
} else {
rows.fatal(errors.New("Unknown type"))
switch fd.Format {
case TextFormatCode:
decoder := &pgtype.GenericText{}
err := decoder.DecodeText(rows.connInfo, buf)
if err != nil {
rows.fatal(err)
}
values = append(values, decoder.Get())
case BinaryFormatCode:
decoder := &pgtype.GenericBinary{}
err := decoder.DecodeBinary(rows.connInfo, buf)
if err != nil {
rows.fatal(err)
}
values = append(values, decoder.Get())
default:
rows.fatal(errors.New("Unknown format code"))
}
}
if rows.Err() != nil {