Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose query metadata in PostgresRowSequence #504

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

MahdiBM
Copy link
Contributor

@MahdiBM MahdiBM commented Aug 22, 2024

Currently the functions that return PostgresRowSequence have no way of reporting back the metadata of the query.
This PR exposes the metadata through the collectWithMetadata() and consume(onRow:) methods.

Copy link

codecov bot commented Aug 22, 2024

Codecov Report

Attention: Patch coverage is 38.88889% with 11 lines in your changes missing coverage. Please review.

Project coverage is 55.04%. Comparing base (9f84290) to head (68084f9).

Files Patch % Lines
Sources/PostgresNIO/New/PostgresRowSequence.swift 35.29% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #504      +/-   ##
==========================================
- Coverage   55.12%   55.04%   -0.08%     
==========================================
  Files         127      127              
  Lines       10174    10188      +14     
==========================================
  Hits         5608     5608              
- Misses       4566     4580      +14     
Files Coverage Δ
Sources/PostgresNIO/New/PSQLRowStream.swift 83.96% <100.00%> (ø)
Sources/PostgresNIO/New/PostgresRowSequence.swift 75.00% <35.29%> (-15.00%) ⬇️

... and 3 files with indirect coverage changes


/// Collect and return all rows, alongside the query metadata.
/// - Returns: The query metadata and the rows.
public func collectWithMetadata() async throws -> (metadata: PostgresQueryMetadata, rows: [PostgresRow]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm open to better names.

Comment on lines +98 to +100
public func consume(
onRow: @Sendable (PostgresRow) throws -> ()
) async throws -> PostgresQueryMetadata {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm open to better names.

Copy link
Contributor Author

@MahdiBM MahdiBM Aug 24, 2024

Choose a reason for hiding this comment

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

Also the closure is intentionally not async since it's generally not a good idea to be doing async things on each row.
It's still possible to accumulate the rows and do whatever you want after.

I could change that if you think with async it would be better.

Comment on lines -246 to +299
let countQuery = PostgresQuery(unsafeSQL: "SELECT COUNT(*) FROM table1")
let countRows = try await connection.query(countQuery, logger: .psqlTest)
var countIterator = countRows.makeAsyncIterator()
let insertedRowsCount = try await countIterator.next()?.decode(Int.self, context: .default)
XCTAssertEqual(rowsCount, insertedRowsCount)
let result = try await connection.query(insertionQuery, logger: .psqlTest)
let metadata = try await result.collectWithMetadata().metadata
XCTAssertEqual(metadata.rows, rowsCount)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The metadata contains the count of the inserted rows so no need to do another query.

@MahdiBM
Copy link
Contributor Author

MahdiBM commented Aug 27, 2024

@fabianfett I've thought a bit more about this and possibly we might want to have another type that wraps PostgresRowSequence in the next major version at least? so we can expose more stuff in the response. Not only the metadata, but maybe even the connection properties? Or who knows what.
I don't have an immediate use-case for connection properties but it looks like some lost info. Maybe someone finds a value in them although they can also be individually queried.

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.

1 participant