Skip to content

Commit 6b885c6

Browse files
committed
Compile pre 6.0
1 parent 604a1b1 commit 6b885c6

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Sources/PostgresNIO/Connection/PostgresConnection.swift

+52
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ extension PostgresConnection {
531531
}
532532
}
533533

534+
#if compiler(>=6.0)
534535
/// Puts the connection into an open transaction state, for the provided `closure`'s lifetime.
535536
///
536537
/// The function starts a transaction by running a `BEGIN` query on the connection against the database. It then
@@ -581,6 +582,57 @@ extension PostgresConnection {
581582
throw transactionError
582583
}
583584
}
585+
#else
586+
/// Puts the connection into an open transaction state, for the provided `closure`'s lifetime.
587+
///
588+
/// The function starts a transaction by running a `BEGIN` query on the connection against the database. It then
589+
/// lends the connection to the user provided closure. The user can then modify the database as they wish. If the user
590+
/// provided closure returns successfully, the function will attempt to commit the changes by running a `COMMIT`
591+
/// query against the database. If the user provided closure throws an error, the function will attempt to rollback the
592+
/// changes made within the closure.
593+
///
594+
/// - Parameters:
595+
/// - logger: The `Logger` to log into for the transaction.
596+
/// - file: The file, the transaction was started in. Used for better error reporting.
597+
/// - line: The line, the transaction was started in. Used for better error reporting.
598+
/// - closure: The user provided code to modify the database. Use the provided connection to run queries.
599+
/// The connection must stay in the transaction mode. Otherwise this method will throw!
600+
/// - Returns: The closure's return value.
601+
public func withTransaction<Result>(
602+
logger: Logger,
603+
file: String = #file,
604+
line: Int = #line,
605+
_ process: (PostgresConnection) async throws -> Result
606+
) async throws -> Result {
607+
do {
608+
try await self.query("BEGIN;", logger: logger)
609+
} catch {
610+
throw PostgresTransactionError(file: file, line: line, beginError: error)
611+
}
612+
613+
var closureHasFinished: Bool = false
614+
do {
615+
let value = try await process(self)
616+
closureHasFinished = true
617+
try await self.query("COMMIT;", logger: logger)
618+
return value
619+
} catch {
620+
var transactionError = PostgresTransactionError(file: file, line: line)
621+
if !closureHasFinished {
622+
transactionError.closureError = error
623+
do {
624+
try await self.query("ROLLBACK;", logger: logger)
625+
} catch {
626+
transactionError.rollbackError = error
627+
}
628+
} else {
629+
transactionError.commitError = error
630+
}
631+
632+
throw transactionError
633+
}
634+
}
635+
#endif
584636
}
585637

586638
// MARK: EventLoopFuture interface

Sources/PostgresNIO/Pool/PostgresClient.swift

+29
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
309309
return try await closure(connection)
310310
}
311311

312+
#if compiler(>=6.0)
312313
/// Lease a connection for the provided `closure`'s lifetime.
313314
///
314315
/// - Parameter closure: A closure that uses the passed `PostgresConnection`. The closure **must not** capture
@@ -351,6 +352,34 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
351352
try await connection.withTransaction(logger: logger, file: file, line: line, closure)
352353
}
353354
}
355+
#else
356+
357+
/// Lease a connection, which is in an open transaction state, for the provided `closure`'s lifetime.
358+
///
359+
/// The function leases a connection from the underlying connection pool and starts a transaction by running a `BEGIN`
360+
/// query on the leased connection against the database. It then lends the connection to the user provided closure.
361+
/// The user can then modify the database as they wish. If the user provided closure returns successfully, the function
362+
/// will attempt to commit the changes by running a `COMMIT` query against the database. If the user provided closure
363+
/// throws an error, the function will attempt to rollback the changes made within the closure.
364+
///
365+
/// - Parameters:
366+
/// - logger: The `Logger` to log into for the transaction.
367+
/// - file: The file, the transaction was started in. Used for better error reporting.
368+
/// - line: The line, the transaction was started in. Used for better error reporting.
369+
/// - closure: The user provided code to modify the database. Use the provided connection to run queries.
370+
/// The connection must stay in the transaction mode. Otherwise this method will throw!
371+
/// - Returns: The closure's return value.
372+
public func withTransaction<Result>(
373+
logger: Logger,
374+
file: String = #file,
375+
line: Int = #line,
376+
_ closure: (PostgresConnection) async throws -> Result
377+
) async throws -> Result {
378+
try await self.withConnection { connection in
379+
try await connection.withTransaction(logger: logger, file: file, line: line, closure)
380+
}
381+
}
382+
#endif
354383

355384
/// Run a query on the Postgres server the client is connected to.
356385
///

0 commit comments

Comments
 (0)