This commit is contained in:
Gwendal Roué committed 2017-03-01 07:56:17 +01:00
1 parent 2d2c596f75
commit 42ebffc4fc
4 files changed
+13 -30

No files matched your search

+2 -9
View File
@@ -5,21 +5,14 @@ Release Notes
**New**
- The Request and TypedRequest protocols for [custom requests](https://github.com/groue/GRDB.swift#custom-requests) can now count:
- The Request protocol for [custom requests](https://github.com/groue/GRDB.swift#custom-requests) learned how to 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>
}
```
Default implementation performs a naive counting based on the request SQL: `SELECT COUNT(*) FROM (...)`. Adopting types can refine the counting SQL by refining their `fetchCount` implementation.
## 0.101.1
+6 -15
View File
@@ -13,8 +13,7 @@ public protocol Request {
/// 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.
/// The number of rows matched by the request.
///
/// Default implementation builds a naive SQL query based on the statement
/// returned by the `prepare` method: `SELECT COUNT(*) FROM (...)`.
@@ -23,28 +22,20 @@ public protocol Request {
/// efficient SQL.
///
/// - parameter db: A database connection.
func countRequest(_ db: Database) throws -> AnyTypedRequest<Int>
func fetchCount(_ db: Database) throws -> Int
}
extension Request {
/// Returns a request that selects the number of rows. Don't call it
/// directly: instead, use the `fetchCount` method.
/// The number of rows matched by the request.
///
/// 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)!
let (statement, _) = try prepare(db)
let sql = "SELECT COUNT(*) FROM (\(statement.sql))"
return try Int.fetchOne(db, sql, arguments: statement.arguments)!
}
}
@@ -234,12 +234,11 @@ extension QueryInterfaceRequest {
// MARK: Counting
/// Returns a request that selects the number of rows. Don't call it
/// directly: instead, use the `fetchCount` method.
/// The number of rows matched by the request.
///
/// - parameter db: A database connection.
public func countRequest(_ db: Database) throws -> AnyTypedRequest<Int> {
return try query.countRequest(db)
public func fetchCount(_ db: Database) throws -> Int {
return try query.fetchCount(db)
}
}
@@ -91,8 +91,8 @@ struct QueryInterfaceSelectQueryDefinition {
}
/// Part of Request protocol
func countRequest(_ db: Database) throws -> AnyTypedRequest<Int> {
return countQuery.bound(to: Int.self)
func fetchCount(_ db: Database) throws -> Int {
return try Int.fetchOne(db, countQuery)!
}
private var countQuery: QueryInterfaceSelectQueryDefinition {