Support for free BEGIN/COMMIT/ROLLBACK statements

This commit is contained in:
Gwendal Roué committed 2016-12-14 08:07:42 +01:00
1 parent 2190e449c1
commit a0d9b9fbe8
3 files changed
+58 -37

No files matched your search

+36 -33
View File
@@ -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)
}
}
+21 -4
View File
@@ -345,15 +345,32 @@ public final class DatabaseCursor<Element> : 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
}
+1
View File
@@ -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.