2225 |
my @fromList = (); |
my @fromList = (); |
2226 |
my @toList = (); |
my @toList = (); |
2227 |
my @bothList = (); |
my @bothList = (); |
2228 |
Trace("Join table build for $entityName.") if T(4); |
Trace("Join table build for $entityName.") if T(metadata => 4); |
2229 |
for my $relationshipName (keys %{$relationshipList}) { |
for my $relationshipName (keys %{$relationshipList}) { |
2230 |
my $relationship = $relationshipList->{$relationshipName}; |
my $relationship = $relationshipList->{$relationshipName}; |
2231 |
# Determine if this relationship has our entity in one of its link fields. |
# Determine if this relationship has our entity in one of its link fields. |
2236 |
if ($toEntity eq $entityName) { |
if ($toEntity eq $entityName) { |
2237 |
# Here the relationship is recursive. |
# Here the relationship is recursive. |
2238 |
push @bothList, $relationshipName; |
push @bothList, $relationshipName; |
2239 |
Trace("Relationship $relationshipName put in both-list.") if T(4); |
Trace("Relationship $relationshipName put in both-list.") if T(metadata => 4); |
2240 |
} else { |
} else { |
2241 |
# Here the relationship comes from the entity. |
# Here the relationship comes from the entity. |
2242 |
push @fromList, $relationshipName; |
push @fromList, $relationshipName; |
2243 |
Trace("Relationship $relationshipName put in from-list.") if T(4); |
Trace("Relationship $relationshipName put in from-list.") if T(metadata => 4); |
2244 |
} |
} |
2245 |
} elsif ($toEntity eq $entityName) { |
} elsif ($toEntity eq $entityName) { |
2246 |
# Here the relationship goes to the entity. |
# Here the relationship goes to the entity. |
2247 |
push @toList, $relationshipName; |
push @toList, $relationshipName; |
2248 |
Trace("Relationship $relationshipName put in to-list.") if T(4); |
Trace("Relationship $relationshipName put in to-list.") if T(metadata => 4); |
2249 |
} |
} |
2250 |
} |
} |
2251 |
# Create the nonrecursive joins. Note that we build two hashes for running |
# Create the nonrecursive joins. Note that we build two hashes for running |
2261 |
# Create joins between the entity and this relationship. |
# Create joins between the entity and this relationship. |
2262 |
my $linkField = "$relationshipName.${linkType}_link"; |
my $linkField = "$relationshipName.${linkType}_link"; |
2263 |
my $joinClause = "$entityName.id = $linkField"; |
my $joinClause = "$entityName.id = $linkField"; |
2264 |
Trace("Entity join clause is $joinClause for $entityName and $relationshipName.") if T(4); |
Trace("Entity join clause is $joinClause for $entityName and $relationshipName.") if T(metadata => 4); |
2265 |
$joinTable{"$entityName/$relationshipName"} = $joinClause; |
$joinTable{"$entityName/$relationshipName"} = $joinClause; |
2266 |
$joinTable{"$relationshipName/$entityName"} = $joinClause; |
$joinTable{"$relationshipName/$entityName"} = $joinClause; |
2267 |
# Create joins between this relationship and the other relationships. |
# Create joins between this relationship and the other relationships. |
2282 |
# relationship and itself are prohibited. |
# relationship and itself are prohibited. |
2283 |
my $relJoinClause = "$otherName.${otherType}_link = $linkField"; |
my $relJoinClause = "$otherName.${otherType}_link = $linkField"; |
2284 |
$joinTable{$joinKey} = $relJoinClause; |
$joinTable{$joinKey} = $relJoinClause; |
2285 |
Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(4); |
Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(metadata => 4); |
2286 |
} |
} |
2287 |
} |
} |
2288 |
} |
} |
2291 |
# relationship can only be ambiguous with another recursive relationship, |
# relationship can only be ambiguous with another recursive relationship, |
2292 |
# and the incoming relationship from the outer loop is never recursive. |
# and the incoming relationship from the outer loop is never recursive. |
2293 |
for my $otherName (@bothList) { |
for my $otherName (@bothList) { |
2294 |
Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(4); |
Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(metadata => 4); |
2295 |
# Join from the left. |
# Join from the left. |
2296 |
$joinTable{"$relationshipName/$otherName"} = |
$joinTable{"$relationshipName/$otherName"} = |
2297 |
"$linkField = $otherName.from_link"; |
"$linkField = $otherName.from_link"; |
2306 |
# rise to situations where we can't create the path we want; however, it is always |
# rise to situations where we can't create the path we want; however, it is always |
2307 |
# possible to get the same effect using multiple queries. |
# possible to get the same effect using multiple queries. |
2308 |
for my $relationshipName (@bothList) { |
for my $relationshipName (@bothList) { |
2309 |
Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(4); |
Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(metadata => 4); |
2310 |
# Join to the entity from each direction. |
# Join to the entity from each direction. |
2311 |
$joinTable{"$entityName/$relationshipName"} = |
$joinTable{"$entityName/$relationshipName"} = |
2312 |
"$entityName.id = $relationshipName.from_link"; |
"$entityName.id = $relationshipName.from_link"; |