Skip to content

PgComposite

[Source]

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).

class val PgComposite is
  FieldData val,
  FieldDataEquatable val,
  Equatable[PgComposite val] ref

Implements


Constructors

create

[Source]

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

Returns


from_fields

[Source]

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


Public fields

let type_oid: U32 val

[Source]


let field_oids: Array[U32 val] val

[Source]


let field_names: Array[String val] val

[Source]


let fields: Array[(FieldData val | None val)] val

[Source]


Public Functions

size

[Source]

Number of fields in the composite.

fun box size()
: USize val

Returns


apply

[Source]

Positional field access.

fun box apply(
  i: USize val)
: (FieldData val | None val) ?

Parameters

Returns


field

[Source]

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.

fun box field(
  name: String val)
: (FieldData val | None val) ?

Parameters

Returns


eq

[Source]

fun box eq(
  that: PgComposite val)
: Bool val

Parameters

Returns


field_data_eq

[Source]

fun box field_data_eq(
  that: FieldData box)
: Bool val

Parameters

Returns


string

[Source]

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).

fun box string()
: String iso^

Returns


ne

[Source]

fun box ne(
  that: PgComposite val)
: Bool val

Parameters

Returns