422 |
# Write the HTML heading stuff. |
# Write the HTML heading stuff. |
423 |
print HTMLOUT "<html>\n<head>\n<title>$title</title>\n"; |
print HTMLOUT "<html>\n<head>\n<title>$title</title>\n"; |
424 |
print HTMLOUT "</head>\n<body>\n"; |
print HTMLOUT "</head>\n<body>\n"; |
425 |
|
# Write the documentation. |
426 |
|
print HTMLOUT $self->DisplayMetaData(); |
427 |
|
# Close the document. |
428 |
|
print HTMLOUT "</body>\n</html>\n"; |
429 |
|
# Close the file. |
430 |
|
close HTMLOUT; |
431 |
|
} |
432 |
|
|
433 |
|
=head3 DisplayMetaData |
434 |
|
|
435 |
|
C<< my $html = $erdb->DisplayMetaData(); >> |
436 |
|
|
437 |
|
Return an HTML description of the database. This description can be used to help users create |
438 |
|
the data to be loaded into the relations and form queries. The output is raw includable HTML |
439 |
|
without any HEAD or BODY tags. |
440 |
|
|
441 |
|
=over 4 |
442 |
|
|
443 |
|
=item filename |
444 |
|
|
445 |
|
The name of the output file. |
446 |
|
|
447 |
|
=back |
448 |
|
|
449 |
|
=cut |
450 |
|
|
451 |
|
sub DisplayMetaData { |
452 |
|
# Get the parameters. |
453 |
|
my ($self) = @_; |
454 |
|
# Get the metadata and the title string. |
455 |
|
my $metadata = $self->{_metaData}; |
456 |
|
# Get the title string. |
457 |
|
my $title = $metadata->{Title}; |
458 |
|
# Get the entity and relationship lists. |
459 |
|
my $entityList = $metadata->{Entities}; |
460 |
|
my $relationshipList = $metadata->{Relationships}; |
461 |
|
# Declare the return variable. |
462 |
|
my $retVal = ""; |
463 |
|
# Open the output file. |
464 |
|
Trace("Building MetaData table of contents.") if T(4); |
465 |
# Here we do the table of contents. It starts as an unordered list of section names. Each |
# Here we do the table of contents. It starts as an unordered list of section names. Each |
466 |
# section contains an ordered list of entity or relationship subsections. |
# section contains an ordered list of entity or relationship subsections. |
467 |
print HTMLOUT "<ul>\n<li><a href=\"#EntitiesSection\">Entities</a>\n<ol>\n"; |
$retVal .= "<ul>\n<li><a href=\"#EntitiesSection\">Entities</a>\n<ol>\n"; |
468 |
# Loop through the Entities, displaying a list item for each. |
# Loop through the Entities, displaying a list item for each. |
469 |
foreach my $key (sort keys %{$entityList}) { |
foreach my $key (sort keys %{$entityList}) { |
470 |
# Display this item. |
# Display this item. |
471 |
print HTMLOUT "<li><a href=\"#$key\">$key</a></li>\n"; |
$retVal .= "<li><a href=\"#$key\">$key</a></li>\n"; |
472 |
} |
} |
473 |
# Close off the entity section and start the relationship section. |
# Close off the entity section and start the relationship section. |
474 |
print HTMLOUT "</ol></li>\n<li><a href=\"#RelationshipsSection\">Relationships</a>\n<ol>\n"; |
$retVal .= "</ol></li>\n<li><a href=\"#RelationshipsSection\">Relationships</a>\n<ol>\n"; |
475 |
# Loop through the Relationships. |
# Loop through the Relationships. |
476 |
foreach my $key (sort keys %{$relationshipList}) { |
foreach my $key (sort keys %{$relationshipList}) { |
477 |
# Display this item. |
# Display this item. |
478 |
my $relationshipTitle = _ComputeRelationshipSentence($key, $relationshipList->{$key}); |
my $relationshipTitle = _ComputeRelationshipSentence($key, $relationshipList->{$key}); |
479 |
print HTMLOUT "<li><a href=\"#$key\">$relationshipTitle</a></li>\n"; |
$retVal .= "<li><a href=\"#$key\">$relationshipTitle</a></li>\n"; |
480 |
} |
} |
481 |
# Close off the relationship section and list the join table section. |
# Close off the relationship section and list the join table section. |
482 |
print HTMLOUT "</ol></li>\n<li><a href=\"#JoinTable\">Join Table</a></li>\n"; |
$retVal .= "</ol></li>\n<li><a href=\"#JoinTable\">Join Table</a></li>\n"; |
483 |
# Close off the table of contents itself. |
# Close off the table of contents itself. |
484 |
print HTMLOUT "</ul>\n"; |
$retVal .= "</ul>\n"; |
485 |
# Now we start with the actual data. Denote we're starting the entity section. |
# Now we start with the actual data. Denote we're starting the entity section. |
486 |
print HTMLOUT "<a name=\"EntitiesSection\"></a><h2>Entities</h2>\n"; |
$retVal .= "<a name=\"EntitiesSection\"></a><h2>Entities</h2>\n"; |
487 |
# Loop through the entities. |
# Loop through the entities. |
488 |
for my $key (sort keys %{$entityList}) { |
for my $key (sort keys %{$entityList}) { |
489 |
Trace("Building MetaData entry for $key entity.") if T(4); |
Trace("Building MetaData entry for $key entity.") if T(4); |
490 |
# Create the entity header. It contains a bookmark and the entity name. |
# Create the entity header. It contains a bookmark and the entity name. |
491 |
print HTMLOUT "<a name=\"$key\"></a><h3>$key</h3>\n"; |
$retVal .= "<a name=\"$key\"></a><h3>$key</h3>\n"; |
492 |
# Get the entity data. |
# Get the entity data. |
493 |
my $entityData = $entityList->{$key}; |
my $entityData = $entityList->{$key}; |
494 |
# If there's descriptive text, display it. |
# If there's descriptive text, display it. |
495 |
if (my $notes = $entityData->{Notes}) { |
if (my $notes = $entityData->{Notes}) { |
496 |
print HTMLOUT "<p>" . _HTMLNote($notes->{content}) . "</p>\n"; |
$retVal .= "<p>" . _HTMLNote($notes->{content}) . "</p>\n"; |
497 |
} |
} |
498 |
# Now we want a list of the entity's relationships. First, we set up the relationship subsection. |
# Now we want a list of the entity's relationships. First, we set up the relationship subsection. |
499 |
print HTMLOUT "<h4>Relationships for <b>$key</b></h4>\n<ul>\n"; |
$retVal .= "<h4>Relationships for <b>$key</b></h4>\n<ul>\n"; |
500 |
# Loop through the relationships. |
# Loop through the relationships. |
501 |
for my $relationship (sort keys %{$relationshipList}) { |
for my $relationship (sort keys %{$relationshipList}) { |
502 |
# Get the relationship data. |
# Get the relationship data. |
506 |
# Get the relationship sentence and append the arity. |
# Get the relationship sentence and append the arity. |
507 |
my $relationshipDescription = _ComputeRelationshipSentence($relationship, $relationshipStructure); |
my $relationshipDescription = _ComputeRelationshipSentence($relationship, $relationshipStructure); |
508 |
# Display the relationship data. |
# Display the relationship data. |
509 |
print HTMLOUT "<li><a href=\"#$relationship\">$relationshipDescription</a></li>\n"; |
$retVal .= "<li><a href=\"#$relationship\">$relationshipDescription</a></li>\n"; |
510 |
} |
} |
511 |
} |
} |
512 |
# Close off the relationship list. |
# Close off the relationship list. |
513 |
print HTMLOUT "</ul>\n"; |
$retVal .= "</ul>\n"; |
514 |
# Get the entity's relations. |
# Get the entity's relations. |
515 |
my $relationList = $entityData->{Relations}; |
my $relationList = $entityData->{Relations}; |
516 |
# Create a header for the relation subsection. |
# Create a header for the relation subsection. |
517 |
print HTMLOUT "<h4>Relations for <b>$key</b></h4>\n"; |
$retVal .= "<h4>Relations for <b>$key</b></h4>\n"; |
518 |
# Loop through the relations, displaying them. |
# Loop through the relations, displaying them. |
519 |
for my $relation (sort keys %{$relationList}) { |
for my $relation (sort keys %{$relationList}) { |
520 |
my $htmlString = _ShowRelationTable($relation, $relationList->{$relation}); |
my $htmlString = _ShowRelationTable($relation, $relationList->{$relation}); |
521 |
print HTMLOUT $htmlString; |
$retVal .= $htmlString; |
522 |
} |
} |
523 |
} |
} |
524 |
# Denote we're starting the relationship section. |
# Denote we're starting the relationship section. |
525 |
print HTMLOUT "<a name=\"RelationshipsSection\"></a><h2>Relationships</h2>\n"; |
$retVal .= "<a name=\"RelationshipsSection\"></a><h2>Relationships</h2>\n"; |
526 |
# Loop through the relationships. |
# Loop through the relationships. |
527 |
for my $key (sort keys %{$relationshipList}) { |
for my $key (sort keys %{$relationshipList}) { |
528 |
Trace("Building MetaData entry for $key relationship.") if T(4); |
Trace("Building MetaData entry for $key relationship.") if T(4); |
530 |
my $relationshipStructure = $relationshipList->{$key}; |
my $relationshipStructure = $relationshipList->{$key}; |
531 |
# Create the relationship header. |
# Create the relationship header. |
532 |
my $headerText = _ComputeRelationshipHeading($key, $relationshipStructure); |
my $headerText = _ComputeRelationshipHeading($key, $relationshipStructure); |
533 |
print HTMLOUT "<h3><a name=\"$key\"></a>$headerText</h3>\n"; |
$retVal .= "<h3><a name=\"$key\"></a>$headerText</h3>\n"; |
534 |
# Get the entity names. |
# Get the entity names. |
535 |
my $fromEntity = $relationshipStructure->{from}; |
my $fromEntity = $relationshipStructure->{from}; |
536 |
my $toEntity = $relationshipStructure->{to}; |
my $toEntity = $relationshipStructure->{to}; |
540 |
# since both sentences will say the same thing. |
# since both sentences will say the same thing. |
541 |
my $arity = $relationshipStructure->{arity}; |
my $arity = $relationshipStructure->{arity}; |
542 |
if ($arity eq "11") { |
if ($arity eq "11") { |
543 |
print HTMLOUT "<p>Each <b>$fromEntity</b> relates to at most one <b>$toEntity</b>.\n"; |
$retVal .= "<p>Each <b>$fromEntity</b> relates to at most one <b>$toEntity</b>.\n"; |
544 |
} else { |
} else { |
545 |
print HTMLOUT "<p>Each <b>$fromEntity</b> relates to multiple <b>$toEntity</b>s.\n"; |
$retVal .= "<p>Each <b>$fromEntity</b> relates to multiple <b>$toEntity</b>s.\n"; |
546 |
if ($arity eq "MM" && $fromEntity ne $toEntity) { |
if ($arity eq "MM" && $fromEntity ne $toEntity) { |
547 |
print HTMLOUT "Each <b>$toEntity</b> relates to multiple <b>$fromEntity</b>s.\n"; |
$retVal .= "Each <b>$toEntity</b> relates to multiple <b>$fromEntity</b>s.\n"; |
548 |
} |
} |
549 |
} |
} |
550 |
print HTMLOUT "</p>\n"; |
$retVal .= "</p>\n"; |
551 |
# If there are notes on this relationship, display them. |
# If there are notes on this relationship, display them. |
552 |
if (my $notes = $relationshipStructure->{Notes}) { |
if (my $notes = $relationshipStructure->{Notes}) { |
553 |
print HTMLOUT "<p>" . _HTMLNote($notes->{content}) . "</p>\n"; |
$retVal .= "<p>" . _HTMLNote($notes->{content}) . "</p>\n"; |
554 |
} |
} |
555 |
# Generate the relationship's relation table. |
# Generate the relationship's relation table. |
556 |
my $htmlString = _ShowRelationTable($key, $relationshipStructure->{Relations}->{$key}); |
my $htmlString = _ShowRelationTable($key, $relationshipStructure->{Relations}->{$key}); |
557 |
print HTMLOUT $htmlString; |
$retVal .= $htmlString; |
558 |
} |
} |
559 |
Trace("Building MetaData join table.") if T(4); |
Trace("Building MetaData join table.") if T(4); |
560 |
# Denote we're starting the join table. |
# Denote we're starting the join table. |
561 |
print HTMLOUT "<a name=\"JoinTable\"></a><h3>Join Table</h3>\n"; |
$retVal .= "<a name=\"JoinTable\"></a><h3>Join Table</h3>\n"; |
562 |
# Create a table header. |
# Create a table header. |
563 |
print HTMLOUT _OpenTable("Join Table", "Source", "Target", "Join Condition"); |
$retVal .= _OpenTable("Join Table", "Source", "Target", "Join Condition"); |
564 |
# Loop through the joins. |
# Loop through the joins. |
565 |
my $joinTable = $metadata->{Joins}; |
my $joinTable = $metadata->{Joins}; |
566 |
my @joinKeys = keys %{$joinTable}; |
my @joinKeys = keys %{$joinTable}; |
573 |
my $target = $self->ComputeObjectSentence($targetRelation); |
my $target = $self->ComputeObjectSentence($targetRelation); |
574 |
my $clause = $joinTable->{$joinKey}; |
my $clause = $joinTable->{$joinKey}; |
575 |
# Display them in a table row. |
# Display them in a table row. |
576 |
print HTMLOUT "<tr><td>$source</td><td>$target</td><td>$clause</td></tr>\n"; |
$retVal .= "<tr><td>$source</td><td>$target</td><td>$clause</td></tr>\n"; |
577 |
} |
} |
578 |
# Close the table. |
# Close the table. |
579 |
print HTMLOUT _CloseTable(); |
$retVal .= _CloseTable(); |
580 |
# Close the document. |
Trace("Built MetaData HTML.") if T(3); |
581 |
print HTMLOUT "</body>\n</html>\n"; |
# Return the HTML. |
582 |
# Close the file. |
return $retVal; |
|
close HTMLOUT; |
|
|
Trace("Built MetaData web page.") if T(3); |
|
583 |
} |
} |
584 |
|
|
585 |
=head3 DumpMetaData |
=head3 DumpMetaData |
780 |
my $fieldType = $fieldTypes->[$i]->{type}; |
my $fieldType = $fieldTypes->[$i]->{type}; |
781 |
# If it's a hash string, digest it in place. |
# If it's a hash string, digest it in place. |
782 |
if ($fieldType eq 'hash-string') { |
if ($fieldType eq 'hash-string') { |
783 |
$fieldList->[$i] = md5_base64($fieldList->[$i]); |
$fieldList->[$i] = $self->DigestKey($fieldList->[$i]); |
784 |
|
} |
785 |
} |
} |
786 |
} |
} |
787 |
|
|
788 |
|
=head3 DigestKey |
789 |
|
|
790 |
|
C<< my $digested = $erdb->DigestKey($keyValue); >> |
791 |
|
|
792 |
|
Return the digested value of a symbolic key. The digested value can then be plugged into a |
793 |
|
key-based search into a table with key-type hash-string. |
794 |
|
|
795 |
|
Currently the digesting process is independent of the database structure, but that may not |
796 |
|
always be the case, so this is an instance method instead of a static method. |
797 |
|
|
798 |
|
=over 4 |
799 |
|
|
800 |
|
=item keyValue |
801 |
|
|
802 |
|
Key value to digest. |
803 |
|
|
804 |
|
=item RETURN |
805 |
|
|
806 |
|
Digested value ofthe key. |
807 |
|
|
808 |
|
=back |
809 |
|
|
810 |
|
=cut |
811 |
|
|
812 |
|
sub DigestKey { |
813 |
|
# Get the parameters. |
814 |
|
my ($self, $keyValue) = @_; |
815 |
|
# Compute the digest. |
816 |
|
my $retVal = md5_base64($keyValue); |
817 |
|
# Return the result. |
818 |
|
return $retVal; |
819 |
} |
} |
820 |
|
|
821 |
=head3 CreateIndex |
=head3 CreateIndex |
979 |
|
|
980 |
=head3 Get |
=head3 Get |
981 |
|
|
982 |
C<< my $query = $erdb->Get(\@objectNames, $filterClause, $param1, $param2, ..., $paramN); >> |
C<< my $query = $erdb->Get(\@objectNames, $filterClause, \@params); >> |
983 |
|
|
984 |
This method returns a query object for entities of a specified type using a specified filter. |
This method returns a query object for entities of a specified type using a specified filter. |
985 |
The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each |
The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each |
987 |
following call requests all B<Genome> objects for the genus specified in the variable |
following call requests all B<Genome> objects for the genus specified in the variable |
988 |
$genus. |
$genus. |
989 |
|
|
990 |
C<< $query = $erdb->Get(['Genome'], "Genome(genus) = ?", $genus); >> |
C<< $query = $erdb->Get(['Genome'], "Genome(genus) = ?", [$genus]); >> |
991 |
|
|
992 |
The WHERE clause contains a single question mark, so there is a single additional |
The WHERE clause contains a single question mark, so there is a single additional |
993 |
parameter representing the parameter value. It would also be possible to code |
parameter representing the parameter value. It would also be possible to code |
1004 |
It is possible to specify multiple entity and relationship names in order to retrieve more than |
It is possible to specify multiple entity and relationship names in order to retrieve more than |
1005 |
one object's data at the same time, which allows highly complex joined queries. For example, |
one object's data at the same time, which allows highly complex joined queries. For example, |
1006 |
|
|
1007 |
C<< $query = $erdb->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", $genus); >> |
C<< $query = $erdb->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >> |
1008 |
|
|
1009 |
If multiple names are specified, then the query processor will automatically determine a |
If multiple names are specified, then the query processor will automatically determine a |
1010 |
join path between the entities and relationships. The algorithm used is very simplistic. |
join path between the entities and relationships. The algorithm used is very simplistic. |
1061 |
|
|
1062 |
C<< "LIMIT 10" >> |
C<< "LIMIT 10" >> |
1063 |
|
|
1064 |
=item param1, param2, ..., paramN |
=item params |
1065 |
|
|
1066 |
Parameter values to be substituted into the filter clause. |
Reference to a list of parameter values to be substituted into the filter clause. |
1067 |
|
|
1068 |
=item RETURN |
=item RETURN |
1069 |
|
|
1075 |
|
|
1076 |
sub Get { |
sub Get { |
1077 |
# Get the parameters. |
# Get the parameters. |
1078 |
my ($self, $objectNames, $filterClause, @params) = @_; |
my ($self, $objectNames, $filterClause, $params) = @_; |
1079 |
# Adjust the list of object names to account for multiple occurrences of the |
# Process the SQL stuff. |
1080 |
# same object. We start with a hash table keyed on object name that will |
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = |
1081 |
# return the object suffix. The first time an object is encountered it will |
$self->_SetupSQL($objectNames, $filterClause); |
1082 |
# not be found in the hash. The next time the hash will map the object name |
# Create the query. |
1083 |
# to 2, then 3, and so forth. |
my $command = "SELECT DISTINCT " . join(".*, ", @{$mappedNameListRef}) . |
1084 |
my %objectHash = (); |
".* $suffix"; |
1085 |
# This list will contain the object names as they are to appear in the |
my $sth = $self->_GetStatementHandle($command, $params); |
|
# FROM list. |
|
|
my @fromList = (); |
|
|
# This list contains the suffixed object name for each object. It is exactly |
|
|
# parallel to the list in the $objectNames parameter. |
|
|
my @mappedNameList = (); |
|
|
# Finally, this hash translates from a mapped name to its original object name. |
|
|
my %mappedNameHash = (); |
|
|
# Now we create the lists. Note that for every single name we push something into |
|
|
# @fromList and @mappedNameList. This insures that those two arrays are exactly |
|
|
# parallel to $objectNames. |
|
|
for my $objectName (@{$objectNames}) { |
|
|
# Get the next suffix for this object. |
|
|
my $suffix = $objectHash{$objectName}; |
|
|
if (! $suffix) { |
|
|
# Here we are seeing the object for the first time. The object name |
|
|
# is used as is. |
|
|
push @mappedNameList, $objectName; |
|
|
push @fromList, $objectName; |
|
|
$mappedNameHash{$objectName} = $objectName; |
|
|
# Denote the next suffix will be 2. |
|
|
$objectHash{$objectName} = 2; |
|
|
} else { |
|
|
# Here we've seen the object before. We construct a new name using |
|
|
# the suffix from the hash and update the hash. |
|
|
my $mappedName = "$objectName$suffix"; |
|
|
$objectHash{$objectName} = $suffix + 1; |
|
|
# The FROM list has the object name followed by the mapped name. This |
|
|
# tells SQL it's still the same table, but we're using a different name |
|
|
# for it to avoid confusion. |
|
|
push @fromList, "$objectName $mappedName"; |
|
|
# The mapped-name list contains the real mapped name. |
|
|
push @mappedNameList, $mappedName; |
|
|
# Finally, enable us to get back from the mapped name to the object name. |
|
|
$mappedNameHash{$mappedName} = $objectName; |
|
|
} |
|
|
} |
|
|
# Construct the SELECT statement. The general pattern is |
|
|
# |
|
|
# SELECT name1.*, name2.*, ... nameN.* FROM name1, name2, ... nameN |
|
|
# |
|
|
my $dbh = $self->{_dbh}; |
|
|
my $command = "SELECT DISTINCT " . join('.*, ', @mappedNameList) . ".* FROM " . |
|
|
join(', ', @fromList); |
|
|
# Check for a filter clause. |
|
|
if ($filterClause) { |
|
|
# Here we have one, so we convert its field names and add it to the query. First, |
|
|
# We create a copy of the filter string we can work with. |
|
|
my $filterString = $filterClause; |
|
|
# Next, we sort the object names by length. This helps protect us from finding |
|
|
# object names inside other object names when we're doing our search and replace. |
|
|
my @sortedNames = sort { length($b) - length($a) } @mappedNameList; |
|
|
# We will also keep a list of conditions to add to the WHERE clause in order to link |
|
|
# entities and relationships as well as primary relations to secondary ones. |
|
|
my @joinWhere = (); |
|
|
# The final preparatory step is to create a hash table of relation names. The |
|
|
# table begins with the relation names already in the SELECT command. We may |
|
|
# need to add relations later if there is filtering on a field in a secondary |
|
|
# relation. The secondary relations are the ones that contain multiply- |
|
|
# occurring or optional fields. |
|
|
my %fromNames = map { $_ => 1 } @sortedNames; |
|
|
# We are ready to begin. We loop through the object names, replacing each |
|
|
# object name's field references by the corresponding SQL field reference. |
|
|
# Along the way, if we find a secondary relation, we will need to add it |
|
|
# to the FROM clause. |
|
|
for my $mappedName (@sortedNames) { |
|
|
# Get the length of the object name plus 2. This is the value we add to the |
|
|
# size of the field name to determine the size of the field reference as a |
|
|
# whole. |
|
|
my $nameLength = 2 + length $mappedName; |
|
|
# Get the real object name for this mapped name. |
|
|
my $objectName = $mappedNameHash{$mappedName}; |
|
|
Trace("Processing $mappedName for object $objectName.") if T(4); |
|
|
# Get the object's field list. |
|
|
my $fieldList = $self->GetFieldTable($objectName); |
|
|
# Find the field references for this object. |
|
|
while ($filterString =~ m/$mappedName\(([^)]*)\)/g) { |
|
|
# At this point, $1 contains the field name, and the current position |
|
|
# is set immediately after the final parenthesis. We pull out the name of |
|
|
# the field and the position and length of the field reference as a whole. |
|
|
my $fieldName = $1; |
|
|
my $len = $nameLength + length $fieldName; |
|
|
my $pos = pos($filterString) - $len; |
|
|
# Insure the field exists. |
|
|
if (!exists $fieldList->{$fieldName}) { |
|
|
Confess("Field $fieldName not found for object $objectName."); |
|
|
} else { |
|
|
Trace("Processing $fieldName at position $pos.") if T(4); |
|
|
# Get the field's relation. |
|
|
my $relationName = $fieldList->{$fieldName}->{relation}; |
|
|
# Now we have a secondary relation. We need to insure it matches the |
|
|
# mapped name of the primary relation. First we peel off the suffix |
|
|
# from the mapped name. |
|
|
my $mappingSuffix = substr $mappedName, length($objectName); |
|
|
# Put the mapping suffix onto the relation name to get the |
|
|
# mapped relation name. |
|
|
my $mappedRelationName = "$relationName$mappingSuffix"; |
|
|
# Insure the relation is in the FROM clause. |
|
|
if (!exists $fromNames{$mappedRelationName}) { |
|
|
# Add the relation to the FROM clause. |
|
|
if ($mappedRelationName eq $relationName) { |
|
|
# The name is un-mapped, so we add it without |
|
|
# any frills. |
|
|
$command .= ", $relationName"; |
|
|
push @joinWhere, "$objectName.id = $relationName.id"; |
|
|
} else { |
|
|
# Here we have a mapping situation. |
|
|
$command .= ", $relationName $mappedRelationName"; |
|
|
push @joinWhere, "$mappedRelationName.id = $mappedName.id"; |
|
|
} |
|
|
# Denote we have this relation available for future fields. |
|
|
$fromNames{$mappedRelationName} = 1; |
|
|
} |
|
|
# Form an SQL field reference from the relation name and the field name. |
|
|
my $sqlReference = "$mappedRelationName." . _FixName($fieldName); |
|
|
# Put it into the filter string in place of the old value. |
|
|
substr($filterString, $pos, $len) = $sqlReference; |
|
|
# Reposition the search. |
|
|
pos $filterString = $pos + length $sqlReference; |
|
|
} |
|
|
} |
|
|
} |
|
|
# The next step is to join the objects together. We only need to do this if there |
|
|
# is more than one object in the object list. We start with the first object and |
|
|
# run through the objects after it. Note also that we make a safety copy of the |
|
|
# list before running through it. |
|
|
my @mappedObjectList = @mappedNameList; |
|
|
my $lastMappedObject = shift @mappedObjectList; |
|
|
# Get the join table. |
|
|
my $joinTable = $self->{_metaData}->{Joins}; |
|
|
# Loop through the object list. |
|
|
for my $thisMappedObject (@mappedObjectList) { |
|
|
# Look for a join using the real object names. |
|
|
my $lastObject = $mappedNameHash{$lastMappedObject}; |
|
|
my $thisObject = $mappedNameHash{$thisMappedObject}; |
|
|
my $joinKey = "$lastObject/$thisObject"; |
|
|
if (!exists $joinTable->{$joinKey}) { |
|
|
# Here there's no join, so we throw an error. |
|
|
Confess("No join exists to connect from $lastMappedObject to $thisMappedObject."); |
|
|
} else { |
|
|
# Get the join clause. |
|
|
my $unMappedJoin = $joinTable->{$joinKey}; |
|
|
# Fix the names. |
|
|
$unMappedJoin =~ s/$lastObject/$lastMappedObject/; |
|
|
$unMappedJoin =~ s/$thisObject/$thisMappedObject/; |
|
|
push @joinWhere, $unMappedJoin; |
|
|
# Save this object as the last object for the next iteration. |
|
|
$lastMappedObject = $thisMappedObject; |
|
|
} |
|
|
} |
|
|
# Now we need to handle the whole ORDER BY / LIMIT thing. The important part |
|
|
# here is we want the filter clause to be empty if there's no WHERE filter. |
|
|
# We'll put the ORDER BY / LIMIT clauses in the following variable. |
|
|
my $orderClause = ""; |
|
|
# Locate the ORDER BY or LIMIT verbs (if any). We use a non-greedy |
|
|
# operator so that we find the first occurrence of either verb. |
|
|
if ($filterString =~ m/^(.*?)\s*(ORDER BY|LIMIT)/g) { |
|
|
# Here we have an ORDER BY or LIMIT verb. Split it off of the filter string. |
|
|
my $pos = pos $filterString; |
|
|
$orderClause = $2 . substr($filterString, $pos); |
|
|
$filterString = $1; |
|
|
} |
|
|
# Add the filter and the join clauses (if any) to the SELECT command. |
|
|
if ($filterString) { |
|
|
Trace("Filter string is \"$filterString\".") if T(4); |
|
|
push @joinWhere, "($filterString)"; |
|
|
} |
|
|
if (@joinWhere) { |
|
|
$command .= " WHERE " . join(' AND ', @joinWhere); |
|
|
} |
|
|
# Add the sort or limit clause (if any) to the SELECT command. |
|
|
if ($orderClause) { |
|
|
$command .= " $orderClause"; |
|
|
} |
|
|
} |
|
|
Trace("SQL query: $command") if T(SQL => 3); |
|
|
Trace("PARMS: '" . (join "', '", @params) . "'") if (T(SQL => 4) && (@params > 0)); |
|
|
my $sth = $dbh->prepare_command($command); |
|
|
# Execute it with the parameters bound in. |
|
|
$sth->execute(@params) || Confess("SELECT error" . $sth->errstr()); |
|
1086 |
# Now we create the relation map, which enables DBQuery to determine the order, name |
# Now we create the relation map, which enables DBQuery to determine the order, name |
1087 |
# and mapped name for each object in the query. |
# and mapped name for each object in the query. |
1088 |
my @relationMap = (); |
my @relationMap = (); |
1089 |
for my $mappedName (@mappedNameList) { |
for my $mappedName (@{$mappedNameListRef}) { |
1090 |
push @relationMap, [$mappedName, $mappedNameHash{$mappedName}]; |
push @relationMap, [$mappedName, $mappedNameHashRef->{$mappedName}]; |
1091 |
} |
} |
1092 |
# Return the statement object. |
# Return the statement object. |
1093 |
my $retVal = DBQuery::_new($self, $sth, \@relationMap); |
my $retVal = DBQuery::_new($self, $sth, \@relationMap); |
1094 |
return $retVal; |
return $retVal; |
1095 |
} |
} |
1096 |
|
|
1097 |
=head3 Delete |
=head3 GetFlat |
1098 |
|
|
1099 |
C<< my $stats = $erdb->Delete($entityName, $objectID); >> |
C<< my @list = $erdb->GetFlat(\@objectNames, $filterClause, \@parameterList, $field); >> |
1100 |
|
|
1101 |
Delete an entity instance from the database. The instance is deleted along with all entity and |
This is a variation of L</GetAll> that asks for only a single field per record and |
1102 |
relationship instances dependent on it. The idea of dependence here is recursive. An object is |
returns a single flattened list. |
|
always dependent on itself. An object is dependent if it is a 1-to-many or many-to-many |
|
|
relationship connected to a dependent entity or the "to" entity connected to a 1-to-many |
|
|
dependent relationship. |
|
1103 |
|
|
1104 |
=over 4 |
=over 4 |
1105 |
|
|
1106 |
=item entityName |
=item objectNames |
1107 |
|
|
1108 |
Name of the entity type for the instance being deleted. |
List containing the names of the entity and relationship objects to be retrieved. |
1109 |
|
|
1110 |
=item objectID |
=item filterClause |
1111 |
|
|
1112 |
ID of the entity instance to be deleted. If the ID contains a wild card character (C<%>), |
WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can |
1113 |
then it is presumed to by a LIKE pattern. |
be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form |
1114 |
|
B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the |
1115 |
|
parameter list as additional parameters. The fields in a filter clause can come from primary |
1116 |
|
entity relations, relationship relations, or secondary entity relations; however, all of the |
1117 |
|
entities and relationships involved must be included in the list of object names. |
1118 |
|
|
1119 |
=item testFlag |
=item parameterList |
1120 |
|
|
1121 |
If TRUE, the delete statements will be traced without being executed. |
List of the parameters to be substituted in for the parameters marks in the filter clause. |
1122 |
|
|
1123 |
=item RETURN |
=item field |
1124 |
|
|
1125 |
Returns a statistics object indicating how many records of each particular table were |
Name of the field to be used to get the elements of the list returned. |
1126 |
deleted. |
|
1127 |
|
=item RETURN |
1128 |
|
|
1129 |
|
Returns a list of values. |
1130 |
|
|
1131 |
|
=back |
1132 |
|
|
1133 |
|
=cut |
1134 |
|
#: Return Type @; |
1135 |
|
sub GetFlat { |
1136 |
|
# Get the parameters. |
1137 |
|
my ($self, $objectNames, $filterClause, $parameterList, $field) = @_; |
1138 |
|
# Construct the query. |
1139 |
|
my $query = $self->Get($objectNames, $filterClause, $parameterList); |
1140 |
|
# Create the result list. |
1141 |
|
my @retVal = (); |
1142 |
|
# Loop through the records, adding the field values found to the result list. |
1143 |
|
while (my $row = $query->Fetch()) { |
1144 |
|
push @retVal, $row->Value($field); |
1145 |
|
} |
1146 |
|
# Return the list created. |
1147 |
|
return @retVal; |
1148 |
|
} |
1149 |
|
|
1150 |
|
=head3 Delete |
1151 |
|
|
1152 |
|
C<< my $stats = $erdb->Delete($entityName, $objectID); >> |
1153 |
|
|
1154 |
|
Delete an entity instance from the database. The instance is deleted along with all entity and |
1155 |
|
relationship instances dependent on it. The idea of dependence here is recursive. An object is |
1156 |
|
always dependent on itself. An object is dependent if it is a 1-to-many or many-to-many |
1157 |
|
relationship connected to a dependent entity or the "to" entity connected to a 1-to-many |
1158 |
|
dependent relationship. |
1159 |
|
|
1160 |
|
=over 4 |
1161 |
|
|
1162 |
|
=item entityName |
1163 |
|
|
1164 |
|
Name of the entity type for the instance being deleted. |
1165 |
|
|
1166 |
|
=item objectID |
1167 |
|
|
1168 |
|
ID of the entity instance to be deleted. If the ID contains a wild card character (C<%>), |
1169 |
|
then it is presumed to by a LIKE pattern. |
1170 |
|
|
1171 |
|
=item testFlag |
1172 |
|
|
1173 |
|
If TRUE, the delete statements will be traced without being executed. |
1174 |
|
|
1175 |
|
=item RETURN |
1176 |
|
|
1177 |
|
Returns a statistics object indicating how many records of each particular table were |
1178 |
|
deleted. |
1179 |
|
|
1180 |
=back |
=back |
1181 |
|
|
1317 |
|
|
1318 |
=head3 GetList |
=head3 GetList |
1319 |
|
|
1320 |
C<< my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, $param1, $param2, ..., $paramN); >> |
C<< my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, \@params); >> |
1321 |
|
|
1322 |
Return a list of object descriptors for the specified objects as determined by the |
Return a list of object descriptors for the specified objects as determined by the |
1323 |
specified filter clause. |
specified filter clause. |
1351 |
filter clause in general; however, odd things may happen if a sort field is from a secondary |
filter clause in general; however, odd things may happen if a sort field is from a secondary |
1352 |
relation. |
relation. |
1353 |
|
|
1354 |
=item param1, param2, ..., paramN |
=item params |
1355 |
|
|
1356 |
Parameter values to be substituted into the filter clause. |
Reference to a list of parameter values to be substituted into the filter clause. |
1357 |
|
|
1358 |
=item RETURN |
=item RETURN |
1359 |
|
|
1365 |
#: Return Type @% |
#: Return Type @% |
1366 |
sub GetList { |
sub GetList { |
1367 |
# Get the parameters. |
# Get the parameters. |
1368 |
my ($self, $objectNames, $filterClause, @params) = @_; |
my ($self, $objectNames, $filterClause, $params) = @_; |
1369 |
# Declare the return variable. |
# Declare the return variable. |
1370 |
my @retVal = (); |
my @retVal = (); |
1371 |
# Perform the query. |
# Perform the query. |
1372 |
my $query = $self->Get($objectNames, $filterClause, @params); |
my $query = $self->Get($objectNames, $filterClause, $params); |
1373 |
# Loop through the results. |
# Loop through the results. |
1374 |
while (my $object = $query->Fetch) { |
while (my $object = $query->Fetch) { |
1375 |
push @retVal, $object; |
push @retVal, $object; |
1378 |
return @retVal; |
return @retVal; |
1379 |
} |
} |
1380 |
|
|
1381 |
|
=head3 GetCount |
1382 |
|
|
1383 |
|
C<< my $count = $erdb->GetCount(\@objectNames, $filter, \@params); >> |
1384 |
|
|
1385 |
|
Return the number of rows found by a specified query. This method would |
1386 |
|
normally be used to count the records in a single table. For example, in a |
1387 |
|
genetics database |
1388 |
|
|
1389 |
|
my $count = $erdb->GetCount(['Genome'], 'Genome(genus-species) LIKE ?', ['homo %']); |
1390 |
|
|
1391 |
|
would return the number of genomes for the genus I<homo>. It is conceivable, however, |
1392 |
|
to use it to return records based on a join. For example, |
1393 |
|
|
1394 |
|
my $count = $erdb->GetCount(['HasFeature', 'Genome'], 'Genome(genus-species) LIKE ?', |
1395 |
|
['homo %']); |
1396 |
|
|
1397 |
|
would return the number of features for genomes in the genus I<homo>. Note that |
1398 |
|
only the rows from the first table are counted. If the above command were |
1399 |
|
|
1400 |
|
my $count = $erdb->GetCount(['Genome', 'Feature'], 'Genome(genus-species) LIKE ?', |
1401 |
|
['homo %']); |
1402 |
|
|
1403 |
|
it would return the number of genomes, not the number of genome/feature pairs. |
1404 |
|
|
1405 |
|
=over 4 |
1406 |
|
|
1407 |
|
=item objectNames |
1408 |
|
|
1409 |
|
Reference to a list of the objects (entities and relationships) included in the |
1410 |
|
query. |
1411 |
|
|
1412 |
|
=item filter |
1413 |
|
|
1414 |
|
A filter clause for restricting the query. The rules are the same as for the L</Get> |
1415 |
|
method. |
1416 |
|
|
1417 |
|
=item params |
1418 |
|
|
1419 |
|
Reference to a list of the parameter values to be substituted for the parameter marks |
1420 |
|
in the filter. |
1421 |
|
|
1422 |
|
=item RETURN |
1423 |
|
|
1424 |
|
Returns a count of the number of records in the first table that would satisfy |
1425 |
|
the query. |
1426 |
|
|
1427 |
|
=back |
1428 |
|
|
1429 |
|
=cut |
1430 |
|
|
1431 |
|
sub GetCount { |
1432 |
|
# Get the parameters. |
1433 |
|
my ($self, $objectNames, $filter, $params) = @_; |
1434 |
|
# Declare the return variable. |
1435 |
|
my $retVal; |
1436 |
|
# Find out if we're counting an entity or a relationship. |
1437 |
|
my $countedField; |
1438 |
|
if ($self->IsEntity($objectNames->[0])) { |
1439 |
|
$countedField = "id"; |
1440 |
|
} else { |
1441 |
|
# For a relationship we count the to-link because it's usually more |
1442 |
|
# numerous. Note we're automatically converting to the SQL form |
1443 |
|
# of the field name (to_link vs. to-link). |
1444 |
|
$countedField = "to_link"; |
1445 |
|
} |
1446 |
|
# Create the SQL command suffix to get the desired records. |
1447 |
|
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = $self->_SetupSQL($objectNames, |
1448 |
|
$filter); |
1449 |
|
# Prefix it with text telling it we want a record count. |
1450 |
|
my $firstObject = $mappedNameListRef->[0]; |
1451 |
|
my $command = "SELECT COUNT($firstObject.$countedField) $suffix"; |
1452 |
|
# Prepare and execute the command. |
1453 |
|
my $sth = $self->_GetStatementHandle($command, $params); |
1454 |
|
# Get the count value. |
1455 |
|
($retVal) = $sth->fetchrow_array(); |
1456 |
|
# Check for a problem. |
1457 |
|
if (! defined($retVal)) { |
1458 |
|
if ($sth->err) { |
1459 |
|
# Here we had an SQL error. |
1460 |
|
Confess("Error retrieving row count: " . $sth->errstr()); |
1461 |
|
} else { |
1462 |
|
# Here we have no result. |
1463 |
|
Confess("No result attempting to retrieve row count."); |
1464 |
|
} |
1465 |
|
} |
1466 |
|
# Return the result. |
1467 |
|
return $retVal; |
1468 |
|
} |
1469 |
|
|
1470 |
=head3 ComputeObjectSentence |
=head3 ComputeObjectSentence |
1471 |
|
|
1472 |
C<< my $sentence = $erdb->ComputeObjectSentence($objectName); >> |
C<< my $sentence = $erdb->ComputeObjectSentence($objectName); >> |
1862 |
# Get the parameters. |
# Get the parameters. |
1863 |
my ($self, $entityType, $ID) = @_; |
my ($self, $entityType, $ID) = @_; |
1864 |
# Create a query. |
# Create a query. |
1865 |
my $query = $self->Get([$entityType], "$entityType(id) = ?", $ID); |
my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]); |
1866 |
# Get the first (and only) object. |
# Get the first (and only) object. |
1867 |
my $retVal = $query->Fetch(); |
my $retVal = $query->Fetch(); |
1868 |
# Return the result. |
# Return the result. |
1975 |
# list is a scalar we convert it into a singleton list. |
# list is a scalar we convert it into a singleton list. |
1976 |
my @parmList = (); |
my @parmList = (); |
1977 |
if (ref $parameterList eq "ARRAY") { |
if (ref $parameterList eq "ARRAY") { |
1978 |
|
Trace("GetAll parm list is an array.") if T(4); |
1979 |
@parmList = @{$parameterList}; |
@parmList = @{$parameterList}; |
1980 |
} else { |
} else { |
1981 |
|
Trace("GetAll parm list is a scalar: $parameterList.") if T(4); |
1982 |
push @parmList, $parameterList; |
push @parmList, $parameterList; |
1983 |
} |
} |
1984 |
# Insure the counter has a value. |
# Insure the counter has a value. |
1990 |
$filterClause .= " LIMIT $count"; |
$filterClause .= " LIMIT $count"; |
1991 |
} |
} |
1992 |
# Create the query. |
# Create the query. |
1993 |
my $query = $self->Get($objectNames, $filterClause, @parmList); |
my $query = $self->Get($objectNames, $filterClause, \@parmList); |
1994 |
# Set up a counter of the number of records read. |
# Set up a counter of the number of records read. |
1995 |
my $fetched = 0; |
my $fetched = 0; |
1996 |
# Loop through the records returned, extracting the fields. Note that if the |
# Loop through the records returned, extracting the fields. Note that if the |
2072 |
return $objectData->{Fields}; |
return $objectData->{Fields}; |
2073 |
} |
} |
2074 |
|
|
2075 |
|
=head2 Data Mining Methods |
2076 |
|
|
2077 |
=head3 GetUsefulCrossValues |
=head3 GetUsefulCrossValues |
2078 |
|
|
2079 |
C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >> |
C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >> |
2135 |
return @retVal; |
return @retVal; |
2136 |
} |
} |
2137 |
|
|
2138 |
|
=head3 FindColumn |
2139 |
|
|
2140 |
|
C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >> |
2141 |
|
|
2142 |
|
Return the location a desired column in a data mining header line. The data |
2143 |
|
mining header line is a tab-separated list of column names. The column |
2144 |
|
identifier is either the numerical index of a column or the actual column |
2145 |
|
name. |
2146 |
|
|
2147 |
|
=over 4 |
2148 |
|
|
2149 |
|
=item headerLine |
2150 |
|
|
2151 |
|
The header line from a data mining command, which consists of a tab-separated |
2152 |
|
list of column names. |
2153 |
|
|
2154 |
|
=item columnIdentifier |
2155 |
|
|
2156 |
|
Either the ordinal number of the desired column (1-based), or the name of the |
2157 |
|
desired column. |
2158 |
|
|
2159 |
|
=item RETURN |
2160 |
|
|
2161 |
|
Returns the array index (0-based) of the desired column. |
2162 |
|
|
2163 |
|
=back |
2164 |
|
|
2165 |
|
=cut |
2166 |
|
|
2167 |
|
sub FindColumn { |
2168 |
|
# Get the parameters. |
2169 |
|
my ($headerLine, $columnIdentifier) = @_; |
2170 |
|
# Declare the return variable. |
2171 |
|
my $retVal; |
2172 |
|
# Split the header line into column names. |
2173 |
|
my @headers = ParseColumns($headerLine); |
2174 |
|
# Determine whether we have a number or a name. |
2175 |
|
if ($columnIdentifier =~ /^\d+$/) { |
2176 |
|
# Here we have a number. Subtract 1 and validate the result. |
2177 |
|
$retVal = $columnIdentifier - 1; |
2178 |
|
if ($retVal < 0 || $retVal > $#headers) { |
2179 |
|
Confess("Invalid column identifer \"$columnIdentifier\": value out of range."); |
2180 |
|
} |
2181 |
|
} else { |
2182 |
|
# Here we have a name. We need to find it in the list. |
2183 |
|
for (my $i = 0; $i <= $#headers && ! defined($retVal); $i++) { |
2184 |
|
if ($headers[$i] eq $columnIdentifier) { |
2185 |
|
$retVal = $i; |
2186 |
|
} |
2187 |
|
} |
2188 |
|
if (! defined($retVal)) { |
2189 |
|
Confess("Invalid column identifier \"$columnIdentifier\": value not found."); |
2190 |
|
} |
2191 |
|
} |
2192 |
|
# Return the result. |
2193 |
|
return $retVal; |
2194 |
|
} |
2195 |
|
|
2196 |
|
=head3 ParseColumns |
2197 |
|
|
2198 |
|
C<< my @columns = ERDB->ParseColumns($line); >> |
2199 |
|
|
2200 |
|
Convert the specified data line to a list of columns. |
2201 |
|
|
2202 |
|
=over 4 |
2203 |
|
|
2204 |
|
=item line |
2205 |
|
|
2206 |
|
A data mining input, consisting of a tab-separated list of columns terminated by a |
2207 |
|
new-line. |
2208 |
|
|
2209 |
|
=item RETURN |
2210 |
|
|
2211 |
|
Returns a list consisting of the column values. |
2212 |
|
|
2213 |
|
=back |
2214 |
|
|
2215 |
|
=cut |
2216 |
|
|
2217 |
|
sub ParseColumns { |
2218 |
|
# Get the parameters. |
2219 |
|
my ($self, $line) = @_; |
2220 |
|
# Chop off the line-end. |
2221 |
|
chomp $line; |
2222 |
|
# Split it into a list. |
2223 |
|
my @retVal = split(/\t/, $line); |
2224 |
|
# Return the result. |
2225 |
|
return @retVal; |
2226 |
|
} |
2227 |
|
|
2228 |
=head2 Internal Utility Methods |
=head2 Internal Utility Methods |
2229 |
|
|
2230 |
|
=head3 SetupSQL |
2231 |
|
|
2232 |
|
Process a list of object names and a filter clause so that they can be used to |
2233 |
|
build an SQL statement. This method takes in a reference to a list of object names |
2234 |
|
and a filter clause. It will return a corrected filter clause, a list of mapped |
2235 |
|
names and the mapped name hash. |
2236 |
|
|
2237 |
|
This is an instance method. |
2238 |
|
|
2239 |
|
=over 4 |
2240 |
|
|
2241 |
|
=item objectNames |
2242 |
|
|
2243 |
|
Reference to a list of the object names to be included in the query. |
2244 |
|
|
2245 |
|
=item filterClause |
2246 |
|
|
2247 |
|
A string containing the WHERE clause for the query (without the C<WHERE>) and also |
2248 |
|
optionally the C<ORDER BY> and C<LIMIT> clauses. |
2249 |
|
|
2250 |
|
=item RETURN |
2251 |
|
|
2252 |
|
Returns a three-element list. The first element is the SQL statement suffix, beginning |
2253 |
|
with the FROM clause. The second element is a reference to a list of the names to be |
2254 |
|
used in retrieving the fields. The third element is a hash mapping the names to the |
2255 |
|
objects they represent. |
2256 |
|
|
2257 |
|
=back |
2258 |
|
|
2259 |
|
=cut |
2260 |
|
|
2261 |
|
sub _SetupSQL { |
2262 |
|
my ($self, $objectNames, $filterClause) = @_; |
2263 |
|
# Adjust the list of object names to account for multiple occurrences of the |
2264 |
|
# same object. We start with a hash table keyed on object name that will |
2265 |
|
# return the object suffix. The first time an object is encountered it will |
2266 |
|
# not be found in the hash. The next time the hash will map the object name |
2267 |
|
# to 2, then 3, and so forth. |
2268 |
|
my %objectHash = (); |
2269 |
|
# This list will contain the object names as they are to appear in the |
2270 |
|
# FROM list. |
2271 |
|
my @fromList = (); |
2272 |
|
# This list contains the suffixed object name for each object. It is exactly |
2273 |
|
# parallel to the list in the $objectNames parameter. |
2274 |
|
my @mappedNameList = (); |
2275 |
|
# Finally, this hash translates from a mapped name to its original object name. |
2276 |
|
my %mappedNameHash = (); |
2277 |
|
# Now we create the lists. Note that for every single name we push something into |
2278 |
|
# @fromList and @mappedNameList. This insures that those two arrays are exactly |
2279 |
|
# parallel to $objectNames. |
2280 |
|
for my $objectName (@{$objectNames}) { |
2281 |
|
# Get the next suffix for this object. |
2282 |
|
my $suffix = $objectHash{$objectName}; |
2283 |
|
if (! $suffix) { |
2284 |
|
# Here we are seeing the object for the first time. The object name |
2285 |
|
# is used as is. |
2286 |
|
push @mappedNameList, $objectName; |
2287 |
|
push @fromList, $objectName; |
2288 |
|
$mappedNameHash{$objectName} = $objectName; |
2289 |
|
# Denote the next suffix will be 2. |
2290 |
|
$objectHash{$objectName} = 2; |
2291 |
|
} else { |
2292 |
|
# Here we've seen the object before. We construct a new name using |
2293 |
|
# the suffix from the hash and update the hash. |
2294 |
|
my $mappedName = "$objectName$suffix"; |
2295 |
|
$objectHash{$objectName} = $suffix + 1; |
2296 |
|
# The FROM list has the object name followed by the mapped name. This |
2297 |
|
# tells SQL it's still the same table, but we're using a different name |
2298 |
|
# for it to avoid confusion. |
2299 |
|
push @fromList, "$objectName $mappedName"; |
2300 |
|
# The mapped-name list contains the real mapped name. |
2301 |
|
push @mappedNameList, $mappedName; |
2302 |
|
# Finally, enable us to get back from the mapped name to the object name. |
2303 |
|
$mappedNameHash{$mappedName} = $objectName; |
2304 |
|
} |
2305 |
|
} |
2306 |
|
# Begin the SELECT suffix. It starts with |
2307 |
|
# |
2308 |
|
# FROM name1, name2, ... nameN |
2309 |
|
# |
2310 |
|
my $suffix = "FROM " . join(', ', @fromList); |
2311 |
|
# Check for a filter clause. |
2312 |
|
if ($filterClause) { |
2313 |
|
# Here we have one, so we convert its field names and add it to the query. First, |
2314 |
|
# We create a copy of the filter string we can work with. |
2315 |
|
my $filterString = $filterClause; |
2316 |
|
# Next, we sort the object names by length. This helps protect us from finding |
2317 |
|
# object names inside other object names when we're doing our search and replace. |
2318 |
|
my @sortedNames = sort { length($b) - length($a) } @mappedNameList; |
2319 |
|
# We will also keep a list of conditions to add to the WHERE clause in order to link |
2320 |
|
# entities and relationships as well as primary relations to secondary ones. |
2321 |
|
my @joinWhere = (); |
2322 |
|
# The final preparatory step is to create a hash table of relation names. The |
2323 |
|
# table begins with the relation names already in the SELECT command. We may |
2324 |
|
# need to add relations later if there is filtering on a field in a secondary |
2325 |
|
# relation. The secondary relations are the ones that contain multiply- |
2326 |
|
# occurring or optional fields. |
2327 |
|
my %fromNames = map { $_ => 1 } @sortedNames; |
2328 |
|
# We are ready to begin. We loop through the object names, replacing each |
2329 |
|
# object name's field references by the corresponding SQL field reference. |
2330 |
|
# Along the way, if we find a secondary relation, we will need to add it |
2331 |
|
# to the FROM clause. |
2332 |
|
for my $mappedName (@sortedNames) { |
2333 |
|
# Get the length of the object name plus 2. This is the value we add to the |
2334 |
|
# size of the field name to determine the size of the field reference as a |
2335 |
|
# whole. |
2336 |
|
my $nameLength = 2 + length $mappedName; |
2337 |
|
# Get the real object name for this mapped name. |
2338 |
|
my $objectName = $mappedNameHash{$mappedName}; |
2339 |
|
Trace("Processing $mappedName for object $objectName.") if T(4); |
2340 |
|
# Get the object's field list. |
2341 |
|
my $fieldList = $self->GetFieldTable($objectName); |
2342 |
|
# Find the field references for this object. |
2343 |
|
while ($filterString =~ m/$mappedName\(([^)]*)\)/g) { |
2344 |
|
# At this point, $1 contains the field name, and the current position |
2345 |
|
# is set immediately after the final parenthesis. We pull out the name of |
2346 |
|
# the field and the position and length of the field reference as a whole. |
2347 |
|
my $fieldName = $1; |
2348 |
|
my $len = $nameLength + length $fieldName; |
2349 |
|
my $pos = pos($filterString) - $len; |
2350 |
|
# Insure the field exists. |
2351 |
|
if (!exists $fieldList->{$fieldName}) { |
2352 |
|
Confess("Field $fieldName not found for object $objectName."); |
2353 |
|
} else { |
2354 |
|
Trace("Processing $fieldName at position $pos.") if T(4); |
2355 |
|
# Get the field's relation. |
2356 |
|
my $relationName = $fieldList->{$fieldName}->{relation}; |
2357 |
|
# Now we have a secondary relation. We need to insure it matches the |
2358 |
|
# mapped name of the primary relation. First we peel off the suffix |
2359 |
|
# from the mapped name. |
2360 |
|
my $mappingSuffix = substr $mappedName, length($objectName); |
2361 |
|
# Put the mapping suffix onto the relation name to get the |
2362 |
|
# mapped relation name. |
2363 |
|
my $mappedRelationName = "$relationName$mappingSuffix"; |
2364 |
|
# Insure the relation is in the FROM clause. |
2365 |
|
if (!exists $fromNames{$mappedRelationName}) { |
2366 |
|
# Add the relation to the FROM clause. |
2367 |
|
if ($mappedRelationName eq $relationName) { |
2368 |
|
# The name is un-mapped, so we add it without |
2369 |
|
# any frills. |
2370 |
|
$suffix .= ", $relationName"; |
2371 |
|
push @joinWhere, "$objectName.id = $relationName.id"; |
2372 |
|
} else { |
2373 |
|
# Here we have a mapping situation. |
2374 |
|
$suffix .= ", $relationName $mappedRelationName"; |
2375 |
|
push @joinWhere, "$mappedRelationName.id = $mappedName.id"; |
2376 |
|
} |
2377 |
|
# Denote we have this relation available for future fields. |
2378 |
|
$fromNames{$mappedRelationName} = 1; |
2379 |
|
} |
2380 |
|
# Form an SQL field reference from the relation name and the field name. |
2381 |
|
my $sqlReference = "$mappedRelationName." . _FixName($fieldName); |
2382 |
|
# Put it into the filter string in place of the old value. |
2383 |
|
substr($filterString, $pos, $len) = $sqlReference; |
2384 |
|
# Reposition the search. |
2385 |
|
pos $filterString = $pos + length $sqlReference; |
2386 |
|
} |
2387 |
|
} |
2388 |
|
} |
2389 |
|
# The next step is to join the objects together. We only need to do this if there |
2390 |
|
# is more than one object in the object list. We start with the first object and |
2391 |
|
# run through the objects after it. Note also that we make a safety copy of the |
2392 |
|
# list before running through it. |
2393 |
|
my @mappedObjectList = @mappedNameList; |
2394 |
|
my $lastMappedObject = shift @mappedObjectList; |
2395 |
|
# Get the join table. |
2396 |
|
my $joinTable = $self->{_metaData}->{Joins}; |
2397 |
|
# Loop through the object list. |
2398 |
|
for my $thisMappedObject (@mappedObjectList) { |
2399 |
|
# Look for a join using the real object names. |
2400 |
|
my $lastObject = $mappedNameHash{$lastMappedObject}; |
2401 |
|
my $thisObject = $mappedNameHash{$thisMappedObject}; |
2402 |
|
my $joinKey = "$lastObject/$thisObject"; |
2403 |
|
if (!exists $joinTable->{$joinKey}) { |
2404 |
|
# Here there's no join, so we throw an error. |
2405 |
|
Confess("No join exists to connect from $lastMappedObject to $thisMappedObject."); |
2406 |
|
} else { |
2407 |
|
# Get the join clause. |
2408 |
|
my $unMappedJoin = $joinTable->{$joinKey}; |
2409 |
|
# Fix the names. |
2410 |
|
$unMappedJoin =~ s/$lastObject/$lastMappedObject/; |
2411 |
|
$unMappedJoin =~ s/$thisObject/$thisMappedObject/; |
2412 |
|
push @joinWhere, $unMappedJoin; |
2413 |
|
# Save this object as the last object for the next iteration. |
2414 |
|
$lastMappedObject = $thisMappedObject; |
2415 |
|
} |
2416 |
|
} |
2417 |
|
# Now we need to handle the whole ORDER BY / LIMIT thing. The important part |
2418 |
|
# here is we want the filter clause to be empty if there's no WHERE filter. |
2419 |
|
# We'll put the ORDER BY / LIMIT clauses in the following variable. |
2420 |
|
my $orderClause = ""; |
2421 |
|
# Locate the ORDER BY or LIMIT verbs (if any). We use a non-greedy |
2422 |
|
# operator so that we find the first occurrence of either verb. |
2423 |
|
if ($filterString =~ m/^(.*?)\s*(ORDER BY|LIMIT)/g) { |
2424 |
|
# Here we have an ORDER BY or LIMIT verb. Split it off of the filter string. |
2425 |
|
my $pos = pos $filterString; |
2426 |
|
$orderClause = $2 . substr($filterString, $pos); |
2427 |
|
$filterString = $1; |
2428 |
|
} |
2429 |
|
# Add the filter and the join clauses (if any) to the SELECT command. |
2430 |
|
if ($filterString) { |
2431 |
|
Trace("Filter string is \"$filterString\".") if T(4); |
2432 |
|
push @joinWhere, "($filterString)"; |
2433 |
|
} |
2434 |
|
if (@joinWhere) { |
2435 |
|
$suffix .= " WHERE " . join(' AND ', @joinWhere); |
2436 |
|
} |
2437 |
|
# Add the sort or limit clause (if any) to the SELECT command. |
2438 |
|
if ($orderClause) { |
2439 |
|
$suffix .= " $orderClause"; |
2440 |
|
} |
2441 |
|
} |
2442 |
|
# Return the suffix, the mapped name list, and the mapped name hash. |
2443 |
|
return ($suffix, \@mappedNameList, \%mappedNameHash); |
2444 |
|
} |
2445 |
|
|
2446 |
|
=head3 GetStatementHandle |
2447 |
|
|
2448 |
|
This method will prepare and execute an SQL query, returning the statement handle. |
2449 |
|
The main reason for doing this here is so that everybody who does SQL queries gets |
2450 |
|
the benefit of tracing. |
2451 |
|
|
2452 |
|
This is an instance method. |
2453 |
|
|
2454 |
|
=over 4 |
2455 |
|
|
2456 |
|
=item command |
2457 |
|
|
2458 |
|
Command to prepare and execute. |
2459 |
|
|
2460 |
|
=item params |
2461 |
|
|
2462 |
|
Reference to a list of the values to be substituted in for the parameter marks. |
2463 |
|
|
2464 |
|
=item RETURN |
2465 |
|
|
2466 |
|
Returns a prepared and executed statement handle from which the caller can extract |
2467 |
|
results. |
2468 |
|
|
2469 |
|
=back |
2470 |
|
|
2471 |
|
=cut |
2472 |
|
|
2473 |
|
sub _GetStatementHandle { |
2474 |
|
# Get the parameters. |
2475 |
|
my ($self, $command, $params) = @_; |
2476 |
|
# Trace the query. |
2477 |
|
Trace("SQL query: $command") if T(SQL => 3); |
2478 |
|
Trace("PARMS: '" . (join "', '", @{$params}) . "'") if (T(SQL => 4) && (@{$params} > 0)); |
2479 |
|
# Get the database handle. |
2480 |
|
my $dbh = $self->{_dbh}; |
2481 |
|
# Prepare the command. |
2482 |
|
my $sth = $dbh->prepare_command($command); |
2483 |
|
# Execute it with the parameters bound in. |
2484 |
|
$sth->execute(@{$params}) || Confess("SELECT error" . $sth->errstr()); |
2485 |
|
# Return the statement handle. |
2486 |
|
return $sth; |
2487 |
|
} |
2488 |
|
|
2489 |
=head3 GetLoadStats |
=head3 GetLoadStats |
2490 |
|
|
2491 |
Return a blank statistics object for use by the load methods. |
Return a blank statistics object for use by the load methods. |