diff --git a/GRDB/FetchRequest/FetchRequest.swift b/GRDB/FetchRequest/FetchRequest.swift index 7c48293d9..5036dd962 100644 --- a/GRDB/FetchRequest/FetchRequest.swift +++ b/GRDB/FetchRequest/FetchRequest.swift @@ -146,24 +146,7 @@ extension FetchRequest { /// Returns the number of rows matched by the request. @warn_unused_result public func fetchCount(db: Database) -> Int { - return fetchAggregate(db, _SQLExpression.Count(_SQLResultColumn.Star(nil)))! - } - - /// Returns the number of non-NULL values of *counted*. - @warn_unused_result - public func fetchCount(db: Database, _ counted: _SQLSelectable) -> Int { - return fetchAggregate(db, _SQLExpression.Count(counted ?? _SQLResultColumn.Star(nil)))! - } - - /// Returns the number of distinct values of *counted*. - @warn_unused_result - public func fetchCount(db: Database, distinct counted: _SQLExpressionType) -> Int { - return fetchAggregate(db, count(distinct: counted.SQLExpression))! - } - - @warn_unused_result - private func fetchAggregate(db: Database, _ selection: _SQLSelectable) -> T? { - return T.fetchOne(db, FetchRequest(query.select([selection]))) + return Int.fetchOne(db, select([_SQLExpression.Count(_SQLResultColumn.Star(nil))]))! } } @@ -291,18 +274,6 @@ extension TableMapping { public static func fetchCount(db: Database) -> Int { return all().fetchCount(db) } - - /// Returns the number of non-NULL values of *counted*. - @warn_unused_result - public static func fetchCount(db: Database, _ counted: _SQLSelectable) -> Int { - return all().fetchCount(db, counted) - } - - /// Returns the number of distinct values of *counted*. - @warn_unused_result - public static func fetchCount(db: Database, distinct counted: _SQLExpressionType) -> Int { - return all().fetchCount(db, distinct: counted) - } } diff --git a/README.md b/README.md index 75484fa10..d9714e999 100644 --- a/README.md +++ b/README.md @@ -1367,7 +1367,6 @@ let personWithEmailCount = dbQueue.inDatabase { db in } ``` -You can generally fetch the average, count, min, max and sum of any column. See the [query interface](#the-query-interface) for more details. You can now jump to: @@ -1748,7 +1747,7 @@ Please bear in mind that the query interface can not generate all possible SQL q - [SQL Operators](#sql-operators) - [SQL Functions](#sql-functions) - [Fetching from Requests](#fetching-from-requests) -- [Counting with Requests](#counting-with-requests) +- [Fetching Aggregated Values](#fetching-aggregated-values) ### Requests @@ -2117,31 +2116,33 @@ Country.fetchAll(db, keys: ["FR", "US"])! ``` -### Counting with Requests +### Fetching Aggregated Values **Requests can count:** ```swift dbQueue.inDatabase { db in + // SELECT COUNT(*) FROM "persons" + let count = Person.all().fetchCount(db) // Int - // SELECT COUNT(*) FROM "persons" ... - request.fetchCount(db) // Int - - // SELECT COUNT(name) FROM "persons" ... - request.fetchCount(db, Col.name) // Int - - // SELECT COUNT(DISTINCT name) FROM "persons" ... - request.fetchCount(db, distinct: Col.name) // Int + // SELECT COUNT(*) FROM "persons" WHERE "email" IS NOT NULL + let count = Person.filter(Col.email != nil).fetchCount(db) // Int } ``` -You can also fetch other aggregated values (average, sum, min, max, etc.) For example: +Other aggregated values can also be selected and fetched (see [SQL Functions](#sql-functions)): ```swift dbQueue.inDatabase { db in // SELECT MIN("age") FROM "persons" let request = Person.select(min(Col.age)) let minAge = Int.fetchOne(db, request) // Int? + + // SELECT MIN("height"), MAX("height") FROM "persons" + let request = Person.select(min(Col.height), max(Col.height)) + let row = Row.fetchOne(db, request)! + let minHeight = row.value(atIndex: 0) as Int? + let maxHeight = row.value(atIndex: 1) as Int? } ``` diff --git a/TODO.md b/TODO.md index bceb82800..8fae2df8d 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,6 @@ - [ ] How to have robust single writer/several readers? - [ ] protocol DatabaseEquatable, with default implementation for MutablePersistable - [ ] Query builder - - [ ] Question the utility of request.fetchCount(db, Col.name) and request.fetchCount(db, distinct: Col.name) - [ ] Check for gross pollution (`1.SQLexpression`) - [ ] SELECT readers.*, books.* FROM ... JOIN ... - [ ] date functions diff --git a/Tests/Public/FetchRequest/FetchRequestTests.swift b/Tests/Public/FetchRequest/FetchRequestTests.swift index 58738902f..da716481b 100644 --- a/Tests/Public/FetchRequest/FetchRequestTests.swift +++ b/Tests/Public/FetchRequest/FetchRequestTests.swift @@ -101,18 +101,6 @@ class FetchRequestTests: GRDBTestCase { XCTAssertEqual(tableRequest.filter(Col.age == 42).fetchCount(db), 3) XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(*) FROM \"readers\" WHERE (\"age\" = 42)") - - XCTAssertEqual(tableRequest.fetchCount(db, Col.age), 4) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(\"age\") FROM \"readers\"") - - XCTAssertEqual(tableRequest.fetchCount(db, Col.age ?? 0), 5) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(IFNULL(\"age\", 0)) FROM \"readers\"") - - XCTAssertEqual(tableRequest.fetchCount(db, distinct: Col.age), 2) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"age\") FROM \"readers\"") - - XCTAssertEqual(tableRequest.fetchCount(db, distinct: Col.age / Col.age), 1) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT (\"age\" / \"age\")) FROM \"readers\"") } } } diff --git a/Tests/Public/FetchRequest/TableMapping+FetchRequestTests.swift b/Tests/Public/FetchRequest/TableMapping+FetchRequestTests.swift index 825fe3ba8..455a4bf26 100644 --- a/Tests/Public/FetchRequest/TableMapping+FetchRequestTests.swift +++ b/Tests/Public/FetchRequest/TableMapping+FetchRequestTests.swift @@ -56,18 +56,6 @@ class TableMappingFetchRequestTests: GRDBTestCase { XCTAssertEqual(Reader.filter(Col.age == 42).fetchCount(db), 3) XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(*) FROM \"readers\" WHERE (\"age\" = 42)") - - XCTAssertEqual(Reader.fetchCount(db, Col.age), 4) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(\"age\") FROM \"readers\"") - - XCTAssertEqual(Reader.fetchCount(db, Col.age ?? 0), 5) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(IFNULL(\"age\", 0)) FROM \"readers\"") - - XCTAssertEqual(Reader.fetchCount(db, distinct: Col.age), 2) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"age\") FROM \"readers\"") - - XCTAssertEqual(Reader.fetchCount(db, distinct: Col.age / Col.age), 1) - XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT (\"age\" / \"age\")) FROM \"readers\"") } } }