Adds Cid/NullCid type

pull/179/head
Manni Wood 2016-09-05 12:15:34 -04:00
parent 7adabc9b93
commit 60ab3403ab
2 changed files with 105 additions and 0 deletions

101
values.go
View File

@ -23,6 +23,7 @@ const (
TextOid = 25
OidOid = 26
XidOid = 28
CidOid = 29
JsonOid = 114
CidrOid = 650
CidrArrayOid = 651
@ -95,6 +96,7 @@ func init() {
"int8": BinaryFormatCode,
"oid": BinaryFormatCode,
"xid": BinaryFormatCode,
"cid": BinaryFormatCode,
"record": BinaryFormatCode,
"text": BinaryFormatCode,
"timestamp": BinaryFormatCode,
@ -384,6 +386,58 @@ func (n NullXid) Encode(w *WriteBuf, oid Oid) error {
return encodeXid(w, oid, n.Xid)
}
// Cid is PostgreSQL's Command Identifier type.
//
// When one does
//
// select cmin, cmax, * from some_table;
//
// it is the data type of the cmin and cmax hidden system columns.
//
// It is currently implemented as an unsigned for byte integer.
// Its definition can be found in src/include/c.h as CommandId
// in the PostgreSQL sources.
type Cid uint32
// NullCid represents a Command Identifier (Cid) that may be null. NullCid implements the
// Scanner and Encoder interfaces so it may be used both as an argument to
// Query[Row] and a destination for Scan.
//
// If Valid is false then the value is NULL.
type NullCid struct {
Cid Cid
Valid bool // Valid is true if Int32 is not NULL
}
func (n *NullCid) Scan(vr *ValueReader) error {
if vr.Type().DataType != CidOid {
return SerializationError(fmt.Sprintf("NullCid.Scan cannot decode OID %d", vr.Type().DataType))
}
if vr.Len() == -1 {
n.Cid, n.Valid = 0, false
return nil
}
n.Valid = true
n.Cid = decodeCid(vr)
return vr.Err()
}
func (n NullCid) FormatCode() int16 { return BinaryFormatCode }
func (n NullCid) Encode(w *WriteBuf, oid Oid) error {
if oid != CidOid {
return SerializationError(fmt.Sprintf("NullCid.Encode cannot encode into OID %d", oid))
}
if !n.Valid {
w.WriteInt32(-1)
return nil
}
return encodeCid(w, oid, n.Cid)
}
// NullInt64 represents an bigint that may be null. NullInt64 implements the
// Scanner and Encoder interfaces so it may be used both as an argument to
// Query[Row] and a destination for Scan.
@ -750,6 +804,8 @@ func Encode(wbuf *WriteBuf, oid Oid, arg interface{}) error {
return encodeOid(wbuf, oid, arg)
case Xid:
return encodeXid(wbuf, oid, arg)
case Cid:
return encodeCid(wbuf, oid, arg)
default:
if strippedArg, ok := stripNamedType(&refVal); ok {
return Encode(wbuf, oid, strippedArg)
@ -876,6 +932,8 @@ func Decode(vr *ValueReader, d interface{}) error {
*v = decodeOid(vr)
case *Xid:
*v = decodeXid(vr)
case *Cid:
*v = decodeCid(vr)
case *string:
*v = decodeText(vr)
case *float32:
@ -1443,6 +1501,49 @@ func encodeXid(w *WriteBuf, oid Oid, value Xid) error {
return nil
}
func decodeCid(vr *ValueReader) Cid {
if vr.Len() == -1 {
vr.Fatal(ProtocolError("Cannot decode null into Cid"))
return Cid(0)
}
if vr.Type().DataType != CidOid {
vr.Fatal(ProtocolError(fmt.Sprintf("Cannot decode oid %v into pgx.Cid", vr.Type().DataType)))
return Cid(0)
}
// Unlikely Cid will ever go over the wire as text format, but who knows?
switch vr.Type().FormatCode {
case TextFormatCode:
s := vr.ReadString(vr.Len())
n, err := strconv.ParseUint(s, 10, 32)
if err != nil {
vr.Fatal(ProtocolError(fmt.Sprintf("Received invalid Oid: %v", s)))
}
return Cid(n)
case BinaryFormatCode:
if vr.Len() != 4 {
vr.Fatal(ProtocolError(fmt.Sprintf("Received an invalid size for an Oid: %d", vr.Len())))
return Cid(0)
}
return Cid(vr.ReadUint32())
default:
vr.Fatal(ProtocolError(fmt.Sprintf("Unknown field description format code: %v", vr.Type().FormatCode)))
return Cid(0)
}
}
func encodeCid(w *WriteBuf, oid Oid, value Cid) error {
if oid != CidOid {
return fmt.Errorf("cannot encode Go %s into oid %d", "pgx.Cid", oid)
}
w.WriteInt32(4)
w.WriteUint32(uint32(value))
return nil
}
func decodeFloat4(vr *ValueReader) float32 {
if vr.Len() == -1 {
vr.Fatal(ProtocolError("Cannot decode null into float32"))

View File

@ -595,6 +595,7 @@ func TestNullX(t *testing.T) {
i16 pgx.NullInt16
i32 pgx.NullInt32
xid pgx.NullXid
cid pgx.NullCid
i64 pgx.NullInt64
f32 pgx.NullFloat32
f64 pgx.NullFloat64
@ -619,6 +620,9 @@ func TestNullX(t *testing.T) {
{"select $1::xid", []interface{}{pgx.NullXid{Xid: 1, Valid: true}}, []interface{}{&actual.xid}, allTypes{xid: pgx.NullXid{Xid: 1, Valid: true}}},
{"select $1::xid", []interface{}{pgx.NullXid{Xid: 1, Valid: false}}, []interface{}{&actual.xid}, allTypes{xid: pgx.NullXid{Xid: 0, Valid: false}}},
{"select $1::xid", []interface{}{pgx.NullXid{Xid: 4294967295, Valid: true}}, []interface{}{&actual.xid}, allTypes{xid: pgx.NullXid{Xid: 4294967295, Valid: true}}},
{"select $1::cid", []interface{}{pgx.NullCid{Cid: 1, Valid: true}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 1, Valid: true}}},
{"select $1::cid", []interface{}{pgx.NullCid{Cid: 1, Valid: false}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 0, Valid: false}}},
{"select $1::cid", []interface{}{pgx.NullCid{Cid: 4294967295, Valid: true}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 4294967295, Valid: true}}},
{"select $1::int8", []interface{}{pgx.NullInt64{Int64: 1, Valid: true}}, []interface{}{&actual.i64}, allTypes{i64: pgx.NullInt64{Int64: 1, Valid: true}}},
{"select $1::int8", []interface{}{pgx.NullInt64{Int64: 1, Valid: false}}, []interface{}{&actual.i64}, allTypes{i64: pgx.NullInt64{Int64: 0, Valid: false}}},
{"select $1::float4", []interface{}{pgx.NullFloat32{Float32: 1.23, Valid: true}}, []interface{}{&actual.f32}, allTypes{f32: pgx.NullFloat32{Float32: 1.23, Valid: true}}},