Fix sqlScannerWrapper NULL handling

https://github.com/jackc/pgx/issues/1312
pull/1372/head
Jack Christensen 2022-09-24 10:29:44 -05:00
parent ac9d4f4d96
commit 335c8621ff
2 changed files with 13 additions and 7 deletions

View File

@ -1921,13 +1921,15 @@ func (w *sqlScannerWrapper) Scan(src any) error {
}
var bufSrc []byte
switch src := src.(type) {
case string:
bufSrc = []byte(src)
case []byte:
bufSrc = src
default:
bufSrc = []byte(fmt.Sprint(bufSrc))
if src != nil {
switch src := src.(type) {
case string:
bufSrc = []byte(src)
case []byte:
bufSrc = src
default:
bufSrc = []byte(fmt.Sprint(bufSrc))
}
}
return w.m.Scan(t.OID, TextFormatCode, bufSrc, w.v)

View File

@ -394,6 +394,10 @@ func TestConnQueryScanArray(t *testing.T) {
err := db.QueryRow("select '{1,2,3}'::bigint[]").Scan(m.SQLScanner(&a))
require.NoError(t, err)
assert.Equal(t, pgtype.Array[int64]{Elements: []int64{1, 2, 3}, Dims: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}}, Valid: true}, a)
err = db.QueryRow("select null::bigint[]").Scan(m.SQLScanner(&a))
require.NoError(t, err)
assert.Equal(t, pgtype.Array[int64]{Elements: nil, Dims: nil, Valid: false}, a)
})
}