Skip to content

Commit df95e65

Browse files
authored
Allow transactions to be nonescapable
1 parent 5e771a1 commit df95e65

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Sources/SQLite/Core/Connection.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public final class Connection {
328328
/// must throw to roll the transaction back.
329329
///
330330
/// - Throws: `Result.Error`, and rethrows.
331-
public func transaction(_ mode: TransactionMode = .deferred, block: @escaping () throws -> Void) throws {
331+
public func transaction(_ mode: TransactionMode = .deferred, block: () throws -> Void) throws {
332332
try transaction("BEGIN \(mode.rawValue) TRANSACTION", block, "COMMIT TRANSACTION", or: "ROLLBACK TRANSACTION")
333333
}
334334

@@ -348,14 +348,14 @@ public final class Connection {
348348
/// The block must throw to roll the savepoint back.
349349
///
350350
/// - Throws: `SQLite.Result.Error`, and rethrows.
351-
public func savepoint(_ name: String = UUID().uuidString, block: @escaping () throws -> Void) throws {
351+
public func savepoint(_ name: String = UUID().uuidString, block: () throws -> Void) throws {
352352
let name = name.quote("'")
353353
let savepoint = "SAVEPOINT \(name)"
354354

355355
try transaction(savepoint, block, "RELEASE \(savepoint)", or: "ROLLBACK TO \(savepoint)")
356356
}
357357

358-
fileprivate func transaction(_ begin: String, _ block: @escaping () throws -> Void, _ commit: String, or rollback: String) throws {
358+
fileprivate func transaction(_ begin: String, _ block: () throws -> Void, _ commit: String, or rollback: String) throws {
359359
return try sync {
360360
try self.run(begin)
361361
do {
@@ -626,11 +626,11 @@ public final class Connection {
626626

627627
// MARK: - Error Handling
628628

629-
func sync<T>(_ block: @escaping () throws -> T) rethrows -> T {
629+
func sync<T>(_ block: () throws -> T) rethrows -> T {
630630
var success: T?
631631
var failure: Error?
632632

633-
let box: () -> Void = {
633+
func execute(_ block: () throws -> T, success: inout T?, failure: inout Error?) {
634634
do {
635635
success = try block()
636636
} catch {
@@ -639,9 +639,11 @@ public final class Connection {
639639
}
640640

641641
if DispatchQueue.getSpecific(key: Connection.queueKey) == queueContext {
642-
box()
642+
execute(block, success: &success, failure: &failure)
643643
} else {
644-
queue.sync(execute: box) // FIXME: rdar://problem/21389236
644+
queue.sync(execute: {
645+
execute(block, success: &success, failure: &failure)
646+
}) // FIXME: rdar://problem/21389236
645647
}
646648

647649
if let failure = failure {

0 commit comments

Comments
 (0)