2
0

Fallback to other format when encoding query arguments

The preferred format may not be possible for certain arguments. For
example, the preferred format for numeric is binary. But if
shopspring/decimal is being used without jackc/pgx-shopspring-decimal
then it will use the database/sql/driver.Valuer interface. This will
return a string. That string should be sent in the text format.

A similar case occurs when encoding a []string into a non-text
PostgreSQL array such as uuid[].
This commit is contained in:
Jack Christensen
2022-08-22 20:06:42 -05:00
parent ae65a8007b
commit 0d5d8e0137
3 changed files with 75 additions and 3 deletions
+21 -1
View File
@@ -1165,7 +1165,7 @@ func TestConnQueryDatabaseSQLDriverValuerWithAutoGeneratedPointerReceiver(t *tes
ensureConnValid(t, conn)
}
func TestConnQueryDatabaseSQLDriverValuerWithBinaryPgTypeThatAcceptsSameType(t *testing.T) {
func TestConnQueryDatabaseSQLDriverScannerWithBinaryPgTypeThatAcceptsSameType(t *testing.T) {
t.Parallel()
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
@@ -1181,6 +1181,26 @@ func TestConnQueryDatabaseSQLDriverValuerWithBinaryPgTypeThatAcceptsSameType(t *
ensureConnValid(t, conn)
}
// https://github.com/jackc/pgx/issues/1273#issuecomment-1221672175
func TestConnQueryDatabaseSQLDriverValuerTextWhenBinaryIsPreferred(t *testing.T) {
t.Parallel()
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
defer closeConn(t, conn)
arg := sql.NullString{String: "1.234", Valid: true}
var result pgtype.Numeric
err := conn.QueryRow(context.Background(), "select $1::numeric", arg).Scan(&result)
require.NoError(t, err)
require.True(t, result.Valid)
f64, err := result.Float64Value()
require.NoError(t, err)
require.Equal(t, pgtype.Float8{Float64: 1.234, Valid: true}, f64)
ensureConnValid(t, conn)
}
func TestConnQueryDatabaseSQLNullX(t *testing.T) {
t.Parallel()