Use Go casing convention for OID

non-blocking
Jack Christensen 2017-06-03 11:53:49 -05:00
parent 071de0b674
commit 2140814606
23 changed files with 119 additions and 119 deletions

View File

@ -18,7 +18,7 @@ import (
type ArrayHeader struct {
ContainsNull bool
ElementOid int32
ElementOID int32
Dimensions []ArrayDimension
}
@ -40,7 +40,7 @@ func (dst *ArrayHeader) DecodeBinary(ci *ConnInfo, src []byte) (int, error) {
dst.ContainsNull = binary.BigEndian.Uint32(src[rp:]) == 1
rp += 4
dst.ElementOid = int32(binary.BigEndian.Uint32(src[rp:]))
dst.ElementOID = int32(binary.BigEndian.Uint32(src[rp:]))
rp += 4
if numDims > 0 {
@ -69,7 +69,7 @@ func (src *ArrayHeader) EncodeBinary(ci *ConnInfo, buf []byte) []byte {
}
buf = pgio.AppendInt32(buf, containsNull)
buf = pgio.AppendInt32(buf, src.ElementOid)
buf = pgio.AppendInt32(buf, src.ElementOID)
for i := range src.Dimensions {
buf = pgio.AppendInt32(buf, src.Dimensions[i].Length)

View File

@ -231,7 +231,7 @@ func (src *BoolArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("bool"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "bool")
}

View File

@ -231,7 +231,7 @@ func (src *ByteaArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("bytea"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "bytea")
}

View File

@ -260,7 +260,7 @@ func (src *CidrArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("cidr"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "cidr")
}

View File

@ -232,7 +232,7 @@ func (src *DateArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("date"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "date")
}

View File

@ -231,7 +231,7 @@ func (src *Float4Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("float4"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "float4")
}

View File

@ -231,7 +231,7 @@ func (src *Float8Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("float8"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "float8")
}

View File

@ -231,7 +231,7 @@ func (src *HstoreArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("hstore"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "hstore")
}

View File

@ -260,7 +260,7 @@ func (src *InetArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("inet"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "inet")
}

View File

@ -259,7 +259,7 @@ func (src *Int2Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("int2"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "int2")
}

View File

@ -259,7 +259,7 @@ func (src *Int4Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("int4"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "int4")
}

View File

@ -259,7 +259,7 @@ func (src *Int8Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("int8"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "int8")
}

View File

@ -259,7 +259,7 @@ func (src *NumericArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error)
}
if dt, ok := ci.DataTypeForName("numeric"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "numeric")
}

30
oid.go
View File

@ -9,18 +9,18 @@ import (
"github.com/jackc/pgx/pgio"
)
// Oid (Object Identifier Type) is, according to
// OID (Object Identifier Type) is, according to
// https://www.postgresql.org/docs/current/static/datatype-oid.html, used
// internally by PostgreSQL as a primary key for various system tables. It is
// currently implemented as an unsigned four-byte integer. Its definition can be
// found in src/include/postgres_ext.h in the PostgreSQL sources. Because it is
// so frequently required to be in a NOT NULL condition Oid cannot be NULL. To
// allow for NULL Oids use OidValue.
type Oid uint32
// so frequently required to be in a NOT NULL condition OID cannot be NULL. To
// allow for NULL OIDs use OIDValue.
type OID uint32
func (dst *Oid) DecodeText(ci *ConnInfo, src []byte) error {
func (dst *OID) DecodeText(ci *ConnInfo, src []byte) error {
if src == nil {
return fmt.Errorf("cannot decode nil into Oid")
return fmt.Errorf("cannot decode nil into OID")
}
n, err := strconv.ParseUint(string(src), 10, 32)
@ -28,13 +28,13 @@ func (dst *Oid) DecodeText(ci *ConnInfo, src []byte) error {
return err
}
*dst = Oid(n)
*dst = OID(n)
return nil
}
func (dst *Oid) DecodeBinary(ci *ConnInfo, src []byte) error {
func (dst *OID) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil {
return fmt.Errorf("cannot decode nil into Oid")
return fmt.Errorf("cannot decode nil into OID")
}
if len(src) != 4 {
@ -42,27 +42,27 @@ func (dst *Oid) DecodeBinary(ci *ConnInfo, src []byte) error {
}
n := binary.BigEndian.Uint32(src)
*dst = Oid(n)
*dst = OID(n)
return nil
}
func (src Oid) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src OID) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return append(buf, strconv.FormatUint(uint64(src), 10)...), nil
}
func (src Oid) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src OID) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return pgio.AppendUint32(buf, uint32(src)), nil
}
// Scan implements the database/sql Scanner interface.
func (dst *Oid) Scan(src interface{}) error {
func (dst *OID) Scan(src interface{}) error {
if src == nil {
return fmt.Errorf("cannot scan NULL into %T", src)
}
switch src := src.(type) {
case int64:
*dst = Oid(src)
*dst = OID(src)
return nil
case string:
return dst.DecodeText(nil, []byte(src))
@ -76,6 +76,6 @@ func (dst *Oid) Scan(src interface{}) error {
}
// Value implements the database/sql/driver Valuer interface.
func (src Oid) Value() (driver.Value, error) {
func (src OID) Value() (driver.Value, error) {
return int64(src), nil
}

View File

@ -4,52 +4,52 @@ import (
"database/sql/driver"
)
// OidValue (Object Identifier Type) is, according to
// https://www.postgresql.org/docs/current/static/datatype-OidValue.html, used
// OIDValue (Object Identifier Type) is, according to
// https://www.postgresql.org/docs/current/static/datatype-OIDValue.html, used
// internally by PostgreSQL as a primary key for various system tables. It is
// currently implemented as an unsigned four-byte integer. Its definition can be
// found in src/include/postgres_ext.h in the PostgreSQL sources.
type OidValue pguint32
type OIDValue pguint32
// Set converts from src to dst. Note that as OidValue is not a general
// Set converts from src to dst. Note that as OIDValue is not a general
// number type Set does not do automatic type conversion as other number
// types do.
func (dst *OidValue) Set(src interface{}) error {
func (dst *OIDValue) Set(src interface{}) error {
return (*pguint32)(dst).Set(src)
}
func (dst *OidValue) Get() interface{} {
func (dst *OIDValue) Get() interface{} {
return (*pguint32)(dst).Get()
}
// AssignTo assigns from src to dst. Note that as OidValue is not a general number
// AssignTo assigns from src to dst. Note that as OIDValue is not a general number
// type AssignTo does not do automatic type conversion as other number types do.
func (src *OidValue) AssignTo(dst interface{}) error {
func (src *OIDValue) AssignTo(dst interface{}) error {
return (*pguint32)(src).AssignTo(dst)
}
func (dst *OidValue) DecodeText(ci *ConnInfo, src []byte) error {
func (dst *OIDValue) DecodeText(ci *ConnInfo, src []byte) error {
return (*pguint32)(dst).DecodeText(ci, src)
}
func (dst *OidValue) DecodeBinary(ci *ConnInfo, src []byte) error {
func (dst *OIDValue) DecodeBinary(ci *ConnInfo, src []byte) error {
return (*pguint32)(dst).DecodeBinary(ci, src)
}
func (src *OidValue) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src *OIDValue) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*pguint32)(src).EncodeText(ci, buf)
}
func (src *OidValue) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src *OIDValue) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*pguint32)(src).EncodeBinary(ci, buf)
}
// Scan implements the database/sql Scanner interface.
func (dst *OidValue) Scan(src interface{}) error {
func (dst *OIDValue) Scan(src interface{}) error {
return (*pguint32)(dst).Scan(src)
}
// Value implements the database/sql/driver Valuer interface.
func (src *OidValue) Value() (driver.Value, error) {
func (src *OIDValue) Value() (driver.Value, error) {
return (*pguint32)(src).Value()
}

View File

@ -8,23 +8,23 @@ import (
"github.com/jackc/pgx/pgtype/testutil"
)
func TestOidValueTranscode(t *testing.T) {
func TestOIDValueTranscode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "oid", []interface{}{
&pgtype.OidValue{Uint: 42, Status: pgtype.Present},
&pgtype.OidValue{Status: pgtype.Null},
&pgtype.OIDValue{Uint: 42, Status: pgtype.Present},
&pgtype.OIDValue{Status: pgtype.Null},
})
}
func TestOidValueSet(t *testing.T) {
func TestOIDValueSet(t *testing.T) {
successfulTests := []struct {
source interface{}
result pgtype.OidValue
result pgtype.OIDValue
}{
{source: uint32(1), result: pgtype.OidValue{Uint: 1, Status: pgtype.Present}},
{source: uint32(1), result: pgtype.OIDValue{Uint: 1, Status: pgtype.Present}},
}
for i, tt := range successfulTests {
var r pgtype.OidValue
var r pgtype.OIDValue
err := r.Set(tt.source)
if err != nil {
t.Errorf("%d: %v", i, err)
@ -36,17 +36,17 @@ func TestOidValueSet(t *testing.T) {
}
}
func TestOidValueAssignTo(t *testing.T) {
func TestOIDValueAssignTo(t *testing.T) {
var ui32 uint32
var pui32 *uint32
simpleTests := []struct {
src pgtype.OidValue
src pgtype.OIDValue
dst interface{}
expected interface{}
}{
{src: pgtype.OidValue{Uint: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
{src: pgtype.OidValue{Status: pgtype.Null}, dst: &pui32, expected: ((*uint32)(nil))},
{src: pgtype.OIDValue{Uint: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
{src: pgtype.OIDValue{Status: pgtype.Null}, dst: &pui32, expected: ((*uint32)(nil))},
}
for i, tt := range simpleTests {
@ -61,11 +61,11 @@ func TestOidValueAssignTo(t *testing.T) {
}
pointerAllocTests := []struct {
src pgtype.OidValue
src pgtype.OIDValue
dst interface{}
expected interface{}
}{
{src: pgtype.OidValue{Uint: 42, Status: pgtype.Present}, dst: &pui32, expected: uint32(42)},
{src: pgtype.OIDValue{Uint: 42, Status: pgtype.Present}, dst: &pui32, expected: uint32(42)},
}
for i, tt := range pointerAllocTests {
@ -80,10 +80,10 @@ func TestOidValueAssignTo(t *testing.T) {
}
errorTests := []struct {
src pgtype.OidValue
src pgtype.OIDValue
dst interface{}
}{
{src: pgtype.OidValue{Status: pgtype.Null}, dst: &ui32},
{src: pgtype.OIDValue{Status: pgtype.Null}, dst: &ui32},
}
for i, tt := range errorTests {

104
pgtype.go
View File

@ -7,47 +7,47 @@ import (
// PostgreSQL oids for common types
const (
BoolOid = 16
ByteaOid = 17
CharOid = 18
NameOid = 19
Int8Oid = 20
Int2Oid = 21
Int4Oid = 23
TextOid = 25
OidOid = 26
TidOid = 27
XidOid = 28
CidOid = 29
JsonOid = 114
CidrOid = 650
CidrArrayOid = 651
Float4Oid = 700
Float8Oid = 701
UnknownOid = 705
InetOid = 869
BoolArrayOid = 1000
Int2ArrayOid = 1005
Int4ArrayOid = 1007
TextArrayOid = 1009
ByteaArrayOid = 1001
VarcharArrayOid = 1015
Int8ArrayOid = 1016
Float4ArrayOid = 1021
Float8ArrayOid = 1022
AclitemOid = 1033
AclitemArrayOid = 1034
InetArrayOid = 1041
VarcharOid = 1043
DateOid = 1082
TimestampOid = 1114
TimestampArrayOid = 1115
DateArrayOid = 1182
TimestamptzOid = 1184
TimestamptzArrayOid = 1185
RecordOid = 2249
UuidOid = 2950
JsonbOid = 3802
BoolOID = 16
ByteaOID = 17
CharOID = 18
NameOID = 19
Int8OID = 20
Int2OID = 21
Int4OID = 23
TextOID = 25
OIDOID = 26
TidOID = 27
XidOID = 28
CidOID = 29
JsonOID = 114
CidrOID = 650
CidrArrayOID = 651
Float4OID = 700
Float8OID = 701
UnknownOID = 705
InetOID = 869
BoolArrayOID = 1000
Int2ArrayOID = 1005
Int4ArrayOID = 1007
TextArrayOID = 1009
ByteaArrayOID = 1001
VarcharArrayOID = 1015
Int8ArrayOID = 1016
Float4ArrayOID = 1021
Float8ArrayOID = 1022
AclitemOID = 1033
AclitemArrayOID = 1034
InetArrayOID = 1041
VarcharOID = 1043
DateOID = 1082
TimestampOID = 1114
TimestampArrayOID = 1115
DateArrayOID = 1182
TimestamptzOID = 1184
TimestamptzArrayOID = 1185
RecordOID = 2249
UuidOID = 2950
JsonbOID = 3802
)
type Status byte
@ -133,42 +133,42 @@ var errBadStatus = errors.New("invalid status")
type DataType struct {
Value Value
Name string
Oid Oid
OID OID
}
type ConnInfo struct {
oidToDataType map[Oid]*DataType
oidToDataType map[OID]*DataType
nameToDataType map[string]*DataType
reflectTypeToDataType map[reflect.Type]*DataType
}
func NewConnInfo() *ConnInfo {
return &ConnInfo{
oidToDataType: make(map[Oid]*DataType, 256),
oidToDataType: make(map[OID]*DataType, 256),
nameToDataType: make(map[string]*DataType, 256),
reflectTypeToDataType: make(map[reflect.Type]*DataType, 256),
}
}
func (ci *ConnInfo) InitializeDataTypes(nameOids map[string]Oid) {
for name, oid := range nameOids {
func (ci *ConnInfo) InitializeDataTypes(nameOIDs map[string]OID) {
for name, oid := range nameOIDs {
var value Value
if t, ok := nameValues[name]; ok {
value = reflect.New(reflect.ValueOf(t).Elem().Type()).Interface().(Value)
} else {
value = &GenericText{}
}
ci.RegisterDataType(DataType{Value: value, Name: name, Oid: oid})
ci.RegisterDataType(DataType{Value: value, Name: name, OID: oid})
}
}
func (ci *ConnInfo) RegisterDataType(t DataType) {
ci.oidToDataType[t.Oid] = &t
ci.oidToDataType[t.OID] = &t
ci.nameToDataType[t.Name] = &t
ci.reflectTypeToDataType[reflect.ValueOf(t.Value).Type()] = &t
}
func (ci *ConnInfo) DataTypeForOid(oid Oid) (*DataType, bool) {
func (ci *ConnInfo) DataTypeForOID(oid OID) (*DataType, bool) {
dt, ok := ci.oidToDataType[oid]
return dt, ok
}
@ -186,7 +186,7 @@ func (ci *ConnInfo) DataTypeForValue(v Value) (*DataType, bool) {
// DeepCopy makes a deep copy of the ConnInfo.
func (ci *ConnInfo) DeepCopy() *ConnInfo {
ci2 := &ConnInfo{
oidToDataType: make(map[Oid]*DataType, len(ci.oidToDataType)),
oidToDataType: make(map[OID]*DataType, len(ci.oidToDataType)),
nameToDataType: make(map[string]*DataType, len(ci.nameToDataType)),
reflectTypeToDataType: make(map[reflect.Type]*DataType, len(ci.reflectTypeToDataType)),
}
@ -195,7 +195,7 @@ func (ci *ConnInfo) DeepCopy() *ConnInfo {
ci2.RegisterDataType(DataType{
Value: reflect.New(reflect.ValueOf(dt.Value).Elem().Type()).Interface().(Value),
Name: dt.Name,
Oid: dt.Oid,
OID: dt.OID,
})
}
@ -250,7 +250,7 @@ func init() {
"name": &Name{},
"numeric": &Numeric{},
"numrange": &Numrange{},
"oid": &OidValue{},
"oid": &OIDValue{},
"path": &Path{},
"point": &Point{},
"polygon": &Polygon{},

View File

@ -88,16 +88,16 @@ func (dst *Record) DecodeBinary(ci *ConnInfo, src []byte) error {
if len(src[rp:]) < 8 {
return fmt.Errorf("Record incomplete %v", src)
}
fieldOid := Oid(binary.BigEndian.Uint32(src[rp:]))
fieldOID := OID(binary.BigEndian.Uint32(src[rp:]))
rp += 4
fieldLen := int(int32(binary.BigEndian.Uint32(src[rp:])))
rp += 4
var binaryDecoder BinaryDecoder
if dt, ok := ci.DataTypeForOid(fieldOid); ok {
if dt, ok := ci.DataTypeForOID(fieldOID); ok {
if binaryDecoder, ok = dt.Value.(BinaryDecoder); !ok {
return fmt.Errorf("unknown oid while decoding record: %v", fieldOid)
return fmt.Errorf("unknown oid while decoding record: %v", fieldOID)
}
}

View File

@ -231,7 +231,7 @@ func (src *TextArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if dt, ok := ci.DataTypeForName("text"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "text")
}

View File

@ -232,7 +232,7 @@ func (src *TimestampArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error
}
if dt, ok := ci.DataTypeForName("timestamp"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "timestamp")
}

View File

@ -232,7 +232,7 @@ func (src *TimestamptzArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, err
}
if dt, ok := ci.DataTypeForName("timestamptz"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "timestamptz")
}

View File

@ -234,7 +234,7 @@ func (src *<%= pgtype_array_type %>) EncodeText(ci *ConnInfo, buf []byte) ([]byt
}
if dt, ok := ci.DataTypeForName("<%= element_type_name %>"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "<%= element_type_name %>")
}

View File

@ -231,7 +231,7 @@ func (src *VarcharArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error)
}
if dt, ok := ci.DataTypeForName("varchar"); ok {
arrayHeader.ElementOid = int32(dt.Oid)
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "varchar")
}