Skip to content

Conversation

SteveLauC
Copy link

@SteveLauC SteveLauC commented Aug 14, 2025

This commit implements PostgresRowSequence.getColumns() to enable users to retrieve column metadata of their query results.

Discussion: #576

Closes #576

This commit implements `PostgresRowSequence.getColumns()` to enable users
to retrieve column metadata of their query results.

Discussion: vapor#576
@SteveLauC
Copy link
Author

SteveLauC commented Aug 14, 2025

  • Based on this comment, RowDescription.Column should stay private, and a new type PostgresColumn is added. This type and its fields are public, but the fields are immutable, and the init() function is private. I did this because I want it to behave like an accessor: users are only allowed to construct it via the getColumns() method, once created, users can only read it.

  • API naming: I would like to directly use columns(), but the compiler complains that there is already a field with the same name. So I go with getColumns().

  • Definition of this new PostgresColumn type: I blindly use the fields of RowDescription.Column

  • Two unit tests are added


Just my two cents — I'm happy to adjust based on the maintainer's feedback. :)

@SteveLauC SteveLauC marked this pull request as ready for review August 14, 2025 05:47
Comment on lines +6 to +26
/// The column name.
public let name: String

/// If the field can be identified as a column of a specific table, the object ID of the table; otherwise zero.
public let tableOID: Int32

/// If the field can be identified as a column of a specific table, the attribute number of the column; otherwise zero.
public let columnAttributeNumber: Int16

/// The object ID of the field's data type.
public let dataType: PostgresDataType

/// The data type size (see pg_type.typlen). Note that negative values denote variable-width types.
public let dataTypeSize: Int16

/// The type modifier (see pg_attribute.atttypmod). The meaning of the modifier is type-specific.
public let dataTypeModifier: Int32

/// The format being used for the field. Currently will be text or binary. In a RowDescription returned
/// from the statement variant of Describe, the format code is not yet known and will always be text.
public let format: PostgresFormat
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we back this with a RowDescription.Column please. Instead of let properties please lets use computed properties.

Example:

public var name: String {
    self.underlying.name
}

Comment on lines +30 to +43
/// Get the column information of the query results.
public func getColumns() -> [PostgresColumn] {
self.columns.map { column in
PostgresColumn(
name: column.name,
tableOID: column.tableOID,
columnAttributeNumber: column.columnAttributeNumber,
dataType: column.dataType,
dataTypeSize: column.dataTypeSize,
dataTypeModifier: column.dataTypeModifier,
format: column.format
)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an O(n) call. Where n equals the number of columns. Do we need a returned Array? Can we return a custom PostgresColumns struct that wraps the existing [RowDescription.Column] structure?

Example:

struct PostgresColumns: Sequence {
    typealias Element == PostgresColumn
   
    var underlying: [RowDescription.Column]

    func makeIterator() -> Iterator {
        Iterator(underlying: self.underlying.makeIterator)
    }

    struct Iterator: IteratorProtocol {
        var underlying: [RowDescription.Column].Iterator

        func next() -> PostgresColumn {
           self.underlying.next()
        }
    }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do the above we can just add a get only property:

var columns: PostgresColumns {
    PostgresColumns(underlying: self.columns)
}

@fabianfett
Copy link
Collaborator

Also thanks for pushing on this @SteveLauC. My apologies for not coming back to you sooner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose Column Metadata on PostgresRowSequence
2 participants