@@ -16,23 +16,24 @@ struct _FluentPostgresDatabase<E: PostgresJSONEncoder, D: PostgresJSONDecoder> {
16
16
extension _FluentPostgresDatabase : Database {
17
17
func execute(
18
18
query: DatabaseQuery ,
19
- onOutput: @escaping @Sendable ( any DatabaseOutput ) -> ( )
19
+ onOutput: @escaping @Sendable ( any DatabaseOutput ) -> Void
20
20
) -> EventLoopFuture < Void > {
21
21
var expression = SQLQueryConverter ( delegate: PostgresConverterDelegate ( ) ) . convert ( query)
22
-
22
+
23
23
/// For `.create` query actions, we want to return the generated IDs, unless the `customIDKey` is the
24
24
/// empty string, which we use as a very hacky signal for "we don't implement this for composite IDs yet".
25
25
if case . create = query. action, query. customIDKey != . some( . string( " " ) ) {
26
26
expression = SQLKit . SQLList ( [ expression, SQLReturning ( . init( ( query. customIDKey ?? . id) . description) ) ] , separator: SQLRaw ( " " ) )
27
27
}
28
-
28
+
29
29
return self . execute ( sql: expression, { onOutput ( $0. databaseOutput ( ) ) } )
30
30
}
31
31
32
32
func execute( schema: DatabaseSchema ) -> EventLoopFuture < Void > {
33
33
let expression = SQLSchemaConverter ( delegate: PostgresConverterDelegate ( ) ) . convert ( schema)
34
34
35
- return self . execute ( sql: expression,
35
+ return self . execute (
36
+ sql: expression,
36
37
// N.B.: Don't fatalError() here; what're users supposed to do about it?
37
38
{ self . logger. debug ( " Unexpected row returned from schema query: \( $0) " ) }
38
39
)
@@ -50,9 +51,11 @@ extension _FluentPostgresDatabase: Database {
50
51
return self . eventLoop. makeSucceededFuture ( ( ) )
51
52
}
52
53
53
- return self . eventLoop. flatten ( e. createCases. map { create in
54
- self . alter ( enum: e. name) . add ( value: create) . run ( )
55
- } )
54
+ return self . eventLoop. flatten (
55
+ e. createCases. map { create in
56
+ self . alter ( enum: e. name) . add ( value: create) . run ( )
57
+ }
58
+ )
56
59
case . delete:
57
60
return self . drop ( enum: e. name) . run ( )
58
61
}
@@ -64,10 +67,12 @@ extension _FluentPostgresDatabase: Database {
64
67
}
65
68
return self . withConnection { conn in
66
69
guard let sqlConn = conn as? any SQLDatabase else {
67
- fatalError ( """
70
+ fatalError (
71
+ """
68
72
Connection yielded by a Fluent+Postgres database is not also an SQLDatabase.
69
73
This is a bug in Fluent; please report it at https://github.com/vapor/fluent-postgres-driver/issues
70
- """ )
74
+ """
75
+ )
71
76
}
72
77
return sqlConn. raw ( " BEGIN " ) . run ( ) . flatMap {
73
78
closure ( conn) . flatMap { result in
@@ -78,16 +83,22 @@ extension _FluentPostgresDatabase: Database {
78
83
}
79
84
}
80
85
}
81
-
86
+
82
87
func withConnection< T> ( _ closure: @escaping @Sendable ( any Database ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
83
88
self . withConnection { ( underlying: any PostgresDatabase ) in
84
- closure ( _FluentPostgresDatabase (
85
- database: underlying. sql ( encodingContext: self . encodingContext, decodingContext: self . decodingContext, queryLogLevel: self . database. queryLogLevel) ,
86
- context: self . context,
87
- encodingContext: self . encodingContext,
88
- decodingContext: self . decodingContext,
89
- inTransaction: true
90
- ) )
89
+ closure (
90
+ _FluentPostgresDatabase (
91
+ database: underlying. sql (
92
+ encodingContext: self . encodingContext,
93
+ decodingContext: self . decodingContext,
94
+ queryLogLevel: self . database. queryLogLevel
95
+ ) ,
96
+ context: self . context,
97
+ encodingContext: self . encodingContext,
98
+ decodingContext: self . decodingContext,
99
+ inTransaction: true
100
+ )
101
+ )
91
102
}
92
103
}
93
104
}
@@ -96,11 +107,11 @@ extension _FluentPostgresDatabase: TransactionControlDatabase {
96
107
func beginTransaction( ) -> EventLoopFuture < Void > {
97
108
self . raw ( " BEGIN " ) . run ( )
98
109
}
99
-
110
+
100
111
func commitTransaction( ) -> EventLoopFuture < Void > {
101
112
self . raw ( " COMMIT " ) . run ( )
102
113
}
103
-
114
+
104
115
func rollbackTransaction( ) -> EventLoopFuture < Void > {
105
116
self . raw ( " ROLLBACK " ) . run ( )
106
117
}
@@ -110,15 +121,15 @@ extension _FluentPostgresDatabase: SQLDatabase {
110
121
var version : ( any SQLDatabaseReportedVersion ) ? { self . database. version }
111
122
var dialect : any SQLDialect { self . database. dialect }
112
123
var queryLogLevel : Logger . Level ? { self . database. queryLogLevel }
113
-
114
- func execute( sql query: any SQLExpression , _ onRow: @escaping @Sendable ( any SQLRow ) -> ( ) ) -> EventLoopFuture < Void > {
124
+
125
+ func execute( sql query: any SQLExpression , _ onRow: @escaping @Sendable ( any SQLRow ) -> Void ) -> EventLoopFuture < Void > {
115
126
self . database. execute ( sql: query, onRow)
116
127
}
117
-
118
- func execute( sql query: any SQLExpression , _ onRow: @escaping @Sendable ( any SQLRow ) -> ( ) ) async throws {
128
+
129
+ func execute( sql query: any SQLExpression , _ onRow: @escaping @Sendable ( any SQLRow ) -> Void ) async throws {
119
130
try await self . database. execute ( sql: query, onRow)
120
131
}
121
-
132
+
122
133
func withSession< R> ( _ closure: @escaping @Sendable ( any SQLDatabase ) async throws -> R ) async throws -> R {
123
134
try await self . database. withSession ( closure)
124
135
}
@@ -128,15 +139,17 @@ extension _FluentPostgresDatabase: PostgresDatabase {
128
139
func send( _ request: any PostgresRequest , logger: Logger ) -> EventLoopFuture < Void > {
129
140
self . withConnection { $0. send ( request, logger: logger) }
130
141
}
131
-
142
+
132
143
func withConnection< T> ( _ closure: @escaping ( PostgresConnection ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
133
144
guard let psqlDb: any PostgresDatabase = self . database as? any PostgresDatabase else {
134
- fatalError ( """
145
+ fatalError (
146
+ """
135
147
Connection yielded by a Fluent+Postgres database is not also a PostgresDatabase.
136
148
This is a bug in Fluent; please report it at https://github.com/vapor/fluent-postgres-driver/issues
137
- """ )
149
+ """
150
+ )
138
151
}
139
-
152
+
140
153
return psqlDb. withConnection ( closure)
141
154
}
142
155
}
0 commit comments