FetchRequest.fetchCount() traps when the intent of the developer can’t be satisfied (yet), as in Person.select(name).distinct.fetchCount(db).

This commit is contained in:
Gwendal Roué committed 2016-03-04 09:15:43 +01:00
1 parent 3d4ee9ee53
commit b7a7cd953e
4 files changed
+63 -26

No files matched your search

+18
View File
@@ -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
+22
View File
@@ -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
}
}
}
@@ -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
@@ -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