-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
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
|
|
||
/// 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]) { |
There was a problem hiding this comment.
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.
public func consume( | ||
onRow: @Sendable (PostgresRow) throws -> () | ||
) async throws -> PostgresQueryMetadata { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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) |
There was a problem hiding this comment.
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.
@fabianfett I've thought a bit more about this and possibly we might want to have another type that wraps |
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.) |
Currently the functions that return
PostgresRowSequence
have no way of reporting back themetadata
of the query.This PR exposes the
metadata
through thecollectWithMetadata()
andconsume(onRow:)
methods.