From e05a64f1cb14b51fe298583f186ce1750bc06fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 30 Apr 2018 08:11:16 +0200 Subject: [PATCH] Cleanup --- .../Association/Association.swift | 12 ++---- .../Association/AssociationQuery.swift | 15 +++---- .../Association/AssociationRequest.swift | 2 +- GRDB/QueryInterface/QueryInterfaceQuery.swift | 42 +++++++++---------- .../QueryInterfaceRequest+Association.swift | 10 ++--- 5 files changed, 39 insertions(+), 42 deletions(-) diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index d0028c0ab..29f5fb6d8 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -201,32 +201,28 @@ extension Association { /// Creates an association that includes another one. The columns of the /// associated record are selected. The returned association does not /// require that the associated database table contains a matching row. - public func including(optional association: A) -> Self where A.LeftAssociated == RightAssociated - { + public func including(optional association: A) -> Self where A.LeftAssociated == RightAssociated { return mapRequest { $0.chain(.optional, association) } } /// Creates an association that includes another one. The columns of the /// associated record are selected. The returned association requires /// that the associated database table contains a matching row. - public func including(required association: A) -> Self where A.LeftAssociated == RightAssociated - { + public func including(required association: A) -> Self where A.LeftAssociated == RightAssociated { return mapRequest { $0.chain(.required, association) } } /// Creates an association that joins another one. The columns of the /// associated record are not selected. The returned association does not /// require that the associated database table contains a matching row. - public func joining(optional association: A) -> Self where A.LeftAssociated == RightAssociated - { + public func joining(optional association: A) -> Self where A.LeftAssociated == RightAssociated { return mapRequest { $0.chain(.optional, association.select([])) } } /// Creates an association that joins another one. The columns of the /// associated record are not selected. The returned association requires /// that the associated database table contains a matching row. - public func joining(required association: A) -> Self where A.LeftAssociated == RightAssociated - { + public func joining(required association: A) -> Self where A.LeftAssociated == RightAssociated { return mapRequest { $0.chain(.required, association.select([])) } } } diff --git a/GRDB/QueryInterface/Association/AssociationQuery.swift b/GRDB/QueryInterface/Association/AssociationQuery.swift index c89a1b25b..bcbfd1cf1 100644 --- a/GRDB/QueryInterface/Association/AssociationQuery.swift +++ b/GRDB/QueryInterface/Association/AssociationQuery.swift @@ -14,11 +14,12 @@ struct AssociationQuery { if let qualifier = qualifier { qualifiers.append(qualifier) } - qualifiers.append(contentsOf: joins.flatMap { $0.rightQuery.allQualifiers }) - return qualifiers + return joins.reduce(into: qualifiers) { + $0.append(contentsOf: $1.query.allQualifiers) + } } - var preparedQuery: AssociationQuery { + var qualifiedQuery: AssociationQuery { var qualifier = SQLTableQualifier(tableName: source.tableName!) var query = self @@ -28,7 +29,7 @@ struct AssociationQuery { query.ordering = ordering.qualified(with: qualifier) query.joins = joins.map { var join = $0 - join.rightQuery = join.rightQuery.preparedQuery + join.query = join.query.qualifiedQuery return join } return query @@ -36,13 +37,13 @@ struct AssociationQuery { var completeSelection: [SQLSelectable] { return joins.reduce(into: selection) { - $0.append(contentsOf: $1.rightQuery.completeSelection) + $0.append(contentsOf: $1.query.completeSelection) } } var completeOrdering: QueryOrdering { return joins.reduce(ordering) { - $0.appending($1.rightQuery.completeOrdering) + $0.appending($1.query.completeOrdering) } } @@ -54,7 +55,7 @@ struct AssociationQuery { var endIndex = startIndex + selectionWidth var scopes: [String: RowAdapter] = [:] for join in joins { - if let (joinAdapter, joinEndIndex) = try join.rightQuery.rowAdapter(db, fromIndex: endIndex, forKeyPath: keyPath + [join.key]) { + if let (joinAdapter, joinEndIndex) = try join.query.rowAdapter(db, fromIndex: endIndex, forKeyPath: keyPath + [join.key]) { GRDBPrecondition(scopes[join.key] == nil, "The association key \"\((keyPath + [join.key]).joined(separator: "."))\" is ambiguous. Use the Association.forKey(_:) method is order to disambiguate.") scopes[join.key] = joinAdapter endIndex = joinEndIndex diff --git a/GRDB/QueryInterface/Association/AssociationRequest.swift b/GRDB/QueryInterface/Association/AssociationRequest.swift index 0c8d54351..90bd1f37a 100644 --- a/GRDB/QueryInterface/Association/AssociationRequest.swift +++ b/GRDB/QueryInterface/Association/AssociationRequest.swift @@ -48,7 +48,7 @@ extension AssociationRequest { { let join = AssociationJoin( op: chainOp, - rightQuery: association.request.query, + query: association.request.query, key: association.key, associationMapping: association.associationMapping) return AssociationRequest(query: query.joining(join)) diff --git a/GRDB/QueryInterface/QueryInterfaceQuery.swift b/GRDB/QueryInterface/QueryInterfaceQuery.swift index 902db778f..6c47f4f63 100644 --- a/GRDB/QueryInterface/QueryInterfaceQuery.swift +++ b/GRDB/QueryInterface/QueryInterfaceQuery.swift @@ -149,13 +149,13 @@ struct QueryInterfaceQuery { var completeSelection: [SQLSelectable] { return joins.reduce(into: selection) { - $0.append(contentsOf: $1.rightQuery.completeSelection) + $0.append(contentsOf: $1.query.completeSelection) } } var completeOrdering: QueryOrdering { return joins.reduce(ordering) { - $0.appending($1.rightQuery.completeOrdering) + $0.appending($1.query.completeOrdering) } } @@ -165,16 +165,17 @@ struct QueryInterfaceQuery { var allQualifiers: [SQLTableQualifier] { var qualifiers: [SQLTableQualifier] = [] - if let qualifier = source.qualifier { + if let qualifier = qualifier { qualifiers.append(qualifier) } - qualifiers.append(contentsOf: joins.flatMap { $0.rightQuery.allQualifiers }) - return qualifiers + return joins.reduce(into: qualifiers) { + $0.append(contentsOf: $1.query.allQualifiers) + } } } extension QueryInterfaceQuery { - private var preparedQuery: QueryInterfaceQuery { + private var qualifiedQuery: QueryInterfaceQuery { var query = self if !joins.isEmpty { @@ -192,7 +193,7 @@ extension QueryInterfaceQuery { query.joins = query.joins.map { var join = $0 - join.rightQuery = join.rightQuery.preparedQuery + join.query = join.query.qualifiedQuery return join } @@ -200,16 +201,16 @@ extension QueryInterfaceQuery { return query } - /// precondition: self is the result of preparedQuery + /// precondition: self is the result of qualifiedQuery private func makeSelectStatement(_ db: Database) throws -> SelectStatement { var arguments: StatementArguments? = StatementArguments() - let sql = try preparedQuery.sql(db, &arguments) + let sql = try self.sql(db, &arguments) let statement = try db.makeSelectStatement(sql) try statement.setArgumentsWithValidation(arguments!) return statement } - /// precondition: self is the result of preparedQuery + /// precondition: self is the result of qualifiedQuery private func rowAdapter(_ db: Database) throws -> RowAdapter? { if joins.isEmpty { return nil @@ -222,7 +223,7 @@ extension QueryInterfaceQuery { var endIndex = selectionWidth var scopes: [String: RowAdapter] = [:] for join in joins { - if let (joinAdapter, joinEndIndex) = try join.rightQuery.rowAdapter(db, fromIndex: endIndex, forKeyPath: [join.key]) { + if let (joinAdapter, joinEndIndex) = try join.query.rowAdapter(db, fromIndex: endIndex, forKeyPath: [join.key]) { GRDBPrecondition(scopes[join.key] == nil, "The association key \"\(join.key)\" is ambiguous. Use the Association.forKey(_:) method is order to disambiguate.") scopes[join.key] = joinAdapter endIndex = joinEndIndex @@ -238,7 +239,7 @@ extension QueryInterfaceQuery { } func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) { - let query = preparedQuery + let query = qualifiedQuery return try (query.makeSelectStatement(db), query.rowAdapter(db)) } @@ -249,7 +250,7 @@ extension QueryInterfaceQuery { /// The database region that the request looks into. func fetchedRegion(_ db: Database) throws -> DatabaseRegion { - let statement = try preparedQuery.makeSelectStatement(db) + let statement = try qualifiedQuery.makeSelectStatement(db) let region = statement.fetchedRegion // Can we intersect the region with rowIds? @@ -421,7 +422,7 @@ extension QueryInterfaceQuery { struct AssociationJoin { var op: AssociationChainOperator - var rightQuery: AssociationQuery + var query: AssociationQuery var key: String var associationMapping: (Database) throws -> AssociationMapping @@ -440,20 +441,19 @@ struct AssociationJoin { sql += "JOIN" } - let rightQualifier = rightQuery.qualifier! - - sql += try " " + rightQuery.source.sourceSQL(db, &arguments) + sql += try " " + query.source.sourceSQL(db, &arguments) + let qualifier = query.qualifier! let filters = try [ - associationMapping(db)(leftQualifier, rightQualifier), - rightQuery.filterPromise.resolve(db) + associationMapping(db)(leftQualifier, qualifier), + query.filterPromise.resolve(db) ].compactMap { $0 } if !filters.isEmpty { sql += " ON " + filters.joined(operator: .and).expressionSQL(&arguments) } - for join in rightQuery.joins { - sql += try " " + join.joinSQL(db, &arguments, leftQualifier: rightQualifier, isRequiredAllowed: isRequiredAllowed) + for join in query.joins { + sql += try " " + join.joinSQL(db, &arguments, leftQualifier: qualifier, isRequiredAllowed: isRequiredAllowed) } return sql diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index 296d0bb19..309da524b 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -5,7 +5,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { { let join = AssociationJoin( op: chainOp, - rightQuery: association.request.query, + query: association.request.query, key: association.key, associationMapping: association.associationMapping) return QueryInterfaceRequest(query: query.joining(join)) @@ -56,16 +56,16 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { extension Association where LeftAssociated: MutablePersistableRecord { func request(from record: LeftAssociated) -> QueryInterfaceRequest { - var query = request.query.preparedQuery // make sure query has a qualifier - let qualifier = query.source.qualifier! + var query = request.query.qualifiedQuery // make sure query has a qualifier + let qualifier = query.qualifier! let recordQualifier = SQLTableQualifier.init(tableName: LeftAssociated.databaseTableName) - + query = query.filter { db in let associationMapping = try self.associationMapping(db) guard let filter = associationMapping(recordQualifier, qualifier) else { fatalError("Can't request from record without association mapping") } - let container = PersistenceContainer(record) + let container = PersistenceContainer(record) // support for record classes: late construction of container return filter.resolvedExpression(inContext: [recordQualifier: container]) }