SelectionInfo.rowIds contains the rowIds fetched by a request

This commit is contained in:
Gwendal Roué committed 2018-01-14 22:05:18 +01:00
1 parent 1a89b2eae5
commit 9c8a9fbe32
8 files changed
+265 -2

No files matched your search

@@ -26,6 +26,11 @@ extension QueryInterfaceRequest : TypedRequest {
public func fetchCount(_ db: Database) throws -> Int {
return try query.fetchCount(db)
}
/// Returns information about the table and columns read by the request.
public func selectionInfo(_ db: Database) throws -> SelectStatement.SelectionInfo {
return try query.selectionInfo(db)
}
}
extension QueryInterfaceRequest {
@@ -181,6 +181,30 @@ extension QueryInterfaceSelectQueryDefinition : Request {
return try Int.fetchOne(db, countQuery)!
}
/// Returns information about the table and columns read by the request.
public func selectionInfo(_ db: Database) throws -> SelectStatement.SelectionInfo {
let (statement, _) = try prepare(db)
var selectionInfo = statement.selectionInfo
selectionInfo.rowIds = try matchedRowIds(db)
return selectionInfo
}
private func matchedRowIds(_ db: Database) throws -> Set<Int64>? {
// Give up unless request feeds from a single database table
guard let source = source else { return nil }
guard case .table(name: let tableName, alias: _) = source else { return nil }
// Give up unless primary key is rowId
let primaryKeyInfo = try db.primaryKey(tableName)
guard primaryKeyInfo.isRowID else { return nil }
// Give up unless there is a where clause
guard let whereExpression = try wherePromise.resolve(db) else { return nil }
// The whereExpression knows better
return whereExpression.matchedRowIds(rowIdName: primaryKeyInfo.rowIDColumn)
}
private var countQuery: QueryInterfaceSelectQueryDefinition {
guard groupByExpressions.isEmpty && limit == nil else {
// SELECT ... GROUP BY ...
@@ -265,6 +265,56 @@ public struct SQLExpressionBinary : SQLExpression {
return SQLExpressionNot(self)
}
}
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
public func matchedRowIds(rowIdName: String?) -> Set<Int64>? {
switch op {
case .equal, .is:
// Look for `id == 1` or `id IS 1`?
switch (lhs, rhs) {
case (let column as Column, let dbValue as DatabaseValue),
(let dbValue as DatabaseValue, let column as Column):
// Look for `id == 1`, `rowid == 1`, `1 == id`, `1 == rowid`
var rowIdNames = [Column.rowID.name.lowercased()]
if let rowIdName = rowIdName {
rowIdNames.append(rowIdName.lowercased())
}
guard rowIdNames.contains(column.name.lowercased()) else {
return nil
}
if let rowId = Int64.fromDatabaseValue(dbValue) {
return [rowId]
} else {
return []
}
default:
return nil
}
case .and:
let lids = lhs.matchedRowIds(rowIdName: rowIdName)
let rids = rhs.matchedRowIds(rowIdName: rowIdName)
switch (lids, rids) {
case (nil, nil): return nil
case let (ids?, nil), let (nil, ids?): return ids
case let (lids?, rids?): return lids.intersection(rids)
}
case .or:
let lids = lhs.matchedRowIds(rowIdName: rowIdName)
let rids = rhs.matchedRowIds(rowIdName: rowIdName)
switch (lids, rids) {
case let (lids?, rids?): return lids.union(rids)
default: return nil
}
default:
return nil
}
}
}
// MARK: - SQLExpressionContains
@@ -296,6 +346,34 @@ struct SQLExpressionContains : SQLExpression {
var negated: SQLExpression {
return SQLExpressionContains(expression, collection, negated: !isNegated)
}
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
func matchedRowIds(rowIdName: String?) -> Set<Int64>? {
// Look for `id IN (1, 2, 3)`
guard let column = expression as? Column,
let array = collection as? SQLExpressionsArray else
{
return nil
}
var rowIdNames = [Column.rowID.name.lowercased()]
if let rowIdName = rowIdName {
rowIdNames.append(rowIdName.lowercased())
}
guard rowIdNames.contains(column.name.lowercased()) else {
return nil
}
var rowIDs: Set<Int64> = []
for expression in array.expressions {
guard let dbValue = expression as? DatabaseValue else { return nil }
if let rowId = Int64.fromDatabaseValue(dbValue) {
rowIDs.insert(rowId)
}
}
return rowIDs
}
}
// MARK: - SQLExpressionBetween
+12
View File
@@ -53,6 +53,11 @@ public protocol SQLExpression : SQLSpecificExpressible, SQLSelectable, SQLOrderi
/// let expression = [1,2,3].contains(Column("id")) // id IN (1,2,3)
/// expression.negated // id NOT IN (1,2,3)
var negated: SQLExpression { get }
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// Returns the rowIds matched by the expression.
func matchedRowIds(rowIdName: String?) -> Set<Int64>?
}
extension SQLExpression {
@@ -67,6 +72,13 @@ extension SQLExpression {
public var negated: SQLExpression {
return SQLExpressionNot(self)
}
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// The default implementation returns nil
public func matchedRowIds(rowIdName: String?) -> Set<Int64>? {
return nil
}
}
// SQLExpression: SQLExpressible