@@ -16,7 +16,7 @@ public struct DatabaseClientPostgres: DatabaseClient {
1616 public typealias Connection = DatabaseConnectionPostgres
1717
1818 var client : PostgresNIO . PostgresClient
19- var logger : Logger
19+ let logger : Logger
2020
2121 /// Create a Postgres database client.
2222 ///
@@ -44,14 +44,18 @@ public struct DatabaseClientPostgres: DatabaseClient {
4444 public func withConnection< T> (
4545 _ closure: ( Connection ) async throws -> T ,
4646 ) async throws ( DatabaseError) -> T {
47- do {
48- return try await client. withConnection { connection in
49- let databaseConnection = DatabaseConnectionPostgres (
47+ let logger = self . logger
48+ let body : ( PostgresConnection ) async throws -> T = { connection in
49+ try await closure (
50+ DatabaseConnectionPostgres (
5051 connection: connection,
5152 logger: logger
5253 )
53- return try await closure ( databaseConnection)
54- }
54+ )
55+ }
56+
57+ do {
58+ return try await client. withConnection ( body)
5559 }
5660 catch let error as DatabaseError {
5761 throw error
@@ -71,23 +75,81 @@ public struct DatabaseClientPostgres: DatabaseClient {
7175 public func withTransaction< T> (
7276 _ closure: ( Connection ) async throws -> T ,
7377 ) async throws ( DatabaseError) -> T {
78+ let logger = self . logger
79+ let beginQuery = PostgresQuery ( unsafeSQL: " BEGIN " , binds: . init( ) )
80+ let commitQuery = PostgresQuery ( unsafeSQL: " COMMIT " , binds: . init( ) )
81+ let rollbackQuery = PostgresQuery ( unsafeSQL: " ROLLBACK " , binds: . init( ) )
82+
7483 do {
75- return try await client. withTransaction (
76- logger: logger
77- ) { connection in
84+ return try await client. withConnection { connection in
7885 let databaseConnection = DatabaseConnectionPostgres (
7986 connection: connection,
8087 logger: logger
8188 )
82- return try await closure ( databaseConnection)
89+
90+ do {
91+ _ = try await connection. query ( beginQuery, logger: logger)
92+ }
93+ catch {
94+ throw DatabaseError . transaction (
95+ DatabaseTransactionErrorPostgres (
96+ beginError: error
97+ )
98+ )
99+ }
100+
101+ do {
102+ let result = try await closure ( databaseConnection)
103+ do {
104+ _ = try await connection. query (
105+ commitQuery,
106+ logger: logger
107+ )
108+ return result
109+ }
110+ catch {
111+ let commitError = error
112+ var rollbackError : ( any Error ) ?
113+ do {
114+ _ = try await connection. query (
115+ rollbackQuery,
116+ logger: logger
117+ )
118+ }
119+ catch {
120+ rollbackError = error
121+ }
122+ throw DatabaseError . transaction (
123+ DatabaseTransactionErrorPostgres (
124+ commitError: commitError,
125+ rollbackError: rollbackError
126+ )
127+ )
128+ }
129+ }
130+ catch {
131+ let closureError = error
132+ var rollbackError : ( any Error ) ?
133+ do {
134+ _ = try await connection. query (
135+ rollbackQuery,
136+ logger: logger
137+ )
138+ }
139+ catch {
140+ rollbackError = error
141+ }
142+ throw DatabaseError . transaction (
143+ DatabaseTransactionErrorPostgres (
144+ closureError: closureError,
145+ rollbackError: rollbackError
146+ )
147+ )
148+ }
83149 }
84150 }
85- catch let error as PostgresTransactionError {
86- throw . transaction(
87- DatabaseTransactionErrorPostgres (
88- underlyingError: error
89- )
90- )
151+ catch let error as DatabaseError {
152+ throw error
91153 }
92154 catch {
93155 throw . connection( error)
0 commit comments