Documentation
¶
Overview ¶
Package types maps PostgreSQL type OIDs to JSON-emitting encoder functions. Encoders take the raw text-format bytes from a DataRow cell and append the JSON form to the destination buffer.
The contract:
- raw == nil means SQL NULL; the encoder writes the JSON literal null.
- the returned slice is dst with the cell appended. No allocations for the common cases (numbers, bools, json passthrough).
- encoders never retain raw beyond their own call.
Index ¶
- Variables
- func EncodeBool(dst, raw []byte) []byte
- func EncodeBytea(dst, raw []byte) []byte
- func EncodeFloat(dst, raw []byte) []byte
- func EncodeInt(dst, raw []byte) []byte
- func EncodeJSONPassthrough(dst, raw []byte) []byte
- func EncodeNumeric(dst, raw []byte) []byte
- func EncodeOpaqueBinary(dst, raw []byte) []byte
- func EncodeQuotedASCII(dst, raw []byte) []byte
- func EncodeString(dst, raw []byte) []byte
- func EncodeUUID(dst, raw []byte) []byte
- func HasBinary(oid protocol.OID) bool
- func HasBinaryEx(oid protocol.OID, binaryNumeric bool) bool
- type Encoder
Constants ¶
This section is empty.
Variables ¶
var EncodeNumericBinary = encodeNumericBinary
EncodeNumericBinary is the exported alias for encodeNumericBinary. The default PickBinary dispatch does NOT pick it — on loopback the text path is faster for typical precisions. Callers that want binary numeric (wide precision on a high-RTT link) can build a custom Encoder table referencing this symbol directly.
Functions ¶
func EncodeBool ¶
EncodeBool: text protocol returns "t" or "f".
func EncodeBytea ¶
EncodeBytea handles both bytea_output forms:
- "hex" (default since 9.0): \xDEADBEEF
- "escape" (legacy): octal-escaped ASCII
In hex form, the only character that needs JSON escaping is the leading backslash; we write `"\\x...."` directly. In escape form we fall through to the generic string escaper.
func EncodeFloat ¶
EncodeFloat handles float4/float8. Postgres can emit "NaN", "Infinity", "-Infinity" — none of which are valid JSON numbers, so we string-quote them. Everything else is a valid JSON number and passes through.
func EncodeInt ¶
EncodeInt validates and emits an integer as a raw JSON number. The validation is cheap (a single pass) and lets us drop the bytes in without strconv.
func EncodeJSONPassthrough ¶
EncodeJSONPassthrough drops json/jsonb's text representation directly into the output. PostgreSQL guarantees the text form is valid JSON (jsonb is canonicalised; json is validated at insert time). We trust that contract, which is the whole point of this fast path.
func EncodeNumeric ¶
EncodeNumeric: same shape as float, but Postgres' numeric never uses exponential notation by default and "NaN" is the only special form.
func EncodeOpaqueBinary ¶
EncodeOpaqueBinary emits the raw binary bytes as a hex-encoded JSON string with a "\\x" prefix, matching PG's own bytea text form. Used as a safe JSON-output fallback when the caller requested binary format for an OID that has no specialised decoder (typical for user-declared composite types in Config.BinaryOIDs). Human-readable enough to debug, always valid JSON.
func EncodeQuotedASCII ¶
EncodeQuotedASCII is a fast path for values we know are pure ASCII with no characters that need escaping (timestamps, dates, intervals as the server formats them). We still validate cheaply.
func EncodeString ¶
EncodeString quotes and escapes the raw bytes as a JSON string.
func EncodeUUID ¶
EncodeUUID writes a quoted UUID. UUID text form is exactly 36 ASCII characters (8-4-4-4-12 hex with dashes). We do a length check for safety but skip per-byte validation: if the server gave us 36 ASCII chars labelled "uuid" we trust them.
Types ¶
type Encoder ¶
func Pick ¶
Pick returns the text-format encoder for the given OID. Anything we do not specifically know about is rendered as a JSON string — that is always correct for the text protocol because the server has already produced a textual rendering.
func PickBinary ¶
PickBinary returns a binary-format encoder for the given OID, or nil if we have no specialised binary path for it (callers should fall back to requesting text format for that column). NUMERIC is excluded here — see PickBinaryEx.
func PickBinaryEx ¶
PickBinaryEx is PickBinary with the BinaryNumeric toggle. When binaryNumeric is true, OIDNumeric returns the binary decoder that parses PG's base-10000 wire format. The default (false) keeps the text decoder — faster on loopback and typical precisions.