inTransaction takes a block that should return .Commit or .Rollback

This commit is contained in:
Gwendal Roué committed 2015-07-01 14:11:24 +02:00
1 parent b9c3254c37
commit 17d7c28f70
6 files changed
+45 -17

No files matched your search

+3 -1
View File
@@ -532,7 +532,7 @@ class DatabaseTests: XCTestCase {
let configuration = DatabaseConfiguration(verbose: true)
let dbQueue = try DatabaseQueue(path: "/tmp/GRDB.sqlite", configuration: configuration)
try dbQueue.inTransaction { db -> Void in
try dbQueue.inTransaction { db in
try db.execute("DROP TABLE IF EXISTS persons")
try db.execute(
"CREATE TABLE persons (" +
@@ -578,6 +578,8 @@ class DatabaseTests: XCTestCase {
try insert4Stmt.reset()
insert4Stmt.bind([":name": "Crystel no age", ":age": nil])
try insert4Stmt.execute()
return .Commit
}
try dbQueue.inDatabase { db -> Void in
+30 -5
View File
@@ -178,14 +178,39 @@ public class Database {
// MARK: - transactions
public func inTransaction(type: TransactionType = .Exclusive, block: () throws -> Void) throws {
public enum TransactionCompletion {
case Commit
case Rollback
}
public func inTransaction(type: TransactionType = .Exclusive, block: () throws -> TransactionCompletion) throws {
var completion: TransactionCompletion = .Rollback
var dbError: ErrorType? = nil
try beginTransaction(type)
do {
try block()
try commit()
completion = try block()
} catch {
try rollback()
throw error
completion = .Rollback
dbError = error
}
do {
switch completion {
case .Commit:
try commit()
case .Rollback:
try rollback()
}
} catch {
if dbError == nil {
dbError = error
}
}
if let dbError = dbError {
throw dbError
}
}
+8 -5
View File
@@ -38,13 +38,16 @@ public struct DatabaseMigrator {
}
private func runMigrations(dbQueue: DatabaseQueue) throws {
try dbQueue.inTransaction { db -> Void in
let appliedMigrationIdentifiers = try db.fetchValues("SELECT identifier FROM db_migrations", type: String.self).map { $0! }
for (position, migration) in self.migrations.enumerate() {
if appliedMigrationIdentifiers.indexOf(migration.identifier) == nil {
let appliedMigrationIdentifiers = try dbQueue.inDatabase { db in
try db.fetchValues("SELECT identifier FROM db_migrations", type: String.self).map { $0! }
}
for (position, migration) in self.migrations.enumerate() {
if appliedMigrationIdentifiers.indexOf(migration.identifier) == nil {
try dbQueue.inTransaction { db in
try migration.block(db: db)
try db.execute("INSERT INTO db_migrations (position, identifier) VALUES (?, ?)", bindings: [position, migration.identifier])
return .Commit
}
}
}
+2 -5
View File
@@ -33,14 +33,13 @@ public class DatabaseQueue {
}
}
public func inTransaction<R>(type: Database.TransactionType = .Exclusive, block: (db: Database) throws -> R) throws -> R {
public func inTransaction(type: Database.TransactionType = .Exclusive, block: (db: Database) throws -> Database.TransactionCompletion) throws {
var dbError: ErrorType?
var result: R? = nil
let database = self.database
dispatch_sync(queue) { () -> Void in
do {
try database.inTransaction(type) { () in
result = try block(db: database)
return try block(db: database)
}
} catch {
dbError = error
@@ -48,8 +47,6 @@ public class DatabaseQueue {
}
if let dbError = dbError {
throw dbError
} else {
return result!
}
}
}
+2
View File
@@ -31,6 +31,8 @@ try dbQueue.inTransaction { db -> Void in
try db.execute(
"INSERT INTO persons (name, age) VALUES (:name, :age)",
bindings: [":name": "Barbara", ":age": 37])
return .Commit
}
try dbQueue.inDatabase { db -> Void in
-1
View File
@@ -1,2 +1 @@
- [ ] Rename GRDB.Error to GRDB.SQLiteError
- [ ] Have inTransaction block return .Commit or .Rollback