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 #313
This commit is contained in:
Jack Christensen 2017-08-29 15:37:22 -05:00
parent 703ce85513
commit 2e630dddf9
2 changed files with 15 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package pgtype
import ( import (
"encoding/binary" "encoding/binary"
"reflect"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -111,6 +112,9 @@ func (dst *Record) DecodeBinary(ci *ConnInfo, src []byte) error {
rp += fieldLen 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 { if err := binaryDecoder.DecodeBinary(ci, fieldBytes); err != nil {
return err return err
} }

View File

@ -35,6 +35,16 @@ func TestRecordTranscode(t *testing.T) {
Status: pgtype.Present, 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)`, sql: `select row('foo'::text, array[1, 2, null, 4]::int4[], 42::int4)`,
expected: pgtype.Record{ expected: pgtype.Record{
@ -87,7 +97,7 @@ func TestRecordTranscode(t *testing.T) {
} }
if !reflect.DeepEqual(tt.expected, result) { 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)
} }
} }
} }