mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
SelectionInfo.rowIds contains the rowIds fetched by a request
This commit is contained in:
1 parent
1a89b2eae5
commit
9c8a9fbe32
8 files changed
+265
-2
No files matched your search
@@ -22,6 +22,14 @@ public protocol Request {
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
func fetchCount(_ db: Database) throws -> Int
|
||||
|
||||
/// Returns information about the table and columns read by the request.
|
||||
///
|
||||
/// This method has a default implementation which is not supposed to
|
||||
/// be replaced.
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
func selectionInfo(_ db: Database) throws -> SelectStatement.SelectionInfo
|
||||
}
|
||||
|
||||
extension Request {
|
||||
@@ -48,6 +56,14 @@ extension Request {
|
||||
let (statement, adapter) = try prepare(db)
|
||||
return SQLRequest(statement.sql, arguments: statement.arguments, adapter: adapter, cached: cached)
|
||||
}
|
||||
|
||||
/// Returns information about the table and columns read by the request.
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
public func selectionInfo(_ db: Database) throws -> SelectStatement.SelectionInfo {
|
||||
let (statement, _) = try prepare(db)
|
||||
return statement.selectionInfo
|
||||
}
|
||||
}
|
||||
|
||||
extension Request {
|
||||
|
||||
@@ -379,6 +379,12 @@ public final class SelectStatement : Statement, AuthorizedStatement {
|
||||
/// `SELECT COUNT(*) FROM t1` -> ["t1"]
|
||||
private var tables: Set<String> = []
|
||||
|
||||
/// The fetched rowIds
|
||||
/// See QueryInterfaceRequestDefinition.selectionInfo(_:)
|
||||
/// `SELECT * FROM t WHERE id IN (1, 2, 3)` -> [1, 2, 3]
|
||||
/// `SELECT * FROM t` -> nil
|
||||
var rowIds: Set<Int64>? = nil
|
||||
|
||||
/// The unknown selection
|
||||
static let unknown = SelectionInfo(isUnknown: true)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -393,8 +393,7 @@ public final class FetchedRecordsController<Record: RowConvertible> {
|
||||
itemsAreIdenticalFactory: ItemComparatorFactory<Record>) throws
|
||||
-> (SelectStatement.SelectionInfo, ItemComparator<Record>)
|
||||
{
|
||||
let (statement, _) = try request.prepare(db)
|
||||
let selectionInfo = statement.selectionInfo
|
||||
let selectionInfo = try request.selectionInfo(db)
|
||||
let itemsAreIdentical = try itemsAreIdenticalFactory(db)
|
||||
return (selectionInfo, itemsAreIdentical)
|
||||
}
|
||||
|
||||
@@ -41,6 +41,129 @@ class StatementSelectionInfoTests : GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSelectionInfoRowIds() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, a TEXT)")
|
||||
struct Record: TableMapping {
|
||||
static let databaseTableName = "foo"
|
||||
}
|
||||
|
||||
// Undefined rowIds
|
||||
|
||||
do {
|
||||
let request = Record.all()
|
||||
try XCTAssertNil(request.selectionInfo(db).rowIds)
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(Column("a") == 1)
|
||||
try XCTAssertNil(request.selectionInfo(db).rowIds)
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(Column("id") >= 1)
|
||||
try XCTAssertNil(request.selectionInfo(db).rowIds)
|
||||
}
|
||||
|
||||
do {
|
||||
let request = Record.filter((Column("id") == 1) || (Column("a") == "foo"))
|
||||
try XCTAssertNil(request.selectionInfo(db).rowIds)
|
||||
}
|
||||
|
||||
// No rowId
|
||||
|
||||
do {
|
||||
let request = Record.filter(Column("id") == nil)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [])
|
||||
}
|
||||
|
||||
do {
|
||||
let request = Record.filter(Column("id") === nil)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [])
|
||||
}
|
||||
|
||||
do {
|
||||
let request = Record.filter(nil == Column("id"))
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [])
|
||||
}
|
||||
|
||||
do {
|
||||
let request = Record.filter(nil === Column("id"))
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [])
|
||||
}
|
||||
|
||||
do {
|
||||
let request = Record.filter((Column("id") == 1) && (Column("id") == 2))
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(key: 1).filter(key: 2)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [])
|
||||
}
|
||||
|
||||
// Single rowId
|
||||
|
||||
do {
|
||||
let request = Record.filter(Column("id") == 1)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(Column("id") === 1)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(Column("id") == 1 && Column("a") == "foo")
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(Column.rowID == 1)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(1 == Column("id"))
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(1 === Column("id"))
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(1 === Column.rowID)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(key: 1)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(key: 1).filter(key: 1)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(key: 1).filter(Column("a") == "foo")
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1])
|
||||
}
|
||||
|
||||
// Multiple rowIds
|
||||
|
||||
do {
|
||||
let request = Record.filter(Column("id") == 1 || Column.rowID == 2)
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1, 2])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter([1, 2, 3].contains(Column("id")))
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1, 2, 3])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter([1, 2, 3].contains(Column.rowID))
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1, 2, 3])
|
||||
}
|
||||
do {
|
||||
let request = Record.filter(keys: [1, 2, 3])
|
||||
try XCTAssertEqual(request.selectionInfo(db).rowIds!, [1, 2, 3])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testUpdateStatement() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
Reference in new issue
Block a user