From b78ecf26dc565f1733cf59373b4903dc3923c30b Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 12 Apr 2019 21:23:57 -0500 Subject: [PATCH] Fix encode empty value --- conn.go | 9 ++++++--- pgtype/testutil/testutil.go | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/conn.go b/conn.go index 05b07e11..6ef1b784 100644 --- a/conn.go +++ b/conn.go @@ -834,11 +834,14 @@ func newencodePreparedStatementArgument(ci *pgtype.ConnInfo, oid pgtype.OID, arg return nil, nil } + // TODO - don't allocate a new buf for each encoded prepared statement. The empty slice is necessary because otherwise empty strings may be encoded as []byte(nil) instead of []byte{} + buf := make([]byte, 0) + switch arg := arg.(type) { case pgtype.BinaryEncoder: - return arg.EncodeBinary(ci, nil) + return arg.EncodeBinary(ci, buf) case pgtype.TextEncoder: - return arg.EncodeText(ci, nil) + return arg.EncodeText(ci, buf) case string: return []byte(arg), nil } @@ -870,7 +873,7 @@ func newencodePreparedStatementArgument(ci *pgtype.ConnInfo, oid pgtype.OID, arg return nil, err } - return value.(pgtype.BinaryEncoder).EncodeBinary(ci, nil) + return value.(pgtype.BinaryEncoder).EncodeBinary(ci, buf) } if strippedArg, ok := stripNamedType(&refVal); ok { diff --git a/pgtype/testutil/testutil.go b/pgtype/testutil/testutil.go index 2cde9961..6ea3a69e 100644 --- a/pgtype/testutil/testutil.go +++ b/pgtype/testutil/testutil.go @@ -137,7 +137,8 @@ func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values [] } result := reflect.New(reflect.TypeOf(derefV)) - err := conn.QueryRow(context.Background(), "test", ForceEncoder(v, fc.formatCode)).Scan(result.Interface()) + + err := conn.QueryRow(context.Background(), "test", vEncoder).Scan(result.Interface()) if err != nil { t.Errorf("%v %d: %v", fc.name, i, err) }