diff --git a/CHANGELOG.md b/CHANGELOG.md index 1120ec52d..9c35c3d0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,19 @@ Release Notes - [#422](https://github.com/groue/GRDB.swift/pull/422): Refining Association Requests +### API diff + +```diff + protocol AggregatingRequest { +- func group(_ expressions: [SQLExpressible]) -> Self ++ func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> Self + } + ++extension TableRequest where Self: AggregatingRequest { ++ func groupByPrimaryKey() -> Self { ++} +``` + ## 3.3.1 Released September 23, 2018 • [diff](https://github.com/groue/GRDB.swift/compare/v3.3.0...v3.3.1) diff --git a/GRDB/QueryInterface/Association/AssociationQuery.swift b/GRDB/QueryInterface/Association/AssociationQuery.swift index 5565c7e18..90e696438 100644 --- a/GRDB/QueryInterface/Association/AssociationQuery.swift +++ b/GRDB/QueryInterface/Association/AssociationQuery.swift @@ -13,7 +13,7 @@ struct AssociationQuery { extension AssociationQuery { init(_ query: QueryInterfaceQuery) { GRDBPrecondition(!query.isDistinct, "Not implemented: join distinct queries") - GRDBPrecondition(query.groupByExpressions.isEmpty, "Can't join aggregated queries") + GRDBPrecondition(query.groupPromise == nil, "Can't join aggregated queries") GRDBPrecondition(query.havingExpression == nil, "Can't join aggregated queries") GRDBPrecondition(query.limit == nil, "Can't join limited queries") diff --git a/GRDB/QueryInterface/QueryInterfaceQuery.swift b/GRDB/QueryInterface/QueryInterfaceQuery.swift index d3e90c28e..eb4de6cbe 100644 --- a/GRDB/QueryInterface/QueryInterfaceQuery.swift +++ b/GRDB/QueryInterface/QueryInterfaceQuery.swift @@ -21,7 +21,7 @@ struct QueryInterfaceQuery { var selection: [SQLSelectable] var isDistinct: Bool var filterPromise: DatabasePromise - var groupByExpressions: [SQLExpression] + var groupPromise: DatabasePromise<[SQLExpression]>? var ordering: QueryOrdering var havingExpression: SQLExpression? var limit: SQLLimit? @@ -32,7 +32,7 @@ struct QueryInterfaceQuery { selection: [SQLSelectable] = [], isDistinct: Bool = false, filterPromise: DatabasePromise = DatabasePromise(value: nil), - groupByExpressions: [SQLExpression] = [], + groupPromise: DatabasePromise<[SQLExpression]>? = nil, ordering: QueryOrdering = QueryOrdering(), havingExpression: SQLExpression? = nil, limit: SQLLimit? = nil, @@ -42,7 +42,7 @@ struct QueryInterfaceQuery { self.selection = selection self.isDistinct = isDistinct self.filterPromise = filterPromise - self.groupByExpressions = groupByExpressions + self.groupPromise = groupPromise self.ordering = ordering self.havingExpression = havingExpression self.limit = limit @@ -88,9 +88,9 @@ extension QueryInterfaceQuery { return query } - func group(_ expressions: [SQLExpressible]) -> QueryInterfaceQuery { + func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> QueryInterfaceQuery { var query = self - query.groupByExpressions = expressions.map { $0.sqlExpression } + query.groupPromise = DatabasePromise { db in try expressions(db).map { $0.sqlExpression } } return query } @@ -160,7 +160,7 @@ extension QueryInterfaceQuery { query.source = source.qualified(with: alias) query.selection = query.selection.map { $0.qualifiedSelectable(with: alias) } query.filterPromise = query.filterPromise.map { [alias] (_, expr) in expr?.qualifiedExpression(with: alias) } - query.groupByExpressions = query.groupByExpressions.map { $0.qualifiedExpression(with: alias) } + query.groupPromise = query.groupPromise?.map { [alias] (_, exprs) in exprs.map { $0.qualifiedExpression(with: alias) } } query.ordering = query.ordering.qualified(with: alias) query.havingExpression = query.havingExpression?.qualifiedExpression(with: alias) @@ -245,9 +245,9 @@ extension QueryInterfaceQuery { sql += " WHERE " + filter.expressionSQL(&context) } - if !groupByExpressions.isEmpty { + if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty { sql += " GROUP BY " - sql += groupByExpressions.map { $0.expressionSQL(&context) } + sql += groupExpressions.map { $0.expressionSQL(&context) } .joined(separator: ", ") } @@ -278,7 +278,7 @@ extension QueryInterfaceQuery { /// precondition: self is the result of finalizedQuery func makeDeleteStatement(_ db: Database) throws -> UpdateStatement { - guard groupByExpressions.isEmpty else { + if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty { // Programmer error fatalError("Can't delete query with GROUP BY clause") } @@ -370,7 +370,7 @@ extension QueryInterfaceQuery { } private var countQuery: QueryInterfaceQuery { - guard groupByExpressions.isEmpty && limit == nil else { + guard groupPromise == nil && limit == nil else { // SELECT ... GROUP BY ... // SELECT ... LIMIT ... return trivialCountQuery diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 11088d4f8..188f44b08 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -122,8 +122,8 @@ extension QueryInterfaceRequest : DerivableRequest, AggregatingRequest { return QueryInterfaceRequest(query: query.filter(predicate)) } - /// Creates a request grouped according to *expressions*. - public func group(_ expressions: [SQLExpressible]) -> QueryInterfaceRequest { + /// Creates a request grouped according to *expressions promise*. + public func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> QueryInterfaceRequest { return QueryInterfaceRequest(query: query.group(expressions)) } diff --git a/GRDB/QueryInterface/RequestProtocols.swift b/GRDB/QueryInterface/RequestProtocols.swift index ec5d46341..3f7480db5 100644 --- a/GRDB/QueryInterface/RequestProtocols.swift +++ b/GRDB/QueryInterface/RequestProtocols.swift @@ -1,5 +1,7 @@ // MARK: - SelectionRequest +/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) +/// /// The protocol for all requests that can refine their selection. /// /// :nodoc: @@ -56,6 +58,8 @@ extension SelectionRequest { // MARK: - FilteredRequest +/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) +/// /// The protocol for all requests that can be filtered. /// /// :nodoc: @@ -103,6 +107,8 @@ extension FilteredRequest { // MARK: - TableRequest { +/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) +/// /// The protocol for all requests that feed from a database table /// /// :nodoc: @@ -222,14 +228,27 @@ extension TableRequest where Self: OrderedRequest { } } +/// :nodoc: +extension TableRequest where Self: AggregatingRequest { + /// Creates a request grouped by primary key. + public func groupByPrimaryKey() -> Self { + let tableName = self.databaseTableName + return group { db in + try db.primaryKey(tableName).columns.map { Column($0) } + } + } +} + // MARK: - AggregatingRequest +/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) +/// /// The protocol for all requests that can aggregate. /// /// :nodoc: public protocol AggregatingRequest { - /// Creates a request grouped according to *expressions*. - func group(_ expressions: [SQLExpressible]) -> Self + /// Creates a request grouped according to *expressions promise*. + func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> Self /// Creates a request with the provided *predicate* added to the /// eventual set of already applied predicates. @@ -238,6 +257,11 @@ public protocol AggregatingRequest { /// :nodoc: extension AggregatingRequest { + /// Creates a request grouped according to *expressions*. + public func group(_ expressions: [SQLExpressible]) -> Self { + return group { _ in expressions } + } + /// Creates a request grouped according to *expressions*. public func group(_ expressions: SQLExpressible...) -> Self { return group(expressions) @@ -267,6 +291,8 @@ extension AggregatingRequest { // MARK: - OrderedRequest +/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) +/// /// The protocol for all requests that be ordered. /// /// :nodoc: