539 |
|
|
540 |
=head3 CreateTables |
=head3 CreateTables |
541 |
|
|
542 |
C<< $datanase->CreateTables(); >> |
C<< $database->CreateTables(); >> |
543 |
|
|
544 |
This method creates the tables for the database from the metadata structure loaded by the |
This method creates the tables for the database from the metadata structure loaded by the |
545 |
constructor. It is expected this function will only be used on rare occasions, when the |
constructor. It is expected this function will only be used on rare occasions, when the |
959 |
$command .= " ORDER BY $orderClause"; |
$command .= " ORDER BY $orderClause"; |
960 |
} |
} |
961 |
} |
} |
962 |
Trace("SQL query: $command") if T(2); |
Trace("SQL query: $command") if T(3); |
963 |
Trace("PARMS: '" . (join "', '", @params) . "'") if (T(3) && (@params > 0)); |
Trace("PARMS: '" . (join "', '", @params) . "'") if (T(4) && (@params > 0)); |
964 |
my $sth = $dbh->prepare_command($command); |
my $sth = $dbh->prepare_command($command); |
965 |
# Execute it with the parameters bound in. |
# Execute it with the parameters bound in. |
966 |
$sth->execute(@params) || Confess("SELECT error" . $sth->errstr()); |
$sth->execute(@params) || Confess("SELECT error" . $sth->errstr()); |
2196 |
my @fromList = (); |
my @fromList = (); |
2197 |
my @toList = (); |
my @toList = (); |
2198 |
my @bothList = (); |
my @bothList = (); |
2199 |
Trace("Join table build for $entityName.") if T(3); |
Trace("Join table build for $entityName.") if T(4); |
2200 |
for my $relationshipName (keys %{$relationshipList}) { |
for my $relationshipName (keys %{$relationshipList}) { |
2201 |
my $relationship = $relationshipList->{$relationshipName}; |
my $relationship = $relationshipList->{$relationshipName}; |
2202 |
# 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. |
2203 |
my $fromEntity = $relationship->{from}; |
my $fromEntity = $relationship->{from}; |
2204 |
my $toEntity = $relationship->{to}; |
my $toEntity = $relationship->{to}; |
2205 |
Trace("Join check for relationship $relationshipName from $fromEntity to $toEntity.") if T(3); |
Trace("Join check for relationship $relationshipName from $fromEntity to $toEntity.") if T(4); |
2206 |
if ($fromEntity eq $entityName) { |
if ($fromEntity eq $entityName) { |
2207 |
if ($toEntity eq $entityName) { |
if ($toEntity eq $entityName) { |
2208 |
# Here the relationship is recursive. |
# Here the relationship is recursive. |
2209 |
push @bothList, $relationshipName; |
push @bothList, $relationshipName; |
2210 |
Trace("Relationship $relationshipName put in both-list.") if T(3); |
Trace("Relationship $relationshipName put in both-list.") if T(4); |
2211 |
} else { |
} else { |
2212 |
# Here the relationship comes from the entity. |
# Here the relationship comes from the entity. |
2213 |
push @fromList, $relationshipName; |
push @fromList, $relationshipName; |
2214 |
Trace("Relationship $relationshipName put in from-list.") if T(3); |
Trace("Relationship $relationshipName put in from-list.") if T(4); |
2215 |
} |
} |
2216 |
} elsif ($toEntity eq $entityName) { |
} elsif ($toEntity eq $entityName) { |
2217 |
# Here the relationship goes to the entity. |
# Here the relationship goes to the entity. |
2218 |
push @toList, $relationshipName; |
push @toList, $relationshipName; |
2219 |
Trace("Relationship $relationshipName put in to-list.") if T(3); |
Trace("Relationship $relationshipName put in to-list.") if T(4); |
2220 |
} |
} |
2221 |
} |
} |
2222 |
# Create the nonrecursive joins. Note that we build two hashes for running |
# Create the nonrecursive joins. Note that we build two hashes for running |
2262 |
# relationship can only be ambiguous with another recursive relationship, |
# relationship can only be ambiguous with another recursive relationship, |
2263 |
# and the incoming relationship from the outer loop is never recursive. |
# and the incoming relationship from the outer loop is never recursive. |
2264 |
for my $otherName (@bothList) { |
for my $otherName (@bothList) { |
2265 |
Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(3); |
Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(4); |
2266 |
# Join from the left. |
# Join from the left. |
2267 |
$joinTable{"$relationshipName/$otherName"} = |
$joinTable{"$relationshipName/$otherName"} = |
2268 |
"$linkField = $otherName.from_link"; |
"$linkField = $otherName.from_link"; |
2277 |
# 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 |
2278 |
# possible to get the same effect using multiple queries. |
# possible to get the same effect using multiple queries. |
2279 |
for my $relationshipName (@bothList) { |
for my $relationshipName (@bothList) { |
2280 |
Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(3); |
Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(4); |
2281 |
# Join to the entity from each direction. |
# Join to the entity from each direction. |
2282 |
$joinTable{"$entityName/$relationshipName"} = |
$joinTable{"$entityName/$relationshipName"} = |
2283 |
"$entityName.id = $relationshipName.from_link"; |
"$entityName.id = $relationshipName.from_link"; |