2
0

Rename pgtype.DataType to pgtype.Type

This commit is contained in:
Jack Christensen
2022-02-21 09:01:48 -06:00
parent a3c351d11a
commit bda10b2ec9
17 changed files with 190 additions and 191 deletions
+1 -1
View File
@@ -918,7 +918,7 @@ func BenchmarkSelectManyRegisteredEnum(b *testing.B) {
err = conn.QueryRow(context.Background(), "select oid from pg_type where typname=$1;", "color").Scan(&oid)
require.NoError(b, err)
conn.ConnInfo().RegisterDataType(&pgtype.DataType{Name: "color", OID: oid, Codec: &pgtype.EnumCodec{}})
conn.ConnInfo().RegisterType(&pgtype.Type{Name: "color", OID: oid, Codec: &pgtype.EnumCodec{}})
b.ResetTimer()
var x, y, z string
+9 -10
View File
@@ -832,9 +832,8 @@ func (c *Conn) sanitizeForSimpleQuery(sql string, args ...interface{}) (string,
return sanitize.SanitizeSQL(sql, valueArgs...)
}
// LoadDataType inspects the database for typeName and produces a pgtype.DataType suitable for
// registration.
func (c *Conn) LoadDataType(ctx context.Context, typeName string) (*pgtype.DataType, error) {
// LoadType inspects the database for typeName and produces a pgtype.Type suitable for registration.
func (c *Conn) LoadType(ctx context.Context, typeName string) (*pgtype.Type, error) {
var oid uint32
err := c.QueryRow(ctx, "select $1::text::regtype::oid;", typeName).Scan(&oid)
@@ -856,23 +855,23 @@ func (c *Conn) LoadDataType(ctx context.Context, typeName string) (*pgtype.DataT
return nil, err
}
dt, ok := c.ConnInfo().DataTypeForOID(elementOID)
dt, ok := c.ConnInfo().TypeForOID(elementOID)
if !ok {
return nil, errors.New("array element OID not registered")
}
return &pgtype.DataType{Name: typeName, OID: oid, Codec: &pgtype.ArrayCodec{ElementDataType: dt}}, nil
return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.ArrayCodec{ElementType: dt}}, nil
case "c": // composite
fields, err := c.getCompositeFields(ctx, oid)
if err != nil {
return nil, err
}
return &pgtype.DataType{Name: typeName, OID: oid, Codec: &pgtype.CompositeCodec{Fields: fields}}, nil
return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.CompositeCodec{Fields: fields}}, nil
case "e": // enum
return &pgtype.DataType{Name: typeName, OID: oid, Codec: &pgtype.EnumCodec{}}, nil
return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.EnumCodec{}}, nil
default:
return &pgtype.DataType{}, errors.New("unknown typtype")
return &pgtype.Type{}, errors.New("unknown typtype")
}
}
@@ -905,11 +904,11 @@ order by attnum`,
[]interface{}{typrelid},
[]interface{}{&fieldName, &fieldOID},
func(qfr QueryFuncRow) error {
dt, ok := c.ConnInfo().DataTypeForOID(fieldOID)
dt, ok := c.ConnInfo().TypeForOID(fieldOID)
if !ok {
return fmt.Errorf("unknown composite type field OID: %v", fieldOID)
}
fields = append(fields, pgtype.CompositeCodecField{Name: fieldName, DataType: dt})
fields = append(fields, pgtype.CompositeCodecField{Name: fieldName, Type: dt})
return nil
})
if err != nil {
+3 -3
View File
@@ -841,11 +841,11 @@ func TestConnInitConnInfo(t *testing.T) {
"text": pgtype.TextOID,
}
for name, oid := range nameOIDs {
dtByName, ok := conn.ConnInfo().DataTypeForName(name)
dtByName, ok := conn.ConnInfo().TypeForName(name)
if !ok {
t.Fatalf("Expected type named %v to be present", name)
}
dtByOID, ok := conn.ConnInfo().DataTypeForOID(oid)
dtByOID, ok := conn.ConnInfo().TypeForOID(oid)
if !ok {
t.Fatalf("Expected type OID %v to be present", oid)
}
@@ -891,7 +891,7 @@ func TestDomainType(t *testing.T) {
if err != nil {
t.Fatalf("did not find uint64 OID, %v", err)
}
conn.ConnInfo().RegisterDataType(&pgtype.DataType{Name: "uint64", OID: uint64OID, Codec: pgtype.NumericCodec{}})
conn.ConnInfo().RegisterType(&pgtype.Type{Name: "uint64", OID: uint64OID, Codec: pgtype.NumericCodec{}})
var n uint64
err = conn.QueryRow(context.Background(), "select $1::uint64", uint64(24)).Scan(&n)
+1 -1
View File
@@ -256,7 +256,7 @@ func TestConnCopyFromJSON(t *testing.T) {
defer closeConn(t, conn)
for _, typeName := range []string{"json", "jsonb"} {
if _, ok := conn.ConnInfo().DataTypeForName(typeName); !ok {
if _, ok := conn.ConnInfo().TypeForName(typeName); !ok {
return // No JSON/JSONB type -- must be running against old PostgreSQL
}
}
+1 -1
View File
@@ -83,7 +83,7 @@ func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, o
return eqb.encodeExtendedParamValue(ci, oid, formatCode, arg)
}
if _, ok := ci.DataTypeForOID(oid); ok {
if _, ok := ci.TypeForOID(oid); ok {
buf, err := ci.Encode(oid, formatCode, arg, eqb.paramValueBytes)
if err != nil {
return nil, err
+12 -12
View File
@@ -38,15 +38,15 @@ type ArraySetter interface {
// ArrayCodec is a codec for any array type.
type ArrayCodec struct {
ElementDataType *DataType
ElementType *Type
}
func (c *ArrayCodec) FormatSupported(format int16) bool {
return c.ElementDataType.Codec.FormatSupported(format)
return c.ElementType.Codec.FormatSupported(format)
}
func (c *ArrayCodec) PreferredFormat() int16 {
return c.ElementDataType.Codec.PreferredFormat()
return c.ElementType.Codec.PreferredFormat()
}
func (c *ArrayCodec) PlanEncode(ci *ConnInfo, oid uint32, format int16, value interface{}) EncodePlan {
@@ -57,7 +57,7 @@ func (c *ArrayCodec) PlanEncode(ci *ConnInfo, oid uint32, format int16, value in
elementType := arrayValuer.IndexType()
elementEncodePlan := ci.PlanEncode(c.ElementDataType.OID, format, elementType)
elementEncodePlan := ci.PlanEncode(c.ElementType.OID, format, elementType)
if elementEncodePlan == nil {
return nil
}
@@ -124,7 +124,7 @@ func (p *encodePlanArrayCodecText) Encode(value interface{}, buf []byte) (newBuf
elemType := reflect.TypeOf(elem)
if lastElemType != elemType {
lastElemType = elemType
encodePlan = p.ci.PlanEncode(p.ac.ElementDataType.OID, TextFormatCode, elem)
encodePlan = p.ci.PlanEncode(p.ac.ElementType.OID, TextFormatCode, elem)
if encodePlan == nil {
return nil, fmt.Errorf("unable to encode %v", array.Index(i))
}
@@ -167,7 +167,7 @@ func (p *encodePlanArrayCodecBinary) Encode(value interface{}, buf []byte) (newB
arrayHeader := ArrayHeader{
Dimensions: dimensions,
ElementOID: p.ac.ElementDataType.OID,
ElementOID: p.ac.ElementType.OID,
}
containsNullIndex := len(buf) + 4
@@ -188,7 +188,7 @@ func (p *encodePlanArrayCodecBinary) Encode(value interface{}, buf []byte) (newB
elemType := reflect.TypeOf(elem)
if lastElemType != elemType {
lastElemType = elemType
encodePlan = p.ci.PlanEncode(p.ac.ElementDataType.OID, BinaryFormatCode, elem)
encodePlan = p.ci.PlanEncode(p.ac.ElementType.OID, BinaryFormatCode, elem)
if encodePlan == nil {
return nil, fmt.Errorf("unable to encode %v", array.Index(i))
}
@@ -218,7 +218,7 @@ func (c *ArrayCodec) PlanScan(ci *ConnInfo, oid uint32, format int16, target int
elementType := arrayScanner.ScanIndexType()
elementScanPlan := ci.PlanScan(c.ElementDataType.OID, format, elementType)
elementScanPlan := ci.PlanScan(c.ElementType.OID, format, elementType)
if _, ok := elementScanPlan.(*scanPlanFail); ok {
return nil
}
@@ -248,9 +248,9 @@ func (c *ArrayCodec) decodeBinary(ci *ConnInfo, arrayOID uint32, src []byte, arr
return nil
}
elementScanPlan := c.ElementDataType.Codec.PlanScan(ci, c.ElementDataType.OID, BinaryFormatCode, array.ScanIndex(0), false)
elementScanPlan := c.ElementType.Codec.PlanScan(ci, c.ElementType.OID, BinaryFormatCode, array.ScanIndex(0), false)
if elementScanPlan == nil {
elementScanPlan = ci.PlanScan(c.ElementDataType.OID, BinaryFormatCode, array.ScanIndex(0))
elementScanPlan = ci.PlanScan(c.ElementType.OID, BinaryFormatCode, array.ScanIndex(0))
}
for i := 0; i < elementCount; i++ {
@@ -286,9 +286,9 @@ func (c *ArrayCodec) decodeText(ci *ConnInfo, arrayOID uint32, src []byte, array
return nil
}
elementScanPlan := c.ElementDataType.Codec.PlanScan(ci, c.ElementDataType.OID, TextFormatCode, array.ScanIndex(0), false)
elementScanPlan := c.ElementType.Codec.PlanScan(ci, c.ElementType.OID, TextFormatCode, array.ScanIndex(0), false)
if elementScanPlan == nil {
elementScanPlan = ci.PlanScan(c.ElementDataType.OID, TextFormatCode, array.ScanIndex(0))
elementScanPlan = ci.PlanScan(c.ElementType.OID, TextFormatCode, array.ScanIndex(0))
}
for i, s := range uta.Elements {
+11 -11
View File
@@ -29,8 +29,8 @@ type CompositeIndexScanner interface {
}
type CompositeCodecField struct {
Name string
DataType *DataType
Name string
Type *Type
}
type CompositeCodec struct {
@@ -39,7 +39,7 @@ type CompositeCodec struct {
func (c *CompositeCodec) FormatSupported(format int16) bool {
for _, f := range c.Fields {
if !f.DataType.Codec.FormatSupported(format) {
if !f.Type.Codec.FormatSupported(format) {
return false
}
}
@@ -83,7 +83,7 @@ func (plan *encodePlanCompositeCodecCompositeIndexGetterToBinary) Encode(value i
builder := NewCompositeBinaryBuilder(plan.ci, buf)
for i, field := range plan.cc.Fields {
builder.AppendValue(field.DataType.OID, getter.Index(i))
builder.AppendValue(field.Type.OID, getter.Index(i))
}
return builder.Finish()
@@ -103,7 +103,7 @@ func (plan *encodePlanCompositeCodecCompositeIndexGetterToText) Encode(value int
b := NewCompositeTextBuilder(plan.ci, buf)
for i, field := range plan.cc.Fields {
b.AppendValue(field.DataType.OID, getter.Index(i))
b.AppendValue(field.Type.OID, getter.Index(i))
}
return b.Finish()
@@ -143,9 +143,9 @@ func (plan *scanPlanBinaryCompositeToCompositeIndexScanner) Scan(src []byte, tar
if scanner.Next() {
fieldTarget := targetScanner.ScanIndex(i)
if fieldTarget != nil {
fieldPlan := plan.ci.PlanScan(field.DataType.OID, BinaryFormatCode, fieldTarget)
fieldPlan := plan.ci.PlanScan(field.Type.OID, BinaryFormatCode, fieldTarget)
if fieldPlan == nil {
return fmt.Errorf("unable to encode %v into OID %d in binary format", field, field.DataType.OID)
return fmt.Errorf("unable to encode %v into OID %d in binary format", field, field.Type.OID)
}
err := fieldPlan.Scan(scanner.Bytes(), fieldTarget)
@@ -182,9 +182,9 @@ func (plan *scanPlanTextCompositeToCompositeIndexScanner) Scan(src []byte, targe
if scanner.Next() {
fieldTarget := targetScanner.ScanIndex(i)
if fieldTarget != nil {
fieldPlan := plan.ci.PlanScan(field.DataType.OID, TextFormatCode, fieldTarget)
fieldPlan := plan.ci.PlanScan(field.Type.OID, TextFormatCode, fieldTarget)
if fieldPlan == nil {
return fmt.Errorf("unable to encode %v into OID %d in text format", field, field.DataType.OID)
return fmt.Errorf("unable to encode %v into OID %d in text format", field, field.Type.OID)
}
err := fieldPlan.Scan(scanner.Bytes(), fieldTarget)
@@ -232,9 +232,9 @@ func (c *CompositeCodec) DecodeValue(ci *ConnInfo, oid uint32, format int16, src
values := make(map[string]interface{}, len(c.Fields))
for i := 0; scanner.Next() && i < len(c.Fields); i++ {
var v interface{}
fieldPlan := ci.PlanScan(c.Fields[i].DataType.OID, TextFormatCode, &v)
fieldPlan := ci.PlanScan(c.Fields[i].Type.OID, TextFormatCode, &v)
if fieldPlan == nil {
return nil, fmt.Errorf("unable to scan OID %d in text format into %v", c.Fields[i].DataType.OID, v)
return nil, fmt.Errorf("unable to scan OID %d in text format into %v", c.Fields[i].Type.OID, v)
}
err := fieldPlan.Scan(scanner.Bytes(), &v)
+8 -8
View File
@@ -24,9 +24,9 @@ create type ct_test as (
require.NoError(t, err)
defer conn.Exec(context.Background(), "drop type ct_test")
dt, err := conn.LoadDataType(context.Background(), "ct_test")
dt, err := conn.LoadType(context.Background(), "ct_test")
require.NoError(t, err)
conn.ConnInfo().RegisterDataType(dt)
conn.ConnInfo().RegisterType(dt)
formats := []struct {
name string
@@ -103,9 +103,9 @@ create type point3d as (
require.NoError(t, err)
defer conn.Exec(context.Background(), "drop type point3d")
dt, err := conn.LoadDataType(context.Background(), "point3d")
dt, err := conn.LoadType(context.Background(), "point3d")
require.NoError(t, err)
conn.ConnInfo().RegisterDataType(dt)
conn.ConnInfo().RegisterType(dt)
formats := []struct {
name string
@@ -138,9 +138,9 @@ create type point3d as (
require.NoError(t, err)
defer conn.Exec(context.Background(), "drop type point3d")
dt, err := conn.LoadDataType(context.Background(), "point3d")
dt, err := conn.LoadType(context.Background(), "point3d")
require.NoError(t, err)
conn.ConnInfo().RegisterDataType(dt)
conn.ConnInfo().RegisterType(dt)
formats := []struct {
name string
@@ -177,9 +177,9 @@ create type point3d as (
require.NoError(t, err)
defer conn.Exec(context.Background(), "drop type point3d")
dt, err := conn.LoadDataType(context.Background(), "point3d")
dt, err := conn.LoadType(context.Background(), "point3d")
require.NoError(t, err)
conn.ConnInfo().RegisterDataType(dt)
conn.ConnInfo().RegisterType(dt)
formats := []struct {
name string
+4 -4
View File
@@ -18,10 +18,10 @@ create type enum_test as enum ('foo', 'bar', 'baz');`)
require.NoError(t, err)
defer conn.Exec(context.Background(), "drop type enum_test")
dt, err := conn.LoadDataType(context.Background(), "enum_test")
dt, err := conn.LoadType(context.Background(), "enum_test")
require.NoError(t, err)
conn.ConnInfo().RegisterDataType(dt)
conn.ConnInfo().RegisterType(dt)
var s string
err = conn.QueryRow(context.Background(), `select 'foo'::enum_test`).Scan(&s)
@@ -55,10 +55,10 @@ create type enum_test as enum ('foo', 'bar', 'baz');`)
require.NoError(t, err)
defer conn.Exec(context.Background(), "drop type enum_test")
dt, err := conn.LoadDataType(context.Background(), "enum_test")
dt, err := conn.LoadType(context.Background(), "enum_test")
require.NoError(t, err)
conn.ConnInfo().RegisterDataType(dt)
conn.ConnInfo().RegisterType(dt)
rows, err := conn.Query(context.Background(), `select 'foo'::enum_test`)
require.NoError(t, err)
+1 -1
View File
@@ -61,7 +61,7 @@ func TestHstoreCodec(t *testing.T) {
t.Skipf("Skipping: cannot find hstore OID")
}
conn.ConnInfo().RegisterDataType(&pgtype.DataType{Name: "hstore", OID: hstoreOID, Codec: pgtype.HstoreCodec{}})
conn.ConnInfo().RegisterType(&pgtype.Type{Name: "hstore", OID: hstoreOID, Codec: pgtype.HstoreCodec{}})
formats := []struct {
name string
+1 -1
View File
@@ -11,7 +11,7 @@ import (
func TestLineTranscode(t *testing.T) {
conn := testutil.MustConnectPgx(t)
defer conn.Close(context.Background())
if _, ok := conn.ConnInfo().DataTypeForName("line"); !ok {
if _, ok := conn.ConnInfo().TypeForName("line"); !ok {
t.Skip("Skipping due to no line type")
}
+122 -122
View File
@@ -163,20 +163,20 @@ func (e *nullAssignmentError) Error() string {
return fmt.Sprintf("cannot assign NULL to %T", e.dst)
}
type DataType struct {
type Type struct {
Codec Codec
Name string
OID uint32
}
type ConnInfo struct {
oidToDataType map[uint32]*DataType
nameToDataType map[string]*DataType
oidToType map[uint32]*Type
nameToType map[string]*Type
reflectTypeToName map[reflect.Type]string
oidToFormatCode map[uint32]int16
oidToResultFormatCode map[uint32]int16
reflectTypeToDataType map[reflect.Type]*DataType
reflectTypeToType map[reflect.Type]*Type
// TryWrapEncodePlanFuncs is a slice of functions that will wrap a value that cannot be encoded by the Codec. Every
// time a wrapper is found the PlanEncode method will be recursively called with the new value. This allows several layers of wrappers
@@ -193,8 +193,8 @@ type ConnInfo struct {
func NewConnInfo() *ConnInfo {
ci := &ConnInfo{
oidToDataType: make(map[uint32]*DataType),
nameToDataType: make(map[string]*DataType),
oidToType: make(map[uint32]*Type),
nameToType: make(map[string]*Type),
reflectTypeToName: make(map[reflect.Type]string),
oidToFormatCode: make(map[uint32]int16),
oidToResultFormatCode: make(map[uint32]int16),
@@ -218,99 +218,99 @@ func NewConnInfo() *ConnInfo {
},
}
ci.RegisterDataType(&DataType{Name: "aclitem", OID: ACLItemOID, Codec: &TextFormatOnlyCodec{TextCodec{}}})
ci.RegisterDataType(&DataType{Name: "bit", OID: BitOID, Codec: BitsCodec{}})
ci.RegisterDataType(&DataType{Name: "bool", OID: BoolOID, Codec: BoolCodec{}})
ci.RegisterDataType(&DataType{Name: "box", OID: BoxOID, Codec: BoxCodec{}})
ci.RegisterDataType(&DataType{Name: "bpchar", OID: BPCharOID, Codec: TextCodec{}})
ci.RegisterDataType(&DataType{Name: "bytea", OID: ByteaOID, Codec: ByteaCodec{}})
ci.RegisterDataType(&DataType{Name: "char", OID: QCharOID, Codec: QCharCodec{}})
ci.RegisterDataType(&DataType{Name: "cid", OID: CIDOID, Codec: Uint32Codec{}})
ci.RegisterDataType(&DataType{Name: "cidr", OID: CIDROID, Codec: InetCodec{}})
ci.RegisterDataType(&DataType{Name: "circle", OID: CircleOID, Codec: CircleCodec{}})
ci.RegisterDataType(&DataType{Name: "date", OID: DateOID, Codec: DateCodec{}})
ci.RegisterDataType(&DataType{Name: "float4", OID: Float4OID, Codec: Float4Codec{}})
ci.RegisterDataType(&DataType{Name: "float8", OID: Float8OID, Codec: Float8Codec{}})
ci.RegisterDataType(&DataType{Name: "inet", OID: InetOID, Codec: InetCodec{}})
ci.RegisterDataType(&DataType{Name: "int2", OID: Int2OID, Codec: Int2Codec{}})
ci.RegisterDataType(&DataType{Name: "int4", OID: Int4OID, Codec: Int4Codec{}})
ci.RegisterDataType(&DataType{Name: "int8", OID: Int8OID, Codec: Int8Codec{}})
ci.RegisterDataType(&DataType{Name: "interval", OID: IntervalOID, Codec: IntervalCodec{}})
ci.RegisterDataType(&DataType{Name: "json", OID: JSONOID, Codec: JSONCodec{}})
ci.RegisterDataType(&DataType{Name: "jsonb", OID: JSONBOID, Codec: JSONBCodec{}})
ci.RegisterDataType(&DataType{Name: "line", OID: LineOID, Codec: LineCodec{}})
ci.RegisterDataType(&DataType{Name: "lseg", OID: LsegOID, Codec: LsegCodec{}})
ci.RegisterDataType(&DataType{Name: "macaddr", OID: MacaddrOID, Codec: MacaddrCodec{}})
ci.RegisterDataType(&DataType{Name: "name", OID: NameOID, Codec: TextCodec{}})
ci.RegisterDataType(&DataType{Name: "numeric", OID: NumericOID, Codec: NumericCodec{}})
ci.RegisterDataType(&DataType{Name: "oid", OID: OIDOID, Codec: Uint32Codec{}})
ci.RegisterDataType(&DataType{Name: "path", OID: PathOID, Codec: PathCodec{}})
ci.RegisterDataType(&DataType{Name: "point", OID: PointOID, Codec: PointCodec{}})
ci.RegisterDataType(&DataType{Name: "polygon", OID: PolygonOID, Codec: PolygonCodec{}})
ci.RegisterDataType(&DataType{Name: "record", OID: RecordOID, Codec: RecordCodec{}})
ci.RegisterDataType(&DataType{Name: "text", OID: TextOID, Codec: TextCodec{}})
ci.RegisterDataType(&DataType{Name: "tid", OID: TIDOID, Codec: TIDCodec{}})
ci.RegisterDataType(&DataType{Name: "time", OID: TimeOID, Codec: TimeCodec{}})
ci.RegisterDataType(&DataType{Name: "timestamp", OID: TimestampOID, Codec: TimestampCodec{}})
ci.RegisterDataType(&DataType{Name: "timestamptz", OID: TimestamptzOID, Codec: TimestamptzCodec{}})
ci.RegisterDataType(&DataType{Name: "unknown", OID: UnknownOID, Codec: TextCodec{}})
ci.RegisterDataType(&DataType{Name: "uuid", OID: UUIDOID, Codec: UUIDCodec{}})
ci.RegisterDataType(&DataType{Name: "varbit", OID: VarbitOID, Codec: BitsCodec{}})
ci.RegisterDataType(&DataType{Name: "varchar", OID: VarcharOID, Codec: TextCodec{}})
ci.RegisterDataType(&DataType{Name: "xid", OID: XIDOID, Codec: Uint32Codec{}})
ci.RegisterType(&Type{Name: "aclitem", OID: ACLItemOID, Codec: &TextFormatOnlyCodec{TextCodec{}}})
ci.RegisterType(&Type{Name: "bit", OID: BitOID, Codec: BitsCodec{}})
ci.RegisterType(&Type{Name: "bool", OID: BoolOID, Codec: BoolCodec{}})
ci.RegisterType(&Type{Name: "box", OID: BoxOID, Codec: BoxCodec{}})
ci.RegisterType(&Type{Name: "bpchar", OID: BPCharOID, Codec: TextCodec{}})
ci.RegisterType(&Type{Name: "bytea", OID: ByteaOID, Codec: ByteaCodec{}})
ci.RegisterType(&Type{Name: "char", OID: QCharOID, Codec: QCharCodec{}})
ci.RegisterType(&Type{Name: "cid", OID: CIDOID, Codec: Uint32Codec{}})
ci.RegisterType(&Type{Name: "cidr", OID: CIDROID, Codec: InetCodec{}})
ci.RegisterType(&Type{Name: "circle", OID: CircleOID, Codec: CircleCodec{}})
ci.RegisterType(&Type{Name: "date", OID: DateOID, Codec: DateCodec{}})
ci.RegisterType(&Type{Name: "float4", OID: Float4OID, Codec: Float4Codec{}})
ci.RegisterType(&Type{Name: "float8", OID: Float8OID, Codec: Float8Codec{}})
ci.RegisterType(&Type{Name: "inet", OID: InetOID, Codec: InetCodec{}})
ci.RegisterType(&Type{Name: "int2", OID: Int2OID, Codec: Int2Codec{}})
ci.RegisterType(&Type{Name: "int4", OID: Int4OID, Codec: Int4Codec{}})
ci.RegisterType(&Type{Name: "int8", OID: Int8OID, Codec: Int8Codec{}})
ci.RegisterType(&Type{Name: "interval", OID: IntervalOID, Codec: IntervalCodec{}})
ci.RegisterType(&Type{Name: "json", OID: JSONOID, Codec: JSONCodec{}})
ci.RegisterType(&Type{Name: "jsonb", OID: JSONBOID, Codec: JSONBCodec{}})
ci.RegisterType(&Type{Name: "line", OID: LineOID, Codec: LineCodec{}})
ci.RegisterType(&Type{Name: "lseg", OID: LsegOID, Codec: LsegCodec{}})
ci.RegisterType(&Type{Name: "macaddr", OID: MacaddrOID, Codec: MacaddrCodec{}})
ci.RegisterType(&Type{Name: "name", OID: NameOID, Codec: TextCodec{}})
ci.RegisterType(&Type{Name: "numeric", OID: NumericOID, Codec: NumericCodec{}})
ci.RegisterType(&Type{Name: "oid", OID: OIDOID, Codec: Uint32Codec{}})
ci.RegisterType(&Type{Name: "path", OID: PathOID, Codec: PathCodec{}})
ci.RegisterType(&Type{Name: "point", OID: PointOID, Codec: PointCodec{}})
ci.RegisterType(&Type{Name: "polygon", OID: PolygonOID, Codec: PolygonCodec{}})
ci.RegisterType(&Type{Name: "record", OID: RecordOID, Codec: RecordCodec{}})
ci.RegisterType(&Type{Name: "text", OID: TextOID, Codec: TextCodec{}})
ci.RegisterType(&Type{Name: "tid", OID: TIDOID, Codec: TIDCodec{}})
ci.RegisterType(&Type{Name: "time", OID: TimeOID, Codec: TimeCodec{}})
ci.RegisterType(&Type{Name: "timestamp", OID: TimestampOID, Codec: TimestampCodec{}})
ci.RegisterType(&Type{Name: "timestamptz", OID: TimestamptzOID, Codec: TimestamptzCodec{}})
ci.RegisterType(&Type{Name: "unknown", OID: UnknownOID, Codec: TextCodec{}})
ci.RegisterType(&Type{Name: "uuid", OID: UUIDOID, Codec: UUIDCodec{}})
ci.RegisterType(&Type{Name: "varbit", OID: VarbitOID, Codec: BitsCodec{}})
ci.RegisterType(&Type{Name: "varchar", OID: VarcharOID, Codec: TextCodec{}})
ci.RegisterType(&Type{Name: "xid", OID: XIDOID, Codec: Uint32Codec{}})
ci.RegisterDataType(&DataType{Name: "daterange", OID: DaterangeOID, Codec: &RangeCodec{ElementDataType: ci.oidToDataType[DateOID]}})
ci.RegisterDataType(&DataType{Name: "int4range", OID: Int4rangeOID, Codec: &RangeCodec{ElementDataType: ci.oidToDataType[Int4OID]}})
ci.RegisterDataType(&DataType{Name: "int8range", OID: Int8rangeOID, Codec: &RangeCodec{ElementDataType: ci.oidToDataType[Int8OID]}})
ci.RegisterDataType(&DataType{Name: "numrange", OID: NumrangeOID, Codec: &RangeCodec{ElementDataType: ci.oidToDataType[NumericOID]}})
ci.RegisterDataType(&DataType{Name: "tsrange", OID: TsrangeOID, Codec: &RangeCodec{ElementDataType: ci.oidToDataType[TimestampOID]}})
ci.RegisterDataType(&DataType{Name: "tstzrange", OID: TstzrangeOID, Codec: &RangeCodec{ElementDataType: ci.oidToDataType[TimestamptzOID]}})
ci.RegisterType(&Type{Name: "daterange", OID: DaterangeOID, Codec: &RangeCodec{ElementType: ci.oidToType[DateOID]}})
ci.RegisterType(&Type{Name: "int4range", OID: Int4rangeOID, Codec: &RangeCodec{ElementType: ci.oidToType[Int4OID]}})
ci.RegisterType(&Type{Name: "int8range", OID: Int8rangeOID, Codec: &RangeCodec{ElementType: ci.oidToType[Int8OID]}})
ci.RegisterType(&Type{Name: "numrange", OID: NumrangeOID, Codec: &RangeCodec{ElementType: ci.oidToType[NumericOID]}})
ci.RegisterType(&Type{Name: "tsrange", OID: TsrangeOID, Codec: &RangeCodec{ElementType: ci.oidToType[TimestampOID]}})
ci.RegisterType(&Type{Name: "tstzrange", OID: TstzrangeOID, Codec: &RangeCodec{ElementType: ci.oidToType[TimestamptzOID]}})
ci.RegisterDataType(&DataType{Name: "_aclitem", OID: ACLItemArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[ACLItemOID]}})
ci.RegisterDataType(&DataType{Name: "_bit", OID: BitArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[BitOID]}})
ci.RegisterDataType(&DataType{Name: "_bool", OID: BoolArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[BoolOID]}})
ci.RegisterDataType(&DataType{Name: "_box", OID: BoxArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[BoxOID]}})
ci.RegisterDataType(&DataType{Name: "_bpchar", OID: BPCharArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[BPCharOID]}})
ci.RegisterDataType(&DataType{Name: "_bytea", OID: ByteaArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[ByteaOID]}})
ci.RegisterDataType(&DataType{Name: "_char", OID: QCharArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[QCharOID]}})
ci.RegisterDataType(&DataType{Name: "_cid", OID: CIDArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[CIDOID]}})
ci.RegisterDataType(&DataType{Name: "_cidr", OID: CIDRArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[CIDROID]}})
ci.RegisterDataType(&DataType{Name: "_circle", OID: CircleArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[CircleOID]}})
ci.RegisterDataType(&DataType{Name: "_date", OID: DateArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[DateOID]}})
ci.RegisterDataType(&DataType{Name: "_daterange", OID: DaterangeArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[DaterangeOID]}})
ci.RegisterDataType(&DataType{Name: "_float4", OID: Float4ArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[Float4OID]}})
ci.RegisterDataType(&DataType{Name: "_float8", OID: Float8ArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[Float8OID]}})
ci.RegisterDataType(&DataType{Name: "_inet", OID: InetArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[InetOID]}})
ci.RegisterDataType(&DataType{Name: "_int2", OID: Int2ArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[Int2OID]}})
ci.RegisterDataType(&DataType{Name: "_int4", OID: Int4ArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[Int4OID]}})
ci.RegisterDataType(&DataType{Name: "_int4range", OID: Int4rangeArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[Int4rangeOID]}})
ci.RegisterDataType(&DataType{Name: "_int8", OID: Int8ArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[Int8OID]}})
ci.RegisterDataType(&DataType{Name: "_int8range", OID: Int8rangeArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[Int8rangeOID]}})
ci.RegisterDataType(&DataType{Name: "_interval", OID: IntervalArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[IntervalOID]}})
ci.RegisterDataType(&DataType{Name: "_json", OID: JSONArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[JSONOID]}})
ci.RegisterDataType(&DataType{Name: "_jsonb", OID: JSONBArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[JSONBOID]}})
ci.RegisterDataType(&DataType{Name: "_line", OID: LineArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[LineOID]}})
ci.RegisterDataType(&DataType{Name: "_lseg", OID: LsegArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[LsegOID]}})
ci.RegisterDataType(&DataType{Name: "_macaddr", OID: MacaddrArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[MacaddrOID]}})
ci.RegisterDataType(&DataType{Name: "_name", OID: NameArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[NameOID]}})
ci.RegisterDataType(&DataType{Name: "_numeric", OID: NumericArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[NumericOID]}})
ci.RegisterDataType(&DataType{Name: "_numrange", OID: NumrangeArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[NumrangeOID]}})
ci.RegisterDataType(&DataType{Name: "_oid", OID: OIDArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[OIDOID]}})
ci.RegisterDataType(&DataType{Name: "_path", OID: PathArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[PathOID]}})
ci.RegisterDataType(&DataType{Name: "_point", OID: PointArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[PointOID]}})
ci.RegisterDataType(&DataType{Name: "_polygon", OID: PolygonArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[PolygonOID]}})
ci.RegisterDataType(&DataType{Name: "_record", OID: RecordArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[RecordOID]}})
ci.RegisterDataType(&DataType{Name: "_text", OID: TextArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[TextOID]}})
ci.RegisterDataType(&DataType{Name: "_tid", OID: TIDArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[TIDOID]}})
ci.RegisterDataType(&DataType{Name: "_time", OID: TimeArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[TimeOID]}})
ci.RegisterDataType(&DataType{Name: "_timestamp", OID: TimestampArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[TimestampOID]}})
ci.RegisterDataType(&DataType{Name: "_timestamptz", OID: TimestamptzArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[TimestamptzOID]}})
ci.RegisterDataType(&DataType{Name: "_tsrange", OID: TsrangeArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[TsrangeOID]}})
ci.RegisterDataType(&DataType{Name: "_tstzrange", OID: TstzrangeArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[TstzrangeOID]}})
ci.RegisterDataType(&DataType{Name: "_uuid", OID: UUIDArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[UUIDOID]}})
ci.RegisterDataType(&DataType{Name: "_varbit", OID: VarbitArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[VarbitOID]}})
ci.RegisterDataType(&DataType{Name: "_varchar", OID: VarcharArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[VarcharOID]}})
ci.RegisterDataType(&DataType{Name: "_xid", OID: XIDArrayOID, Codec: &ArrayCodec{ElementDataType: ci.oidToDataType[XIDOID]}})
ci.RegisterType(&Type{Name: "_aclitem", OID: ACLItemArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[ACLItemOID]}})
ci.RegisterType(&Type{Name: "_bit", OID: BitArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[BitOID]}})
ci.RegisterType(&Type{Name: "_bool", OID: BoolArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[BoolOID]}})
ci.RegisterType(&Type{Name: "_box", OID: BoxArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[BoxOID]}})
ci.RegisterType(&Type{Name: "_bpchar", OID: BPCharArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[BPCharOID]}})
ci.RegisterType(&Type{Name: "_bytea", OID: ByteaArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[ByteaOID]}})
ci.RegisterType(&Type{Name: "_char", OID: QCharArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[QCharOID]}})
ci.RegisterType(&Type{Name: "_cid", OID: CIDArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[CIDOID]}})
ci.RegisterType(&Type{Name: "_cidr", OID: CIDRArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[CIDROID]}})
ci.RegisterType(&Type{Name: "_circle", OID: CircleArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[CircleOID]}})
ci.RegisterType(&Type{Name: "_date", OID: DateArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[DateOID]}})
ci.RegisterType(&Type{Name: "_daterange", OID: DaterangeArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[DaterangeOID]}})
ci.RegisterType(&Type{Name: "_float4", OID: Float4ArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[Float4OID]}})
ci.RegisterType(&Type{Name: "_float8", OID: Float8ArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[Float8OID]}})
ci.RegisterType(&Type{Name: "_inet", OID: InetArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[InetOID]}})
ci.RegisterType(&Type{Name: "_int2", OID: Int2ArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[Int2OID]}})
ci.RegisterType(&Type{Name: "_int4", OID: Int4ArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[Int4OID]}})
ci.RegisterType(&Type{Name: "_int4range", OID: Int4rangeArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[Int4rangeOID]}})
ci.RegisterType(&Type{Name: "_int8", OID: Int8ArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[Int8OID]}})
ci.RegisterType(&Type{Name: "_int8range", OID: Int8rangeArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[Int8rangeOID]}})
ci.RegisterType(&Type{Name: "_interval", OID: IntervalArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[IntervalOID]}})
ci.RegisterType(&Type{Name: "_json", OID: JSONArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[JSONOID]}})
ci.RegisterType(&Type{Name: "_jsonb", OID: JSONBArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[JSONBOID]}})
ci.RegisterType(&Type{Name: "_line", OID: LineArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[LineOID]}})
ci.RegisterType(&Type{Name: "_lseg", OID: LsegArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[LsegOID]}})
ci.RegisterType(&Type{Name: "_macaddr", OID: MacaddrArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[MacaddrOID]}})
ci.RegisterType(&Type{Name: "_name", OID: NameArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[NameOID]}})
ci.RegisterType(&Type{Name: "_numeric", OID: NumericArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[NumericOID]}})
ci.RegisterType(&Type{Name: "_numrange", OID: NumrangeArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[NumrangeOID]}})
ci.RegisterType(&Type{Name: "_oid", OID: OIDArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[OIDOID]}})
ci.RegisterType(&Type{Name: "_path", OID: PathArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[PathOID]}})
ci.RegisterType(&Type{Name: "_point", OID: PointArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[PointOID]}})
ci.RegisterType(&Type{Name: "_polygon", OID: PolygonArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[PolygonOID]}})
ci.RegisterType(&Type{Name: "_record", OID: RecordArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[RecordOID]}})
ci.RegisterType(&Type{Name: "_text", OID: TextArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[TextOID]}})
ci.RegisterType(&Type{Name: "_tid", OID: TIDArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[TIDOID]}})
ci.RegisterType(&Type{Name: "_time", OID: TimeArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[TimeOID]}})
ci.RegisterType(&Type{Name: "_timestamp", OID: TimestampArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[TimestampOID]}})
ci.RegisterType(&Type{Name: "_timestamptz", OID: TimestamptzArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[TimestamptzOID]}})
ci.RegisterType(&Type{Name: "_tsrange", OID: TsrangeArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[TsrangeOID]}})
ci.RegisterType(&Type{Name: "_tstzrange", OID: TstzrangeArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[TstzrangeOID]}})
ci.RegisterType(&Type{Name: "_uuid", OID: UUIDArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[UUIDOID]}})
ci.RegisterType(&Type{Name: "_varbit", OID: VarbitArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[VarbitOID]}})
ci.RegisterType(&Type{Name: "_varchar", OID: VarcharArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[VarcharOID]}})
ci.RegisterType(&Type{Name: "_xid", OID: XIDArrayOID, Codec: &ArrayCodec{ElementType: ci.oidToType[XIDOID]}})
registerDefaultPgTypeVariants := func(name, arrayName string, value interface{}) {
// T
@@ -361,49 +361,49 @@ func NewConnInfo() *ConnInfo {
return ci
}
func (ci *ConnInfo) RegisterDataType(t *DataType) {
ci.oidToDataType[t.OID] = t
ci.nameToDataType[t.Name] = t
func (ci *ConnInfo) RegisterType(t *Type) {
ci.oidToType[t.OID] = t
ci.nameToType[t.Name] = t
ci.oidToFormatCode[t.OID] = t.Codec.PreferredFormat()
ci.reflectTypeToDataType = nil // Invalidated by type registration
ci.reflectTypeToType = nil // Invalidated by type registration
}
// RegisterDefaultPgType registers a mapping of a Go type to a PostgreSQL type name. Typically the data type to be
// encoded or decoded is determined by the PostgreSQL OID. But if the OID of a value to be encoded or decoded is
// unknown, this additional mapping will be used by DataTypeForValue to determine a suitable data type.
// unknown, this additional mapping will be used by TypeForValue to determine a suitable data type.
func (ci *ConnInfo) RegisterDefaultPgType(value interface{}, name string) {
ci.reflectTypeToName[reflect.TypeOf(value)] = name
ci.reflectTypeToDataType = nil // Invalidated by registering a default type
ci.reflectTypeToType = nil // Invalidated by registering a default type
}
func (ci *ConnInfo) DataTypeForOID(oid uint32) (*DataType, bool) {
dt, ok := ci.oidToDataType[oid]
func (ci *ConnInfo) TypeForOID(oid uint32) (*Type, bool) {
dt, ok := ci.oidToType[oid]
return dt, ok
}
func (ci *ConnInfo) DataTypeForName(name string) (*DataType, bool) {
dt, ok := ci.nameToDataType[name]
func (ci *ConnInfo) TypeForName(name string) (*Type, bool) {
dt, ok := ci.nameToType[name]
return dt, ok
}
func (ci *ConnInfo) buildReflectTypeToDataType() {
ci.reflectTypeToDataType = make(map[reflect.Type]*DataType)
func (ci *ConnInfo) buildReflectTypeToType() {
ci.reflectTypeToType = make(map[reflect.Type]*Type)
for reflectType, name := range ci.reflectTypeToName {
if dt, ok := ci.nameToDataType[name]; ok {
ci.reflectTypeToDataType[reflectType] = dt
if dt, ok := ci.nameToType[name]; ok {
ci.reflectTypeToType[reflectType] = dt
}
}
}
// DataTypeForValue finds a data type suitable for v. Use RegisterDataType to register types that can encode and decode
// TypeForValue finds a data type suitable for v. Use RegisterType to register types that can encode and decode
// themselves. Use RegisterDefaultPgType to register that can be handled by a registered data type.
func (ci *ConnInfo) DataTypeForValue(v interface{}) (*DataType, bool) {
if ci.reflectTypeToDataType == nil {
ci.buildReflectTypeToDataType()
func (ci *ConnInfo) TypeForValue(v interface{}) (*Type, bool) {
if ci.reflectTypeToType == nil {
ci.buildReflectTypeToType()
}
dt, ok := ci.reflectTypeToDataType[reflect.TypeOf(v)]
dt, ok := ci.reflectTypeToType[reflect.TypeOf(v)]
return dt, ok
}
@@ -1018,11 +1018,11 @@ func (ci *ConnInfo) PlanScan(oid uint32, formatCode int16, target interface{}) S
}
}
var dt *DataType
var dt *Type
if dataType, ok := ci.DataTypeForOID(oid); ok {
if dataType, ok := ci.TypeForOID(oid); ok {
dt = dataType
} else if dataType, ok := ci.DataTypeForValue(target); ok {
} else if dataType, ok := ci.TypeForValue(target); ok {
dt = dataType
oid = dt.OID // Preserve assumed OID in case we are recursively called below.
}
@@ -1123,15 +1123,15 @@ func codecDecodeToTextFormat(codec Codec, ci *ConnInfo, oid uint32, format int16
// found then nil is returned.
func (ci *ConnInfo) PlanEncode(oid uint32, format int16, value interface{}) EncodePlan {
var dt *DataType
var dt *Type
if oid == 0 {
if dataType, ok := ci.DataTypeForValue(value); ok {
if dataType, ok := ci.TypeForValue(value); ok {
dt = dataType
oid = dt.OID // Preserve assumed OID in case we are recursively called below.
}
} else {
if dataType, ok := ci.DataTypeForOID(oid); ok {
if dataType, ok := ci.TypeForOID(oid); ok {
dt = dataType
}
}
+10 -10
View File
@@ -72,11 +72,11 @@ func (r *GenericRange) SetBoundTypes(lower, upper BoundType) error {
// RangeCodec is a codec for any range type.
type RangeCodec struct {
ElementDataType *DataType
ElementType *Type
}
func (c *RangeCodec) FormatSupported(format int16) bool {
return c.ElementDataType.Codec.FormatSupported(format)
return c.ElementType.Codec.FormatSupported(format)
}
func (c *RangeCodec) PreferredFormat() int16 {
@@ -149,7 +149,7 @@ func (plan *encodePlanRangeCodecRangeValuerToBinary) Encode(value interface{}, b
sp := len(buf)
buf = pgio.AppendInt32(buf, -1)
lowerPlan := plan.ci.PlanEncode(plan.rc.ElementDataType.OID, BinaryFormatCode, lower)
lowerPlan := plan.ci.PlanEncode(plan.rc.ElementType.OID, BinaryFormatCode, lower)
if lowerPlan == nil {
return nil, fmt.Errorf("cannot encode %v as element of range", lower)
}
@@ -173,7 +173,7 @@ func (plan *encodePlanRangeCodecRangeValuerToBinary) Encode(value interface{}, b
sp := len(buf)
buf = pgio.AppendInt32(buf, -1)
upperPlan := plan.ci.PlanEncode(plan.rc.ElementDataType.OID, BinaryFormatCode, upper)
upperPlan := plan.ci.PlanEncode(plan.rc.ElementType.OID, BinaryFormatCode, upper)
if upperPlan == nil {
return nil, fmt.Errorf("cannot encode %v as element of range", upper)
}
@@ -223,7 +223,7 @@ func (plan *encodePlanRangeCodecRangeValuerToText) Encode(value interface{}, buf
return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
}
lowerPlan := plan.ci.PlanEncode(plan.rc.ElementDataType.OID, TextFormatCode, lower)
lowerPlan := plan.ci.PlanEncode(plan.rc.ElementType.OID, TextFormatCode, lower)
if lowerPlan == nil {
return nil, fmt.Errorf("cannot encode %v as element of range", lower)
}
@@ -244,7 +244,7 @@ func (plan *encodePlanRangeCodecRangeValuerToText) Encode(value interface{}, buf
return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
}
upperPlan := plan.ci.PlanEncode(plan.rc.ElementDataType.OID, TextFormatCode, upper)
upperPlan := plan.ci.PlanEncode(plan.rc.ElementType.OID, TextFormatCode, upper)
if upperPlan == nil {
return nil, fmt.Errorf("cannot encode %v as element of range", upper)
}
@@ -311,7 +311,7 @@ func (plan *scanPlanBinaryRangeToRangeScanner) Scan(src []byte, target interface
lowerTarget, upperTarget := rangeScanner.ScanBounds()
if ubr.LowerType == Inclusive || ubr.LowerType == Exclusive {
lowerPlan := plan.ci.PlanScan(plan.rc.ElementDataType.OID, BinaryFormatCode, lowerTarget)
lowerPlan := plan.ci.PlanScan(plan.rc.ElementType.OID, BinaryFormatCode, lowerTarget)
if lowerPlan == nil {
return fmt.Errorf("cannot scan into %v from range element", lowerTarget)
}
@@ -323,7 +323,7 @@ func (plan *scanPlanBinaryRangeToRangeScanner) Scan(src []byte, target interface
}
if ubr.UpperType == Inclusive || ubr.UpperType == Exclusive {
upperPlan := plan.ci.PlanScan(plan.rc.ElementDataType.OID, BinaryFormatCode, upperTarget)
upperPlan := plan.ci.PlanScan(plan.rc.ElementType.OID, BinaryFormatCode, upperTarget)
if upperPlan == nil {
return fmt.Errorf("cannot scan into %v from range element", upperTarget)
}
@@ -361,7 +361,7 @@ func (plan *scanPlanTextRangeToRangeScanner) Scan(src []byte, target interface{}
lowerTarget, upperTarget := rangeScanner.ScanBounds()
if utr.LowerType == Inclusive || utr.LowerType == Exclusive {
lowerPlan := plan.ci.PlanScan(plan.rc.ElementDataType.OID, TextFormatCode, lowerTarget)
lowerPlan := plan.ci.PlanScan(plan.rc.ElementType.OID, TextFormatCode, lowerTarget)
if lowerPlan == nil {
return fmt.Errorf("cannot scan into %v from range element", lowerTarget)
}
@@ -373,7 +373,7 @@ func (plan *scanPlanTextRangeToRangeScanner) Scan(src []byte, target interface{}
}
if utr.UpperType == Inclusive || utr.UpperType == Exclusive {
upperPlan := plan.ci.PlanScan(plan.rc.ElementDataType.OID, TextFormatCode, upperTarget)
upperPlan := plan.ci.PlanScan(plan.rc.ElementType.OID, TextFormatCode, upperTarget)
if upperPlan == nil {
return fmt.Errorf("cannot scan into %v from range element", upperTarget)
}
+1 -1
View File
@@ -257,7 +257,7 @@ func (rows *connRows) Values() ([]interface{}, error) {
continue
}
if dt, ok := rows.connInfo.DataTypeForOID(fd.DataTypeOID); ok {
if dt, ok := rows.connInfo.TypeForOID(fd.DataTypeOID); ok {
value, err := dt.Codec.DecodeValue(rows.connInfo, fd.DataTypeOID, fd.Format, buf)
if err != nil {
rows.fatal(err)
+1 -1
View File
@@ -519,7 +519,7 @@ func (r *Rows) Columns() []string {
// ColumnTypeDatabaseTypeName returns the database system type name. If the name is unknown the OID is returned.
func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {
if dt, ok := r.conn.conn.ConnInfo().DataTypeForOID(r.rows.FieldDescriptions()[index].DataTypeOID); ok {
if dt, ok := r.conn.conn.ConnInfo().TypeForOID(r.rows.FieldDescriptions()[index].DataTypeOID); ok {
return strings.ToUpper(dt.Name)
}
+2 -2
View File
@@ -79,7 +79,7 @@ func convertSimpleArgument(ci *pgtype.ConnInfo, arg interface{}) (interface{}, e
return int64(arg), nil
}
if _, found := ci.DataTypeForValue(arg); found {
if _, found := ci.TypeForValue(arg); found {
buf, err := ci.Encode(0, TextFormatCode, arg, nil)
if err != nil {
return nil, err
@@ -123,7 +123,7 @@ func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, oid uint32
return encodePreparedStatementArgument(ci, buf, oid, arg)
}
if _, ok := ci.DataTypeForOID(oid); ok {
if _, ok := ci.TypeForOID(oid); ok {
sp := len(buf)
buf = pgio.AppendInt32(buf, -1)
argBuf, err := ci.Encode(oid, BinaryFormatCode, arg, buf)
+2 -2
View File
@@ -79,7 +79,7 @@ func TestJSONAndJSONBTranscode(t *testing.T) {
testWithAndWithoutPreferSimpleProtocol(t, func(t *testing.T, conn *pgx.Conn) {
for _, typename := range []string{"json", "jsonb"} {
if _, ok := conn.ConnInfo().DataTypeForName(typename); !ok {
if _, ok := conn.ConnInfo().TypeForName(typename); !ok {
continue // No JSON/JSONB type -- must be running against old PostgreSQL
}
@@ -96,7 +96,7 @@ func TestJSONAndJSONBTranscodeExtendedOnly(t *testing.T) {
defer closeConn(t, conn)
for _, typename := range []string{"json", "jsonb"} {
if _, ok := conn.ConnInfo().DataTypeForName(typename); !ok {
if _, ok := conn.ConnInfo().TypeForName(typename); !ok {
continue // No JSON/JSONB type -- must be running against old PostgreSQL
}
testJSONSingleLevelStringMap(t, conn, typename)