From d1d1a5da7764dbd44b5645afe90ea6dac9b47474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 24 May 2017 13:53:28 +0200 Subject: [PATCH] Improve SelectionInfo for SQLite 3.19.0 --- GRDB/Core/Database.swift | 23 +++++++++++++++------- GRDB/Core/Statement.swift | 23 +++++++++++++++------- Tests/GRDBTests/SelectStatementTests.swift | 16 +++++++++------ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 6a7940aec..961b26f5a 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -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() } diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index d382bd4d3..dca5fe112 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -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, 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] = [:] // [TableName: Set] + // `SELECT a, b FROM t1` -> ["t1": ["a", "b"]] + private var columns: [String: Set] = [:] + + // `SELECT COUNT(*) FROM t1` -> ["t1"] + private var tables: Set = [] 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: ", ") diff --git a/Tests/GRDBTests/SelectStatementTests.swift b/Tests/GRDBTests/SelectStatementTests.swift index 155437a1c..ac05e90e9 100644 --- a/Tests/GRDBTests/SelectStatementTests.swift +++ b/Tests/GRDBTests/SelectStatementTests.swift @@ -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])