|
| 1 | +extension PostgresRowSequence { |
| 2 | + /// A ``PostgresColumns`` collection containing metadata about the columns in the query result. |
| 3 | + public var columns: PostgresColumns { |
| 4 | + PostgresColumns(underlying: self._columns) |
| 5 | + } |
| 6 | +} |
| 7 | + |
| 8 | +/// A collection of ``PostgresColumn`` column metadata for a PostgreSQL query result. |
| 9 | +/// |
| 10 | +/// You can access metadata about the columns in a query result from ``PostgresRowSequence/columns``. |
| 11 | +public struct PostgresColumns: Sequence, Sendable { |
| 12 | + public typealias Element = PostgresColumn |
| 13 | + |
| 14 | + var underlying: [RowDescription.Column] |
| 15 | + |
| 16 | + public func makeIterator() -> Iterator { |
| 17 | + Iterator(underlying: self.underlying.makeIterator()) |
| 18 | + } |
| 19 | + |
| 20 | + public struct Iterator: IteratorProtocol { |
| 21 | + var underlying: [RowDescription.Column].Iterator |
| 22 | + |
| 23 | + public mutating func next() -> PostgresColumn? { |
| 24 | + guard let next = self.underlying.next() else { |
| 25 | + return nil |
| 26 | + } |
| 27 | + return PostgresColumn(underlying: next) |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +extension PostgresColumns: Collection, Equatable { |
| 33 | + public typealias Index = Int |
| 34 | + |
| 35 | + public var startIndex: Index { self.underlying.startIndex } |
| 36 | + public var endIndex: Index { self.underlying.endIndex } |
| 37 | + |
| 38 | + public subscript(position: Index) -> PostgresColumn { |
| 39 | + PostgresColumn(underlying: self.underlying[position]) |
| 40 | + } |
| 41 | + |
| 42 | + public func index(after i: Int) -> Int { |
| 43 | + self.underlying.index(after: i) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Metadata for a single column in a PostgreSQL query result. |
| 48 | +public struct PostgresColumn: Hashable, Sendable { |
| 49 | + let underlying: RowDescription.Column |
| 50 | + |
| 51 | + /// The field name. |
| 52 | + public var name: String { |
| 53 | + self.underlying.name |
| 54 | + } |
| 55 | + |
| 56 | + /// If the field can be identified as a column of a specific table, the object ID of the table; otherwise zero. |
| 57 | + public var tableOID: Int32 { |
| 58 | + self.underlying.tableOID |
| 59 | + } |
| 60 | + |
| 61 | + /// If the field can be identified as a column of a specific table, the attribute number of the column; otherwise zero. |
| 62 | + public var columnAttributeNumber: Int16 { |
| 63 | + self.underlying.columnAttributeNumber |
| 64 | + } |
| 65 | + |
| 66 | + /// The object ID of the field's data type. |
| 67 | + public var dataType: PostgresDataType { |
| 68 | + self.underlying.dataType |
| 69 | + } |
| 70 | + |
| 71 | + /// The data type size (see pg_type.typlen). Note that negative values denote variable-width types. |
| 72 | + public var dataTypeSize: Int16 { |
| 73 | + self.underlying.dataTypeSize |
| 74 | + } |
| 75 | + |
| 76 | + /// The type modifier (see pg_attribute.atttypmod). The meaning of the modifier is type-specific. |
| 77 | + public var dataTypeModifier: Int32 { |
| 78 | + self.underlying.dataTypeModifier |
| 79 | + } |
| 80 | + |
| 81 | + /// The format being used for the field. Currently will be text or binary. |
| 82 | + /// In a RowDescription returned from the statement variant of Describe, the format code is not yet known and will always be text. |
| 83 | + public var format: PostgresFormat { |
| 84 | + self.underlying.format |
| 85 | + } |
| 86 | +} |
0 commit comments