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:
@@ -2,6 +2,8 @@ package pgtype_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
pgx "github.com/jackc/pgx/v5"
|
||||
@@ -124,6 +126,37 @@ func TestArrayCodecAnySlice(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/jackc/pgx/issues/1273#issuecomment-1218262703
|
||||
func TestArrayCodecSliceArgConversion(t *testing.T) {
|
||||
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
|
||||
arg := []string{
|
||||
"3ad95bfd-ecea-4032-83c3-0c823cafb372",
|
||||
"951baf11-c0cc-4afc-a779-abff0611dbf1",
|
||||
"8327f244-7e2f-45e7-a10b-fbdc9d6f3378",
|
||||
}
|
||||
|
||||
var expected []pgtype.UUID
|
||||
|
||||
for _, s := range arg {
|
||||
buf, err := hex.DecodeString(strings.ReplaceAll(s, "-", ""))
|
||||
require.NoError(t, err)
|
||||
var u pgtype.UUID
|
||||
copy(u.Bytes[:], buf)
|
||||
u.Valid = true
|
||||
expected = append(expected, u)
|
||||
}
|
||||
|
||||
var actual []pgtype.UUID
|
||||
err := conn.QueryRow(
|
||||
ctx,
|
||||
"select $1::uuid[]",
|
||||
arg,
|
||||
).Scan(&actual)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, actual)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArrayCodecDecodeValue(t *testing.T) {
|
||||
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, _ testing.TB, conn *pgx.Conn) {
|
||||
for _, tt := range []struct {
|
||||
|
||||
Reference in New Issue
Block a user