diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ed8a8f4c..b93c2d12f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 + } + ``` + + ## 0.101.1 Released January 20, 2017 diff --git a/GRDB/Core/Request.swift b/GRDB/Core/Request.swift index 6ea20ddaf..9264cfea4 100644 --- a/GRDB/Core/Request.swift +++ b/GRDB/Core/Request.swift @@ -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 +} + +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 { + 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 { diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 6cbed55e4..0f30a5c7e 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -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 { + return try query.countRequest(db) } } diff --git a/GRDB/QueryInterface/QueryInterfaceSelectQueryDefinition.swift b/GRDB/QueryInterface/QueryInterfaceSelectQueryDefinition.swift index 45f8be2df..8a63432c7 100644 --- a/GRDB/QueryInterface/QueryInterfaceSelectQueryDefinition.swift +++ b/GRDB/QueryInterface/QueryInterfaceSelectQueryDefinition.swift @@ -90,8 +90,8 @@ struct QueryInterfaceSelectQueryDefinition { return sql } - /// Returns a fetch request that counts the number of rows matched by self. - var countRequest: AnyTypedRequest { + /// Part of Request protocol + func countRequest(_ db: Database) throws -> AnyTypedRequest { return countQuery.bound(to: Int.self) } diff --git a/TODO.md b/TODO.md index 9b8408aad..44906087b 100644 --- a/TODO.md +++ b/TODO.md @@ -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