Skip to content

Commit

Permalink
Fix #156 and #157
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Dec 21, 2016
1 parent 000f1d0 commit 302b9c2
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions GRDB/Core/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,10 @@ public final class Database {
// optimization prevents transaction observers from observing
// individual deletions.
sqlite3_set_authorizer(sqliteConnection, { (_, actionCode, CString1, CString2, CString3, CString4) -> Int32 in
if actionCode == SQLITE_DELETE {
if actionCode == SQLITE_DELETE && String(cString: CString1!) != "sqlite_master" {
// Prevent [truncate optimization](https://www.sqlite.org/lang_delete.html#truncateopt)
// so that transaction observers can observe individual deletions.
print("IGNORE DELETE FROM \(String(cString: CString1!))")
return SQLITE_IGNORE
} else {
return SQLITE_OK
Expand Down Expand Up @@ -1351,6 +1352,8 @@ final class StatementCompilationObserver {
/// Not nil if a statement is a BEGIN/COMMIT/ROLLBACK/RELEASE transaction/savepoint statement.
var transactionStatementInfo: UpdateStatement.TransactionStatementInfo?

var isDropTableStatement = false

init(_ database: Database) {
self.database = database
}
Expand All @@ -1362,6 +1365,9 @@ final class StatementCompilationObserver {
switch actionCode {
case SQLITE_DROP_TABLE, SQLITE_DROP_TEMP_TABLE, SQLITE_DROP_TEMP_VIEW, SQLITE_DROP_VIEW, SQLITE_DETACH, SQLITE_ALTER_TABLE, SQLITE_DROP_VTABLE, SQLITE_CREATE_INDEX, SQLITE_CREATE_TEMP_INDEX, SQLITE_DROP_INDEX, SQLITE_DROP_TEMP_INDEX:
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
if actionCode == SQLITE_DROP_TABLE {
observer.isDropTableStatement = true
}
observer.invalidatesDatabaseSchemaCache = true
case SQLITE_READ:
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
Expand All @@ -1371,10 +1377,13 @@ final class StatementCompilationObserver {
observer.databaseEventKinds.append(.insert(tableName: String(cString: CString1!)))
case SQLITE_DELETE:
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
observer.databaseEventKinds.append(.delete(tableName: String(cString: CString1!)))
// Prevent [truncate optimization](https://www.sqlite.org/lang_delete.html#truncateopt)
// so that transaction observers can observe individual deletions.
return SQLITE_IGNORE
let tableName = String(cString: CString1!)
if tableName != "sqlite_master" && !observer.isDropTableStatement {
observer.databaseEventKinds.append(.delete(tableName: tableName))
// Prevent [truncate optimization](https://www.sqlite.org/lang_delete.html#truncateopt)
// so that transaction observers can observe individual deletions.
return SQLITE_IGNORE
}
case SQLITE_UPDATE:
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
observer.insertUpdateEventKind(tableName: String(cString: CString1!), columnName: String(cString: CString2!))
Expand All @@ -1400,6 +1409,7 @@ final class StatementCompilationObserver {
databaseEventKinds = []
invalidatesDatabaseSchemaCache = false
transactionStatementInfo = nil
isDropTableStatement = false
}

func insertUpdateEventKind(tableName: String, columnName: String) {
Expand Down

0 comments on commit 302b9c2

Please sign in to comment.