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 7 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 61.70%. Comparing base (f2a6394) to head (e70add9).

Files with missing lines Patch % Lines
Sources/PostgresNIO/New/PostgresRowSequence.swift 35.29% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #504      +/-   ##
==========================================
- Coverage   61.77%   61.70%   -0.07%     
==========================================
  Files         125      125              
  Lines       10072    10086      +14     
==========================================
+ Hits         6222     6224       +2     
- Misses       3850     3862      +12     
Files with missing lines Coverage Δ
Sources/PostgresNIO/New/PSQLRowStream.swift 86.36% <100.00%> (ø)
Sources/PostgresNIO/New/PostgresRowSequence.swift 75.75% <35.29%> (-14.63%) ⬇️

... and 1 file 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.

@bridger
Copy link

bridger commented Oct 9, 2024

I'd love this addition! I have queries that should update exactly 1 row. I'd like to check that the update was successful, but I can't check without the metadata. (These queries currently don't return the row itself.)

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.

2 participants