From 8e2cdeebc3eadf6e6971c1ee992287b05b30a528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 1 May 2018 15:03:36 +0200 Subject: [PATCH] Cleanup --- .../Association/Association.swift | 21 +++++++++---------- GRDB/QueryInterface/QueryInterfaceQuery.swift | 20 ++++++++---------- .../QueryInterfaceRequest.swift | 4 ++++ 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 6edc0fd32..bd94d6551 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -280,19 +280,18 @@ extension Association where LeftAssociated: MutablePersistableRecord { /// let team: Team = ... /// let players = try team.players.fetchAll(db) // [Player] func request(from record: LeftAssociated) -> QueryInterfaceRequest { + // Turn `JOIN associated ON associated.recordId = record.id` + // into `FROM associated WHERE associated.recordId = 1` let associationAlias = TableAlias() - let query = request.query.qualified(with: associationAlias) let recordAlias = TableAlias(tableName: LeftAssociated.databaseTableName) - - // Turn `associated.recordId = record.id` into `associated.recordId = 1` - let resolvedQuery = query.filter { db in - guard let joinCondition = try self.joinCondition(db)(recordAlias, associationAlias) else { - fatalError("Can't request from record without join condition") - } - let container = PersistenceContainer(record) // support for record classes: late construction of container - return joinCondition.resolvedExpression(inContext: [recordAlias: container]) + return QueryInterfaceRequest(request) + .aliased(associationAlias) + .filter { db in + guard let joinCondition = try self.joinCondition(db)(recordAlias, associationAlias) else { + fatalError("Can't request from record without join condition") + } + let container = PersistenceContainer(record) // support for record classes: late construction of container + return joinCondition.resolvedExpression(inContext: [recordAlias: container]) } - - return QueryInterfaceRequest(query: QueryInterfaceQuery(resolvedQuery)) } } diff --git a/GRDB/QueryInterface/QueryInterfaceQuery.swift b/GRDB/QueryInterface/QueryInterfaceQuery.swift index 4396a12b4..fb73ba727 100644 --- a/GRDB/QueryInterface/QueryInterfaceQuery.swift +++ b/GRDB/QueryInterface/QueryInterfaceQuery.swift @@ -49,6 +49,15 @@ struct QueryInterfaceQuery { self.joins = joins } + init(_ query: AssociationQuery) { + self.init( + source: query.source, + selection: query.selection, + filterPromise: query.filterPromise, + ordering: query.ordering, + joins: query.joins) + } + func sql(_ db: Database, _ context: inout SQLGenerationContext) throws -> String { var sql = "SELECT" @@ -393,17 +402,6 @@ extension QueryInterfaceQuery { } } -extension QueryInterfaceQuery { - init(_ query: AssociationQuery) { - self.init( - source: query.source, - selection: query.selection, - filterPromise: query.filterPromise, - ordering: query.ordering, - joins: query.joins) - } -} - // MARK: - AssociationJoin /// Not to be mismatched with SQL join operators (inner join, left join). diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index f1edafcab..e7646ef13 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -7,6 +7,10 @@ public struct QueryInterfaceRequest { init(query: QueryInterfaceQuery) { self.query = query } + + init(_ request: AssociationRequest) { + self.query = QueryInterfaceQuery(request.query) + } } extension QueryInterfaceRequest : FetchRequest {