Reentrancy documentation and cleanup

This commit is contained in:
Gwendal Roué committed 2015-09-09 06:02:23 +02:00
1 parent f0c82a1016
commit 53a825fb55
2 files changed
+16 -27

No files matched your search

+2 -18
View File
@@ -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
}
}
+14 -9
View File
@@ -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<R>(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)
}
}