mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Fix #583 by keeping a single authorizer active for the whole lifetime of a database connection
SQLite invalidates statements when authorizer changes, which can lead to undesired behaviors. This is why we now call sqlite3_set_authorizer only once. See http://sqlite.1065341.n5.nabble.com/Issue-report-sqlite3-set-authorizer-triggers-error-4-516-SQLITE-ABORT-ROLLBACK-during-statement-itern-td107972.html
This commit is contained in:
1 parent
bb959fa5c3
commit
a59f4ef096
1 file changed
+16
-20
+16
-20
@@ -130,26 +130,7 @@ public final class Database {
|
||||
var lastErrorMessage: String? { return String(cString: sqlite3_errmsg(sqliteConnection)) }
|
||||
|
||||
// Statement authorizer
|
||||
var authorizer: StatementAuthorizer? {
|
||||
didSet {
|
||||
switch (oldValue, authorizer) {
|
||||
case (.some, nil):
|
||||
sqlite3_set_authorizer(sqliteConnection, nil, nil)
|
||||
case (nil, .some):
|
||||
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
|
||||
sqlite3_set_authorizer(
|
||||
sqliteConnection,
|
||||
{ (dbPointer, actionCode, cString1, cString2, cString3, cString4) -> Int32 in
|
||||
guard let dbPointer = dbPointer else { return SQLITE_OK }
|
||||
let db = Unmanaged<Database>.fromOpaque(dbPointer).takeUnretainedValue()
|
||||
return db.authorizer!.authorize(actionCode, cString1, cString2, cString3, cString4)
|
||||
},
|
||||
dbPointer)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
var authorizer: StatementAuthorizer?
|
||||
|
||||
// Transaction observers management
|
||||
lazy var observationBroker = DatabaseObservationBroker(self)
|
||||
@@ -221,6 +202,7 @@ extension Database {
|
||||
setupBusyMode()
|
||||
setupDefaultFunctions()
|
||||
setupDefaultCollations()
|
||||
setupAuthorizer()
|
||||
observationBroker.installCommitAndRollbackHooks()
|
||||
try activateExtendedCodes()
|
||||
|
||||
@@ -352,6 +334,20 @@ extension Database {
|
||||
add(collation: .localizedStandardCompare)
|
||||
}
|
||||
|
||||
private func setupAuthorizer() {
|
||||
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
|
||||
sqlite3_set_authorizer(
|
||||
sqliteConnection,
|
||||
{ (dbPointer, actionCode, cString1, cString2, cString3, cString4) -> Int32 in
|
||||
guard let dbPointer = dbPointer else { return SQLITE_OK }
|
||||
let db = Unmanaged<Database>.fromOpaque(dbPointer).takeUnretainedValue()
|
||||
guard let authorizer = db.authorizer else { return SQLITE_OK }
|
||||
return authorizer.authorize(actionCode, cString1, cString2, cString3, cString4)
|
||||
},
|
||||
dbPointer)
|
||||
}
|
||||
|
||||
|
||||
private func activateExtendedCodes() throws {
|
||||
let code = sqlite3_extended_result_codes(sqliteConnection, 1)
|
||||
guard code == SQLITE_OK else {
|
||||
|
||||
Reference in new issue
Block a user