2
0

Text formatted values except bytea can be directly scanned to []byte

This significantly improves performance of scanning text to []byte as it
avoids multiple allocations and copies.
This commit is contained in:
Jack Christensen
2020-09-05 11:13:53 -05:00
parent 9da6afcad7
commit e7d2b057a7
2 changed files with 13 additions and 1 deletions
+5 -1
View File
@@ -779,7 +779,7 @@ func (ci *ConnInfo) PlanScan(oid uint32, formatCode int16, dst interface{}) Scan
}
case *[]byte:
switch oid {
case ByteaOID, TextOID, VarcharOID:
case ByteaOID, TextOID, VarcharOID, JSONOID:
return scanPlanBinaryBytes{}
}
case BinaryDecoder:
@@ -789,6 +789,10 @@ func (ci *ConnInfo) PlanScan(oid uint32, formatCode int16, dst interface{}) Scan
switch dst.(type) {
case *string:
return scanPlanString{}
case *[]byte:
if oid != ByteaOID {
return scanPlanBinaryBytes{}
}
case TextDecoder:
return scanPlanDstTextDecoder{}
}
+8
View File
@@ -79,6 +79,14 @@ func TestConnInfoScanTextFormatInterfacePtr(t *testing.T) {
assert.Equal(t, "foo", got)
}
func TestConnInfoScanTextFormatNonByteaIntoByteSlice(t *testing.T) {
ci := pgtype.NewConnInfo()
var got []byte
err := ci.Scan(pgtype.JSONBOID, pgx.TextFormatCode, []byte("{}"), &got)
require.NoError(t, err)
assert.Equal(t, []byte("{}"), got)
}
func TestConnInfoScanBinaryFormatInterfacePtr(t *testing.T) {
ci := pgtype.NewConnInfo()
var got interface{}