@@ -3,6 +3,7 @@ import FluentSQL
33struct _FluentSQLiteDatabase {
44 let database : SQLiteDatabase
55 let context : DatabaseContext
6+ let inTransaction : Bool
67}
78
89extension _FluentSQLiteDatabase : Database {
@@ -43,9 +44,16 @@ extension _FluentSQLiteDatabase: Database {
4344 }
4445
4546 func transaction< T> ( _ closure: @escaping ( Database ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
46- self . database. withConnection { conn in
47+ guard !self . inTransaction else {
48+ return closure ( self )
49+ }
50+ return self . database. withConnection { conn in
4751 conn. query ( " BEGIN TRANSACTION " ) . flatMap { _ in
48- let db = _FluentSQLiteDatabase ( database: conn, context: self . context)
52+ let db = _FluentSQLiteDatabase (
53+ database: conn,
54+ context: self . context,
55+ inTransaction: true
56+ )
4957 return closure ( db) . flatMap { result in
5058 conn. query ( " COMMIT TRANSACTION " ) . map { _ in
5159 result
@@ -63,6 +71,7 @@ extension _FluentSQLiteDatabase: Database {
6371 switch schema. action {
6472 case . update:
6573 if schema. createFields. isEmpty {
74+ self . logger. warning ( " Ignoring schema update. SQLite only supports adding columns to existing tables " )
6675 return self . eventLoop. makeSucceededFuture ( ( ) )
6776 }
6877 default :
@@ -89,7 +98,7 @@ extension _FluentSQLiteDatabase: Database {
8998
9099 func withConnection< T> ( _ closure: @escaping ( Database ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
91100 self . database. withConnection {
92- closure ( _FluentSQLiteDatabase ( database: $0, context: self . context) )
101+ closure ( _FluentSQLiteDatabase ( database: $0, context: self . context, inTransaction : self . inTransaction ) )
93102 }
94103 }
95104}
@@ -134,18 +143,25 @@ private struct LastInsertRow: DatabaseOutput {
134143 return self
135144 }
136145
137- func contains( _ path: [ FieldKey ] ) -> Bool {
138- path [ 0 ] == . id || path [ 0 ] == self . customIDKey
146+ func contains( _ key: FieldKey ) -> Bool {
147+ key == . id || key == self . customIDKey
148+ }
149+
150+ func decodeNil( _ key: FieldKey ) throws -> Bool {
151+ guard key == . id || key == self . customIDKey else {
152+ fatalError ( " Cannot decode field from last insert row: \( key) . " )
153+ }
154+ return false
139155 }
140156
141- func decode< T> ( _ path : [ FieldKey ] , as type: T . Type ) throws -> T where T : Decodable {
142- guard path [ 0 ] == . id || path [ 0 ] == self . customIDKey else {
143- fatalError ( " Cannot decode field from last insert row: \( path ) . " )
157+ func decode< T> ( _ key : FieldKey , as type: T . Type ) throws -> T where T : Decodable {
158+ guard key == . id || key == self . customIDKey else {
159+ fatalError ( " Cannot decode field from last insert row: \( key ) . " )
144160 }
145161 if let autoincrementInitializable = T . self as? AutoincrementIDInitializable . Type {
146162 return autoincrementInitializable. init ( autoincrementID: self . lastAutoincrementID) as! T
147163 } else {
148- fatalError ( " Unsupported database generated identiifer type: \( T . self) " )
164+ fatalError ( " Unsupported database generated identifier type: \( T . self) " )
149165 }
150166 }
151167}
0 commit comments