From 42ebffc4fceb44a3523d65377890c04d202f01ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 1 Mar 2017 07:56:17 +0100 Subject: [PATCH] Keep Request.fetchCount() simple (https://github.com/groue/GRDB.swift/issues/176#issuecomment-282783884) --- CHANGELOG.md | 11 ++-------- GRDB/Core/Request.swift | 21 ++++++------------- .../QueryInterfaceRequest.swift | 7 +++---- .../QueryInterfaceSelectQueryDefinition.swift | 4 ++-- 4 files changed, 13 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b93c2d12f..b5e836b32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 - } - ``` + 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 diff --git a/GRDB/Core/Request.swift b/GRDB/Core/Request.swift index 9264cfea4..dd960abda 100644 --- a/GRDB/Core/Request.swift +++ b/GRDB/Core/Request.swift @@ -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 + 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 { - 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)! } } diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 0f30a5c7e..4986243eb 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -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 { - return try query.countRequest(db) + public func fetchCount(_ db: Database) throws -> Int { + return try query.fetchCount(db) } } diff --git a/GRDB/QueryInterface/QueryInterfaceSelectQueryDefinition.swift b/GRDB/QueryInterface/QueryInterfaceSelectQueryDefinition.swift index 8a63432c7..7c520d49e 100644 --- a/GRDB/QueryInterface/QueryInterfaceSelectQueryDefinition.swift +++ b/GRDB/QueryInterface/QueryInterfaceSelectQueryDefinition.swift @@ -91,8 +91,8 @@ struct QueryInterfaceSelectQueryDefinition { } /// Part of Request protocol - func countRequest(_ db: Database) throws -> AnyTypedRequest { - return countQuery.bound(to: Int.self) + func fetchCount(_ db: Database) throws -> Int { + return try Int.fetchOne(db, countQuery)! } private var countQuery: QueryInterfaceSelectQueryDefinition {