mirror of https://github.com/jackc/pgx.git
Rename Composite to CompositeType.
This harmonizes the naming with EnumType and ArrayType.non-blocking
parent
036101deb5
commit
4a6bd41a36
|
@ -100,7 +100,7 @@ func BenchmarkBinaryEncodingComposite(b *testing.B) {
|
||||||
ci := pgtype.NewConnInfo()
|
ci := pgtype.NewConnInfo()
|
||||||
f1 := 2
|
f1 := 2
|
||||||
f2 := ptrS("bar")
|
f2 := ptrS("bar")
|
||||||
c := pgtype.NewComposite(&pgtype.Int4{}, &pgtype.Text{})
|
c := pgtype.NewCompositeType(&pgtype.Int4{}, &pgtype.Text{})
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
|
@ -164,7 +164,7 @@ func BenchmarkBinaryDecodingCompositeScan(b *testing.B) {
|
||||||
var f1 int
|
var f1 int
|
||||||
var f2 *string
|
var f2 *string
|
||||||
|
|
||||||
c := pgtype.NewComposite(&pgtype.Int4{}, &pgtype.Text{})
|
c := pgtype.NewCompositeType(&pgtype.Int4{}, &pgtype.Text{})
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
|
|
|
@ -7,23 +7,23 @@ import (
|
||||||
errors "golang.org/x/xerrors"
|
errors "golang.org/x/xerrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Composite struct {
|
type CompositeType struct {
|
||||||
fields []Value
|
fields []Value
|
||||||
Status Status
|
Status Status
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewComposite creates a Composite object, which acts as a "schema" for
|
// NewCompositeType creates a Composite object, which acts as a "schema" for
|
||||||
// SQL composite values.
|
// SQL composite values.
|
||||||
// To pass Composite as SQL parameter first set it's fields, either by
|
// To pass Composite as SQL parameter first set it's fields, either by
|
||||||
// passing initialized Value{} instances to NewComposite or by calling
|
// passing initialized Value{} instances to NewCompositeType or by calling
|
||||||
// SetFields method
|
// SetFields method
|
||||||
// To read composite fields back pass result of Scan() method
|
// To read composite fields back pass result of Scan() method
|
||||||
// to query Scan function.
|
// to query Scan function.
|
||||||
func NewComposite(fields ...Value) *Composite {
|
func NewCompositeType(fields ...Value) *CompositeType {
|
||||||
return &Composite{fields, Present}
|
return &CompositeType{fields, Present}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src Composite) Get() interface{} {
|
func (src CompositeType) Get() interface{} {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case Present:
|
case Present:
|
||||||
return src
|
return src
|
||||||
|
@ -35,9 +35,9 @@ func (src Composite) Get() interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set is called internally when passing query arguments.
|
// Set is called internally when passing query arguments.
|
||||||
func (dst *Composite) Set(src interface{}) error {
|
func (dst *CompositeType) Set(src interface{}) error {
|
||||||
if src == nil {
|
if src == nil {
|
||||||
*dst = Composite{Status: Null}
|
*dst = CompositeType{Status: Null}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,11 +60,11 @@ func (dst *Composite) Set(src interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssignTo should never be called on composite value directly
|
// AssignTo should never be called on composite value directly
|
||||||
func (src Composite) AssignTo(dst interface{}) error {
|
func (src CompositeType) AssignTo(dst interface{}) error {
|
||||||
return errors.New("Pass Composite.Scan() to deconstruct composite")
|
return errors.New("Pass Composite.Scan() to deconstruct composite")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src Composite) EncodeBinary(ci *ConnInfo, buf []byte) (newBuf []byte, err error) {
|
func (src CompositeType) EncodeBinary(ci *ConnInfo, buf []byte) (newBuf []byte, err error) {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case Null:
|
case Null:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -78,7 +78,7 @@ func (src Composite) EncodeBinary(ci *ConnInfo, buf []byte) (newBuf []byte, err
|
||||||
// Opposite to Record, fields in a composite act as a "schema"
|
// Opposite to Record, fields in a composite act as a "schema"
|
||||||
// and decoding fails if SQL value can't be assigned due to
|
// and decoding fails if SQL value can't be assigned due to
|
||||||
// type mismatch
|
// type mismatch
|
||||||
func (dst *Composite) DecodeBinary(ci *ConnInfo, buf []byte) (err error) {
|
func (dst *CompositeType) DecodeBinary(ci *ConnInfo, buf []byte) (err error) {
|
||||||
if buf == nil {
|
if buf == nil {
|
||||||
dst.Status = Null
|
dst.Status = Null
|
||||||
return nil
|
return nil
|
||||||
|
@ -118,7 +118,7 @@ func (dst *Composite) DecodeBinary(ci *ConnInfo, buf []byte) (err error) {
|
||||||
// Rest of arguments are set in the order of fields in the composite
|
// Rest of arguments are set in the order of fields in the composite
|
||||||
//
|
//
|
||||||
// Use of Scan method doesn't modify original composite
|
// Use of Scan method doesn't modify original composite
|
||||||
func (src Composite) Scan(isNull *bool, dst ...interface{}) BinaryDecoderFunc {
|
func (src CompositeType) Scan(isNull *bool, dst ...interface{}) BinaryDecoderFunc {
|
||||||
return func(ci *ConnInfo, buf []byte) error {
|
return func(ci *ConnInfo, buf []byte) error {
|
||||||
if err := src.DecodeBinary(ci, buf); err != nil {
|
if err := src.DecodeBinary(ci, buf); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -139,7 +139,7 @@ func (src Composite) Scan(isNull *bool, dst ...interface{}) BinaryDecoderFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetFields sets Composite's fields to corresponding values
|
// SetFields sets Composite's fields to corresponding values
|
||||||
func (dst *Composite) SetFields(values ...interface{}) error {
|
func (dst *CompositeType) SetFields(values ...interface{}) error {
|
||||||
if len(values) != len(dst.fields) {
|
if len(values) != len(dst.fields) {
|
||||||
return errors.Errorf("Number of fields don't match. Composite has %d fields", len(dst.fields))
|
return errors.Errorf("Number of fields don't match. Composite has %d fields", len(dst.fields))
|
||||||
}
|
}
|
|
@ -31,7 +31,7 @@ create type mytype as (
|
||||||
var a int
|
var a int
|
||||||
var b *string
|
var b *string
|
||||||
|
|
||||||
c := pgtype.NewComposite(&pgtype.Int4{}, &pgtype.Text{})
|
c := pgtype.NewCompositeType(&pgtype.Int4{}, &pgtype.Text{})
|
||||||
c.SetFields(2, "bar")
|
c.SetFields(2, "bar")
|
||||||
|
|
||||||
err = conn.QueryRow(context.Background(), "select $1::mytype", qrf, c).
|
err = conn.QueryRow(context.Background(), "select $1::mytype", qrf, c).
|
Loading…
Reference in New Issue