diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 2be63da26..57e49d7a8 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -211,38 +211,29 @@ extension Association { } } -///// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) -///// :nodoc: -//public struct JoinCondition { -// var columnMapping: [(left: Column, right: Column)] -// -// func sqlExpression(leftAlias: TableAlias, rightAlias: TableAlias) -> SQLExpression? { -// return columnMapping -// .map { $0.right.qualifiedExpression(with: rightAlias) == $0.left.qualifiedExpression(with: leftAlias) } -// .joined(operator: .and) -// } -//} - -///// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) -///// :nodoc: -//public typealias JoinCondition = (_ leftAlias: TableAlias, _ rightAlias: TableAlias) -> SQLExpression? - -/// Turns a ForeignKeyRequest into a JoinCondition +/// The condition that links two joined tables. +/// +/// We only support one kind of join condition, today: foreign keys. +/// +/// SELECT ... +/// FROM book +/// JOIN author ON author.id = book.authorId +/// <--the join condition---> +/// +/// When we eventually add support for new ways to join tables, JoinCondition +/// is the type we'll need to update. +/// +/// The Equatable conformance is used when we merge associations. Two +/// associations can be merged if and only if their join conditions +/// are equal: +/// +/// let request = Book +/// .include(required: Book.author) +/// .include(required: Book.author) public struct JoinCondition: Equatable { var foreignKeyRequest: ForeignKeyRequest var originIsLeft: Bool -// func fetch(_ db: Database) throws -> JoinCondition { -// let foreignKeyMapping = try foreignKeyRequest.fetch(db).mapping -// let columnMapping: [(left: Column, right: Column)] -// if originIsLeft { -// columnMapping = foreignKeyMapping.map { (left: Column($0.origin), right: Column($0.destination)) } -// } else { -// columnMapping = foreignKeyMapping.map { (left: Column($0.destination), right: Column($0.origin)) } -// } -// return JoinCondition(columnMapping: columnMapping) -// } - func sqlExpression(_ db: Database, leftAlias: TableAlias, rightAlias: TableAlias) throws -> SQLExpression? { let foreignKeyMapping = try foreignKeyRequest.fetch(db).mapping let columnMapping: [(left: Column, right: Column)] diff --git a/GRDB/QueryInterface/Association/AssociationQuery.swift b/GRDB/QueryInterface/Association/AssociationQuery.swift index fe72d63d3..5e028d2d9 100644 --- a/GRDB/QueryInterface/Association/AssociationQuery.swift +++ b/GRDB/QueryInterface/Association/AssociationQuery.swift @@ -61,12 +61,11 @@ extension AssociationQuery { func appendingJoin(_ join: AssociationJoin, forKey key: String) -> AssociationQuery { var query = self - if let existingJoin = query.joins[key] { + if let existingJoin = query.joins.removeValue(forKey: key) { guard let mergedJoin = existingJoin.merged(with: join) else { // can't merge fatalError("The association key \"\(key)\" is ambiguous. Use the Association.forKey(_:) method is order to disambiguate.") } - query.joins.removeValue(forKey: key) query.joins.append(value: mergedJoin, forKey: key) } else { query.joins.append(value: join, forKey: key) diff --git a/GRDB/QueryInterface/QueryInterfaceQuery.swift b/GRDB/QueryInterface/QueryInterfaceQuery.swift index 0b5735b5c..d3e90c28e 100644 --- a/GRDB/QueryInterface/QueryInterfaceQuery.swift +++ b/GRDB/QueryInterface/QueryInterfaceQuery.swift @@ -132,12 +132,11 @@ extension QueryInterfaceQuery { func appendingJoin(_ join: AssociationJoin, forKey key: String) -> QueryInterfaceQuery { var query = self - if let existingJoin = query.joins[key] { + if let existingJoin = query.joins.removeValue(forKey: key) { guard let mergedJoin = existingJoin.merged(with: join) else { // can't merge fatalError("The association key \"\(key)\" is ambiguous. Use the Association.forKey(_:) method is order to disambiguate.") } - query.joins.removeValue(forKey: key) query.joins.append(value: mergedJoin, forKey: key) } else { query.joins.append(value: join, forKey: key) diff --git a/GRDB/Utils/OrderedDictionary.swift b/GRDB/Utils/OrderedDictionary.swift index 249a50034..bf00bb3d5 100644 --- a/GRDB/Utils/OrderedDictionary.swift +++ b/GRDB/Utils/OrderedDictionary.swift @@ -50,11 +50,11 @@ extension OrderedDictionary: Collection { typealias Index = Int var startIndex: Int { - return keys.startIndex + return 0 } var endIndex: Int { - return keys.endIndex + return keys.count } func index(after i: Int) -> Int {