@@ -294,7 +294,7 @@ public final class PostgresConnection {
294294
295295 // MARK: Query
296296
297- func query ( _ query: PostgresQuery , logger: Logger ) -> EventLoopFuture < PSQLRowStream > {
297+ private func queryStream ( _ query: PostgresQuery , logger: Logger ) -> EventLoopFuture < PSQLRowStream > {
298298 var logger = logger
299299 logger [ postgresMetadataKey: . connectionID] = " \( self . id) "
300300 guard query. binds. count <= Int ( Int16 . max) else {
@@ -433,6 +433,8 @@ extension PostgresConnection {
433433 }
434434}
435435
436+ // MARK: Async/Await Interface
437+
436438#if swift(>=5.5) && canImport(_Concurrency)
437439extension PostgresConnection {
438440
@@ -489,7 +491,8 @@ extension PostgresConnection {
489491 let context = ExtendedQueryContext (
490492 query: query,
491493 logger: logger,
492- promise: promise)
494+ promise: promise
495+ )
493496
494497 self . channel. write ( PSQLTask . extendedQuery ( context) , promise: nil )
495498
@@ -498,7 +501,64 @@ extension PostgresConnection {
498501}
499502#endif
500503
501- // MARK: PostgresDatabase
504+ // MARK: EventLoopFuture interface
505+
506+ extension PostgresConnection {
507+
508+ /// Run a query on the Postgres server the connection is connected to and collect all rows.
509+ ///
510+ /// - Parameters:
511+ /// - query: The ``PostgresQuery`` to run
512+ /// - logger: The `Logger` to log into for the query
513+ /// - file: The file, the query was started in. Used for better error reporting.
514+ /// - line: The line, the query was started in. Used for better error reporting.
515+ /// - Returns: An EventLoopFuture, that allows access to the future ``PostgresQueryResult``.
516+ public func query(
517+ _ query: PostgresQuery ,
518+ logger: Logger ,
519+ file: String = #file,
520+ line: Int = #line
521+ ) -> EventLoopFuture < PostgresQueryResult > {
522+ self . queryStream ( query, logger: logger) . flatMap { rowStream in
523+ rowStream. all ( ) . flatMapThrowing { rows -> PostgresQueryResult in
524+ guard let metadata = PostgresQueryMetadata ( string: rowStream. commandTag) else {
525+ throw PSQLError . invalidCommandTag ( rowStream. commandTag)
526+ }
527+ return PostgresQueryResult ( metadata: metadata, rows: rows)
528+ }
529+ }
530+ }
531+
532+ /// Run a query on the Postgres server the connection is connected to and iterate the rows in a callback.
533+ ///
534+ /// - Note: This API does not support back-pressure. If you need back-pressure please use the query
535+ /// API, that supports structured concurrency.
536+ /// - Parameters:
537+ /// - query: The ``PostgresQuery`` to run
538+ /// - logger: The `Logger` to log into for the query
539+ /// - file: The file, the query was started in. Used for better error reporting.
540+ /// - line: The line, the query was started in. Used for better error reporting.
541+ /// - onRow: A closure that is invoked for every row.
542+ /// - Returns: An EventLoopFuture, that allows access to the future ``PostgresQueryMetadata``.
543+ public func query(
544+ _ query: PostgresQuery ,
545+ logger: Logger ,
546+ file: String = #file,
547+ line: Int = #line,
548+ _ onRow: @escaping ( PostgresRow ) throws -> ( )
549+ ) -> EventLoopFuture < PostgresQueryMetadata > {
550+ self . queryStream ( query, logger: logger) . flatMap { rowStream in
551+ rowStream. onRow ( onRow) . flatMapThrowing { ( ) -> PostgresQueryMetadata in
552+ guard let metadata = PostgresQueryMetadata ( string: rowStream. commandTag) else {
553+ throw PSQLError . invalidCommandTag ( rowStream. commandTag)
554+ }
555+ return metadata
556+ }
557+ }
558+ }
559+ }
560+
561+ // MARK: PostgresDatabase conformance
502562
503563extension PostgresConnection : PostgresDatabase {
504564 public func send(
@@ -513,14 +573,14 @@ extension PostgresConnection: PostgresDatabase {
513573
514574 switch command {
515575 case . query( let query, let onMetadata, let onRow) :
516- resultFuture = self . query ( query, logger: logger) . flatMap { stream in
576+ resultFuture = self . queryStream ( query, logger: logger) . flatMap { stream in
517577 return stream. onRow ( onRow) . map { _ in
518578 onMetadata ( PostgresQueryMetadata ( string: stream. commandTag) !)
519579 }
520580 }
521581
522582 case . queryAll( let query, let onResult) :
523- resultFuture = self . query ( query, logger: logger) . flatMap { rows in
583+ resultFuture = self . queryStream ( query, logger: logger) . flatMap { rows in
524584 return rows. all ( ) . map { allrows in
525585 onResult ( . init( metadata: PostgresQueryMetadata ( string: rows. commandTag) !, rows: allrows) )
526586 }
0 commit comments