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:
+21
-1
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user