2
0

Composite().Row() helper for working with composites without registration

This commit is contained in:
Maxim Ivanov
2020-04-20 22:38:20 +00:00
parent 04ff904ff5
commit e283f322e1
2 changed files with 30 additions and 0 deletions
+18
View File
@@ -92,6 +92,24 @@ func (src composite) DecodeBinary(ci *ConnInfo, buf []byte) (err error) {
return errors.New("Pass pgtype.Row() to Scan to deconstruct Composite")
}
// Row method creates composite BinaryEncoder. It's main purpose
// is to build composite query argument inplace without registering
// pgtype.Composite in ConnInfo first
func (src composite) Row(values ...interface{}) BinaryEncoderFunc {
return func(ci *ConnInfo, buf []byte) ([]byte, error) {
if len(values) != len(src.fields) {
return nil, errors.Errorf("Number of fields don't match. Composite has %d fields", len(src.fields))
}
for i, v := range values {
if err := src.fields[i].Set(v); err != nil {
return nil, err
}
}
src.status = Present
return src.EncodeBinary(ci, buf)
}
}
// DecodeBinary is called when pgtype.Row() is passed to Scan() to
// deconstruct composite value
func (r rowValue) DecodeBinary(ci *ConnInfo, src []byte) error {
+12
View File
@@ -110,6 +110,18 @@ func BenchmarkBinaryEncodingRow(b *testing.B) {
}
x = buf
}
func BenchmarkBinaryEncodingRowInplace(b *testing.B) {
buf := make([]byte, 0, 128)
ci := pgtype.NewConnInfo()
f1 := 2
f2 := ptrS("bar")
b.ResetTimer()
for n := 0; n < b.N; n++ {
buf, _ = pgtype.Composite(&pgtype.Int4{}, &pgtype.Text{}).Row(f1, f2).EncodeBinary(ci, buf[:0])
}
x = buf
}
var dstRaw MyCompositeRaw