2
0

Fix encode empty value

This commit is contained in:
Jack Christensen
2019-04-12 21:23:57 -05:00
parent 072391f4a8
commit b78ecf26dc
2 changed files with 8 additions and 4 deletions
+6 -3
View File
@@ -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 {
+2 -1
View File
@@ -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)
}