mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Cleanup
This commit is contained in:
1 parent
9130fe7b38
commit
7f10bf4005
4 files changed
+79
-64
No files matched your search
@@ -263,7 +263,7 @@ extension Database {
|
||||
/// database cache), or RELEASE SAVEPOINT (which alters the savepoint stack)
|
||||
static func preconditionValidSelectStatement(sql: String, authorizer: StatementCompilationAuthorizer) {
|
||||
GRDBPrecondition(authorizer.invalidatesDatabaseSchemaCache == false, "Invalid statement type for query \(String(reflecting: sql)): use UpdateStatement instead.")
|
||||
GRDBPrecondition(authorizer.transactionStatementInfo == nil, "Invalid statement type for query \(String(reflecting: sql)): use UpdateStatement instead.")
|
||||
GRDBPrecondition(authorizer.transactionEffect == nil, "Invalid statement type for query \(String(reflecting: sql)): use UpdateStatement instead.")
|
||||
|
||||
// Don't check for authorizer.databaseEventKinds.isEmpty
|
||||
//
|
||||
|
||||
@@ -73,7 +73,22 @@ extension Database {
|
||||
}
|
||||
}
|
||||
|
||||
// This class is responsible for transaction observers
|
||||
/// This class provides core support for transaction observers.
|
||||
///
|
||||
/// Broker knows about transaction commits and rollbacks, through
|
||||
/// sqlite3_commit_hook and sqlite3_rollback_hook.
|
||||
///
|
||||
/// Broker knows about the statements that modify the transaction and savepoints
|
||||
/// state of the database connection (BEGIN/COMMIT/ROLLBACK/RELEASE), through
|
||||
/// UpdateStatement.transactionEffect. This property has been computed at
|
||||
/// statement compilation time by StatementCompilationAuthorizer,
|
||||
/// through sqlite3_set_authorizer.
|
||||
///
|
||||
/// Broker knows about individual database changes (row insertions, updates,
|
||||
/// and deletions) through sqlite3_update_hook and sqlite3_preupdate_hook.
|
||||
/// In order to be notified of all row deletions, the [truncate optimization](https://www.sqlite.org/lang_delete.html#truncateopt)
|
||||
/// is disabled by StatementCompilationAuthorizer and TruncateOptimizationBlocker
|
||||
/// through sqlite3_set_authorizer.
|
||||
class DatabaseObservationBroker {
|
||||
unowned var database: Database
|
||||
var savepointStack = SavepointStack()
|
||||
@@ -169,46 +184,44 @@ class DatabaseObservationBroker {
|
||||
// Wait for next statement
|
||||
activeTransactionObservations = []
|
||||
|
||||
if let transactionStatementInfo = statement.transactionStatementInfo {
|
||||
if let transactionEffect = statement.transactionEffect {
|
||||
// Statement has modified transaction/savepoint state
|
||||
|
||||
switch transactionStatementInfo {
|
||||
case .transaction(action: let action):
|
||||
switch action {
|
||||
case .begin:
|
||||
break
|
||||
case .commit:
|
||||
if case .none = self.transactionState {
|
||||
// A COMMIT statement has ended a deferred transaction
|
||||
// that did not open, and sqlite_commit_hook was not
|
||||
// called.
|
||||
//
|
||||
// BEGIN DEFERRED TRANSACTION
|
||||
// COMMIT
|
||||
self.transactionState = .commit
|
||||
}
|
||||
case .rollback:
|
||||
break
|
||||
switch transactionEffect {
|
||||
case .beginTransaction:
|
||||
break
|
||||
|
||||
case .commitTransaction:
|
||||
if case .none = self.transactionState {
|
||||
// A COMMIT statement has ended a deferred transaction
|
||||
// that did not open, and sqlite_commit_hook was not
|
||||
// called.
|
||||
//
|
||||
// BEGIN DEFERRED TRANSACTION
|
||||
// COMMIT
|
||||
self.transactionState = .commit
|
||||
}
|
||||
case .savepoint(name: let name, action: let action):
|
||||
switch action {
|
||||
case .begin:
|
||||
savepointStack.savepointDidStart(name)
|
||||
case .release:
|
||||
savepointStack.statementDidRelease(name)
|
||||
if savepointStack.isEmpty {
|
||||
// Notify buffered events
|
||||
let eventsBuffer = savepointStack.eventsBuffer
|
||||
savepointStack.clear()
|
||||
for (event, observations) in eventsBuffer {
|
||||
for observation in observations {
|
||||
event.send(to: observation)
|
||||
}
|
||||
|
||||
case .rollbackTransaction:
|
||||
break
|
||||
|
||||
case .beginSavepoint(let name):
|
||||
savepointStack.savepointDidBegin(name)
|
||||
|
||||
case .releaseSavepoint(let name):
|
||||
savepointStack.savepointDidRelease(name)
|
||||
if savepointStack.isEmpty {
|
||||
// Notify buffered events
|
||||
let eventsBuffer = savepointStack.eventsBuffer
|
||||
savepointStack.clear()
|
||||
for (event, observations) in eventsBuffer {
|
||||
for observation in observations {
|
||||
event.send(to: observation)
|
||||
}
|
||||
}
|
||||
case .rollback:
|
||||
savepointStack.savepointDidRollback(name)
|
||||
}
|
||||
|
||||
case .rollbackSavepoint(let name):
|
||||
savepointStack.savepointDidRollback(name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -903,7 +916,7 @@ class SavepointStack {
|
||||
savepoints.removeAll()
|
||||
}
|
||||
|
||||
func savepointDidStart(_ name: String) {
|
||||
func savepointDidBegin(_ name: String) {
|
||||
savepoints.append((name: name.lowercased(), index: eventsBuffer.count))
|
||||
}
|
||||
|
||||
@@ -932,7 +945,7 @@ class SavepointStack {
|
||||
// > transaction stack and releases savepoints backwards in time until it
|
||||
// > releases a savepoint with a matching savepoint-name. Prior savepoints,
|
||||
// > even savepoints with matching savepoint-names, are unchanged.
|
||||
func statementDidRelease(_ name: String) {
|
||||
func savepointDidRelease(_ name: String) {
|
||||
let name = name.lowercased()
|
||||
while let pair = savepoints.last, pair.name != name {
|
||||
savepoints.removeLast()
|
||||
|
||||
@@ -416,21 +416,13 @@ public final class StatementCursor: Cursor {
|
||||
/// return .commit
|
||||
/// }
|
||||
public final class UpdateStatement : Statement, AuthorizedStatement {
|
||||
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)
|
||||
enum TransactionEffect {
|
||||
case beginTransaction
|
||||
case commitTransaction
|
||||
case rollbackTransaction
|
||||
case beginSavepoint(String)
|
||||
case releaseSavepoint(String)
|
||||
case rollbackSavepoint(String)
|
||||
}
|
||||
|
||||
/// If true, the database schema cache gets invalidated after this statement
|
||||
@@ -441,7 +433,7 @@ public final class UpdateStatement : Statement, AuthorizedStatement {
|
||||
/// when executed
|
||||
private(set) var needsTruncateOptimizationPreventionDuringExecution: Bool
|
||||
|
||||
private(set) var transactionStatementInfo: TransactionStatementInfo?
|
||||
private(set) var transactionEffect: TransactionEffect?
|
||||
private(set) var databaseEventKinds: [DatabaseEventKind]
|
||||
|
||||
/// Creates a prepared statement.
|
||||
@@ -473,7 +465,7 @@ public final class UpdateStatement : Statement, AuthorizedStatement {
|
||||
prepFlags: prepFlags)
|
||||
self.invalidatesDatabaseSchemaCache = authorizer.invalidatesDatabaseSchemaCache
|
||||
self.needsTruncateOptimizationPreventionDuringExecution = authorizer.needsTruncateOptimizationPreventionDuringExecution
|
||||
self.transactionStatementInfo = authorizer.transactionStatementInfo
|
||||
self.transactionEffect = authorizer.transactionEffect
|
||||
self.databaseEventKinds = authorizer.databaseEventKinds
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ final class StatementCompilationAuthorizer : StatementAuthorizer {
|
||||
|
||||
/// Not nil if a statement is a BEGIN/COMMIT/ROLLBACK/RELEASE transaction or
|
||||
/// savepoint statement.
|
||||
var transactionStatementInfo: UpdateStatement.TransactionStatementInfo?
|
||||
var transactionEffect: UpdateStatement.TransactionEffect?
|
||||
|
||||
/// True if statement may trigger the truncate optimization
|
||||
var needsTruncateOptimizationPreventionDuringExecution = false
|
||||
@@ -86,16 +86,26 @@ final class StatementCompilationAuthorizer : StatementAuthorizer {
|
||||
return SQLITE_OK
|
||||
|
||||
case SQLITE_TRANSACTION:
|
||||
guard let rawAction = cString1.map({ String(cString: $0) }) else { return SQLITE_OK }
|
||||
let action = UpdateStatement.TransactionStatementInfo.TransactionAction(rawValue: rawAction)!
|
||||
transactionStatementInfo = .transaction(action: action)
|
||||
guard let cString1 = cString1 else { return SQLITE_OK }
|
||||
if strcmp(cString1, "BEGIN") == 0 {
|
||||
transactionEffect = .beginTransaction
|
||||
} else if strcmp(cString1, "COMMIT") == 0 {
|
||||
transactionEffect = .commitTransaction
|
||||
} else if strcmp(cString1, "ROLLBACK") == 0 {
|
||||
transactionEffect = .rollbackTransaction
|
||||
}
|
||||
return SQLITE_OK
|
||||
|
||||
case SQLITE_SAVEPOINT:
|
||||
guard let rawAction = cString1.map({ String(cString: $0) }) else { return SQLITE_OK }
|
||||
guard let savepointName = cString2.map({ String(cString: $0) }) else { return SQLITE_OK }
|
||||
let action = UpdateStatement.TransactionStatementInfo.SavepointAction(rawValue: rawAction)!
|
||||
transactionStatementInfo = .savepoint(name: savepointName, action: action)
|
||||
guard let cString1 = cString1 else { return SQLITE_OK }
|
||||
guard let name = cString2.map({ String(cString: $0) }) else { return SQLITE_OK }
|
||||
if strcmp(cString1, "BEGIN") == 0 {
|
||||
transactionEffect = .beginSavepoint(name)
|
||||
} else if strcmp(cString1, "RELEASE") == 0 {
|
||||
transactionEffect = .releaseSavepoint(name)
|
||||
} else if strcmp(cString1, "ROLLBACK") == 0 {
|
||||
transactionEffect = .rollbackSavepoint(name)
|
||||
}
|
||||
return SQLITE_OK
|
||||
|
||||
case SQLITE_FUNCTION:
|
||||
|
||||
Reference in new issue
Block a user