Skip to content

Commit 660ace1

Browse files
committedFeb 13, 2020
beta 4
1 parent 65c487e commit 660ace1

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed
 

‎Sources/FluentSQLiteDriver/FluentSQLiteDatabase.swift

+17
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ extension _FluentSQLiteDatabase: Database {
3232
}
3333
}
3434
}
35+
36+
func transaction<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
37+
self.database.withConnection { conn in
38+
conn.query("BEGIN TRANSACTION").flatMap { _ in
39+
let db = _FluentSQLiteDatabase(database: conn, context: self.context)
40+
return closure(db).flatMap { result in
41+
conn.query("COMMIT TRANSACTION").map { _ in
42+
result
43+
}
44+
}.flatMapError { error in
45+
conn.query("ROLLBACK TRANSACTION").flatMapThrowing { _ in
46+
throw error
47+
}
48+
}
49+
}
50+
}
51+
}
3552

3653
func execute(schema: DatabaseSchema) -> EventLoopFuture<Void> {
3754
let sql = SQLSchemaConverter(delegate: SQLiteConverterDelegate()).convert(schema)

‎Tests/FluentSQLiteDriverTests/FluentSQLiteDriverTests.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ final class FluentSQLiteDriverTests: XCTestCase {
3232
try self.benchmarker.testEagerLoadParent()
3333
}
3434

35-
func testEagerLoadParentJoin() throws {
36-
try self.benchmarker.testEagerLoadParentJoin()
37-
}
38-
3935
func testEagerLoadParentJSON() throws {
4036
try self.benchmarker.testEagerLoadParentJSON()
4137
}
@@ -100,6 +96,10 @@ final class FluentSQLiteDriverTests: XCTestCase {
10096
try self.benchmarker.testTimestampable()
10197
}
10298

99+
func testTransaction() throws {
100+
try self.benchmarker.testTransaction()
101+
}
102+
103103
func testModelMiddleware() throws {
104104
try self.benchmarker.testModelMiddleware()
105105
}
@@ -164,10 +164,18 @@ final class FluentSQLiteDriverTests: XCTestCase {
164164
try self.benchmarker.testRange()
165165
}
166166

167+
func testRelationMethods() throws {
168+
try self.benchmarker.testRelationMethods()
169+
}
170+
167171
func testNonstandardIDKey() throws {
168172
try self.benchmarker.testNonstandardIDKey()
169173
}
170174

175+
func testGroup() throws {
176+
try self.benchmarker.testGroup()
177+
}
178+
171179
var benchmarker: FluentBenchmarker {
172180
return .init(database: self.database)
173181
}

0 commit comments

Comments
 (0)