From b1e25e4ea49c995a914679a7e85d47b4101f2ed9 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sun, 25 Aug 2019 00:32:11 -0500 Subject: [PATCH] Add format code helpers to ConnInfo --- pgtype.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pgtype.go b/pgtype.go index 6e187ae4..3391a04f 100644 --- a/pgtype.go +++ b/pgtype.go @@ -170,6 +170,8 @@ 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 { @@ -177,6 +179,8 @@ func NewConnInfo() *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}) @@ -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) { @@ -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{