mirror of
https://github.com/jackc/pgx.git
synced 2025-05-09 17:10:08 +00:00
Because reading a record type requires the decoder to be able to look up oid to type mapping and types such as hstore have types that are not fixed between different PostgreSQL servers it was necessary to restructure the pgtype system so all encoders and decodes take a *ConnInfo that includes oid/name/type information.
124 lines
2.4 KiB
Go
124 lines
2.4 KiB
Go
package pgtype
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/pgio"
|
|
)
|
|
|
|
// Tid is PostgreSQL's Tuple Identifier type.
|
|
//
|
|
// When one does
|
|
//
|
|
// select ctid, * from some_table;
|
|
//
|
|
// it is the data type of the ctid hidden system column.
|
|
//
|
|
// It is currently implemented as a pair unsigned two byte integers.
|
|
// Its conversion functions can be found in src/backend/utils/adt/tid.c
|
|
// in the PostgreSQL sources.
|
|
type Tid struct {
|
|
BlockNumber uint32
|
|
OffsetNumber uint16
|
|
Status Status
|
|
}
|
|
|
|
func (dst *Tid) Set(src interface{}) error {
|
|
return fmt.Errorf("cannot convert %v to Tid", src)
|
|
}
|
|
|
|
func (dst *Tid) Get() interface{} {
|
|
switch dst.Status {
|
|
case Present:
|
|
return dst
|
|
case Null:
|
|
return nil
|
|
default:
|
|
return dst.Status
|
|
}
|
|
}
|
|
|
|
func (src *Tid) AssignTo(dst interface{}) error {
|
|
return fmt.Errorf("cannot assign %v to %T", src, dst)
|
|
}
|
|
|
|
func (dst *Tid) DecodeText(ci *ConnInfo, src []byte) error {
|
|
if src == nil {
|
|
*dst = Tid{Status: Null}
|
|
return nil
|
|
}
|
|
|
|
if len(src) < 5 {
|
|
return fmt.Errorf("invalid length for tid: %v", len(src))
|
|
}
|
|
|
|
parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2)
|
|
if len(parts) < 2 {
|
|
return fmt.Errorf("invalid format for tid")
|
|
}
|
|
|
|
blockNumber, err := strconv.ParseUint(parts[0], 10, 32)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
offsetNumber, err := strconv.ParseUint(parts[1], 10, 16)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*dst = Tid{BlockNumber: uint32(blockNumber), OffsetNumber: uint16(offsetNumber), Status: Present}
|
|
return nil
|
|
}
|
|
|
|
func (dst *Tid) DecodeBinary(ci *ConnInfo, src []byte) error {
|
|
if src == nil {
|
|
*dst = Tid{Status: Null}
|
|
return nil
|
|
}
|
|
|
|
if len(src) != 6 {
|
|
return fmt.Errorf("invalid length for tid: %v", len(src))
|
|
}
|
|
|
|
*dst = Tid{
|
|
BlockNumber: binary.BigEndian.Uint32(src),
|
|
OffsetNumber: binary.BigEndian.Uint16(src[4:]),
|
|
Status: Present,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (src Tid) EncodeText(ci *ConnInfo, w io.Writer) (bool, error) {
|
|
switch src.Status {
|
|
case Null:
|
|
return true, nil
|
|
case Undefined:
|
|
return false, errUndefined
|
|
}
|
|
|
|
_, err := io.WriteString(w, fmt.Sprintf(`(%d,%d)`, src.BlockNumber, src.OffsetNumber))
|
|
return false, err
|
|
}
|
|
|
|
func (src Tid) EncodeBinary(ci *ConnInfo, w io.Writer) (bool, error) {
|
|
switch src.Status {
|
|
case Null:
|
|
return true, nil
|
|
case Undefined:
|
|
return false, errUndefined
|
|
}
|
|
|
|
_, err := pgio.WriteUint32(w, src.BlockNumber)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
_, err = pgio.WriteUint16(w, src.OffsetNumber)
|
|
return false, err
|
|
}
|