From b7a7cd953edc7cf12b78b3036744a282a570e8cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 4 Mar 2016 09:15:43 +0100 Subject: [PATCH] =?UTF-8?q?FetchRequest.fetchCount()=20traps=20when=20the?= =?UTF-8?q?=20intent=20of=20the=20developer=20can=E2=80=99t=20be=20satisfi?= =?UTF-8?q?ed=20(yet),=20as=20in=20Person.select(name).distinct.fetchCount?= =?UTF-8?q?(db).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GRDB/FetchRequest/FetchRequest.swift | 18 ++++++++++ GRDB/FetchRequest/SQLSelectQuery.swift | 22 ++++++++++++ .../FetchRequest/FetchRequestTests.swift | 35 ++++++++++++------- .../TableMapping+FetchRequestTests.swift | 14 +------- 4 files changed, 63 insertions(+), 26 deletions(-) diff --git a/GRDB/FetchRequest/FetchRequest.swift b/GRDB/FetchRequest/FetchRequest.swift index 3551f4135..fd8aaa616 100644 --- a/GRDB/FetchRequest/FetchRequest.swift +++ b/GRDB/FetchRequest/FetchRequest.swift @@ -168,11 +168,29 @@ extension FetchRequest { /// Returns the number of rows matched by the request. @warn_unused_result public func fetchCount(db: Database) -> Int { + guard !query.distinct else { + fatalError("Counting distinct fetch requests is not implemented yet.") + } + guard query.hasSingleTableSourceAndSelectsAllColumns else { + fatalError("Counting fetch requests with custom selection is not implemented yet.") + } return Int.fetchOne(db, select([_SQLExpression.Count(_SQLResultColumn.Star(nil))]))! } } +extension _SQLSelectQuery { + /// Support for FetchRequest.fetchCount() + private var hasSingleTableSourceAndSelectsAllColumns: Bool { + guard let source = source else { return false } + guard case .Table(name: let tableName, alias: _) = source else { return false } + guard selection.count == 1 else { return false } + guard selection[0].isStar(tableName: tableName) else { return false } + return true + } +} + + extension FetchRequest { // MARK: FetchRequest as subquery diff --git a/GRDB/FetchRequest/SQLSelectQuery.swift b/GRDB/FetchRequest/SQLSelectQuery.swift index 3390ee7bd..f25d67d55 100644 --- a/GRDB/FetchRequest/SQLSelectQuery.swift +++ b/GRDB/FetchRequest/SQLSelectQuery.swift @@ -257,6 +257,14 @@ extension _SQLDerivedExpressionType { public func countedSQL(db: Database, inout _ bindings: [DatabaseValueConvertible?]) throws -> String { return try sqlExpression.sql(db, &bindings) } + + /// This method is an implementation detail of the query interface. + /// Do not use it directly. + /// + /// See https://github.com/groue/GRDB.swift/#the-query-interface + public func isStar(tableName tableName: String) -> Bool { + return false + } } extension _SQLDerivedExpressionType { @@ -506,6 +514,7 @@ extension _SQLExpression : _SQLDerivedExpressionType { public protocol _SQLSelectable { func resultColumnSQL(db: Database, inout _ bindings: [DatabaseValueConvertible?]) throws -> String func countedSQL(db: Database, inout _ bindings: [DatabaseValueConvertible?]) throws -> String + func isStar(tableName tableName: String) -> Bool } enum _SQLResultColumn { @@ -536,6 +545,19 @@ extension _SQLResultColumn : _SQLSelectable { return try expression.sql(db, &bindings) } } + + func isStar(tableName tableName: String) -> Bool { + switch self { + case .Star(let sourceName): + if let sourceName = sourceName { + return sourceName == tableName + } else { + return true + } + case .Expression: + return false + } + } } diff --git a/Tests/Public/FetchRequest/FetchRequestTests.swift b/Tests/Public/FetchRequest/FetchRequestTests.swift index 85e06c56e..8420268e2 100644 --- a/Tests/Public/FetchRequest/FetchRequestTests.swift +++ b/Tests/Public/FetchRequest/FetchRequestTests.swift @@ -105,19 +105,28 @@ class FetchRequestTests: GRDBTestCase { } } - func testFetchCountWithCustomSelect() { - dbQueue.inDatabase { db in - XCTAssertEqual(tableRequest.select(Col.name).fetchCount(db), 0) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(\"name\") FROM \"readers\"") - } - } - - func testFetchCountDistinctWithCustomSelect() { - dbQueue.inDatabase { db in - XCTAssertEqual(tableRequest.select(Col.name).distinct.fetchCount(db), 0) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"name\") FROM \"readers\"") - } - } +// func testFetchCountWithCustomSelect() { +// dbQueue.inDatabase { db in +// XCTAssertEqual(tableRequest.select(Col.name).fetchCount(db), 0) +// XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(\"name\") FROM \"readers\"") +// +// XCTAssertEqual(tableRequest.select(Col.name).distinct.fetchCount(db), 0) +// XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"name\") FROM \"readers\"") +// +// XCTAssertEqual(tableRequest.select(Col.age * 2).distinct.fetchCount(db), 0) +// XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"age\" * 2) FROM \"readers\"") +// +// XCTAssertEqual(tableRequest.select((Col.age * 2).aliased("ignored")).distinct.fetchCount(db), 0) +// XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"age\" * 2) FROM \"readers\"") +// } +// } +// +// func testFetchCountWithDistinct() { +// dbQueue.inDatabase { db in +// XCTAssertEqual(tableRequest.distinct.fetchCount(db), 0) +// XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(*) FROM (SELECT DISTINCT * FROM \"readers\")") +// } +// } // MARK: - Select diff --git a/Tests/Public/FetchRequest/TableMapping+FetchRequestTests.swift b/Tests/Public/FetchRequest/TableMapping+FetchRequestTests.swift index f5c84718a..83a8fb003 100644 --- a/Tests/Public/FetchRequest/TableMapping+FetchRequestTests.swift +++ b/Tests/Public/FetchRequest/TableMapping+FetchRequestTests.swift @@ -60,19 +60,7 @@ class TableMappingFetchRequestTests: GRDBTestCase { } } - func testFetchCountWithCustomSelect() { - dbQueue.inDatabase { db in - XCTAssertEqual(Reader.select(Col.name).fetchCount(db), 0) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(\"name\") FROM \"readers\"") - } - } - - func testFetchCountDistinctWithCustomSelect() { - dbQueue.inDatabase { db in - XCTAssertEqual(Reader.select(Col.name).distinct.fetchCount(db), 0) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"name\") FROM \"readers\"") - } - } + // TODO: import counting tests from FetchRequestTests // MARK: - Select