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:
@@ -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{}
|
||||
}
|
||||
|
||||
@@ -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{}
|
||||
|
||||
Reference in New Issue
Block a user