Add DecodeValue and DecodeDatabaseSQLValue for ArrayCodec

query-exec-mode
Jack Christensen 2022-01-31 20:39:50 -06:00
parent 558748ef9c
commit ef7114a8ce
2 changed files with 53 additions and 12 deletions

View File

@ -332,26 +332,29 @@ func (spac *scanPlanArrayCodec) Scan(src []byte, dst interface{}) error {
}
}
func (c ArrayCodec) DecodeDatabaseSQLValue(ci *ConnInfo, oid uint32, format int16, src []byte) (driver.Value, error) {
func (c *ArrayCodec) DecodeDatabaseSQLValue(ci *ConnInfo, oid uint32, format int16, src []byte) (driver.Value, error) {
if src == nil {
return nil, nil
}
// var n int64
// err := c.PlanScan(ci, oid, format, &n, true).Scan(ci, oid, format, src, &n)
// return n, err
return nil, fmt.Errorf("not implemented")
switch format {
case TextFormatCode:
return string(src), nil
case BinaryFormatCode:
buf := make([]byte, len(src))
copy(buf, src)
return buf, nil
default:
return nil, fmt.Errorf("unknown format code %d", format)
}
}
func (c ArrayCodec) DecodeValue(ci *ConnInfo, oid uint32, format int16, src []byte) (interface{}, error) {
func (c *ArrayCodec) DecodeValue(ci *ConnInfo, oid uint32, format int16, src []byte) (interface{}, error) {
if src == nil {
return nil, nil
}
// var n int16
// err := c.PlanScan(ci, oid, format, &n, true).Scan(ci, oid, format, src, &n)
// return n, err
return nil, fmt.Errorf("not implemented")
var slice []interface{}
err := ci.PlanScan(oid, format, &slice).Scan(src, &slice)
return slice, err
}

View File

@ -6,6 +6,7 @@ import (
"github.com/jackc/pgx/v5/pgtype/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestArrayCodec(t *testing.T) {
@ -70,3 +71,40 @@ func TestArrayCodecAnySlice(t *testing.T) {
assert.Equalf(t, tt.expected, actual, "%d", i)
}
}
func TestArrayCodecDecodeValue(t *testing.T) {
conn := testutil.MustConnectPgx(t)
defer testutil.MustCloseContext(t, conn)
for _, tt := range []struct {
sql string
expected interface{}
}{
{
sql: `select '{}'::int4[]`,
expected: []interface{}{},
},
{
sql: `select '{1,2}'::int8[]`,
expected: []interface{}{int64(1), int64(2)},
},
{
sql: `select '{foo,bar}'::text[]`,
expected: []interface{}{"foo", "bar"},
},
} {
t.Run(tt.sql, func(t *testing.T) {
rows, err := conn.Query(context.Background(), tt.sql)
require.NoError(t, err)
for rows.Next() {
values, err := rows.Values()
require.NoError(t, err)
require.Len(t, values, 1)
require.Equal(t, tt.expected, values[0])
}
require.NoError(t, rows.Err())
})
}
}