Fix decoding row with same type values

Row decoding was reusing and returning connection owned values for
decoding. Instead allocate new value each time.

fixes 
pull/315/head
Jack Christensen 2017-08-29 15:37:22 -05:00
parent ef9e5159bf
commit 47c0e9cbac
3 changed files with 22 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package pgtype
import (
"encoding/binary"
"reflect"
"github.com/pkg/errors"
)
@ -111,6 +112,9 @@ func (dst *Record) DecodeBinary(ci *ConnInfo, src []byte) error {
rp += fieldLen
}
// Duplicate struct to scan into
binaryDecoder = reflect.New(reflect.ValueOf(binaryDecoder).Elem().Type()).Interface().(BinaryDecoder)
if err := binaryDecoder.DecodeBinary(ci, fieldBytes); err != nil {
return err
}

View File

@ -35,6 +35,16 @@ func TestRecordTranscode(t *testing.T) {
Status: pgtype.Present,
},
},
{
sql: `select row(100.0::float4, 1.09::float4)`,
expected: pgtype.Record{
Fields: []pgtype.Value{
&pgtype.Float4{Float: 100, Status: pgtype.Present},
&pgtype.Float4{Float: 1.09, Status: pgtype.Present},
},
Status: pgtype.Present,
},
},
{
sql: `select row('foo'::text, array[1, 2, null, 4]::int4[], 42::int4)`,
expected: pgtype.Record{
@ -87,7 +97,7 @@ func TestRecordTranscode(t *testing.T) {
}
if !reflect.DeepEqual(tt.expected, result) {
t.Errorf("%d: expected %v, got %v", i, tt.expected, result)
t.Errorf("%d: expected %#v, got %#v", i, tt.expected, result)
}
}
}

View File

@ -931,6 +931,13 @@ func TestRowDecode(t *testing.T) {
time.Date(2015, 1, 1, 8, 12, 42, 0, time.UTC).Local(),
},
},
{
"select row(100.0::float, 1.09::float)",
[]interface{}{
float64(100),
float64(1.09),
},
},
}
for i, tt := range tests {