This commit is contained in:
Gwendal Roué committed 2019-03-15 08:51:02 +01:00
1 parent 784ebcc8e9
commit 5fbf56c373
4 files changed
+21 -77

No files matched your search

@@ -246,28 +246,28 @@ extension Association {
/// associated record are selected. The returned association does not
/// require that the associated database table contains a matching row.
public func including<A: Association>(optional association: A) -> Self where A.OriginRowDecoder == RowDecoder {
return mapRelation { association.sqlAssociation.relation(from: $0, joinKind: .optional) }
return mapRelation { association.sqlAssociation.relation(from: $0, required: false) }
}
/// 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<A: Association>(required association: A) -> Self where A.OriginRowDecoder == RowDecoder {
return mapRelation { association.sqlAssociation.relation(from: $0, joinKind: .required) }
return mapRelation { association.sqlAssociation.relation(from: $0, required: true) }
}
/// 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<A: Association>(optional association: A) -> Self where A.OriginRowDecoder == RowDecoder {
return mapRelation { association.select([]).sqlAssociation.relation(from: $0, joinKind: .optional) }
return mapRelation { association.select([]).sqlAssociation.relation(from: $0, required: false) }
}
/// 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<A: Association>(required association: A) -> Self where A.OriginRowDecoder == RowDecoder {
return mapRelation { association.select([]).sqlAssociation.relation(from: $0, joinKind: .required) }
return mapRelation { association.select([]).sqlAssociation.relation(from: $0, required: true) }
}
}
@@ -440,9 +440,9 @@ public /* TODO: internal */ struct SQLAssociation {
}
/// Support for joining methods joining(optional:), etc.
func relation(from origin: SQLRelation, joinKind: SQLJoin.Kind) -> SQLRelation {
func relation(from origin: SQLRelation, required: Bool) -> SQLRelation {
let headJoin = SQLJoin(
kind: joinKind,
isRequired: required,
condition: head.condition,
relation: head.relation)
@@ -466,7 +466,7 @@ public /* TODO: internal */ struct SQLAssociation {
let reducedHead = Element(key: next.key, condition: next.condition, relation: nextRelation)
let reducedTail = Array(tail.dropFirst())
let reducedAssociation = SQLAssociation(head: reducedHead, tail: reducedTail)
return reducedAssociation.relation(from: origin, joinKind: joinKind)
return reducedAssociation.relation(from: origin, required: required)
}
/// Support for (TableRecord & EncodableRecord).request(for:).
@@ -510,6 +510,6 @@ public /* TODO: internal */ struct SQLAssociation {
reversedHead.relation = pivotRelation.select([])
let reversedTail = Array(reversedElements.dropFirst())
let reversedAssociation = SQLAssociation(head: reversedHead, tail: reversedTail)
return reversedAssociation.relation(from: head.relation, joinKind: .required)
return reversedAssociation.relation(from: head.relation, required: true)
}
}
@@ -7,7 +7,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
public func including<A: Association>(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder {
return mapQuery {
$0.mapRelation {
association.sqlAssociation.relation(from: $0, joinKind: .optional)
association.sqlAssociation.relation(from: $0, required: false)
}
}
}
@@ -18,7 +18,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
public func including<A: Association>(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder {
return mapQuery {
$0.mapRelation {
association.sqlAssociation.relation(from: $0, joinKind: .required)
association.sqlAssociation.relation(from: $0, required: true)
}
}
}
@@ -29,7 +29,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
public func joining<A: Association>(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder {
return mapQuery {
$0.mapRelation {
association.select([]).sqlAssociation.relation(from: $0, joinKind: .optional)
association.select([]).sqlAssociation.relation(from: $0, required: false)
}
}
}
@@ -40,7 +40,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
public func joining<A: Association>(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder {
return mapQuery {
$0.mapRelation {
association.select([]).sqlAssociation.relation(from: $0, joinKind: .required)
association.select([]).sqlAssociation.relation(from: $0, required: true)
}
}
}
+2 -58
View File
@@ -201,54 +201,6 @@ extension SQLRelation {
// MARK: - SQLJoin
struct SQLJoin {
/// Not to be mismatched with SQL join operators (inner join, left join).
///
/// SQLJoin.Kind is designed to be hierarchically nested, unlike
/// SQL join operators.
///
/// Consider the following request for (A, B, C) tuples:
///
/// let r = A.including(optional: A.b.including(required: B.c))
///
/// It chains three associations, the first optional, the second required.
///
/// It looks like it means: "Give me all As, along with their Bs, granted those
/// Bs have their Cs. For As whose B has no C, give me a nil B".
///
/// It can not be expressed as one left join, and a regular join, as below,
/// Because this would not honor the first optional:
///
/// -- dubious
/// SELECT a.*, b.*, c.*
/// FROM a
/// LEFT JOIN b ON ...
/// JOIN c ON ...
///
/// Instead, it should:
/// - allow (A + missing (B + C))
/// - prevent (A + (B + missing C)).
///
/// This can be expressed in SQL with two left joins, and an extra condition:
///
/// -- likely correct
/// SELECT a.*, b.*, c.*
/// FROM a
/// LEFT JOIN b ON ...
/// LEFT JOIN c ON ...
/// WHERE NOT((b.id IS NOT NULL) AND (c.id IS NULL)) -- no B without C
///
/// This is currently not implemented, and requires a little more thought.
/// I don't even know if inventing a whole new way to perform joins should even
/// be on the table. But we have a hierarchical way to express joined queries,
/// and they have a meaning:
///
/// // what is my meaning?
/// A.including(optional: A.b.including(required: B.c))
enum Kind {
case required, optional
}
/// The condition that links two joined tables.
///
/// Currently, we only support one kind of join condition: foreign keys.
@@ -327,7 +279,7 @@ struct SQLJoin {
}
}
var kind: Kind
var isRequired: Bool
var condition: Condition
var relation: SQLRelation
}
@@ -439,16 +391,8 @@ extension SQLJoin {
return nil
}
let mergedKind: SQLJoin.Kind
switch (kind, other.kind) {
case (.required, _), (_, .required):
mergedKind = .required
default:
mergedKind = .optional
}
return SQLJoin(
kind: mergedKind,
isRequired: isRequired || other.isRequired,
condition: condition,
relation: mergedRelation)
}
@@ -404,12 +404,12 @@ private enum SQLQualifiedSource {
/// A "qualified" join, where all tables are identified with a table alias.
private struct SQLQualifiedJoin {
private let kind: SQLJoin.Kind
private let isRequired: Bool
private let condition: SQLJoin.Condition
let relation: SQLQualifiedRelation
init(_ join: SQLJoin) {
self.kind = join.kind
self.isRequired = join.isRequired
self.condition = join.condition
self.relation = SQLQualifiedRelation(join.relation)
}
@@ -417,16 +417,16 @@ private struct SQLQualifiedJoin {
func sql(_ db: Database,_ context: inout SQLGenerationContext, leftAlias: TableAlias, isRequiredAllowed: Bool) throws -> String {
var isRequiredAllowed = isRequiredAllowed
var sql = ""
switch kind {
case .optional:
isRequiredAllowed = false
sql += "LEFT JOIN"
case .required:
if isRequired {
guard isRequiredAllowed else {
// TODO: chainOptionalRequired
fatalError("Not implemented: chaining a required association behind an optional association")
}
sql += "JOIN"
} else {
isRequiredAllowed = false
sql += "LEFT JOIN"
}
sql += try " " + relation.source.sql(db, &context)