2
0

Fix: driver.Value representation of bytea should be []byte not string

https://github.com/jackc/pgx/issues/1445
This commit is contained in:
Jack Christensen
2022-12-21 17:54:42 -06:00
parent 29ad306e47
commit d737852654
3 changed files with 29 additions and 1 deletions
+21
View File
@@ -3,6 +3,7 @@ package pgtype_test
import (
"bytes"
"context"
"fmt"
"testing"
pgx "github.com/jackc/pgx/v5"
@@ -114,3 +115,23 @@ func TestUndecodedBytes(t *testing.T) {
require.Equal(t, buf, []byte{0, 0, 0, 1})
})
}
func TestByteaCodecDecodeDatabaseSQLValue(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
var buf []byte
err := conn.QueryRow(ctx, `select '\xa1b2c3d4'::bytea`).Scan(sqlScannerFunc(func(src any) error {
switch src := src.(type) {
case []byte:
buf = make([]byte, len(src))
copy(buf, src)
return nil
default:
return fmt.Errorf("expected []byte, got %T", src)
}
}))
require.NoError(t, err)
require.Len(t, buf, 4)
require.Equal(t, buf, []byte{0xa1, 0xb2, 0xc3, 0xd4})
})
}