From 17d7c28f70d29dfdb74ba9a2cfbb904cbe4a5fc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 1 Jul 2015 14:11:24 +0200 Subject: [PATCH] inTransaction takes a block that should return .Commit or .Rollback --- GRDB Tests/DatabaseTests.swift | 4 +++- GRDB/Database.swift | 35 +++++++++++++++++++++++++++++----- GRDB/DatabaseMigrator.swift | 13 ++++++++----- GRDB/DatabaseQueue.swift | 7 ++----- README.md | 2 ++ TODO.md | 1 - 6 files changed, 45 insertions(+), 17 deletions(-) diff --git a/GRDB Tests/DatabaseTests.swift b/GRDB Tests/DatabaseTests.swift index fccf1d536..f552ca28d 100644 --- a/GRDB Tests/DatabaseTests.swift +++ b/GRDB Tests/DatabaseTests.swift @@ -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 diff --git a/GRDB/Database.swift b/GRDB/Database.swift index 69745bcbf..7f6feffca 100644 --- a/GRDB/Database.swift +++ b/GRDB/Database.swift @@ -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 } } diff --git a/GRDB/DatabaseMigrator.swift b/GRDB/DatabaseMigrator.swift index 0f1a1fe95..cee619dc2 100644 --- a/GRDB/DatabaseMigrator.swift +++ b/GRDB/DatabaseMigrator.swift @@ -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 } } } diff --git a/GRDB/DatabaseQueue.swift b/GRDB/DatabaseQueue.swift index 72ec686e3..7a11e39b4 100644 --- a/GRDB/DatabaseQueue.swift +++ b/GRDB/DatabaseQueue.swift @@ -33,14 +33,13 @@ public class DatabaseQueue { } } - public func inTransaction(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! } } } diff --git a/README.md b/README.md index 81f07645e..3e9be877f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/TODO.md b/TODO.md index a6a0a8c75..2eb262ef8 100644 --- a/TODO.md +++ b/TODO.md @@ -1,2 +1 @@ - [ ] Rename GRDB.Error to GRDB.SQLiteError -- [ ] Have inTransaction block return .Commit or .Rollback