Remove request.fetchCount(db, expr) and request.fetchCount(db, distinct: expr)

This commit is contained in:
Gwendal Roué committed 2016-01-29 13:23:55 +01:00
1 parent cc0f76d75b
commit f08fc31e64
5 files changed
+14 -67

No files matched your search

+1 -30
View File
@@ -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<T: DatabaseValueConvertible>(db: Database, _ selection: _SQLSelectable) -> T? {
return T.fetchOne(db, FetchRequest<Void>(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)
}
}
+13 -12
View File
@@ -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?
}
```
-1
View File
@@ -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
@@ -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\"")
}
}
}
@@ -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\"")
}
}
}