This commit is contained in:
Gwendal Roué committed 2017-03-01 05:32:52 +01:00
1 parent fa464b489d
commit 2d2c596f75
5 files changed
+64 -5

No files matched your search

+21
View File
@@ -1,6 +1,27 @@
Release Notes
=============
## Next version
**New**
- The Request and TypedRequest protocols for [custom requests](https://github.com/groue/GRDB.swift#custom-requests) can now count:
```swift
let request: Request = ...
let count = try request.fetchCount(db) // Int
```
Default implementation performs a naive counting: `SELECT COUNT(*) FROM (...)`. Adopting types can refine the counting SQL by implementing the `countRequest` method:
```swift
protocol Request {
// customization point
func countRequest(_ db: Database) throws -> AnyTypedRequest<Int>
}
```
## 0.101.1
Released January 20, 2017
+34
View File
@@ -12,6 +12,40 @@ public protocol Request {
/// A tuple that contains a prepared statement that is ready to be
/// executed, and an eventual row adapter.
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?)
/// Returns a request that selects the number of rows. Don't call it
/// directly: instead, use the `fetchCount` method.
///
/// Default implementation builds a naive SQL query based on the statement
/// returned by the `prepare` method: `SELECT COUNT(*) FROM (...)`.
///
/// Adopting types can refine this countRequest method and return more
/// efficient SQL.
///
/// - parameter db: A database connection.
func countRequest(_ db: Database) throws -> AnyTypedRequest<Int>
}
extension Request {
/// Returns a request that selects the number of rows. Don't call it
/// directly: instead, use the `fetchCount` method.
///
/// This default implementation builds a naive SQL query based on the
/// statement returned by the `prepare` method: `SELECT COUNT(*) FROM (...)`.
///
/// - parameter db: A database connection.
public func countRequest(_ db: Database) throws -> AnyTypedRequest<Int> {
let (statement, _) = try prepare(db)
let sql = statement.sql
return SQLRequest("SELECT COUNT(*) FROM (\(sql))", arguments: statement.arguments).bound(to: Int.self)
}
/// The number of rows matched by the request.
///
/// - parameter db: A database connection.
public func fetchCount(_ db: Database) throws -> Int {
return try countRequest(db).fetchOne(db)!
}
}
extension Request {
@@ -234,11 +234,12 @@ extension QueryInterfaceRequest {
// MARK: Counting
/// The number of rows matched by the request.
/// Returns a request that selects the number of rows. Don't call it
/// directly: instead, use the `fetchCount` method.
///
/// - parameter db: A database connection.
public func fetchCount(_ db: Database) throws -> Int {
return try query.countRequest.fetchOne(db)!
public func countRequest(_ db: Database) throws -> AnyTypedRequest<Int> {
return try query.countRequest(db)
}
}
@@ -90,8 +90,8 @@ struct QueryInterfaceSelectQueryDefinition {
return sql
}
/// Returns a fetch request that counts the number of rows matched by self.
var countRequest: AnyTypedRequest<Int> {
/// Part of Request protocol
func countRequest(_ db: Database) throws -> AnyTypedRequest<Int> {
return countQuery.bound(to: Int.self)
}
+3
View File
@@ -1,6 +1,9 @@
- [ ] registerMigrationWithDisabledForeignKeyChecks should be renamed registerMigrationWithDeferredForeignKeyChecks
- [ ] Enable extended result codes (https://github.com/groue/GRDB.swift/issues/171)
- [ ] Request.fetchCount() (see https://github.com/groue/GRDB.swift/issues/176#issuecomment-282783884). This method should be a customization point, not an extension.
- [X] implementation
- [ ] tests
- [ ] documentation
- [ ] Check that https://github.com/groue/GRDB.swift/issues/172#issuecomment-282511719 is true (manual deferred foreign key check)
- [ ] Document how to query external content tables (https://github.com/groue/GRDB.swift/issues/178)
- [ ] SQLiteLib 3.17.0