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:
|
case *[]byte:
|
||||||
switch oid {
|
switch oid {
|
||||||
case ByteaOID, TextOID, VarcharOID:
|
case ByteaOID, TextOID, VarcharOID, JSONOID:
|
||||||
return scanPlanBinaryBytes{}
|
return scanPlanBinaryBytes{}
|
||||||
}
|
}
|
||||||
case BinaryDecoder:
|
case BinaryDecoder:
|
||||||
@@ -789,6 +789,10 @@ func (ci *ConnInfo) PlanScan(oid uint32, formatCode int16, dst interface{}) Scan
|
|||||||
switch dst.(type) {
|
switch dst.(type) {
|
||||||
case *string:
|
case *string:
|
||||||
return scanPlanString{}
|
return scanPlanString{}
|
||||||
|
case *[]byte:
|
||||||
|
if oid != ByteaOID {
|
||||||
|
return scanPlanBinaryBytes{}
|
||||||
|
}
|
||||||
case TextDecoder:
|
case TextDecoder:
|
||||||
return scanPlanDstTextDecoder{}
|
return scanPlanDstTextDecoder{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,6 +79,14 @@ func TestConnInfoScanTextFormatInterfacePtr(t *testing.T) {
|
|||||||
assert.Equal(t, "foo", got)
|
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) {
|
func TestConnInfoScanBinaryFormatInterfacePtr(t *testing.T) {
|
||||||
ci := pgtype.NewConnInfo()
|
ci := pgtype.NewConnInfo()
|
||||||
var got interface{}
|
var got interface{}
|
||||||
|
|||||||
Reference in New Issue
Block a user