Skip to content

Commit

Permalink
Add format code helpers to ConnInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
jackc committed Aug 25, 2019
1 parent 7d83f9b commit b1e25e4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pgtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,17 @@ type ConnInfo struct {
oidToDataType map[uint32]*DataType
nameToDataType map[string]*DataType
reflectTypeToDataType map[reflect.Type]*DataType
oidToParamFormatCode map[uint32]int16
oidToResultFormatCode map[uint32]int16
}

func NewConnInfo() *ConnInfo {
ci := &ConnInfo{
oidToDataType: make(map[uint32]*DataType, 128),
nameToDataType: make(map[string]*DataType, 128),
reflectTypeToDataType: make(map[reflect.Type]*DataType, 128),
oidToParamFormatCode: make(map[uint32]int16, 128),
oidToResultFormatCode: make(map[uint32]int16, 128),
}

ci.RegisterDataType(DataType{Value: &ACLItemArray{}, Name: "_aclitem", OID: ACLItemArrayOID})
Expand Down Expand Up @@ -262,6 +266,22 @@ func (ci *ConnInfo) RegisterDataType(t DataType) {
ci.oidToDataType[t.OID] = &t
ci.nameToDataType[t.Name] = &t
ci.reflectTypeToDataType[reflect.ValueOf(t.Value).Type()] = &t

{
var formatCode int16
if _, ok := t.Value.(BinaryEncoder); ok {
formatCode = BinaryFormatCode
}
ci.oidToParamFormatCode[t.OID] = formatCode
}

{
var formatCode int16
if _, ok := t.Value.(BinaryDecoder); ok {
formatCode = BinaryFormatCode
}
ci.oidToResultFormatCode[t.OID] = formatCode
}
}

func (ci *ConnInfo) DataTypeForOID(oid uint32) (*DataType, bool) {
Expand All @@ -279,6 +299,22 @@ func (ci *ConnInfo) DataTypeForValue(v Value) (*DataType, bool) {
return dt, ok
}

func (ci *ConnInfo) ParamFormatCodeForOID(oid uint32) int16 {
fc, ok := ci.oidToParamFormatCode[oid]
if ok {
return fc
}
return TextFormatCode
}

func (ci *ConnInfo) ResultFormatCodeForOID(oid uint32) int16 {
fc, ok := ci.oidToResultFormatCode[oid]
if ok {
return fc
}
return TextFormatCode
}

// DeepCopy makes a deep copy of the ConnInfo.
func (ci *ConnInfo) DeepCopy() *ConnInfo {
ci2 := &ConnInfo{
Expand Down

0 comments on commit b1e25e4

Please sign in to comment.