From 9b4e55be287a67e29883a3d46510df5059011525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 1 Mar 2017 08:24:43 +0100 Subject: [PATCH] Documentation for Request.fetchCount() (https://github.com/groue/GRDB.swift/issues/176#issuecomment-282783884) --- GRDB/Core/Request.swift | 4 +-- .../QueryInterfaceRequest.swift | 2 +- README.md | 33 ++++++++++++------- TODO.md | 4 +-- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/GRDB/Core/Request.swift b/GRDB/Core/Request.swift index dd960abda..afae642a9 100644 --- a/GRDB/Core/Request.swift +++ b/GRDB/Core/Request.swift @@ -13,7 +13,7 @@ public protocol Request { /// executed, and an eventual row adapter. func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) - /// The number of rows matched by the request. + /// The number of rows fetched by the request. /// /// Default implementation builds a naive SQL query based on the statement /// returned by the `prepare` method: `SELECT COUNT(*) FROM (...)`. @@ -26,7 +26,7 @@ public protocol Request { } extension Request { - /// The number of rows matched by the request. + /// The number of rows fetched by the request. /// /// This default implementation builds a naive SQL query based on the /// statement returned by the `prepare` method: `SELECT COUNT(*) FROM (...)`. diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 4986243eb..759544029 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -234,7 +234,7 @@ extension QueryInterfaceRequest { // MARK: Counting - /// The number of rows matched by the request. + /// The number of rows fetched by the request. /// /// - parameter db: A database connection. public func fetchCount(_ db: Database) throws -> Int { diff --git a/README.md b/README.md index 403c2d53a..8ae40fec9 100644 --- a/README.md +++ b/README.md @@ -3242,17 +3242,17 @@ try request.deleteAll() ```swift // Custom SQL is always welcome -try Person.fetchAll(db, "SELECT ...") // [Person] +try Person.fetchAll(db, "SELECT ...") // [Person] ``` But you may prefer to bring some elegance back in, and build custom requests on top of the `Request` and `TypedRequest` protocols: ```swift // No custom SQL in sight -try Person.someCustomRequest().fetchAll(db) // [Person] +try Person.customRequest().fetchAll(db) // [Person] ``` -Unlike QueryInterfaceRequest, these protocols can't count or delete. But they can fetch: +Unlike QueryInterfaceRequest, these protocols can't delete. But they can fetch and count: ```swift /// The protocol for all types that define a way to fetch values from @@ -3261,6 +3261,9 @@ 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?) + + /// The number of rows fetched by the request. + func fetchCount(_ db: Database) throws -> Int } /// The protocol for typed fetch requests. @@ -3270,7 +3273,12 @@ protocol TypedRequest : Request { } ``` -The `prepare(_:)` function returns a tuple made of a [prepared statement](#prepared-statements) and an optional [row adapter](#row-adapters). The prepared statement tells which SQL query should be executed. The row adapter can help *presenting* the fetched rows in the way expected by the row consumers (we'll see an example below). +The `prepare` method returns a tuple made of a [prepared statement](#prepared-statements) and an optional [row adapter](#row-adapters). The prepared statement tells which SQL query should be executed. The row adapter can help *presenting* the fetched rows in the way expected by the row consumers (we'll see an example below). + +The `fetchCount` method has a default implementation that builds a correct but naive SQL query from the statement returned by `prepare`: `SELECT COUNT(*) FROM (...)`. Adopting types can refine the counting SQL by refining their `fetchCount` implementation. + + +### Fetching From Custom Requests A Request doesn't know what to fetch, but it can feed the [fetching methods](#fetching-methods) of any fetchable type ([Row](#fetching-rows), [value](#value-queries), or [record](#records)): @@ -3290,7 +3298,10 @@ try request.fetchAll(db) // [Person] try request.fetchOne(db) // Person? ``` -**To build requests**, you can create your own type that adopts the protocols, derive requests from other requests, or use one of the built-in concrete types: + +### Building Custom Requests + +**To build custom requests**, you can create your own type that adopts the protocols, or derive requests from other requests, or use one of the built-in concrete types: - [Request](http://cocoadocs.org/docsets/GRDB.swift/0.101.1/Protocols/Request.html): the protocol for all requests - [TypedRequest](http://cocoadocs.org/docsets/GRDB.swift/0.101.1/Protocols/TypedRequest.html): the protocol for all typed requests @@ -3301,8 +3312,7 @@ try request.fetchOne(db) // Person? Rebind the fetched type of requests: ```swift -let maxScore = Player // Int? - .select(max(scoreColumn)) +let maxScore = Player.select(max(scoreColumn)) .bound(to: Int.self) .fetchOne(db) ``` @@ -3311,13 +3321,14 @@ Bind custom SQL requests to your record types: ```swift extension Person { - static func someCustomRequest() -> AnyTypedRequest { - let sqlRequest = SQLRequest("SELECT * FROM persons") - return sqlRequest.bound(to: Person.self) + static func customRequest(...) -> AnyTypedRequest { + let request = SQLRequest("SELECT ...", arguments: ...) + return request.bound(to: Person.self) } } -try Person.someCustomRequest().fetchAll(db) // [Person] +try Person.customRequest(...).fetchAll(db) // [Person] +try Person.customRequest(...).fetchCount(db) // Int ``` Use [row adapters](#row-adapters) to ease the consumption of complex rows: diff --git a/TODO.md b/TODO.md index b2337182c..739a9b0a0 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +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] 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 - [X] tests - - [ ] documentation + - [X] 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