Skip to content

Structure

PostgresColumns

A collection of PostgresColumn column metadata for a PostgreSQL query result.
struct PostgresColumns

Overview

You can access metadata about the columns in a query result from columns.

// 1. Discover all user tables via the Postgres metadata tables
let tables = try await client.query("""
    SELECT table_name
    FROM information_schema.tables
    WHERE table_schema = 'public' AND table_type = 'BASE TABLE'
    """
)

for try await tableName in tables.decode(String.self) {
    print("## \(tableName)")

    // 2. Load rows from each table without knowing the schema upfront
    // > Note: table names are identifiers, not values — they cannot be passed as bind parameters, so we must use `unsafeSQL` here.
    let rows = try await client.query(
        PostgresQuery(unsafeSQL: #"SELECT * FROM "\#(tableName)""#)
    )

    for try await row in rows {
        // 3. Access column metadata via `rows.columns`
        for (column, metadata) in zip(row, rows.columns) {
            let value: Any
            // 4. Dynamically decode column values based on their metadata data type
            switch metadata.dataType {
            case .int2:
                value = try column.decode(Int16.self)
            case .int4:
                value = try column.decode(Int32.self)
            case .int8:
                value = try column.decode(Int64.self)
            case .float4:
                value = try column.decode(Float.self)
            case .text:
                value = try column.decode(String.self)
            case .timestamp, .timestamptz:
                value = try column.decode(Date.self)
            case .bytea:
                value = "<\(try column.decode(ByteBuffer.self).readableBytes) bytes>"
            default:
                // Fallback: most types can be decoded as String
                value = (try? column.decode(String.self)) ?? "<unknown>"
            }
            print("  `\(metadata.name)` (\(metadata.dataType), value: `\(value)`)")
        }
    }
}

Topics

Structures

  • PostgresColumns.Iterator
    A type that provides the sequence’s iteration interface and encapsulates its iteration state.

Instance Methods

  • makeIterator()
    Returns an iterator over the elements of this sequence.

Type Aliases

Default Implementations

Relationships

Conforms To

  • Swift.Collection
  • Swift.Copyable
  • Swift.Equatable
  • Swift.Escapable
  • Swift.Sendable
  • Swift.SendableMetatype
  • Swift.Sequence

See Also