Improve SelectionInfo for SQLite 3.19.0

This commit is contained in:
Gwendal Roué authored and Gwendal Roué committed 2017-05-28 13:15:41 +02:00
1 parent 713d58612e
commit d1d1a5da77
3 files changed
+42 -20

No files matched your search

+16 -7
View File
@@ -1489,7 +1489,13 @@ final class StatementCompilationObserver {
guard let tableName = cString1.map({ String(cString: $0) }) else { return SQLITE_OK }
guard let columnName = cString2.map({ String(cString: $0) }) else { return SQLITE_OK }
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
observer.selectionInfo.insert(column: columnName, ofTable: tableName)
if columnName.isEmpty {
// SELECT COUNT(*) FROM table
observer.selectionInfo.insert(table: tableName)
} else {
// SELECT column FROM table
observer.selectionInfo.insert(column: columnName, ofTable: tableName)
}
case SQLITE_INSERT:
guard let tableName = cString1.map({ String(cString: $0) }) else { return SQLITE_OK }
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
@@ -1520,14 +1526,17 @@ final class StatementCompilationObserver {
let action = UpdateStatement.TransactionStatementInfo.SavepointAction(rawValue: rawAction)!
observer.transactionStatementInfo = .savepoint(name: savepointName, action: action)
case SQLITE_FUNCTION:
// Starting SQLite 3.19.0, `SELECT COUNT(*) FROM table` triggers
// an authorization callback for SQLITE_READ with an empty
// column: http://www.sqlite.org/changes.html#version_3_19_0
//
// Before SQLite 3.19.0, `SELECT COUNT(*) FROM table` does not
// trigger any authorization callback that tells about the
// counted table: any use of the COUNT function makes the
// selection undetermined.
guard sqlite3_libversion_number() < 3019000 else { return SQLITE_OK }
guard let functionName = cString2.map({ String(cString: $0) }) else { return SQLITE_OK }
if functionName.uppercased() == "COUNT" {
// As soon as a request uses the COUNT function, we don't
// know which table is involved. For example, the
// `SELECT COUNT(*) FROM persons` request never ever tells
// GRDB about the `persons` table.
//
// We ignore the actual selection.
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
observer.selectionInfo = SelectStatement.SelectionInfo.unknown()
}
+16 -7
View File
@@ -259,11 +259,15 @@ public final class SelectStatement : Statement {
/// Information about the table and columns read by a SelectStatement
public struct SelectionInfo : CustomStringConvertible {
mutating func insert(table: String) {
tables.insert(table)
}
mutating func insert(column: String, ofTable table: String) {
if selection[table] != nil {
selection[table]!.insert(column)
if columns[table] != nil {
columns[table]!.insert(column)
} else {
selection[table] = [column]
columns[table] = [column]
}
}
@@ -272,12 +276,12 @@ public final class SelectStatement : Statement {
/// Relevant iff isUnknown is false
func contains(anyColumnFrom table: String) -> Bool {
return selection.index(forKey: table) != nil
return tables.contains(table) || columns.index(forKey: table) != nil
}
/// Relevant iff isUnknown is false
func contains(anyColumnIn columns: Set<String>, from table: String) -> Bool {
return !(selection[table]?.isDisjoint(with: columns) ?? true)
return tables.contains(table) || !(self.columns[table]?.isDisjoint(with: columns) ?? true)
}
init() {
@@ -288,7 +292,11 @@ public final class SelectStatement : Statement {
return self.init(isUnknown: true)
}
private var selection: [String: Set<String>] = [:] // [TableName: Set<ColumnName>]
// `SELECT a, b FROM t1` -> ["t1": ["a", "b"]]
private var columns: [String: Set<String>] = [:]
// `SELECT COUNT(*) FROM t1` -> ["t1"]
private var tables: Set<String> = []
private init(isUnknown: Bool) {
self.isUnknown = isUnknown
@@ -296,7 +304,8 @@ public final class SelectStatement : Statement {
/// A textual representation of `self`.
public var description: String {
return selection
// TODO: include tables in the description
return columns
.sorted { $0.key < $1.key }
.map { (table, columns) in "\(table)(\(columns.sorted().joined(separator: ", ")))" }
.joined(separator: ", ")
+10 -6
View File
@@ -189,12 +189,16 @@ class SelectStatementTests : GRDBTestCase {
db.makeSelectStatement("SELECT id, id3, a FROM table1"),
db.makeSelectStatement("SELECT table1.id, table1.a, table2.a FROM table1 JOIN table2 ON table1.id = table2.id"),
// This last request always triggers its observer because its selectionInfo is doubtful.
// We need SQLite support in order to avoid this doubt.
// TODO: discuss this on SQLite mailing-list.
// This last request triggers its observer or not, depending on the SQLite version.
// Before SQLite 3.19.0, its selectionInfo is doubtful, and every database change is deemed impactful.
// Starting SQLite 3.19.0, it knows that only table1 is involved.
//
// See doubtfulCountFunction below
db.makeSelectStatement("SELECT COUNT(*) FROM table1"),
]
let doubtfulCountFunction = (sqlite3_libversion_number() < 3019000)
let observers = statements.map { Observer(selectionInfo: $0.selectionInfo) }
for observer in observers {
db.add(transactionObserver: observer)
@@ -206,7 +210,7 @@ class SelectStatementTests : GRDBTestCase {
XCTAssertEqual(observers.map { $0.triggered }, [true, true, true, true])
try db.execute("INSERT INTO table2 (id, a, b) VALUES (NULL, 0, 0)")
XCTAssertEqual(observers.map { $0.triggered }, [false, false, true, true])
XCTAssertEqual(observers.map { $0.triggered }, [false, false, true, doubtfulCountFunction])
try db.execute("UPDATE table1 SET a = 1")
XCTAssertEqual(observers.map { $0.triggered }, [true, true, true, true])
@@ -215,10 +219,10 @@ class SelectStatementTests : GRDBTestCase {
XCTAssertEqual(observers.map { $0.triggered }, [true, false, false, true])
try db.execute("UPDATE table2 SET a = 1")
XCTAssertEqual(observers.map { $0.triggered }, [false, false, true, true])
XCTAssertEqual(observers.map { $0.triggered }, [false, false, true, doubtfulCountFunction])
try db.execute("UPDATE table2 SET b = 1")
XCTAssertEqual(observers.map { $0.triggered }, [false, false, false, true])
XCTAssertEqual(observers.map { $0.triggered }, [false, false, false, doubtfulCountFunction])
try db.execute("UPDATE table3 SET id = 2 WHERE id = 1")
XCTAssertEqual(observers.map { $0.triggered }, [true, true, false, true])