1+ import FluentKit
12import FluentSQL
2- import SQLiteKit
33import NIOCore
4- import SQLiteNIO
54import SQLKit
6- import FluentKit
5+ import SQLiteKit
6+ import SQLiteNIO
77
88struct FluentSQLiteDatabase : Database , SQLDatabase , SQLiteDatabase {
99 let database : any SQLiteDatabase
@@ -12,28 +12,27 @@ struct FluentSQLiteDatabase: Database, SQLDatabase, SQLiteDatabase {
1212 let dataDecoder : SQLiteDataDecoder
1313 let queryLogLevel : Logger . Level ?
1414 let inTransaction : Bool
15-
15+
1616 private func adjustFluentQuery( _ original: DatabaseQuery , _ converted: any SQLExpression ) -> any SQLExpression {
1717 /// For `.create` query actions, we want to return the generated IDs, unless the `customIDKey` is the
1818 /// empty string, which we use as a very hacky signal for "we don't implement this for composite IDs yet".
19- if case . create = original. action, original. customIDKey != . some( . string( " " ) ) {
20- return SQLKit . SQLList ( [ converted, SQLReturning ( . init( ( original. customIDKey ?? . id) . description) ) ] , separator: SQLRaw ( " " ) )
21- } else {
19+ guard case . create = original. action, original. customIDKey != . some( . string( " " ) ) else {
2220 return converted
2321 }
22+ return SQLKit . SQLList ( [ converted, SQLReturning ( . init( ( original. customIDKey ?? . id) . description) ) ] , separator: SQLRaw ( " " ) )
2423 }
25-
24+
2625 // Database
27-
28- func execute( query: DatabaseQuery , onOutput: @escaping @Sendable ( any DatabaseOutput ) -> ( ) ) -> EventLoopFuture < Void > {
26+
27+ func execute( query: DatabaseQuery , onOutput: @escaping @Sendable ( any DatabaseOutput ) -> Void ) -> EventLoopFuture < Void > {
2928 /// SQLiteKit will handle applying the configured data decoder to each row when providing `SQLRow`s.
30- return self . execute (
29+ self . execute (
3130 sql: self . adjustFluentQuery ( query, SQLQueryConverter ( delegate: SQLiteConverterDelegate ( ) ) . convert ( query) ) ,
3231 { onOutput ( $0. databaseOutput ( ) ) }
3332 )
3433 }
35-
36- func execute( query: DatabaseQuery , onOutput: @escaping @Sendable ( any DatabaseOutput ) -> ( ) ) async throws {
34+
35+ func execute( query: DatabaseQuery , onOutput: @escaping @Sendable ( any DatabaseOutput ) -> Void ) async throws {
3736 try await self . execute (
3837 sql: self . adjustFluentQuery ( query, SQLQueryConverter ( delegate: SQLiteConverterDelegate ( ) ) . convert ( query) ) ,
3938 { onOutput ( $0. databaseOutput ( ) ) }
@@ -42,20 +41,22 @@ struct FluentSQLiteDatabase: Database, SQLDatabase, SQLiteDatabase {
4241
4342 func execute( schema: DatabaseSchema ) -> EventLoopFuture < Void > {
4443 var schema = schema
45-
44+
4645 if schema. action == . update {
47- schema. updateFields = schema. updateFields. filter { switch $0 { // Filter out enum updates.
48- case . dataType( _, . enum( _) ) : return false
49- default : return true
50- } }
46+ schema. updateFields = schema. updateFields. filter {
47+ switch $0 { // Filter out enum updates.
48+ case . dataType( _, . enum( _) ) : false
49+ default : true
50+ }
51+ }
5152 guard schema. createConstraints. isEmpty, schema. updateFields. isEmpty, schema. deleteConstraints. isEmpty else {
5253 return self . eventLoop. makeFailedFuture ( FluentSQLiteUnsupportedAlter ( ) )
5354 }
5455 if schema. createFields. isEmpty, schema. deleteFields. isEmpty { // If there were only enum updates, bail out.
5556 return self . eventLoop. makeSucceededFuture ( ( ) )
5657 }
5758 }
58-
59+
5960 return self . execute (
6061 sql: SQLSchemaConverter ( delegate: SQLiteConverterDelegate ( ) ) . convert ( schema) ,
6162 { self . logger. debug ( " Unexpected row returned from schema query: \( $0) " ) }
@@ -65,30 +66,29 @@ struct FluentSQLiteDatabase: Database, SQLDatabase, SQLiteDatabase {
6566 func execute( enum: DatabaseEnum ) -> EventLoopFuture < Void > {
6667 self . eventLoop. makeSucceededFuture ( ( ) )
6768 }
68-
69+
6970 func withConnection< T> ( _ closure: @escaping @Sendable ( any Database ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
7071 self . eventLoop. makeFutureWithTask { try await self . withConnection { try await closure ( $0) . get ( ) } }
7172 }
72-
73+
7374 func withConnection< T> ( _ closure: @escaping @Sendable ( any Database ) async throws -> T ) async throws -> T {
7475 try await self . withConnection {
75- try await closure ( FluentSQLiteDatabase (
76- database: $0,
77- context: self . context,
78- dataEncoder: self . dataEncoder,
79- dataDecoder: self . dataDecoder,
80- queryLogLevel: self . queryLogLevel,
81- inTransaction: self . inTransaction
82- ) )
76+ try await closure (
77+ FluentSQLiteDatabase (
78+ database: $0,
79+ context: self . context,
80+ dataEncoder: self . dataEncoder,
81+ dataDecoder: self . dataDecoder,
82+ queryLogLevel: self . queryLogLevel,
83+ inTransaction: self . inTransaction
84+ ) )
8385 }
8486 }
85-
87+
8688 func transaction< T> ( _ closure: @escaping @Sendable ( any Database ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
87- self . inTransaction ?
88- closure ( self ) :
89- self . eventLoop. makeFutureWithTask { try await self . transaction { try await closure ( $0) . get ( ) } }
89+ self . inTransaction ? closure ( self ) : self . eventLoop. makeFutureWithTask { try await self . transaction { try await closure ( $0) . get ( ) } }
9090 }
91-
91+
9292 func transaction< T> ( _ closure: @escaping @Sendable ( any Database ) async throws -> T ) async throws -> T {
9393 guard !self . inTransaction else {
9494 return try await closure ( self )
@@ -103,11 +103,11 @@ struct FluentSQLiteDatabase: Database, SQLDatabase, SQLiteDatabase {
103103 queryLogLevel: self . queryLogLevel,
104104 inTransaction: true
105105 )
106-
106+
107107 try await db. raw ( " BEGIN TRANSACTION " ) . run ( )
108108 do {
109109 let result = try await closure ( db)
110-
110+
111111 try await db. raw ( " COMMIT TRANSACTION " ) . run ( )
112112 return result
113113 } catch {
@@ -116,31 +116,32 @@ struct FluentSQLiteDatabase: Database, SQLDatabase, SQLiteDatabase {
116116 }
117117 }
118118 }
119-
119+
120120 // SQLDatabase
121121
122122 var dialect : any SQLDialect {
123123 self . database. sql ( encoder: self . dataEncoder, decoder: self . dataDecoder, queryLogLevel: self . queryLogLevel) . dialect
124124 }
125-
125+
126126 var version : ( any SQLDatabaseReportedVersion ) ? {
127127 self . database. sql ( encoder: self . dataEncoder, decoder: self . dataDecoder, queryLogLevel: self . queryLogLevel) . version
128128 }
129-
130- func execute( sql query: any SQLExpression , _ onRow: @escaping @Sendable ( any SQLRow ) -> ( ) ) -> EventLoopFuture < Void > {
129+
130+ func execute( sql query: any SQLExpression , _ onRow: @escaping @Sendable ( any SQLRow ) -> Void ) -> EventLoopFuture < Void > {
131131 self . database. sql ( encoder: self . dataEncoder, decoder: self . dataDecoder, queryLogLevel: self . queryLogLevel) . execute ( sql: query, onRow)
132132 }
133-
134- func execute( sql query: any SQLExpression , _ onRow: @escaping @Sendable ( any SQLRow ) -> ( ) ) async throws {
135- try await self . database. sql ( encoder: self . dataEncoder, decoder: self . dataDecoder, queryLogLevel: self . queryLogLevel) . execute ( sql: query, onRow)
133+
134+ func execute( sql query: any SQLExpression , _ onRow: @escaping @Sendable ( any SQLRow ) -> Void ) async throws {
135+ try await self . database. sql ( encoder: self . dataEncoder, decoder: self . dataDecoder, queryLogLevel: self . queryLogLevel) . execute (
136+ sql: query, onRow)
136137 }
137-
138+
138139 func withSession< R> ( _ closure: @escaping @Sendable ( any SQLDatabase ) async throws -> R ) async throws -> R {
139140 try await self . database. sql ( encoder: self . dataEncoder, decoder: self . dataDecoder, queryLogLevel: self . queryLogLevel) . withSession ( closure)
140141 }
141142
142143 // SQLiteDatabase
143-
144+
144145 func query( _ query: String , _ binds: [ SQLiteData ] , logger: Logger , _ onRow: @escaping @Sendable ( SQLiteRow ) -> Void ) -> EventLoopFuture < Void > {
145146 self . withConnection { $0. query ( query, binds, logger: logger, onRow) }
146147 }
0 commit comments