Decoders for PostgreSQL backend messages: ErrorResponse/NoticeResponse
fields, RowDescription, DataRow, and text-format value decoding by type
OID. Internal to the pg package.
Values
OID-BOOL
OID-BOOL: Int 16
OID-BYTEA
OID-BYTEA: Int 17
OID-FLOAT4
OID-FLOAT4: Int 700
OID-FLOAT8
OID-FLOAT8: Int 701
OID-INT2
OID-INT2: Int 21
OID-INT4
OID-INT4: Int 23
OID-INT8
OID-INT8: Int 20
OID-JSON
OID-JSON: Int 114
OID-JSONB
OID-JSONB: Int 3802
OID-NUMERIC
OID-NUMERIC: Int 1700
OID-OID
OID-OID: Int 26
OID-VARCHAR
OID-VARCHAR: Int 1043
Functions
decode-value
fn (oid: Int, raw: Bytes): Any
Decode a column's raw text-format bytes into a Hot value using its
type OID. Integers become Int, float/numeric become Dec, bool becomes
Bool, json/jsonb are parsed, bytea is hex-decoded, and everything else
(text, varchar, uuid, timestamps, ...) becomes a Str — or Bytes when
the value is not valid UTF-8 (a non-UTF8 client_encoding), rather
than silently substituting replacement characters.
Example
::pg::decode/decode-value(23, Bytes("42")) // 42
::pg::decode/decode-value(16, Bytes("t")) // true
::pg::decode/decode-value(1700, Bytes("1.5")) // 1.5
parse-data-row
fn (payload: Bytes, fields: Vec): Map
Parse a DataRow payload into a {column-name: value} map, decoding
each column by the OID from the matching RowDescription field.
A column length of -1 denotes SQL NULL.
Example
::wire ::pg::wire
fields [{name: "id", oid: 23}, {name: "note", oid: 25}]
payload concat(::wire/int16(2), ::wire/int32(2), Bytes("42"), ::wire/int32(-1))
::pg::decode/parse-data-row(payload, fields) // {id: 42, note: null}
parse-error
fn (payload: Bytes): Map
Parse an ErrorResponse/NoticeResponse payload into a map with the
common fields decoded: severity (S), code (C, the SQLSTATE),
message (M), detail (D), hint (H). fields holds the raw
field map for anything else.
Example
payload concat(Bytes("C"), ::pg::wire/cstring("42P01"), Bytes("M"), ::pg::wire/cstring("no such table"), Bytes([0]))
::pg::decode/parse-error(payload).code // "42P01"
::pg::decode/parse-error(payload).message // "no such table"
parse-row-description
fn (payload: Bytes): Vec
Parse a RowDescription payload into a Vec of {name, oid} field
descriptors, in column order.
Example
::wire ::pg::wire
payload concat(::wire/int16(1), ::wire/cstring("id"), ::wire/int32(0), ::wire/int16(0), ::wire/int32(23), ::wire/int16(4), ::wire/int32(-1), ::wire/int16(0))
::pg::decode/parse-row-description(payload) // [{name: "id", oid: 23}]