From 53a825fb5545ae7bc9ff67948631ab004c830f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 9 Sep 2015 06:02:23 +0200 Subject: [PATCH] Reentrancy documentation and cleanup --- GRDB/Core/Database.swift | 20 ++------------------ GRDB/Core/DatabaseQueue.swift | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 2e2f808ac..fbaf0f3eb 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -261,6 +261,8 @@ public final class Database { If the block throws an error, the transaction is rollbacked and the error is rethrown. + This method is not reentrant: you can't nest transactions. + - parameter type: The transaction type See https://www.sqlite.org/lang_transaction.html - parameter block: A block that executes SQL statements and return either @@ -292,13 +294,7 @@ public final class Database { } } - private var insideTransaction: Bool = false - private func beginTransaction(type: TransactionType = .Exclusive) throws { - guard insideTransaction == false else { - fatalError("Can not begin a transaction inside another transaction.") - } - switch type { case .Deferred: try execute("BEGIN DEFERRED TRANSACTION") @@ -307,26 +303,14 @@ public final class Database { case .Exclusive: try execute("BEGIN EXCLUSIVE TRANSACTION") } - - insideTransaction = true } private func rollback() throws { - guard insideTransaction == true else { - fatalError("Can not rollback: no current transaction.") - } - try execute("ROLLBACK TRANSACTION") - insideTransaction = false } private func commit() throws { - guard insideTransaction == true else { - fatalError("Can not commit: no current transaction.") - } - try execute("COMMIT TRANSACTION") - insideTransaction = false } } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index a472e93e3..aa588da10 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -62,19 +62,21 @@ public final class DatabaseQueue { // MARK: - Database access /** - Executes a block in the database queue. + Synchronously executes a block in the database queue. dbQueue.inDatabase { db in db.fetch(...) } - - This method is not reentrant. + + This method is reentrant. - parameter block: A block that accesses the databse. - throws: The error thrown by the block. */ public func inDatabase(block: (db: Database) throws -> Void) rethrows { - try inQueue { try block(db: self.database) } + try inQueue { + try block(db: self.database) + } } /** @@ -84,17 +86,20 @@ public final class DatabaseQueue { db.fetch(...) } - This method is not reentrant. + This method is reentrant. - parameter block: A block that accesses the databse. - throws: The error thrown by the block. */ public func inDatabase(block: (db: Database) throws -> R) rethrows -> R { - return try inQueue { return try block(db: self.database) } + return try inQueue { + return try block(db: self.database) + } } /** - Executes a block in the database queue, wrapped inside a transaction. + Synchronously executes a block in the database queue, wrapped inside a + transaction. If the block throws an error, the transaction is rollbacked and the error is rethrown. @@ -104,7 +109,7 @@ public final class DatabaseQueue { return .Commit } - This method is not reentrant. + This method is not reentrant: you can't nest transactions. - parameter type: The transaction type (default Exclusive) See https://www.sqlite.org/lang_transaction.html @@ -115,7 +120,7 @@ public final class DatabaseQueue { public func inTransaction(type: Database.TransactionType = .Exclusive, block: (db: Database) throws -> Database.TransactionCompletion) rethrows { let database = self.database try inQueue { - try self.database.inTransaction(type) { + try database.inTransaction(type) { try block(db: database) } }