6 |
use Data::Dumper; |
use Data::Dumper; |
7 |
use XML::Simple; |
use XML::Simple; |
8 |
use DBQuery; |
use DBQuery; |
9 |
use DBObject; |
use ERDBObject; |
10 |
use Stats; |
use Stats; |
11 |
use Time::HiRes qw(gettimeofday); |
use Time::HiRes qw(gettimeofday); |
12 |
use Digest::MD5 qw(md5_base64); |
use Digest::MD5 qw(md5_base64); |
|
use FIG; |
|
13 |
use CGI; |
use CGI; |
14 |
|
use WikiTools; |
15 |
|
|
16 |
=head1 Entity-Relationship Database Package |
=head1 Entity-Relationship Database Package |
17 |
|
|
373 |
'medium-string' => |
'medium-string' => |
374 |
{ sqlType => 'VARCHAR(160)', maxLen => 160, avgLen => 40, sort => "", |
{ sqlType => 'VARCHAR(160)', maxLen => 160, avgLen => 40, sort => "", |
375 |
indexMod => 0, notes => "character string, 0 to 160 characters"}, |
indexMod => 0, notes => "character string, 0 to 160 characters"}, |
376 |
|
'long-string' => |
377 |
|
{ sqlType => 'VARCHAR(500)', maxLen => 500, avglen => 255, sort => "", |
378 |
|
indexMod => 0, notes => "character string, 0 to 500 characters"}, |
379 |
); |
); |
380 |
|
|
381 |
# Table translating arities into natural language. |
# Table translating arities into natural language. |
390 |
Entities => 'Entity', |
Entities => 'Entity', |
391 |
Fields => 'Field', |
Fields => 'Field', |
392 |
Indexes => 'Index', |
Indexes => 'Index', |
393 |
IndexFields => 'IndexField' |
IndexFields => 'IndexField', |
394 |
|
Issues => 'Issue', |
395 |
|
Shapes => 'Shape' |
396 |
}, |
}, |
397 |
KeyAttr => { Relationship => 'name', |
KeyAttr => { Relationship => 'name', |
398 |
Entity => 'name', |
Entity => 'name', |
399 |
Field => 'name' |
Field => 'name', |
400 |
|
Shape => 'name' |
401 |
}, |
}, |
402 |
SuppressEmpty => 1, |
SuppressEmpty => 1, |
403 |
); |
); |
404 |
|
|
405 |
my %XmlInOpts = ( |
my %XmlInOpts = ( |
406 |
ForceArray => ['Field', 'Index', 'IndexField', 'Relationship', 'Entity'], |
ForceArray => [qw(Field Index IndexField Relationship Entity Shape)], |
407 |
ForceContent => 1, |
ForceContent => 1, |
408 |
NormalizeSpace => 2, |
NormalizeSpace => 2, |
409 |
); |
); |
412 |
XMLDecl => 1, |
XMLDecl => 1, |
413 |
); |
); |
414 |
|
|
|
|
|
415 |
=head2 Public Methods |
=head2 Public Methods |
416 |
|
|
417 |
=head3 new |
=head3 new |
418 |
|
|
419 |
C<< my $database = ERDB->new($dbh, $metaFileName); >> |
my $database = ERDB->new($dbh, $metaFileName); |
420 |
|
|
421 |
Create a new ERDB object. |
Create a new ERDB object. |
422 |
|
|
436 |
|
|
437 |
sub new { |
sub new { |
438 |
# Get the parameters. |
# Get the parameters. |
439 |
my ($class, $dbh, $metaFileName, $options) = @_; |
my ($class, $dbh, $metaFileName, %options) = @_; |
440 |
# Load the meta-data. |
# Load the meta-data. |
441 |
my $metaData = _LoadMetaData($metaFileName); |
my $metaData = _LoadMetaData($metaFileName); |
442 |
# Create the object. |
# Create the object. |
450 |
|
|
451 |
=head3 ShowMetaData |
=head3 ShowMetaData |
452 |
|
|
453 |
C<< $erdb->ShowMetaData($fileName); >> |
$erdb->ShowMetaData($fileName); |
454 |
|
|
455 |
This method outputs a description of the database. This description can be used to help users create |
This method outputs a description of the database. This description can be used to help users create |
456 |
the data to be loaded into the relations. |
the data to be loaded into the relations. |
491 |
|
|
492 |
=head3 DisplayMetaData |
=head3 DisplayMetaData |
493 |
|
|
494 |
C<< my $html = $erdb->DisplayMetaData(); >> |
my $html = $erdb->DisplayMetaData(); |
495 |
|
|
496 |
Return an HTML description of the database. This description can be used to help users create |
Return an HTML description of the database. This description can be used to help users create |
497 |
the data to be loaded into the relations and form queries. The output is raw includable HTML |
the data to be loaded into the relations and form queries. The output is raw includable HTML |
647 |
|
|
648 |
=head3 DumpMetaData |
=head3 DumpMetaData |
649 |
|
|
650 |
C<< $erdb->DumpMetaData(); >> |
$erdb->DumpMetaData(); |
651 |
|
|
652 |
Return a dump of the metadata structure. |
Return a dump of the metadata structure. |
653 |
|
|
660 |
return Data::Dumper::Dumper($self->{_metaData}); |
return Data::Dumper::Dumper($self->{_metaData}); |
661 |
} |
} |
662 |
|
|
663 |
|
=head3 GenerateWikiData |
664 |
|
|
665 |
|
my @wikiLines = $erdb->GenerateWikiData(); |
666 |
|
|
667 |
|
Build a description of the database for the wiki. The database will be |
668 |
|
organized into a single page, with sections for each entity and relationship. |
669 |
|
The return value is a list of text lines. |
670 |
|
|
671 |
|
=cut |
672 |
|
|
673 |
|
sub GenerateWikiData { |
674 |
|
# Get the parameters. |
675 |
|
my ($self) = @_; |
676 |
|
# We'll build the wiki text in here. |
677 |
|
my @retVal = (); |
678 |
|
# Get the metadata object. |
679 |
|
my $metadata = $self->{_metaData}; |
680 |
|
# Get the title string. This will become the page name. |
681 |
|
my $title = $metadata->{Title}->{content}; |
682 |
|
# Get the entity and relationship lists. |
683 |
|
my $entityList = $metadata->{Entities}; |
684 |
|
my $relationshipList = $metadata->{Relationships}; |
685 |
|
my $shapeList = $metadata->{Shapes}; |
686 |
|
# Start with the introductory text. |
687 |
|
push @retVal, WikiTools::Heading(2, "Introduction"); |
688 |
|
if (my $notes = $metadata->{Notes}) { |
689 |
|
push @retVal, WikiNote($notes->{content}); |
690 |
|
} |
691 |
|
# Generate issue list. |
692 |
|
if (my $issues = $metadata->{Issues}) { |
693 |
|
push @retVal, WikiTools::Heading(3, 'Issues'); |
694 |
|
push @retVal, WikiTools::List(map { $_->{content} } @{$issues}); |
695 |
|
} |
696 |
|
# Start the entity section. |
697 |
|
push @retVal, WikiTools::Heading(2, "Entities"); |
698 |
|
# Loop through the entities. Note that unlike the situation with HTML, we |
699 |
|
# don't need to generate the table of contents manually, just the data |
700 |
|
# itself. |
701 |
|
for my $key (sort keys %$entityList) { |
702 |
|
# Create a header for this entity. |
703 |
|
push @retVal, "", WikiTools::Heading(3, $key); |
704 |
|
# Get the entity data. |
705 |
|
my $entityData = $entityList->{$key}; |
706 |
|
# Plant the notes here, if there are any. |
707 |
|
push @retVal, _ObjectNotes($entityData); |
708 |
|
# Now we list the entity's relationships (if any). First, we build a list |
709 |
|
# of the relationships relevant to this entity. |
710 |
|
my @rels = (); |
711 |
|
for my $rel (sort keys %$relationshipList) { |
712 |
|
my $relStructure = $relationshipList->{$rel}; |
713 |
|
if ($relStructure->{from} eq $key || $relStructure->{to} eq $key) { |
714 |
|
# Get the relationship sentence. |
715 |
|
my $relSentence = _ComputeRelationshipSentence($rel, $relStructure); |
716 |
|
# Linkify it. |
717 |
|
my $linkedRel = WikiTools::LinkMarkup("#$rel", $rel); |
718 |
|
$relSentence =~ s/$rel/$linkedRel/; |
719 |
|
push @rels, $relSentence; |
720 |
|
} |
721 |
|
} |
722 |
|
# Add the relationships as a Wiki list. |
723 |
|
push @retVal, WikiTools::List(@rels); |
724 |
|
# Get the entity's relations. |
725 |
|
my $relationList = $entityData->{Relations}; |
726 |
|
# Loop through the relations, displaying them. |
727 |
|
for my $relation (sort keys %{$relationList}) { |
728 |
|
my $wikiString = _WikiRelationTable($relation, $relationList->{$relation}); |
729 |
|
push @retVal, $wikiString; |
730 |
|
} |
731 |
|
} |
732 |
|
# Now the entities are documented. Next we do the relationships. |
733 |
|
push @retVal, WikiTools::Heading(2, "Relationships"); |
734 |
|
for my $key (sort keys %$relationshipList) { |
735 |
|
my $relationshipData = $relationshipList->{$key}; |
736 |
|
# Create the relationship heading. |
737 |
|
push @retVal, WikiTools::Heading(3, $key); |
738 |
|
# Describe the relationship arity. Note there's a bit of trickiness involving recursive |
739 |
|
# many-to-many relationships. In a normal many-to-many we use two sentences to describe |
740 |
|
# the arity (one for each direction). This is a bad idea for a recursive relationship, |
741 |
|
# since both sentences will say the same thing. |
742 |
|
my $arity = $relationshipData->{arity}; |
743 |
|
my $fromEntity = $relationshipData->{from}; |
744 |
|
my $toEntity = $relationshipData->{to}; |
745 |
|
my @listElements = (); |
746 |
|
my $boldCode = WikiTools::BoldCode(); |
747 |
|
if ($arity eq "11") { |
748 |
|
push @listElements, "Each $boldCode$fromEntity$boldCode relates to at most one $boldCode$toEntity$boldCode."; |
749 |
|
} else { |
750 |
|
push @listElements, "Each $boldCode$fromEntity$boldCode relates to multiple $boldCode${toEntity}s$boldCode."; |
751 |
|
if ($arity eq "MM" && $fromEntity ne $toEntity) { |
752 |
|
push @listElements, "Each $boldCode$toEntity$boldCode relates to multiple $boldCode${fromEntity}s$boldCode."; |
753 |
|
} |
754 |
|
} |
755 |
|
push @retVal, WikiTools::List(@listElements); |
756 |
|
# Plant the notes here, if there are any. |
757 |
|
push @retVal, _ObjectNotes($relationshipData); |
758 |
|
# Finally, the relationship table. |
759 |
|
my $wikiString = _WikiRelationTable($key, $relationshipData->{Relations}->{$key}); |
760 |
|
push @retVal, $wikiString; |
761 |
|
} |
762 |
|
# Now loop through the miscellaneous shapes. |
763 |
|
if ($shapeList) { |
764 |
|
push @retVal, WikiTools::Heading(2, "Miscellaneous"); |
765 |
|
for my $shape (sort keys %$shapeList) { |
766 |
|
push @retVal, WikiTools::Heading(3, $shape); |
767 |
|
my $shapeData = $shapeList->{$shape}; |
768 |
|
push @retVal, _ObjectNotes($shapeData); |
769 |
|
} |
770 |
|
} |
771 |
|
# All done. Return the lines. |
772 |
|
return @retVal; |
773 |
|
} |
774 |
|
|
775 |
|
|
776 |
|
=head3 CreatePPO |
777 |
|
|
778 |
|
ERDB::CreatePPO($erdbXMLFile, $ppoXMLFile); |
779 |
|
|
780 |
|
Create a PPO XML file from an ERDB data definition XML file. At the |
781 |
|
current time, the PPO XML file can be used to create a database with |
782 |
|
similar functionality. Eventually, the PPO will be able to use the |
783 |
|
created XML to access the live ERDB database. |
784 |
|
|
785 |
|
=over 4 |
786 |
|
|
787 |
|
=item erdbXMLFile |
788 |
|
|
789 |
|
Name of the XML data definition file for the ERDB database. This |
790 |
|
file must exist. |
791 |
|
|
792 |
|
=item ppoXMLFile |
793 |
|
|
794 |
|
Output file for the PPO XML definition. If this file exists, it |
795 |
|
will be overwritten. |
796 |
|
|
797 |
|
=back |
798 |
|
|
799 |
|
=cut |
800 |
|
|
801 |
|
sub CreatePPO { |
802 |
|
# Get the parameters. |
803 |
|
my ($erdbXMLFile, $ppoXMLFile) = @_; |
804 |
|
# First, we want to slurp in the ERDB XML file in its raw form. |
805 |
|
my $xml = ReadMetaXML($erdbXMLFile); |
806 |
|
# Create a variable to hold all of the objects in the PPO project. |
807 |
|
my @objects = (); |
808 |
|
# Get the relationship hash. |
809 |
|
my $relationships = $xml->{Relationships}; |
810 |
|
# Loop through the entities. |
811 |
|
my $entities = $xml->{Entities}; |
812 |
|
for my $entityName (keys %{$entities}) { |
813 |
|
# Get the entity's data structures. |
814 |
|
my $entityObject = $entities->{$entityName}; |
815 |
|
# We put the object's fields in here, according to their type. |
816 |
|
my (@object_refs, @scalars, @indexes, @arrays); |
817 |
|
# Create the ID field for the entity. We get the key type from the |
818 |
|
# entity object and compute the corresponding SQL type. |
819 |
|
my $type = $TypeTable{$entityObject->{keyType}}->{sqlType}; |
820 |
|
push @scalars, { label => 'id', type => $type }; |
821 |
|
# Loop through the entity fields. |
822 |
|
for my $fieldName ( keys %{$entityObject->{Fields}} ) { |
823 |
|
# Get the field object. |
824 |
|
my $fieldObject = $entityObject->{Fields}->{$fieldName}; |
825 |
|
# Convert it to a scalar tag. |
826 |
|
my $scalar = _CreatePPOField($fieldName, $fieldObject); |
827 |
|
# If we have a relation, this field is stored in an array. |
828 |
|
# otherwise, it is a scalar. The array tag has scalars |
829 |
|
# stored as an XML array. In ERDB, there is only ever one, |
830 |
|
# but PPO can have more. |
831 |
|
my $relation = $fieldObject->{relation}; |
832 |
|
if ($relation) { |
833 |
|
push @arrays, { scalar => [$scalar] }; |
834 |
|
} else { |
835 |
|
push @scalars, $scalar; |
836 |
|
} |
837 |
|
} |
838 |
|
# Loop through the relationships. If this entity is the to-entity |
839 |
|
# on a relationship of 1M arity, then it is implemented as a PPO |
840 |
|
# object reference. |
841 |
|
for my $relationshipName (keys %{$relationships}) { |
842 |
|
# Get the relationship data. |
843 |
|
my $relationshipData = $relationships->{$relationshipName}; |
844 |
|
# If we have a from for this entity and an arity of 1M, we |
845 |
|
# have an object reference. |
846 |
|
if ($relationshipData->{to} eq $entityName && |
847 |
|
$relationshipData->{arity} eq '1M') { |
848 |
|
# Build the object reference tag. |
849 |
|
push @object_refs, { label => $relationshipName, |
850 |
|
type => $relationshipData->{from} }; |
851 |
|
} |
852 |
|
} |
853 |
|
# Create the indexes. |
854 |
|
my $indexList = $entityObject->{Indexes}; |
855 |
|
push @indexes, map { _CreatePPOIndex($_) } @{$indexList}; |
856 |
|
# Build the object XML tree. |
857 |
|
my $object = { label => $entityName, |
858 |
|
object_ref => \@object_refs, |
859 |
|
scalar => \@scalars, |
860 |
|
index => \@indexes, |
861 |
|
array => \@arrays |
862 |
|
}; |
863 |
|
# Push the object onto the objects list. |
864 |
|
push @objects, $object; |
865 |
|
} |
866 |
|
# Loop through the relationships, searching for MMs. The 1Ms were |
867 |
|
# already handled by the entity search above. |
868 |
|
for my $relationshipName (keys %{$relationships}) { |
869 |
|
# Get this relationship's object. |
870 |
|
my $relationshipObject = $relationships->{$relationshipName}; |
871 |
|
# Only proceed if it's many-to-many. |
872 |
|
if ($relationshipObject->{arity} eq 'MM') { |
873 |
|
# Create the tag lists for the relationship object. |
874 |
|
my (@object_refs, @scalars, @indexes); |
875 |
|
# The relationship will be created as an object with object |
876 |
|
# references for its links to the participating entities. |
877 |
|
my %links = ( from_link => $relationshipObject->{from}, |
878 |
|
to_link => $relationshipObject->{to} ); |
879 |
|
for my $link (keys %links) { |
880 |
|
# Create an object_ref tag for this piece of the |
881 |
|
# relationship (from or to). |
882 |
|
my $object_ref = { label => $link, |
883 |
|
type => $links{$link} }; |
884 |
|
push @object_refs, $object_ref; |
885 |
|
} |
886 |
|
# Loop through the intersection data fields, creating scalar tags. |
887 |
|
# There are no fancy array tags in a relationship. |
888 |
|
for my $fieldName (keys %{$relationshipObject->{Fields}}) { |
889 |
|
my $fieldObject = $relationshipObject->{Fields}->{$fieldName}; |
890 |
|
push @scalars, _CreatePPOField($fieldName, $fieldObject); |
891 |
|
} |
892 |
|
# Finally, the indexes: currently we cannot support the to-index and |
893 |
|
# from-index in PPO, so we just process the alternate indexes. |
894 |
|
my $indexList = $relationshipObject->{Indexes}; |
895 |
|
push @indexes, map { _CreatePPOIndex($_) } @{$indexList}; |
896 |
|
# Wrap up all the stuff about this relationship. |
897 |
|
my $object = { label => $relationshipName, |
898 |
|
scalar => \@scalars, |
899 |
|
object_ref => \@object_refs, |
900 |
|
index => \@indexes |
901 |
|
}; |
902 |
|
# Push it into the object list. |
903 |
|
push @objects, $object; |
904 |
|
} |
905 |
|
} |
906 |
|
# Compute a title. |
907 |
|
my $title; |
908 |
|
if ($erdbXMLFile =~ /(\/|^)([^\/]+)DBD\.xml/) { |
909 |
|
# Here we have a standard file name we can use for a title. |
910 |
|
$title = $2; |
911 |
|
} else { |
912 |
|
# Here the file name is non-standard, so we carve up the |
913 |
|
# database title. |
914 |
|
$title = $xml->{Title}->{content}; |
915 |
|
$title =~ s/\s\.,//g; |
916 |
|
} |
917 |
|
# Wrap up the XML as a project. |
918 |
|
my $ppoXML = { project => { label => $title, |
919 |
|
object => \@objects }}; |
920 |
|
# Write out the results. |
921 |
|
my $ppoString = XML::Simple::XMLout($ppoXML, |
922 |
|
AttrIndent => 1, |
923 |
|
KeepRoot => 1); |
924 |
|
Tracer::PutFile($ppoXMLFile, [ $ppoString ]); |
925 |
|
} |
926 |
|
|
927 |
=head3 FindIndexForEntity |
=head3 FindIndexForEntity |
928 |
|
|
929 |
C<< my $indexFound = ERDB::FindIndexForEntity($xml, $entityName, $attributeName); >> |
my $indexFound = ERDB::FindIndexForEntity($xml, $entityName, $attributeName); |
930 |
|
|
931 |
This method locates the entry in an entity's index list that begins with the |
This method locates the entry in an entity's index list that begins with the |
932 |
specified attribute name. If the entity has no index list, one will be |
specified attribute name. If the entity has no index list, one will be |
996 |
|
|
997 |
=head3 CreateTables |
=head3 CreateTables |
998 |
|
|
999 |
C<< $erdb->CreateTables(); >> |
$erdb->CreateTables(); |
1000 |
|
|
1001 |
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 |
1002 |
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 |
1020 |
|
|
1021 |
=head3 CreateTable |
=head3 CreateTable |
1022 |
|
|
1023 |
C<< $erdb->CreateTable($tableName, $indexFlag, $estimatedRows); >> |
$erdb->CreateTable($tableName, $indexFlag, $estimatedRows); |
1024 |
|
|
1025 |
Create the table for a relation and optionally create its indexes. |
Create the table for a relation and optionally create its indexes. |
1026 |
|
|
1076 |
my $estimation = undef; |
my $estimation = undef; |
1077 |
if ($estimatedRows) { |
if ($estimatedRows) { |
1078 |
$estimation = [$self->EstimateRowSize($relationName), $estimatedRows]; |
$estimation = [$self->EstimateRowSize($relationName), $estimatedRows]; |
1079 |
|
Trace("$estimation->[1] rows of $estimation->[0] bytes each.") if T(3); |
1080 |
} |
} |
1081 |
# Create the table. |
# Create the table. |
1082 |
Trace("Creating table $relationName: $fieldThing") if T(2); |
Trace("Creating table $relationName: $fieldThing") if T(2); |
1091 |
|
|
1092 |
=head3 VerifyFields |
=head3 VerifyFields |
1093 |
|
|
1094 |
C<< my $count = $erdb->VerifyFields($relName, \@fieldList); >> |
my $count = $erdb->VerifyFields($relName, \@fieldList); |
1095 |
|
|
1096 |
Run through the list of proposed field values, insuring that all the character fields are |
Run through the list of proposed field values, insuring that all the character fields are |
1097 |
below the maximum length. If any fields are too long, they will be truncated in place. |
below the maximum length. If any fields are too long, they will be truncated in place. |
1134 |
my $oldString = $fieldList->[$i]; |
my $oldString = $fieldList->[$i]; |
1135 |
if (length($oldString) > $maxLen) { |
if (length($oldString) > $maxLen) { |
1136 |
# Here it's too big, so we truncate it. |
# Here it's too big, so we truncate it. |
1137 |
Trace("Truncating field $i in relation $relName to $maxLen characters from \"$oldString\".") if T(1); |
Trace("Truncating field $i ($fieldTypes->[$i]->{name}) in relation $relName to $maxLen characters from \"$oldString\".") if T(1); |
1138 |
$fieldList->[$i] = substr $oldString, 0, $maxLen; |
$fieldList->[$i] = substr $oldString, 0, $maxLen; |
1139 |
$retVal++; |
$retVal++; |
1140 |
} |
} |
1146 |
|
|
1147 |
=head3 DigestFields |
=head3 DigestFields |
1148 |
|
|
1149 |
C<< $erdb->DigestFields($relName, $fieldList); >> |
$erdb->DigestFields($relName, $fieldList); |
1150 |
|
|
1151 |
Digest the strings in the field list that correspond to data type C<hash-string> in the |
Digest the strings in the field list that correspond to data type C<hash-string> in the |
1152 |
specified relation. |
specified relation. |
1186 |
|
|
1187 |
=head3 DigestKey |
=head3 DigestKey |
1188 |
|
|
1189 |
C<< my $digested = $erdb->DigestKey($keyValue); >> |
my $digested = $erdb->DigestKey($keyValue); |
1190 |
|
|
1191 |
Return the digested value of a symbolic key. The digested value can then be plugged into a |
Return the digested value of a symbolic key. The digested value can then be plugged into a |
1192 |
key-based search into a table with key-type hash-string. |
key-based search into a table with key-type hash-string. |
1219 |
|
|
1220 |
=head3 CreateIndex |
=head3 CreateIndex |
1221 |
|
|
1222 |
C<< $erdb->CreateIndex($relationName); >> |
$erdb->CreateIndex($relationName); |
1223 |
|
|
1224 |
Create the indexes for a relation. If a table is being loaded from a large source file (as |
Create the indexes for a relation. If a table is being loaded from a large source file (as |
1225 |
is the case in L</LoadTable>), it is sometimes best to create the indexes after the load. |
is the case in L</LoadTable>), it is sometimes best to create the indexes after the load. |
1275 |
|
|
1276 |
=head3 GetSecondaryFields |
=head3 GetSecondaryFields |
1277 |
|
|
1278 |
C<< my %fieldTuples = $erdb->GetSecondaryFields($entityName); >> |
my %fieldTuples = $erdb->GetSecondaryFields($entityName); |
1279 |
|
|
1280 |
This method will return a list of the name and type of each of the secondary |
This method will return a list of the name and type of each of the secondary |
1281 |
fields for a specified entity. Secondary fields are stored in two-column tables |
fields for a specified entity. Secondary fields are stored in two-column tables |
1316 |
|
|
1317 |
=head3 GetFieldRelationName |
=head3 GetFieldRelationName |
1318 |
|
|
1319 |
C<< my $name = $erdb->GetFieldRelationName($objectName, $fieldName); >> |
my $name = $erdb->GetFieldRelationName($objectName, $fieldName); |
1320 |
|
|
1321 |
Return the name of the relation containing a specified field. |
Return the name of the relation containing a specified field. |
1322 |
|
|
1357 |
|
|
1358 |
=head3 DeleteValue |
=head3 DeleteValue |
1359 |
|
|
1360 |
C<< my $numDeleted = $erdb->DeleteValue($entityName, $id, $fieldName, $fieldValue); >> |
my $numDeleted = $erdb->DeleteValue($entityName, $id, $fieldName, $fieldValue); |
1361 |
|
|
1362 |
Delete secondary field values from the database. This method can be used to delete all |
Delete secondary field values from the database. This method can be used to delete all |
1363 |
values of a specified field for a particular entity instance, or only a single value. |
values of a specified field for a particular entity instance, or only a single value. |
1440 |
|
|
1441 |
=head3 LoadTables |
=head3 LoadTables |
1442 |
|
|
1443 |
C<< my $stats = $erdb->LoadTables($directoryName, $rebuild); >> |
my $stats = $erdb->LoadTables($directoryName, $rebuild); |
1444 |
|
|
1445 |
This method will load the database tables from a directory. The tables must already have been created |
This method will load the database tables from a directory. The tables must already have been created |
1446 |
in the database. (This can be done by calling L</CreateTables>.) The caller passes in a directory name; |
in the database. (This can be done by calling L</CreateTables>.) The caller passes in a directory name; |
1500 |
|
|
1501 |
=head3 GetTableNames |
=head3 GetTableNames |
1502 |
|
|
1503 |
C<< my @names = $erdb->GetTableNames; >> |
my @names = $erdb->GetTableNames; |
1504 |
|
|
1505 |
Return a list of the relations required to implement this database. |
Return a list of the relations required to implement this database. |
1506 |
|
|
1517 |
|
|
1518 |
=head3 GetEntityTypes |
=head3 GetEntityTypes |
1519 |
|
|
1520 |
C<< my @names = $erdb->GetEntityTypes; >> |
my @names = $erdb->GetEntityTypes; |
1521 |
|
|
1522 |
Return a list of the entity type names. |
Return a list of the entity type names. |
1523 |
|
|
1534 |
|
|
1535 |
=head3 GetDataTypes |
=head3 GetDataTypes |
1536 |
|
|
1537 |
C<< my %types = ERDB::GetDataTypes(); >> |
my %types = ERDB::GetDataTypes(); |
1538 |
|
|
1539 |
Return a table of ERDB data types. The table returned is a hash of hashes. |
Return a table of ERDB data types. The table returned is a hash of hashes. |
1540 |
The keys of the big hash are the datatypes. Each smaller hash has several |
The keys of the big hash are the datatypes. Each smaller hash has several |
1553 |
|
|
1554 |
=head3 IsEntity |
=head3 IsEntity |
1555 |
|
|
1556 |
C<< my $flag = $erdb->IsEntity($entityName); >> |
my $flag = $erdb->IsEntity($entityName); |
1557 |
|
|
1558 |
Return TRUE if the parameter is an entity name, else FALSE. |
Return TRUE if the parameter is an entity name, else FALSE. |
1559 |
|
|
1580 |
|
|
1581 |
=head3 Get |
=head3 Get |
1582 |
|
|
1583 |
C<< my $query = $erdb->Get(\@objectNames, $filterClause, \@params); >> |
my $query = $erdb->Get(\@objectNames, $filterClause, \@params); |
1584 |
|
|
1585 |
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. |
1586 |
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 |
1588 |
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 |
1589 |
$genus. |
$genus. |
1590 |
|
|
1591 |
C<< $query = $erdb->Get(['Genome'], "Genome(genus) = ?", [$genus]); >> |
$query = $erdb->Get(['Genome'], "Genome(genus) = ?", [$genus]); |
1592 |
|
|
1593 |
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 |
1594 |
parameter representing the parameter value. It would also be possible to code |
parameter representing the parameter value. It would also be possible to code |
1595 |
|
|
1596 |
C<< $query = $erdb->Get(['Genome'], "Genome(genus) = \'$genus\'"); >> |
$query = $erdb->Get(['Genome'], "Genome(genus) = \'$genus\'"); |
1597 |
|
|
1598 |
however, this version of the call would generate a syntax error if there were any quote |
however, this version of the call would generate a syntax error if there were any quote |
1599 |
characters inside the variable C<$genus>. |
characters inside the variable C<$genus>. |
1605 |
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 |
1606 |
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, |
1607 |
|
|
1608 |
C<< $query = $erdb->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >> |
$query = $erdb->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); |
1609 |
|
|
1610 |
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 |
1611 |
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. |
1641 |
with an ORDER BY clause. For example, the following filter string gets all genomes for a |
with an ORDER BY clause. For example, the following filter string gets all genomes for a |
1642 |
particular genus and sorts them by species name. |
particular genus and sorts them by species name. |
1643 |
|
|
1644 |
C<< "Genome(genus) = ? ORDER BY Genome(species)" >> |
"Genome(genus) = ? ORDER BY Genome(species)" |
1645 |
|
|
1646 |
Note that the case is important. Only an uppercase "ORDER BY" with a single space will |
Note that the case is important. Only an uppercase "ORDER BY" with a single space will |
1647 |
be processed. The idea is to make it less likely to find the verb by accident. |
be processed. The idea is to make it less likely to find the verb by accident. |
1654 |
be the last thing in the filter clause, and it contains only the word "LIMIT" followed by |
be the last thing in the filter clause, and it contains only the word "LIMIT" followed by |
1655 |
a positive number. So, for example |
a positive number. So, for example |
1656 |
|
|
1657 |
C<< "Genome(genus) = ? ORDER BY Genome(species) LIMIT 10" >> |
"Genome(genus) = ? ORDER BY Genome(species) LIMIT 10" |
1658 |
|
|
1659 |
will only return the first ten genomes for the specified genus. The ORDER BY clause is not |
will only return the first ten genomes for the specified genus. The ORDER BY clause is not |
1660 |
required. For example, to just get the first 10 genomes in the B<Genome> table, you could |
required. For example, to just get the first 10 genomes in the B<Genome> table, you could |
1661 |
use |
use |
1662 |
|
|
1663 |
C<< "LIMIT 10" >> |
"LIMIT 10" |
1664 |
|
|
1665 |
=item params |
=item params |
1666 |
|
|
1681 |
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = |
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = |
1682 |
$self->_SetupSQL($objectNames, $filterClause); |
$self->_SetupSQL($objectNames, $filterClause); |
1683 |
# Create the query. |
# Create the query. |
1684 |
my $command = "SELECT DISTINCT " . join(".*, ", @{$mappedNameListRef}) . |
my $command = "SELECT " . join(".*, ", @{$mappedNameListRef}) . |
1685 |
".* $suffix"; |
".* $suffix"; |
1686 |
my $sth = $self->_GetStatementHandle($command, $params); |
my $sth = $self->_GetStatementHandle($command, $params); |
1687 |
# 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 |
1699 |
|
|
1700 |
=head3 Search |
=head3 Search |
1701 |
|
|
1702 |
C<< my $query = $erdb->Search($searchExpression, $idx, \@objectNames, $filterClause, \@params); >> |
my $query = $erdb->Search($searchExpression, $idx, \@objectNames, $filterClause, \@params); |
1703 |
|
|
1704 |
Perform a full text search with filtering. The search will be against a specified object |
Perform a full text search with filtering. The search will be against a specified object |
1705 |
in the object name list. That object will get an extra field containing the search |
in the object name list. That object will get an extra field containing the search |
1782 |
$self->_SetupSQL($objectNames, $filterClause, $matchClause); |
$self->_SetupSQL($objectNames, $filterClause, $matchClause); |
1783 |
# Create the query. Note that the match clause is inserted at the front of |
# Create the query. Note that the match clause is inserted at the front of |
1784 |
# the select fields. |
# the select fields. |
1785 |
my $command = "SELECT DISTINCT $matchClause, " . join(".*, ", @{$mappedNameListRef}) . |
my $command = "SELECT $matchClause, " . join(".*, ", @{$mappedNameListRef}) . |
1786 |
".* $suffix"; |
".* $suffix"; |
1787 |
my $sth = $self->_GetStatementHandle($command, \@myParams); |
my $sth = $self->_GetStatementHandle($command, \@myParams); |
1788 |
# 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 |
1796 |
|
|
1797 |
=head3 GetFlat |
=head3 GetFlat |
1798 |
|
|
1799 |
C<< my @list = $erdb->GetFlat(\@objectNames, $filterClause, \@parameterList, $field); >> |
my @list = $erdb->GetFlat(\@objectNames, $filterClause, \@parameterList, $field); |
1800 |
|
|
1801 |
This is a variation of L</GetAll> that asks for only a single field per record and |
This is a variation of L</GetAll> that asks for only a single field per record and |
1802 |
returns a single flattened list. |
returns a single flattened list. |
1849 |
|
|
1850 |
=head3 SpecialFields |
=head3 SpecialFields |
1851 |
|
|
1852 |
C<< my %specials = $erdb->SpecialFields($entityName); >> |
my %specials = $erdb->SpecialFields($entityName); |
1853 |
|
|
1854 |
Return a hash mapping special fields in the specified entity to the value of their |
Return a hash mapping special fields in the specified entity to the value of their |
1855 |
C<special> attribute. This enables the subclass to get access to the special field |
C<special> attribute. This enables the subclass to get access to the special field |
1891 |
|
|
1892 |
=head3 Delete |
=head3 Delete |
1893 |
|
|
1894 |
C<< my $stats = $erdb->Delete($entityName, $objectID, %options); >> |
my $stats = $erdb->Delete($entityName, $objectID, %options); |
1895 |
|
|
1896 |
Delete an entity instance from the database. The instance is deleted along with all entity and |
Delete an entity instance from the database. The instance is deleted along with all entity and |
1897 |
relationship instances dependent on it. The definition of I<dependence> is recursive. |
relationship instances dependent on it. The definition of I<dependence> is recursive. |
2076 |
|
|
2077 |
=head3 Disconnect |
=head3 Disconnect |
2078 |
|
|
2079 |
C<< $erdb->Disconnect($relationshipName, $originEntityName, $originEntityID); >> |
$erdb->Disconnect($relationshipName, $originEntityName, $originEntityID); |
2080 |
|
|
2081 |
Disconnect an entity instance from all the objects to which it is related. This |
Disconnect an entity instance from all the objects to which it is related. This |
2082 |
will delete each relationship instance that connects to the specified entity. |
will delete each relationship instance that connects to the specified entity. |
2115 |
# Loop through the ends of the relationship. |
# Loop through the ends of the relationship. |
2116 |
for my $dir ('from', 'to') { |
for my $dir ('from', 'to') { |
2117 |
if ($structure->{$dir} eq $originEntityName) { |
if ($structure->{$dir} eq $originEntityName) { |
|
# Delete all relationship instances on this side of the entity instance. |
|
|
Trace("Disconnecting in $dir direction with ID \"$originEntityID\"."); |
|
|
$dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ?", 0, $originEntityID); |
|
2118 |
$found = 1; |
$found = 1; |
2119 |
|
# Here we want to delete all relationship instances on this side of the |
2120 |
|
# entity instance. |
2121 |
|
Trace("Disconnecting in $dir direction with ID \"$originEntityID\"."); |
2122 |
|
# We do this delete in batches to keep it from dragging down the |
2123 |
|
# server. |
2124 |
|
my $limitClause = ($FIG_Config::delete_limit ? "LIMIT $FIG_Config::delete_limit" : ""); |
2125 |
|
my $done = 0; |
2126 |
|
while (! $done) { |
2127 |
|
# Do the delete. |
2128 |
|
my $rows = $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ? $limitClause", 0, $originEntityID); |
2129 |
|
# See if we're done. We're done if no rows were found or the delete is unlimited. |
2130 |
|
$done = ($rows == 0 || ! $limitClause); |
2131 |
|
} |
2132 |
} |
} |
2133 |
} |
} |
2134 |
# Insure we found the entity on at least one end. |
# Insure we found the entity on at least one end. |
2140 |
|
|
2141 |
=head3 DeleteRow |
=head3 DeleteRow |
2142 |
|
|
2143 |
C<< $erdb->DeleteRow($relationshipName, $fromLink, $toLink, \%values); >> |
$erdb->DeleteRow($relationshipName, $fromLink, $toLink, \%values); |
2144 |
|
|
2145 |
Delete a row from a relationship. In most cases, only the from-link and to-link are |
Delete a row from a relationship. In most cases, only the from-link and to-link are |
2146 |
needed; however, for relationships with intersection data values can be specified |
needed; however, for relationships with intersection data values can be specified |
2193 |
$dbh->SQL($command, undef, @parms); |
$dbh->SQL($command, undef, @parms); |
2194 |
} |
} |
2195 |
|
|
2196 |
|
=head3 DeleteLike |
2197 |
|
|
2198 |
|
my $deleteCount = $erdb->DeleteLike($relName, $filter, \@parms); |
2199 |
|
|
2200 |
|
Delete all the relationship rows that satisfy a particular filter condition. Unlike a normal |
2201 |
|
filter, only fields from the relationship itself can be used. |
2202 |
|
|
2203 |
|
=over 4 |
2204 |
|
|
2205 |
|
=item relName |
2206 |
|
|
2207 |
|
Name of the relationship whose records are to be deleted. |
2208 |
|
|
2209 |
|
=item filter |
2210 |
|
|
2211 |
|
A filter clause (L</Get>-style) for the delete query. |
2212 |
|
|
2213 |
|
=item parms |
2214 |
|
|
2215 |
|
Reference to a list of parameters for the filter clause. |
2216 |
|
|
2217 |
|
=item RETURN |
2218 |
|
|
2219 |
|
Returns a count of the number of rows deleted. |
2220 |
|
|
2221 |
|
=back |
2222 |
|
|
2223 |
|
=cut |
2224 |
|
|
2225 |
|
sub DeleteLike { |
2226 |
|
# Get the parameters. |
2227 |
|
my ($self, $objectName, $filter, $parms) = @_; |
2228 |
|
# Declare the return variable. |
2229 |
|
my $retVal; |
2230 |
|
# Insure the parms argument is an array reference if the caller left it off. |
2231 |
|
if (! defined($parms)) { |
2232 |
|
$parms = []; |
2233 |
|
} |
2234 |
|
# Insure we have a relationship. The main reason for this is if we delete an entity |
2235 |
|
# instance we have to yank out a bunch of other stuff with it. |
2236 |
|
if ($self->IsEntity($objectName)) { |
2237 |
|
Confess("Cannot use DeleteLike on $objectName, because it is not a relationship."); |
2238 |
|
} else { |
2239 |
|
# Create the SQL command suffix to get the desierd records. |
2240 |
|
my ($suffix) = $self->_SetupSQL([$objectName], $filter); |
2241 |
|
# Convert it to a DELETE command. |
2242 |
|
my $command = "DELETE $suffix"; |
2243 |
|
# Execute the command. |
2244 |
|
my $dbh = $self->{_dbh}; |
2245 |
|
my $result = $dbh->SQL($command, 0, @{$parms}); |
2246 |
|
# Check the results. Note we convert the "0D0" result to a real zero. |
2247 |
|
# A failure causes an abnormal termination, so the caller isn't going to |
2248 |
|
# worry about it. |
2249 |
|
if (! defined $result) { |
2250 |
|
Confess("Error deleting from $objectName: " . $dbh->errstr()); |
2251 |
|
} elsif ($result == 0) { |
2252 |
|
$retVal = 0; |
2253 |
|
} else { |
2254 |
|
$retVal = $result; |
2255 |
|
} |
2256 |
|
} |
2257 |
|
# Return the result count. |
2258 |
|
return $retVal; |
2259 |
|
} |
2260 |
|
|
2261 |
=head3 SortNeeded |
=head3 SortNeeded |
2262 |
|
|
2263 |
C<< my $parms = $erdb->SortNeeded($relationName); >> |
my $parms = $erdb->SortNeeded($relationName); |
2264 |
|
|
2265 |
Return the pipe command for the sort that should be applied to the specified |
Return the pipe command for the sort that should be applied to the specified |
2266 |
relation when creating the load file. |
relation when creating the load file. |
2358 |
|
|
2359 |
=head3 GetList |
=head3 GetList |
2360 |
|
|
2361 |
C<< my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, \@params); >> |
my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, \@params); |
2362 |
|
|
2363 |
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 |
2364 |
specified filter clause. |
specified filter clause. |
2386 |
with an ORDER BY clause. For example, the following filter string gets all genomes for a |
with an ORDER BY clause. For example, the following filter string gets all genomes for a |
2387 |
particular genus and sorts them by species name. |
particular genus and sorts them by species name. |
2388 |
|
|
2389 |
C<< "Genome(genus) = ? ORDER BY Genome(species)" >> |
"Genome(genus) = ? ORDER BY Genome(species)" |
2390 |
|
|
2391 |
The rules for field references in a sort order are the same as those for field references in the |
The rules for field references in a sort order are the same as those for field references in the |
2392 |
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 |
2398 |
|
|
2399 |
=item RETURN |
=item RETURN |
2400 |
|
|
2401 |
Returns a list of B<DBObject>s that satisfy the query conditions. |
Returns a list of B<ERDBObject>s that satisfy the query conditions. |
2402 |
|
|
2403 |
=back |
=back |
2404 |
|
|
2421 |
|
|
2422 |
=head3 GetCount |
=head3 GetCount |
2423 |
|
|
2424 |
C<< my $count = $erdb->GetCount(\@objectNames, $filter, \@params); >> |
my $count = $erdb->GetCount(\@objectNames, $filter, \@params); |
2425 |
|
|
2426 |
Return the number of rows found by a specified query. This method would |
Return the number of rows found by a specified query. This method would |
2427 |
normally be used to count the records in a single table. For example, in a |
normally be used to count the records in a single table. For example, in a |
2514 |
|
|
2515 |
=head3 ComputeObjectSentence |
=head3 ComputeObjectSentence |
2516 |
|
|
2517 |
C<< my $sentence = $erdb->ComputeObjectSentence($objectName); >> |
my $sentence = $erdb->ComputeObjectSentence($objectName); |
2518 |
|
|
2519 |
Check an object name, and if it is a relationship convert it to a relationship sentence. |
Check an object name, and if it is a relationship convert it to a relationship sentence. |
2520 |
|
|
2549 |
|
|
2550 |
=head3 DumpRelations |
=head3 DumpRelations |
2551 |
|
|
2552 |
C<< $erdb->DumpRelations($outputDirectory); >> |
$erdb->DumpRelations($outputDirectory); |
2553 |
|
|
2554 |
Write the contents of all the relations to tab-delimited files in the specified directory. |
Write the contents of all the relations to tab-delimited files in the specified directory. |
2555 |
Each file will have the same name as the relation dumped, with an extension of DTX. |
Each file will have the same name as the relation dumped, with an extension of DTX. |
2591 |
|
|
2592 |
=head3 InsertValue |
=head3 InsertValue |
2593 |
|
|
2594 |
C<< $erdb->InsertValue($entityID, $fieldName, $value); >> |
$erdb->InsertValue($entityID, $fieldName, $value); |
2595 |
|
|
2596 |
This method will insert a new value into the database. The value must be one |
This method will insert a new value into the database. The value must be one |
2597 |
associated with a secondary relation, since primary values cannot be inserted: |
associated with a secondary relation, since primary values cannot be inserted: |
2654 |
|
|
2655 |
=head3 InsertObject |
=head3 InsertObject |
2656 |
|
|
2657 |
C<< $erdb->InsertObject($objectType, \%fieldHash); >> |
$erdb->InsertObject($objectType, \%fieldHash); |
2658 |
|
|
2659 |
Insert an object into the database. The object is defined by a type name and then a hash |
Insert an object into the database. The object is defined by a type name and then a hash |
2660 |
of field names to values. Field values in the primary relation are represented by scalars. |
of field names to values. Field values in the primary relation are represented by scalars. |
2663 |
example, the following line inserts an inactive PEG feature named C<fig|188.1.peg.1> with aliases |
example, the following line inserts an inactive PEG feature named C<fig|188.1.peg.1> with aliases |
2664 |
C<ZP_00210270.1> and C<gi|46206278>. |
C<ZP_00210270.1> and C<gi|46206278>. |
2665 |
|
|
2666 |
C<< $erdb->InsertObject('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >> |
$erdb->InsertObject('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); |
2667 |
|
|
2668 |
The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and |
The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and |
2669 |
property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>. |
property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>. |
2670 |
|
|
2671 |
C<< $erdb->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >> |
$erdb->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); |
2672 |
|
|
2673 |
=over 4 |
=over 4 |
2674 |
|
|
2791 |
|
|
2792 |
=head3 UpdateEntity |
=head3 UpdateEntity |
2793 |
|
|
2794 |
C<< $erdb->UpdateEntity($entityName, $id, \%fields); >> |
$erdb->UpdateEntity($entityName, $id, \%fields); |
2795 |
|
|
2796 |
Update the values of an entity. This is an unprotected update, so it should only be |
Update the values of an entity. This is an unprotected update, so it should only be |
2797 |
done if the database resides on a database server. |
done if the database resides on a database server. |
2849 |
|
|
2850 |
=head3 LoadTable |
=head3 LoadTable |
2851 |
|
|
2852 |
C<< my $results = $erdb->LoadTable($fileName, $relationName, $truncateFlag); >> |
my $results = $erdb->LoadTable($fileName, $relationName, %options); |
2853 |
|
|
2854 |
Load data from a tab-delimited file into a specified table, optionally re-creating the table |
Load data from a tab-delimited file into a specified table, optionally re-creating the table |
2855 |
first. |
first. |
2864 |
|
|
2865 |
Name of the relation to be loaded. This is the same as the table name. |
Name of the relation to be loaded. This is the same as the table name. |
2866 |
|
|
2867 |
=item truncateFlag |
=item options |
2868 |
|
|
2869 |
TRUE if the table should be dropped and re-created, else FALSE |
A hash of load options. |
2870 |
|
|
2871 |
=item RETURN |
=item RETURN |
2872 |
|
|
2874 |
|
|
2875 |
=back |
=back |
2876 |
|
|
2877 |
|
The permissible options are as follows. |
2878 |
|
|
2879 |
|
=over 4 |
2880 |
|
|
2881 |
|
=item truncate |
2882 |
|
|
2883 |
|
If TRUE, then the table will be erased before loading. |
2884 |
|
|
2885 |
|
=item mode |
2886 |
|
|
2887 |
|
Mode in which the load should operate, either C<low_priority> or C<concurrent>. |
2888 |
|
This option is only applicable to a MySQL database. |
2889 |
|
|
2890 |
|
=item partial |
2891 |
|
|
2892 |
|
If TRUE, then it is assumed that this is a partial load, and the table will not |
2893 |
|
be analyzed and compacted at the end. |
2894 |
|
|
2895 |
|
=back |
2896 |
|
|
2897 |
=cut |
=cut |
2898 |
sub LoadTable { |
sub LoadTable { |
2899 |
# Get the parameters. |
# Get the parameters. |
2900 |
my ($self, $fileName, $relationName, $truncateFlag) = @_; |
my ($self, $fileName, $relationName, %options) = @_; |
2901 |
# Create the statistical return object. |
# Create the statistical return object. |
2902 |
my $retVal = _GetLoadStats(); |
my $retVal = _GetLoadStats(); |
2903 |
# Trace the fact of the load. |
# Trace the fact of the load. |
2909 |
# Get the relation data. |
# Get the relation data. |
2910 |
my $relation = $self->_FindRelation($relationName); |
my $relation = $self->_FindRelation($relationName); |
2911 |
# Check the truncation flag. |
# Check the truncation flag. |
2912 |
if ($truncateFlag) { |
if ($options{truncate}) { |
2913 |
Trace("Creating table $relationName") if T(2); |
Trace("Creating table $relationName") if T(2); |
2914 |
# Compute the row count estimate. We take the size of the load file, |
# Compute the row count estimate. We take the size of the load file, |
2915 |
# divide it by the estimated row size, and then multiply by 1.5 to |
# divide it by the estimated row size, and then multiply by 2 to |
2916 |
# leave extra room. We postulate a minimum row count of 1000 to |
# leave extra room. We postulate a minimum row count of 1000 to |
2917 |
# prevent problems with incoming empty load files. |
# prevent problems with incoming empty load files. |
2918 |
my $rowSize = $self->EstimateRowSize($relationName); |
my $rowSize = $self->EstimateRowSize($relationName); |
2919 |
my $estimate = FIG::max($fileSize * 1.5 / $rowSize, 1000); |
my $estimate = $fileSize * 8 / $rowSize; |
2920 |
|
if ($estimate < 1000) { |
2921 |
|
$estimate = 1000; |
2922 |
|
} |
2923 |
# Re-create the table without its index. |
# Re-create the table without its index. |
2924 |
$self->CreateTable($relationName, 0, $estimate); |
$self->CreateTable($relationName, 0, $estimate); |
2925 |
# If this is a pre-index DBMS, create the index here. |
# If this is a pre-index DBMS, create the index here. |
2935 |
# Load the table. |
# Load the table. |
2936 |
my $rv; |
my $rv; |
2937 |
eval { |
eval { |
2938 |
$rv = $dbh->load_table(file => $fileName, tbl => $relationName); |
$rv = $dbh->load_table(file => $fileName, tbl => $relationName, style => $options{mode}); |
2939 |
}; |
}; |
2940 |
if (!defined $rv) { |
if (!defined $rv) { |
2941 |
$retVal->AddMessage($@) if ($@); |
$retVal->AddMessage($@) if ($@); |
2946 |
$retVal->Add("tables"); |
$retVal->Add("tables"); |
2947 |
my $size = -s $fileName; |
my $size = -s $fileName; |
2948 |
Trace("$size bytes loaded into $relationName.") if T(2); |
Trace("$size bytes loaded into $relationName.") if T(2); |
2949 |
|
$retVal->Add("bytes", $size); |
2950 |
# If we're rebuilding, we need to create the table indexes. |
# If we're rebuilding, we need to create the table indexes. |
2951 |
if ($truncateFlag) { |
if ($options{truncate}) { |
2952 |
# Indexes are created here for PostGres. For PostGres, indexes are |
# Indexes are created here for PostGres. For PostGres, indexes are |
2953 |
# best built at the end. For MySQL, the reverse is true. |
# best built at the end. For MySQL, the reverse is true. |
2954 |
if (! $dbh->{_preIndex}) { |
if (! $dbh->{_preIndex}) { |
2969 |
} |
} |
2970 |
} |
} |
2971 |
# Analyze the table to improve performance. |
# Analyze the table to improve performance. |
2972 |
|
if (! $options{partial}) { |
2973 |
Trace("Analyzing and compacting $relationName.") if T(3); |
Trace("Analyzing and compacting $relationName.") if T(3); |
2974 |
$dbh->vacuum_it($relationName); |
$dbh->vacuum_it($relationName); |
2975 |
|
} |
2976 |
Trace("$relationName load completed.") if T(3); |
Trace("$relationName load completed.") if T(3); |
2977 |
# Return the statistics. |
# Return the statistics. |
2978 |
return $retVal; |
return $retVal; |
2980 |
|
|
2981 |
=head3 CreateSearchIndex |
=head3 CreateSearchIndex |
2982 |
|
|
2983 |
C<< $erdb->CreateSearchIndex($objectName); >> |
$erdb->CreateSearchIndex($objectName); |
2984 |
|
|
2985 |
Check for a full-text search index on the specified entity or relationship object, and |
Check for a full-text search index on the specified entity or relationship object, and |
2986 |
if one is required, rebuild it. |
if one is required, rebuild it. |
3017 |
|
|
3018 |
=head3 DropRelation |
=head3 DropRelation |
3019 |
|
|
3020 |
C<< $erdb->DropRelation($relationName); >> |
$erdb->DropRelation($relationName); |
3021 |
|
|
3022 |
Physically drop a relation from the database. |
Physically drop a relation from the database. |
3023 |
|
|
3045 |
|
|
3046 |
=head3 MatchSqlPattern |
=head3 MatchSqlPattern |
3047 |
|
|
3048 |
C<< my $matched = ERDB::MatchSqlPattern($value, $pattern); >> |
my $matched = ERDB::MatchSqlPattern($value, $pattern); |
3049 |
|
|
3050 |
Determine whether or not a specified value matches an SQL pattern. An SQL |
Determine whether or not a specified value matches an SQL pattern. An SQL |
3051 |
pattern has two wild card characters: C<%> that matches multiple characters, |
pattern has two wild card characters: C<%> that matches multiple characters, |
3128 |
|
|
3129 |
=head3 GetEntity |
=head3 GetEntity |
3130 |
|
|
3131 |
C<< my $entityObject = $erdb->GetEntity($entityType, $ID); >> |
my $entityObject = $erdb->GetEntity($entityType, $ID); |
3132 |
|
|
3133 |
Return an object describing the entity instance with a specified ID. |
Return an object describing the entity instance with a specified ID. |
3134 |
|
|
3144 |
|
|
3145 |
=item RETURN |
=item RETURN |
3146 |
|
|
3147 |
Returns a B<DBObject> representing the desired entity instance, or an undefined value if no |
Returns a B<ERDBObject> representing the desired entity instance, or an undefined value if no |
3148 |
instance is found with the specified key. |
instance is found with the specified key. |
3149 |
|
|
3150 |
=back |
=back |
3164 |
|
|
3165 |
=head3 GetChoices |
=head3 GetChoices |
3166 |
|
|
3167 |
C<< my @values = $erdb->GetChoices($entityName, $fieldName); >> |
my @values = $erdb->GetChoices($entityName, $fieldName); |
3168 |
|
|
3169 |
Return a list of all the values for the specified field that are represented in the |
Return a list of all the values for the specified field that are represented in the |
3170 |
specified entity. |
specified entity. |
3219 |
|
|
3220 |
=head3 GetEntityValues |
=head3 GetEntityValues |
3221 |
|
|
3222 |
C<< my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); >> |
my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); |
3223 |
|
|
3224 |
Return a list of values from a specified entity instance. If the entity instance |
Return a list of values from a specified entity instance. If the entity instance |
3225 |
does not exist, an empty list is returned. |
does not exist, an empty list is returned. |
3263 |
|
|
3264 |
=head3 GetAll |
=head3 GetAll |
3265 |
|
|
3266 |
C<< my @list = $erdb->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >> |
my @list = $erdb->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); |
3267 |
|
|
3268 |
Return a list of values taken from the objects returned by a query. The first three |
Return a list of values taken from the objects returned by a query. The first three |
3269 |
parameters correspond to the parameters of the L</Get> method. The final parameter is |
parameters correspond to the parameters of the L</Get> method. The final parameter is |
3277 |
fields specified returns multiple values, they are flattened in with the rest. For |
fields specified returns multiple values, they are flattened in with the rest. For |
3278 |
example, the following call will return a list of the features in a particular |
example, the following call will return a list of the features in a particular |
3279 |
spreadsheet cell, and each feature will be represented by a list containing the |
spreadsheet cell, and each feature will be represented by a list containing the |
3280 |
feature ID followed by all of its aliases. |
feature ID followed by all of its essentiality determinations. |
3281 |
|
|
3282 |
C<< @query = $erdb->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >> |
@query = $erdb->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(essential)']); |
3283 |
|
|
3284 |
=over 4 |
=over 4 |
3285 |
|
|
3357 |
|
|
3358 |
=head3 Exists |
=head3 Exists |
3359 |
|
|
3360 |
C<< my $found = $sprout->Exists($entityName, $entityID); >> |
my $found = $sprout->Exists($entityName, $entityID); |
3361 |
|
|
3362 |
Return TRUE if an entity exists, else FALSE. |
Return TRUE if an entity exists, else FALSE. |
3363 |
|
|
3392 |
|
|
3393 |
=head3 EstimateRowSize |
=head3 EstimateRowSize |
3394 |
|
|
3395 |
C<< my $rowSize = $erdb->EstimateRowSize($relName); >> |
my $rowSize = $erdb->EstimateRowSize($relName); |
3396 |
|
|
3397 |
Estimate the row size of the specified relation. The estimated row size is computed by adding |
Estimate the row size of the specified relation. The estimated row size is computed by adding |
3398 |
up the average length for each data type. |
up the average length for each data type. |
3430 |
|
|
3431 |
=head3 GetFieldTable |
=head3 GetFieldTable |
3432 |
|
|
3433 |
C<< my $fieldHash = $self->GetFieldTable($objectnName); >> |
my $fieldHash = $self->GetFieldTable($objectnName); |
3434 |
|
|
3435 |
Get the field structure for a specified entity or relationship. |
Get the field structure for a specified entity or relationship. |
3436 |
|
|
3459 |
|
|
3460 |
=head3 SplitKeywords |
=head3 SplitKeywords |
3461 |
|
|
3462 |
C<< my @keywords = ERDB::SplitKeywords($keywordString); >> |
my @keywords = ERDB::SplitKeywords($keywordString); |
3463 |
|
|
3464 |
This method returns a list of the positive keywords in the specified |
This method returns a list of the positive keywords in the specified |
3465 |
keyword string. All of the operators will have been stripped off, |
keyword string. All of the operators will have been stripped off, |
3508 |
|
|
3509 |
=head3 ValidateFieldName |
=head3 ValidateFieldName |
3510 |
|
|
3511 |
C<< my $okFlag = ERDB::ValidateFieldName($fieldName); >> |
my $okFlag = ERDB::ValidateFieldName($fieldName); |
3512 |
|
|
3513 |
Return TRUE if the specified field name is valid, else FALSE. Valid field names must |
Return TRUE if the specified field name is valid, else FALSE. Valid field names must |
3514 |
be hyphenated words subject to certain restrictions. |
be hyphenated words subject to certain restrictions. |
3563 |
|
|
3564 |
=head3 ReadMetaXML |
=head3 ReadMetaXML |
3565 |
|
|
3566 |
C<< my $rawMetaData = ERDB::ReadDBD($fileName); >> |
my $rawMetaData = ERDB::ReadDBD($fileName); |
3567 |
|
|
3568 |
This method reads a raw database definition XML file and returns it. |
This method reads a raw database definition XML file and returns it. |
3569 |
Normally, the metadata used by the ERDB system has been processed and |
Normally, the metadata used by the ERDB system has been processed and |
3596 |
|
|
3597 |
=head3 GetEntityFieldHash |
=head3 GetEntityFieldHash |
3598 |
|
|
3599 |
C<< my $fieldHashRef = ERDB::GetEntityFieldHash($structure, $entityName); >> |
my $fieldHashRef = ERDB::GetEntityFieldHash($structure, $entityName); |
3600 |
|
|
3601 |
Get the field hash of the named entity in the specified raw XML structure. |
Get the field hash of the named entity in the specified raw XML structure. |
3602 |
The field hash may not exist, in which case we need to create it. |
The field hash may not exist, in which case we need to create it. |
3638 |
|
|
3639 |
=head3 WriteMetaXML |
=head3 WriteMetaXML |
3640 |
|
|
3641 |
C<< ERDB::WriteMetaXML($structure, $fileName); >> |
ERDB::WriteMetaXML($structure, $fileName); |
3642 |
|
|
3643 |
Write the metadata XML to a file. This method is the reverse of L</ReadMetaXML>, and is |
Write the metadata XML to a file. This method is the reverse of L</ReadMetaXML>, and is |
3644 |
used to update the database definition. It must be used with care, however, since it |
used to update the database definition. It must be used with care, however, since it |
3677 |
Except for C<[p]>, all the codes are closed by slash-codes. So, for |
Except for C<[p]>, all the codes are closed by slash-codes. So, for |
3678 |
example, C<[b]Feature[/b]> displays the string C<Feature> in boldface. |
example, C<[b]Feature[/b]> displays the string C<Feature> in boldface. |
3679 |
|
|
3680 |
C<< my $realHtml = ERDB::HTMLNote($dataString); >> |
my $realHtml = ERDB::HTMLNote($dataString); |
3681 |
|
|
3682 |
=over 4 |
=over 4 |
3683 |
|
|
3707 |
return $retVal; |
return $retVal; |
3708 |
} |
} |
3709 |
|
|
3710 |
|
=head3 WikiNote |
3711 |
|
|
3712 |
|
Convert a note or comment to Wiki text by replacing some bulletin-board codes with HTML. The codes |
3713 |
|
supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph. |
3714 |
|
Except for C<[p]>, all the codes are closed by slash-codes. So, for |
3715 |
|
example, C<[b]Feature[/b]> displays the string C<Feature> in boldface. |
3716 |
|
|
3717 |
|
my $wikiText = ERDB::WikiNote($dataString); |
3718 |
|
|
3719 |
|
=over 4 |
3720 |
|
|
3721 |
|
=item dataString |
3722 |
|
|
3723 |
|
String to convert to Wiki text. |
3724 |
|
|
3725 |
|
=item RETURN |
3726 |
|
|
3727 |
|
An Wiki text string derived from the input string. |
3728 |
|
|
3729 |
|
=back |
3730 |
|
|
3731 |
|
=cut |
3732 |
|
|
3733 |
|
sub WikiNote { |
3734 |
|
# Get the parameter. |
3735 |
|
my ($dataString) = @_; |
3736 |
|
# HTML-escape the text. |
3737 |
|
my $retVal = CGI::escapeHTML($dataString); |
3738 |
|
# Substitute the bulletin board codes. |
3739 |
|
my $italic = WikiTools::ItalicCode(); |
3740 |
|
$retVal =~ s/\[\/?i\]/$italic/g; |
3741 |
|
my $bold = WikiTools::BoldCode(); |
3742 |
|
$retVal =~ s/\[\/?b\]/$bold/g; |
3743 |
|
# Paragraph breaks are the same no matter which Wiki you're using. |
3744 |
|
$retVal =~ s!\[p\]!\n\n!g; |
3745 |
|
# Now we do the links, which are complicated by the need to know two |
3746 |
|
# things: the target URL and the text. |
3747 |
|
while ($retVal =~ /\[link\s+([^\]]+)\]([^\[]+)\[\/link\]/g) { |
3748 |
|
# Replace the matched string with the Wiki markup for links. Note that |
3749 |
|
# $-[0] is the starting position of the match for the entire expression, |
3750 |
|
# and $+[0] is past the ending position. |
3751 |
|
substr $retVal, $-[0], $+[0] - $-[0], WikiTools::LinkMarkup($1, $2); |
3752 |
|
} |
3753 |
|
# Return the result. |
3754 |
|
return $retVal; |
3755 |
|
} |
3756 |
|
|
3757 |
=head3 BeginTran |
=head3 BeginTran |
3758 |
|
|
3759 |
C<< $erdb->BeginTran(); >> |
$erdb->BeginTran(); |
3760 |
|
|
3761 |
Start a database transaction. |
Start a database transaction. |
3762 |
|
|
3770 |
|
|
3771 |
=head3 CommitTran |
=head3 CommitTran |
3772 |
|
|
3773 |
C<< $erdb->CommitTran(); >> |
$erdb->CommitTran(); |
3774 |
|
|
3775 |
Commit an active database transaction. |
Commit an active database transaction. |
3776 |
|
|
3783 |
|
|
3784 |
=head3 RollbackTran |
=head3 RollbackTran |
3785 |
|
|
3786 |
C<< $erdb->RollbackTran(); >> |
$erdb->RollbackTran(); |
3787 |
|
|
3788 |
Roll back an active database transaction. |
Roll back an active database transaction. |
3789 |
|
|
3794 |
$self->{_dbh}->roll_tran(); |
$self->{_dbh}->roll_tran(); |
3795 |
} |
} |
3796 |
|
|
3797 |
|
=head3 UpdateField |
3798 |
|
|
3799 |
|
my $count = $erdb->UpdateField($objectNames, $fieldName, $oldValue, $newValue, $filter, $parms); |
3800 |
|
|
3801 |
|
Update all occurrences of a specific field value to a new value. The number of rows changed will be |
3802 |
|
returned. |
3803 |
|
|
3804 |
|
=over 4 |
3805 |
|
|
3806 |
|
=item fieldName |
3807 |
|
|
3808 |
|
Name of the field in standard I<objectName>C<(>I<fieldName>C<)> format. |
3809 |
|
|
3810 |
|
=item oldValue |
3811 |
|
|
3812 |
|
Value to be modified. All occurrences of this value in the named field will be replaced by the |
3813 |
|
new value. |
3814 |
|
|
3815 |
|
=item newValue |
3816 |
|
|
3817 |
|
New value to be substituted for the old value when it's found. |
3818 |
|
|
3819 |
|
=item filter |
3820 |
|
|
3821 |
|
A standard ERDB filter clause (see L</Get>). The filter will be applied before any substitutions take place. |
3822 |
|
|
3823 |
|
=item parms |
3824 |
|
|
3825 |
|
Reference to a list of parameter values in the filter. |
3826 |
|
|
3827 |
|
=item RETURN |
3828 |
|
|
3829 |
|
Returns the number of rows modified. |
3830 |
|
|
3831 |
|
=back |
3832 |
|
|
3833 |
|
=cut |
3834 |
|
|
3835 |
|
sub UpdateField { |
3836 |
|
# Get the parameters. |
3837 |
|
my ($self, $fieldName, $oldValue, $newValue, $filter, $parms) = @_; |
3838 |
|
# Get the object and field names from the field name parameter. |
3839 |
|
$fieldName =~ /^([^(]+)\(([^)]+)\)/; |
3840 |
|
my $objectName = $1; |
3841 |
|
my $realFieldName = _FixName($2); |
3842 |
|
# Add the old value to the filter. Note we allow the possibility that no |
3843 |
|
# filter was specified. |
3844 |
|
my $realFilter = "$fieldName = ?"; |
3845 |
|
if ($filter) { |
3846 |
|
$realFilter .= " AND $filter"; |
3847 |
|
} |
3848 |
|
# Format the query filter. |
3849 |
|
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = |
3850 |
|
$self->_SetupSQL([$objectName], $realFilter); |
3851 |
|
# Create the query. Since there is only one object name, the mapped-name data is not |
3852 |
|
# necessary. Neither is the FROM clause. |
3853 |
|
$suffix =~ s/^FROM.+WHERE\s+//; |
3854 |
|
# Create the update statement. |
3855 |
|
my $command = "UPDATE $objectName SET $realFieldName = ? WHERE $suffix"; |
3856 |
|
# Get the database handle. |
3857 |
|
my $dbh = $self->{_dbh}; |
3858 |
|
# Add the old and new values to the parameter list. Note we allow the possibility that |
3859 |
|
# there are no user-supplied parameters. |
3860 |
|
my @params = ($newValue, $oldValue); |
3861 |
|
if (defined $parms) { |
3862 |
|
push @params, @{$parms}; |
3863 |
|
} |
3864 |
|
# Execute the update. |
3865 |
|
my $retVal = $dbh->SQL($command, 0, @params); |
3866 |
|
# Make the funky zero a real zero. |
3867 |
|
if ($retVal == 0) { |
3868 |
|
$retVal = 0; |
3869 |
|
} |
3870 |
|
# Return the result. |
3871 |
|
return $retVal; |
3872 |
|
} |
3873 |
|
|
3874 |
|
|
3875 |
=head2 Data Mining Methods |
=head2 Data Mining Methods |
3876 |
|
|
3877 |
=head3 GetUsefulCrossValues |
=head3 GetUsefulCrossValues |
3878 |
|
|
3879 |
C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >> |
my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); |
3880 |
|
|
3881 |
Return a list of the useful attributes that would be returned by a B<Cross> call |
Return a list of the useful attributes that would be returned by a B<Cross> call |
3882 |
from an entity of the source entity type through the specified relationship. This |
from an entity of the source entity type through the specified relationship. This |
3937 |
|
|
3938 |
=head3 FindColumn |
=head3 FindColumn |
3939 |
|
|
3940 |
C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >> |
my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); |
3941 |
|
|
3942 |
Return the location a desired column in a data mining header line. The data |
Return the location a desired column in a data mining header line. The data |
3943 |
mining header line is a tab-separated list of column names. The column |
mining header line is a tab-separated list of column names. The column |
3995 |
|
|
3996 |
=head3 ParseColumns |
=head3 ParseColumns |
3997 |
|
|
3998 |
C<< my @columns = ERDB::ParseColumns($line); >> |
my @columns = ERDB::ParseColumns($line); |
3999 |
|
|
4000 |
Convert the specified data line to a list of columns. |
Convert the specified data line to a list of columns. |
4001 |
|
|
4027 |
|
|
4028 |
=head2 Virtual Methods |
=head2 Virtual Methods |
4029 |
|
|
4030 |
|
=head3 _CreatePPOIndex |
4031 |
|
|
4032 |
|
my $index = ERDB::_CreatePPOIndex($indexObject); |
4033 |
|
|
4034 |
|
Convert the XML for an ERDB index to the XML structure for a PPO |
4035 |
|
index. |
4036 |
|
|
4037 |
|
=over 4 |
4038 |
|
|
4039 |
|
=item indexObject |
4040 |
|
|
4041 |
|
ERDB XML structure for an index. |
4042 |
|
|
4043 |
|
=item RETURN |
4044 |
|
|
4045 |
|
PPO XML structure for the same index. |
4046 |
|
|
4047 |
|
=back |
4048 |
|
|
4049 |
|
=cut |
4050 |
|
|
4051 |
|
sub _CreatePPOIndex { |
4052 |
|
# Get the parameters. |
4053 |
|
my ($indexObject) = @_; |
4054 |
|
# The incoming index contains a list of the index fields in the IndexFields |
4055 |
|
# member. We loop through it to create the index tags. |
4056 |
|
my @fields = map { { label => _FixName($_->{name}) } } @{$indexObject->{IndexFields}}; |
4057 |
|
# Wrap the fields in attribute tags. |
4058 |
|
my $retVal = { attribute => \@fields }; |
4059 |
|
# Return the result. |
4060 |
|
return $retVal; |
4061 |
|
} |
4062 |
|
|
4063 |
|
=head3 _CreatePPOField |
4064 |
|
|
4065 |
|
my $fieldXML = ERDB::_CreatePPOField($fieldName, $fieldObject); |
4066 |
|
|
4067 |
|
Convert the ERDB XML structure for a field to a PPO scalar XML structure. |
4068 |
|
|
4069 |
|
=over 4 |
4070 |
|
|
4071 |
|
=item fieldName |
4072 |
|
|
4073 |
|
Name of the scalar field. |
4074 |
|
|
4075 |
|
=item fieldObject |
4076 |
|
|
4077 |
|
ERDB XML structure describing the field. |
4078 |
|
|
4079 |
|
=item RETURN |
4080 |
|
|
4081 |
|
Returns a PPO XML structure for the same field. |
4082 |
|
|
4083 |
|
=back |
4084 |
|
|
4085 |
|
=cut |
4086 |
|
|
4087 |
|
sub _CreatePPOField { |
4088 |
|
# Get the parameters. |
4089 |
|
my ($fieldName, $fieldObject) = @_; |
4090 |
|
# Get the field type. |
4091 |
|
my $type = $TypeTable{$fieldObject->{type}}->{sqlType}; |
4092 |
|
# Fix up the field name. |
4093 |
|
$fieldName = _FixName($fieldName); |
4094 |
|
# Build the scalar tag. |
4095 |
|
my $retVal = { label => $fieldName, type => $type }; |
4096 |
|
# Return the result. |
4097 |
|
return $retVal; |
4098 |
|
} |
4099 |
|
|
4100 |
=head3 CleanKeywords |
=head3 CleanKeywords |
4101 |
|
|
4102 |
C<< my $cleanedString = $erdb->CleanKeywords($searchExpression); >> |
my $cleanedString = $erdb->CleanKeywords($searchExpression); |
4103 |
|
|
4104 |
Clean up a search expression or keyword list. This is a virtual method that may |
Clean up a search expression or keyword list. This is a virtual method that may |
4105 |
be overridden by the subclass. The base-class method removes extra spaces |
be overridden by the subclass. The base-class method removes extra spaces |
4136 |
|
|
4137 |
=head3 GetSourceObject |
=head3 GetSourceObject |
4138 |
|
|
4139 |
C<< my $source = $erdb->GetSourceObject($entityName); >> |
my $source = $erdb->GetSourceObject($entityName); |
4140 |
|
|
4141 |
Return the object to be used in loading special attributes of the specified entity. The |
Return the object to be used in loading special attributes of the specified entity. The |
4142 |
algorithm for loading special attributes is stored in the C<DataGen> elements of the |
algorithm for loading special attributes is stored in the C<DataGen> elements of the |
4146 |
|
|
4147 |
=head3 _RelationMap |
=head3 _RelationMap |
4148 |
|
|
4149 |
C<< my @relationMap = _RelationMap($mappedNameHashRef, $mappedNameListRef); >> |
my @relationMap = _RelationMap($mappedNameHashRef, $mappedNameListRef); |
4150 |
|
|
4151 |
Create the relation map for an SQL query. The relation map is used by B<DBObject> |
Create the relation map for an SQL query. The relation map is used by B<ERDBObject> |
4152 |
to determine how to interpret the results of the query. |
to determine how to interpret the results of the query. |
4153 |
|
|
4154 |
=over 4 |
=over 4 |
4165 |
=item RETURN |
=item RETURN |
4166 |
|
|
4167 |
Returns a list of 2-tuples. Each tuple consists of an object name as used in the |
Returns a list of 2-tuples. Each tuple consists of an object name as used in the |
4168 |
query followed by the actual name of that object. This enables the B<DBObject> to |
query followed by the actual name of that object. This enables the B<ERDBObject> to |
4169 |
determine the order of the tables in the query and which object name belongs to each |
determine the order of the tables in the query and which object name belongs to each |
4170 |
mapped object name. Most of the time these two values are the same; however, if a |
mapped object name. Most of the time these two values are the same; however, if a |
4171 |
relation occurs twice in the query, the relation name in the field list and WHERE |
relation occurs twice in the query, the relation name in the field list and WHERE |
4459 |
sub _GetStatementHandle { |
sub _GetStatementHandle { |
4460 |
# Get the parameters. |
# Get the parameters. |
4461 |
my ($self, $command, $params) = @_; |
my ($self, $command, $params) = @_; |
4462 |
|
Confess("Invalid parameter list.") if (! defined($params) || ref($params) ne 'ARRAY'); |
4463 |
# Trace the query. |
# Trace the query. |
4464 |
Trace("SQL query: $command") if T(SQL => 3); |
Trace("SQL query: $command") if T(SQL => 3); |
4465 |
Trace("PARMS: '" . (join "', '", @{$params}) . "'") if (T(SQL => 4) && (@{$params} > 0)); |
Trace("PARMS: '" . (join "', '", @{$params}) . "'") if (T(SQL => 4) && (@{$params} > 0)); |
4697 |
# be a null string. |
# be a null string. |
4698 |
if ($fileName ne "") { |
if ($fileName ne "") { |
4699 |
# Load the relation from the file. |
# Load the relation from the file. |
4700 |
$retVal = $self->LoadTable($fileName, $relationName, $rebuild); |
$retVal = $self->LoadTable($fileName, $relationName, truncate => $rebuild); |
4701 |
} elsif ($rebuild) { |
} elsif ($rebuild) { |
4702 |
# Here we are rebuilding, but no file exists, so we just re-create the table. |
# Here we are rebuilding, but no file exists, so we just re-create the table. |
4703 |
$self->CreateTable($relationName, 1); |
$self->CreateTable($relationName, 1); |
4709 |
|
|
4710 |
=head3 _LoadMetaData |
=head3 _LoadMetaData |
4711 |
|
|
4712 |
C<< my $metadata = ERDB::_LoadMetaData($filename); >> |
my $metadata = ERDB::_LoadMetaData($filename); |
4713 |
|
|
4714 |
This method loads the data describing this database from an XML file into a metadata structure. |
This method loads the data describing this database from an XML file into a metadata structure. |
4715 |
The resulting structure is a set of nested hash tables containing all the information needed to |
The resulting structure is a set of nested hash tables containing all the information needed to |
5067 |
|
|
5068 |
=head3 _ProcessIndexes |
=head3 _ProcessIndexes |
5069 |
|
|
5070 |
C<< ERDB::_ProcessIndexes($indexList, $relation); >> |
ERDB::_ProcessIndexes($indexList, $relation); |
5071 |
|
|
5072 |
Build the data structures for the specified indexes in the specified relation. |
Build the data structures for the specified indexes in the specified relation. |
5073 |
|
|
5444 |
return $retVal; |
return $retVal; |
5445 |
} |
} |
5446 |
|
|
5447 |
=head2 HTML Documentation Utility Methods |
=head2 Documentation Utility Methods |
5448 |
|
|
5449 |
=head3 _ComputeRelationshipSentence |
=head3 _ComputeRelationshipSentence |
5450 |
|
|
5476 |
# Get the parameters. |
# Get the parameters. |
5477 |
my ($relationshipName, $relationshipStructure) = @_; |
my ($relationshipName, $relationshipStructure) = @_; |
5478 |
# Format the relationship sentence. |
# Format the relationship sentence. |
5479 |
my $result = "$relationshipStructure->{from} <b>$relationshipName</b> $relationshipStructure->{to}"; |
my $result = "$relationshipStructure->{from} $relationshipName $relationshipStructure->{to}"; |
5480 |
# Compute the arity. |
# Compute the arity. |
5481 |
my $arityCode = $relationshipStructure->{arity}; |
my $arityCode = $relationshipStructure->{arity}; |
5482 |
my $arity = $ArityTable{$arityCode}; |
my $arity = $ArityTable{$arityCode}; |
5521 |
return $result; |
return $result; |
5522 |
} |
} |
5523 |
|
|
5524 |
|
=head3 _WikiRelationTable |
5525 |
|
|
5526 |
|
Generate the Wiki text for a particular relation. The relation's data will be formatted as a |
5527 |
|
table with three columns-- the field name, the field type, and the field description. |
5528 |
|
|
5529 |
|
This is a static method. |
5530 |
|
|
5531 |
|
=over 4 |
5532 |
|
|
5533 |
|
=item relationName |
5534 |
|
|
5535 |
|
Name of the relation being formatted. |
5536 |
|
|
5537 |
|
=item relationData |
5538 |
|
|
5539 |
|
Hash containing the relation's fields and indexes. |
5540 |
|
|
5541 |
|
=item RETURN |
5542 |
|
|
5543 |
|
Returns a Wiki string that can be used to display the relation name and all of its fields. |
5544 |
|
|
5545 |
|
=back |
5546 |
|
|
5547 |
|
=cut |
5548 |
|
|
5549 |
|
sub _WikiRelationTable { |
5550 |
|
# Get the parameters. |
5551 |
|
my ($relationName, $relationData) = @_; |
5552 |
|
# We'll create a list of lists in here, then call WikiTools::Table to |
5553 |
|
# convert it into a table. |
5554 |
|
my @rows = (); |
5555 |
|
# Push in the header row. |
5556 |
|
push @rows, [qw(Field Type Description)]; |
5557 |
|
# Loop through the fields. |
5558 |
|
for my $field (@{$relationData->{Fields}}) { |
5559 |
|
# Create this field's row. We always have a name and type. |
5560 |
|
my @row = ($field->{name}, $field->{type}); |
5561 |
|
# If we have a description, add it as the third column. |
5562 |
|
if (exists $field->{Notes}) { |
5563 |
|
push @row, WikiNote($field->{Notes}->{content}); |
5564 |
|
} |
5565 |
|
# Push this row onto the table list. |
5566 |
|
push @rows, \@row; |
5567 |
|
} |
5568 |
|
# Store the rows as a Wiki table with a level-4 heading. |
5569 |
|
my $retVal = join("\n\n", WikiTools::Heading(4, "$relationName Table"), |
5570 |
|
WikiTools::Table(@rows)); |
5571 |
|
# Now we show the relation's indexes. These are formatted as another |
5572 |
|
# table. |
5573 |
|
@rows = (); |
5574 |
|
# Push in the header row. |
5575 |
|
push @rows, [qw(Index Unique Fields Notes)]; |
5576 |
|
# Get the index hash. |
5577 |
|
my $indexTable = $relationData->{Indexes}; |
5578 |
|
# Loop through the indexes. For an entity, there is always at least one index. |
5579 |
|
# For a relationship, there are at least two. The upshot is we don't need to |
5580 |
|
# worry about accidentally generating a frivolous table here. |
5581 |
|
for my $indexName (sort keys %$indexTable) { |
5582 |
|
my $indexData = $indexTable->{$indexName}; |
5583 |
|
# Determine whether or not the index is unique. |
5584 |
|
my $unique = ((exists $indexData->{Unique} && $indexData->{Unique} eq "true") ? |
5585 |
|
"yes" : ""); |
5586 |
|
# Get the field list. |
5587 |
|
my $fields = join(', ', @{$indexData->{IndexFields}}); |
5588 |
|
# Get the note text. |
5589 |
|
my $description = ""; |
5590 |
|
if (my $note = $indexData->{Notes}) { |
5591 |
|
$description = WikiNote($note->{content}); |
5592 |
|
} |
5593 |
|
# Format this row. |
5594 |
|
my @row = ($indexName, $unique, $fields, $description); |
5595 |
|
push @rows, \@row; |
5596 |
|
} |
5597 |
|
# Add the index list to the result. |
5598 |
|
$retVal .= "\n\n" . WikiTools::Table(@rows); |
5599 |
|
} |
5600 |
|
|
5601 |
=head3 _ShowRelationTable |
=head3 _ShowRelationTable |
5602 |
|
|
5603 |
Generate the HTML string for a particular relation. The relation's data will be formatted as an HTML |
Generate the HTML string for a particular relation. The relation's data will be formatted as an HTML |
5771 |
return $htmlString; |
return $htmlString; |
5772 |
} |
} |
5773 |
|
|
5774 |
|
=head3 _ObjectNotes |
5775 |
|
|
5776 |
|
my @noteParagraphs = _ObjectNotes($objectData); |
5777 |
|
|
5778 |
|
Return a list of the notes and asides for an entity or relationship in |
5779 |
|
Wiki format. |
5780 |
|
|
5781 |
|
=over 4 |
5782 |
|
|
5783 |
|
=item objectData |
5784 |
|
|
5785 |
|
The metadata for the desired entity or relationship. |
5786 |
|
|
5787 |
|
=item RETURN |
5788 |
|
|
5789 |
|
Returns a list of text paragraphs in Wiki markup form. |
5790 |
|
|
5791 |
|
=back |
5792 |
|
|
5793 |
|
=cut |
5794 |
|
|
5795 |
|
sub _ObjectNotes { |
5796 |
|
# Get the parameters. |
5797 |
|
my ($objectData) = @_; |
5798 |
|
# Declare the return variable. |
5799 |
|
my @retVal; |
5800 |
|
# Loop through the types of notes. |
5801 |
|
for my $noteType (qw(Notes Asides)) { |
5802 |
|
my $text = $objectData->{$noteType}; |
5803 |
|
if ($text) { |
5804 |
|
push @retVal, "", WikiNote($text->{content}); |
5805 |
|
} |
5806 |
|
} |
5807 |
|
# Return the result. |
5808 |
|
return @retVal; |
5809 |
|
} |
5810 |
|
|
5811 |
1; |
1; |