SQLAssociation.associationForFirst()

This commit is contained in:
Gwendal Roué committed 2020-04-18 16:40:22 +02:00
1 parent 4483f037a4
commit 6f125bd3d7
2 files changed
+6 -4

No files matched your search

@@ -103,10 +103,7 @@ public struct HasManyAssociation<Origin: TableRecord, Destination: TableRecord>:
/// TODO
public var first: HasOneAssociation<Origin, Destination> {
assert(sqlAssociation.steps.count == 1)
var step = sqlAssociation.steps[0]
step.firstOnly = true
return HasOneAssociation(sqlAssociation: SQLAssociation(steps: [step]))
HasOneAssociation(sqlAssociation: sqlAssociation.associationForFirst())
}
/// TODO
@@ -97,6 +97,10 @@ public /* TODO: internal */ struct SQLAssociation {
SQLAssociation(steps: other.steps + steps)
}
func associationForFirst() -> Self {
SQLAssociation(steps: steps.map { $0.with(\.firstOnly, true) })
}
/// Given an origin alias and rows, returns the destination of the
/// association as a relation.
///
@@ -204,6 +208,7 @@ struct SQLAssociationStep: Refinable {
var key: SQLAssociationKey
var condition: SQLAssociationCondition
var relation: SQLRelation
// TODO: I have problems understanding what cardinality=toMany + firstOnly=true mean.
var cardinality: SQLAssociationCardinality
var firstOnly: Bool