PgComposite¶
A PostgreSQL composite type (user-defined structured type created with
CREATE TYPE ... AS (...)). Contains the composite's own type OID,
per-field OIDs and names, and an ordered sequence of field values where
each value is either a FieldData value or None (SQL NULL).
Used for both decoding composites from query results and encoding composites as query parameters:
// As a query parameter — use from_fields for safe construction
let addr = PgComposite.from_fields(16400,
recover val
[as (String, U32, (FieldData | None)):
("street", 25, "123 Main St")
("city", 25, "Springfield")
("zip_code", 23, I32(62704))]
end)
session.execute(PreparedQuery("INSERT INTO users (home) VALUES ($1)",
recover val [as FieldDataTypes: addr] end), receiver)
// From a result field — positional access
match field.value
| let c: PgComposite =>
match try c(0)? end
| let street: String => // use street
| None => // NULL field
end
end
// From a result field — named access
match field.value
| let c: PgComposite =>
match try c.field("city")? end
| let city: String => // use city
end
end
Register composite types with CodecRegistry.with_composite_type() before
use. Unregistered composite OIDs fall back to RawBytes (binary format)
or String (text format).
Implements¶
- FieldData val
- FieldDataEquatable val
- Equatable[PgComposite val] ref
Constructors¶
create¶
Construct from pre-split parallel arrays. Used internally by the decode
path where names, OIDs, and values arrive as separate arrays. Prefer
from_fields for user construction.
new val create(
type_oid': U32 val,
field_oids': Array[U32 val] val,
field_names': Array[String val] val,
fields': Array[(FieldData val | None val)] val)
: PgComposite val^ ?
Parameters¶
- type_oid': U32 val
- field_oids': Array[U32 val] val
- field_names': Array[String val] val
- fields': Array[(FieldData val | None val)] val
Returns¶
- PgComposite val^ ?
from_fields¶
Construct from (name, oid, value) triples. This is the preferred
constructor for user-created composites (query parameters), since it
keeps each field's name, OID, and value together and eliminates the
risk of misalignment across parallel arrays.
let addr = PgComposite.from_fields(16400,
recover val
[as (String, U32, (FieldData | None)):
("street", 25, "123 Main St")
("city", 25, "Springfield")
("zip_code", 23, I32(62704))]
end)
new val from_fields(
type_oid': U32 val,
descriptors: Array[(String val , U32 val , (FieldData val | None val))] val)
: PgComposite val^
Parameters¶
Returns¶
- PgComposite val^
Public fields¶
let type_oid: U32 val¶
let field_oids: Array[U32 val] val¶
let field_names: Array[String val] val¶
let fields: Array[(FieldData val | None val)] val¶
Public Functions¶
size¶
Number of fields in the composite.
Returns¶
- USize val
apply¶
Positional field access.
Parameters¶
- i: USize val
Returns¶
field¶
Named field access. Searches field_names for a match and returns
the corresponding value. Errors if the name is not found. If duplicate
names exist (PostgreSQL allows this), returns the first match.
Parameters¶
- name: String val
Returns¶
eq¶
Parameters¶
- that: PgComposite val
Returns¶
- Bool val
field_data_eq¶
Parameters¶
- that: FieldData box
Returns¶
- Bool val
string¶
PostgreSQL composite literal format: (val1,val2,val3). Field values
are double-quoted when they contain parentheses, commas, double-quotes,
backslashes, or whitespace. Empty strings are quoted ("") to
distinguish from NULL. NULL fields are unquoted empty positions.
Double-quotes and backslashes inside values are escaped by doubling
("" and \\ respectively).
Returns¶
- String iso^
ne¶
Parameters¶
- that: PgComposite val
Returns¶
- Bool val