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
struct IteratorA type that provides the sequence’s iteration interface and encapsulates its iteration state.
Instance Methods
func makeIterator() -> PostgresColumns.IteratorReturns an iterator over the elements of this sequence.
Type Aliases
typealias ElementA type representing the sequence’s elements.
Default Implementations
Relationships
Conforms To
Swift.CollectionSwift.CopyableSwift.EquatableSwift.EscapableSwift.SendableSwift.SendableMetatypeSwift.Sequence
See Also
struct PostgresQueryA Postgres SQL query that can be executed on a Postgres server. Contains the raw SQL string and bindings.struct PostgresBindingsstruct PostgresRowPostgresRowrepresents a single table row that is received from the server for a query or a prepared statement. Its element type isPostgresCell.struct PostgresRowSequenceAn async sequence ofPostgresRows.struct PostgresColumnMetadata for a single column in a PostgreSQL query result.struct PostgresRandomAccessRowA random access row ofPostgresCells. Its initialization is O(n), where n is the number of columns in the row. All subsequent cell accesses are O(1).struct PostgresCellA representation of a cell value within aPostgresRowandPostgresRandomAccessRow.struct PostgresQueryMetadata