diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index c1a7da4a7..828fad783 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -119,8 +119,7 @@ extension TableRecord where Self: EncodableRecord { let destinationRelation = association.sqlAssociation.destinationRelation(fromOriginRows: { db in try [Row(PersistenceContainer(db, self))] }) - let query = SQLSelectQuery(relation: destinationRelation) - return QueryInterfaceRequest(query: query) + return QueryInterfaceRequest(relation: destinationRelation) } } diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 27e80357e..f1f321c64 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -31,6 +31,14 @@ public struct QueryInterfaceRequest { var query: SQLSelectQuery + init(query: SQLSelectQuery) { + self.query = query + } + + init(relation: SQLRelation) { + self.init(query: SQLSelectQuery(relation: relation)) + } + @usableFromInline var needsPrefetch: Bool { return query.relation.needsPrefetch @@ -375,8 +383,7 @@ extension TableRecord { let relation = SQLRelation( source: .table(tableName: databaseTableName, alias: nil), selection: databaseSelection) - let query = SQLSelectQuery(relation: relation) - return QueryInterfaceRequest(query: query) + return QueryInterfaceRequest(relation: relation) } /// Creates a request which fetches no record. diff --git a/GRDB/QueryInterface/Row+QueryInterface.swift b/GRDB/QueryInterface/Row+QueryInterface.swift index e6a83ee00..8e2fdff86 100644 --- a/GRDB/QueryInterface/Row+QueryInterface.swift +++ b/GRDB/QueryInterface/Row+QueryInterface.swift @@ -69,19 +69,30 @@ extension Row { let prefetchedAssociations = query.relation.prefetchedAssociations() for prefetchedAssociation in prefetchedAssociations { - let pivotAlias = TableAlias() - let pivotColumnMapping = try prefetchedAssociation.pivotCondition.columnMapping(db) - let pivotRightColumns = pivotColumnMapping.map { $0.right } + let groupingAlias = TableAlias() + let groupingColumns = try prefetchedAssociation.pivotCondition.columnMapping(db).map { $0.right } let prefetchedRelation = prefetchedAssociation - .mapPivotRelation { $0.qualified(with: pivotAlias) } + .mapPivotRelation { $0.qualified(with: groupingAlias) } .destinationRelation(fromOriginRows: { _ in rows }) - .annotated(with: pivotRightColumns.map { pivotAlias[Column($0)].aliased("grdb_\($0)") }) - let prefetchedQuery = SQLSelectQuery(relation: prefetchedRelation) - let prefetchedRequest = QueryInterfaceRequest(query: prefetchedQuery) + .annotated(with: groupingColumns.map { groupingAlias[Column($0)].aliased("grdb_\($0)") }) + let prefetchedRequest = QueryInterfaceRequest(relation: prefetchedRelation) let prefetchedRows = try prefetchedRequest.fetchAll(db) + let groupedRows = group(prefetchedRows, on: groupingColumns.map { "grdb_\($0)" }) } return rows } + + /// - precondition: Columns all exist in all rows. All rows have the same + /// columnns, in the same order. + private static func group(_ rows: [Row], on columns: [String]) -> [[DatabaseValue]: [Row]] { + guard let firstRow = rows.first else { + return [:] + } + let indexes: [Int] = columns.map { firstRow.index(ofColumn: $0)! } + return Dictionary(grouping: rows, by: { row in + indexes.map { row.impl.databaseValue(atUncheckedIndex: $0) } + }) + } } extension QueryInterfaceRequest where RowDecoder == Row {