Tests for Statement.SelectionInfo.description

This commit is contained in:
Gwendal Roué committed 2017-09-08 13:18:45 +02:00
1 parent fedee67bda
commit 0309abca6f
3 files changed
+28 -16

No files matched your search

+1 -5
View File
@@ -1525,7 +1525,7 @@ final class StatementCompilationObserver {
let observer = Unmanaged<StatementCompilationObserver>.fromOpaque(observerPointer!).takeUnretainedValue()
if columnName.isEmpty {
// SELECT COUNT(*) FROM table
observer.selectionInfo.insert(table: tableName)
observer.selectionInfo.insert(allColumnsOfTable: tableName)
} else {
// SELECT column FROM table
observer.selectionInfo.insert(column: columnName, ofTable: tableName)
@@ -2320,10 +2320,6 @@ public enum DatabaseEventKind {
/// Returns whether event has any impact on tables and columns described
/// by selectionInfo.
public func impacts(_ selectionInfo: SelectStatement.SelectionInfo) -> Bool {
if selectionInfo.isUnknown {
return true
}
switch self {
case .delete(let tableName):
return selectionInfo.contains(anyColumnFrom: tableName)
+21 -11
View File
@@ -280,7 +280,7 @@ public final class SelectStatement : Statement {
/// Information about the table and columns read by a SelectStatement
public struct SelectionInfo : CustomStringConvertible {
mutating func insert(table: String) {
mutating func insert(allColumnsOfTable table: String) {
tables.insert(table)
}
@@ -288,16 +288,15 @@ public final class SelectStatement : Statement {
columns[table, default: []].insert(column)
}
/// If true, selection is unknown
let isUnknown: Bool
/// Relevant iff isUnknown is false
/// Returns true if isUnknown is true
func contains(anyColumnFrom table: String) -> Bool {
if isUnknown { return true }
return tables.contains(table) || columns.index(forKey: table) != nil
}
/// Relevant iff isUnknown is false
/// Returns true if isUnknown is true
func contains(anyColumnIn columns: Set<String>, from table: String) -> Bool {
if isUnknown { return true }
return tables.contains(table) || !(self.columns[table]?.isDisjoint(with: columns) ?? true)
}
@@ -309,6 +308,9 @@ public final class SelectStatement : Statement {
return self.init(isUnknown: true)
}
/// If true, selection is unknown
private let isUnknown: Bool
// `SELECT a, b FROM t1` -> ["t1": ["a", "b"]]
private var columns: [String: Set<String>] = [:]
@@ -321,11 +323,19 @@ public final class SelectStatement : Statement {
/// A textual representation of `self`.
public var description: String {
// TODO: include tables in the description
return columns
.sorted { $0.key < $1.key }
.map { (table, columns) in "\(table)(\(columns.sorted().joined(separator: ", ")))" }
.joined(separator: ", ")
if isUnknown {
return "unknown"
}
return tables.union(columns.keys)
.sorted()
.map { table -> String in
if let columns = columns[table] {
return "\(table)(\(columns.sorted().joined(separator: ",")))"
} else {
return "\(table)(*)"
}
}
.joined(separator: ",")
}
}
}
@@ -203,6 +203,12 @@ class SelectStatementTests : GRDBTestCase {
let doubtfulCountFunction = (sqlite3_libversion_number() < 3019000)
let observers = statements.map { Observer(selectionInfo: $0.selectionInfo) }
if doubtfulCountFunction {
XCTAssertEqual(observers.map { $0.selectionInfo.description }, ["table1(a,b,id,id3,id4)","table1(a,id,id3)", "table1(a,id),table2(a,id)", "unknown"])
} else {
XCTAssertEqual(observers.map { $0.selectionInfo.description }, ["table1(a,b,id,id3,id4)","table1(a,id,id3)", "table1(a,id),table2(a,id)", "table1(*)"])
}
for observer in observers {
db.add(transactionObserver: observer)
}