From a0d9b9fbe8dd1b29d3eb293d27347ddf8eb3618b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 14 Dec 2016 08:07:42 +0100 Subject: [PATCH] Support for free BEGIN/COMMIT/ROLLBACK statements --- GRDB/Core/Database.swift | 69 ++++++++++++++++++++------------------- GRDB/Core/Statement.swift | 25 +++++++++++--- TODO.md | 1 + 3 files changed, 58 insertions(+), 37 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index f785bdd2c..c8c8d944c 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -637,7 +637,7 @@ extension Database { database: self, sqliteStatement: sqliteStatement!, invalidatesDatabaseSchemaCache: observer.invalidatesDatabaseSchemaCache, - savepointAction: observer.savepointAction, + transactionStatementInfo: observer.transactionStatementInfo, databaseEventKinds: observer.databaseEventKinds) let arguments = try consumeArguments(statement) statement.unsafeSetArguments(arguments) @@ -1289,8 +1289,8 @@ final class StatementCompilationObserver { /// cache, but not adding a table. var invalidatesDatabaseSchemaCache = false - /// Not nil if a statement is a BEGIN/RELEASE/ROLLBACK savepoint statement. - var savepointAction: (name: String, action: SavepointActionKind)? + /// Not nil if a statement is a BEGIN/COMMIT/ROLLBACK/RELEASE transaction/savepoint statement. + var transactionStatementInfo: UpdateStatement.TransactionStatementInfo? init(_ database: Database) { self.database = database @@ -1316,11 +1316,15 @@ final class StatementCompilationObserver { case SQLITE_UPDATE: let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self) observer.insertUpdateEventKind(tableName: String(cString: CString1!), columnName: String(cString: CString2!)) + case SQLITE_TRANSACTION: + let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self) + let action = UpdateStatement.TransactionStatementInfo.TransactionAction(rawValue: String(cString: CString1!))! + observer.transactionStatementInfo = .transaction(action: action) case SQLITE_SAVEPOINT: let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self) let name = String(cString: CString2!) - let action = SavepointActionKind(rawValue: String(cString: CString1!))! - observer.savepointAction = (name: name, action: action) + let action = UpdateStatement.TransactionStatementInfo.SavepointAction(rawValue: String(cString: CString1!))! + observer.transactionStatementInfo = .savePoint(name: name, action: action) default: break } @@ -1333,7 +1337,7 @@ final class StatementCompilationObserver { selectionInfo = SelectStatement.SelectionInfo() databaseEventKinds = [] invalidatesDatabaseSchemaCache = false - savepointAction = nil + transactionStatementInfo = nil } func insertUpdateEventKind(tableName: String, columnName: String) { @@ -1353,12 +1357,6 @@ final class StatementCompilationObserver { } } -enum SavepointActionKind : String { - case begin = "BEGIN" - case release = "RELEASE" - case rollback = "ROLLBACK" -} - // ========================================================================= // MARK: - Transactions & Savepoint @@ -1517,7 +1515,6 @@ extension Database { case .exclusive: try execute("BEGIN EXCLUSIVE TRANSACTION") } - isInsideExplicitTransaction = true } private func rollback(underlyingError: Error? = nil) throws { @@ -1549,15 +1546,10 @@ extension Database { throw error } } - - savepointStack.clear() // TODO: write tests that fail when we remove this line. Hint: those tests must not use any transaction observer because savepointStack.clear() is already called in willCommit() and didRollback() - isInsideExplicitTransaction = false } private func commit() throws { try execute("COMMIT TRANSACTION") - savepointStack.clear() // TODO: write tests that fail when we remove this line. Hint: those tests must not use any transaction observer because savepointStack.clear() is already called in willCommit() and didRollback() - isInsideExplicitTransaction = false } /// Add a transaction observer, so that it gets notified of @@ -1603,7 +1595,7 @@ extension Database { /// database cache), or RELEASE SAVEPOINT (which alters the savepoint stack) static func preconditionValidSelectStatement(sql: String, observer: StatementCompilationObserver) { GRDBPrecondition(observer.invalidatesDatabaseSchemaCache == false, "Invalid statement type for query \(String(reflecting: sql)): use UpdateStatement instead.") - GRDBPrecondition(observer.savepointAction == nil, "Invalid statement type for query \(String(reflecting: sql)): use UpdateStatement instead.") + GRDBPrecondition(observer.transactionStatementInfo == nil, "Invalid statement type for query \(String(reflecting: sql)): use UpdateStatement instead.") // Don't check for observer.databaseEventKinds.isEmpty // @@ -1674,23 +1666,34 @@ extension Database { clearSchemaCache() } - if let savepointAction = statement.savepointAction { - switch savepointAction.action { - case .begin: - savepointStack.beginSavepoint(named: savepointAction.name) - case .release: - savepointStack.releaseSavepoint(named: savepointAction.name) - if savepointStack.isEmpty { - let eventsBuffer = savepointStack.eventsBuffer - savepointStack.clear() - for (event, notifiedObservers) in eventsBuffer { - for observer in notifiedObservers.flatMap({ $0.transactionObserver }) { - event.send(to: observer) + if let transactionStatementInfo = statement.transactionStatementInfo { + switch transactionStatementInfo { + case .transaction(action: let action): + switch action { + case .begin: + isInsideExplicitTransaction = true + case .commit, .rollback: + savepointStack.clear() // TODO: write tests that fail when we remove this line. Hint: those tests must not use any transaction observer because savepointStack.clear() is already called in willCommit() and didRollback() + isInsideExplicitTransaction = false + } + case .savePoint(name: let name, action: let action): + switch action { + case .begin: + savepointStack.beginSavepoint(named: name) + case .release: + savepointStack.releaseSavepoint(named: name) + if savepointStack.isEmpty { + let eventsBuffer = savepointStack.eventsBuffer + savepointStack.clear() + for (event, notifiedObservers) in eventsBuffer { + for observer in notifiedObservers.flatMap({ $0.transactionObserver }) { + event.send(to: observer) + } } } + case .rollback: + savepointStack.rollbackSavepoint(named: name) } - case .rollback: - savepointStack.rollbackSavepoint(named: savepointAction.name) } } diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 33029f190..66f059471 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -345,15 +345,32 @@ public final class DatabaseCursor : Cursor { /// return .commit /// } public final class UpdateStatement : Statement { + enum TransactionStatementInfo { + enum SavepointAction : String { + case begin = "BEGIN" + case release = "RELEASE" + case rollback = "ROLLBACK" + } + + enum TransactionAction : String { + case begin = "BEGIN" + case commit = "COMMIT" + case rollback = "ROLLBACK" + } + + case transaction(action: TransactionAction) + case savePoint(name: String, action: SavepointAction) + } + /// If true, the database schema cache gets invalidated after this statement /// is executed. private(set) var invalidatesDatabaseSchemaCache: Bool - private(set) var savepointAction: (name: String, action: SavepointActionKind)? + private(set) var transactionStatementInfo: TransactionStatementInfo? private(set) var databaseEventKinds: [DatabaseEventKind] - init(database: Database, sqliteStatement: SQLiteStatement, invalidatesDatabaseSchemaCache: Bool, savepointAction: (name: String, action: SavepointActionKind)?, databaseEventKinds: [DatabaseEventKind]) { + init(database: Database, sqliteStatement: SQLiteStatement, invalidatesDatabaseSchemaCache: Bool, transactionStatementInfo: TransactionStatementInfo?, databaseEventKinds: [DatabaseEventKind]) { self.invalidatesDatabaseSchemaCache = invalidatesDatabaseSchemaCache - self.savepointAction = savepointAction + self.transactionStatementInfo = transactionStatementInfo self.databaseEventKinds = databaseEventKinds super.init(database: database, sqliteStatement: sqliteStatement) } @@ -365,7 +382,7 @@ public final class UpdateStatement : Statement { let observer = StatementCompilationObserver(database) try super.init(database: database, sql: sql, observer: observer) self.invalidatesDatabaseSchemaCache = observer.invalidatesDatabaseSchemaCache - self.savepointAction = observer.savepointAction + self.transactionStatementInfo = observer.transactionStatementInfo self.databaseEventKinds = observer.databaseEventKinds } diff --git a/TODO.md b/TODO.md index 36706b30d..ca51cbebb 100644 --- a/TODO.md +++ b/TODO.md @@ -3,6 +3,7 @@ - [ ] FetchedRecordsController: handle fetch errors - [ ] Swift 3.0.2 (Xcode 8.2): "Type inference will properly unwrap optionals when used with generics and implicitly-unwrapped optionals." Maybe this fixes `row.value(named: "foo") as? Int`? - [ ] Refactor Database notion of transaction/savepoints into a single type. Support INSERT OR ROLLBACK. + - [X] Observe begin/commit/rollback statements - Since some statements may implicitly rollback transactions, we can not rely on explicit rollback statements to infer the transaction state. We can only rely on sqlite3_rollback_hook, assuming it is called even for implicit rollbacks (test with an INSERT OR ROLLBACK statement). - If we rely on sqlite3_rollback_hook to handle rollbacks, it may be a good idea to rely on sqlite3_commit_hook to handle commits (and have the commit() and rollback() simply perform statements and return) - we'll have to deal with two input sources for dealing with transactions: statement compilation observation for savepoint statements, and transaction hooks for transaction statements. Tricky.