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); |
13 |
use FIG; |
use CGI; |
14 |
|
# use WikiTools; ## HACK |
15 |
|
|
16 |
=head1 Entity-Relationship Database Package |
=head1 Entity-Relationship Database Package |
17 |
|
|
60 |
B<start-position>, which indicates where in the contig that the sequence begins. This attribute |
B<start-position>, which indicates where in the contig that the sequence begins. This attribute |
61 |
is implemented as the C<start_position> field in the C<IsMadeUpOf> relation. |
is implemented as the C<start_position> field in the C<IsMadeUpOf> relation. |
62 |
|
|
63 |
The database itself is described by an XML file using the F<ERDatabase.xsd> schema. In addition to |
The database itself is described by an XML file. In addition to all the data required to define |
64 |
all the data required to define the entities, relationships, and attributes, the schema provides |
the entities, relationships, and attributes, the schema provides space for notes describing |
65 |
space for notes describing the data and what it means. These notes are used by L</ShowMetaData> |
the data and what it means. These notes are used by L</ShowMetaData> to generate documentation |
66 |
to generate documentation for the database. |
for the database. |
67 |
|
|
68 |
|
Special support is provided for text searching. An entity field can be marked as <em>searchable</em>, |
69 |
|
in which case it will be used to generate a text search index in which the user searches for words |
70 |
|
in the field instead of a particular field value. |
71 |
|
|
72 |
Finally, every entity and relationship object has a flag indicating if it is new or old. The object |
Finally, every entity and relationship object has a flag indicating if it is new or old. The object |
73 |
is considered I<old> if it was loaded by the L</LoadTables> method. It is considered I<new> if it |
is considered I<old> if it was loaded by the L</LoadTables> method. It is considered I<new> if it |
74 |
was inserted by the L</InsertObject> method. |
was inserted by the L</InsertObject> method. |
75 |
|
|
|
To facilitate testing, the ERDB module supports automatic generation of test data. This process |
|
|
is described in the L</GenerateEntity> and L</GenerateConnection> methods, though it is not yet |
|
|
fully implemented. |
|
|
|
|
76 |
=head2 XML Database Description |
=head2 XML Database Description |
77 |
|
|
78 |
=head3 Data Types |
=head3 Data Types |
92 |
|
|
93 |
32-bit signed integer |
32-bit signed integer |
94 |
|
|
95 |
|
=item counter |
96 |
|
|
97 |
|
32-bit unsigned integer |
98 |
|
|
99 |
=item date |
=item date |
100 |
|
|
101 |
64-bit unsigned integer, representing a PERL date/time value |
64-bit unsigned integer, representing a PERL date/time value |
191 |
|
|
192 |
Name of the field. The field name should contain only letters, digits, and hyphens (C<->), |
Name of the field. The field name should contain only letters, digits, and hyphens (C<->), |
193 |
and the first character should be a letter. Most underlying databases are case-insensitive |
and the first character should be a letter. Most underlying databases are case-insensitive |
194 |
with the respect to field names, so a best practice is to use lower-case letters only. |
with the respect to field names, so a best practice is to use lower-case letters only. Finally, |
195 |
|
the name C<search-relevance> has special meaning for full-text searches and should not be |
196 |
|
used as a field name. |
197 |
|
|
198 |
=item type |
=item type |
199 |
|
|
212 |
entity, the fields without a relation attribute are said to belong to the |
entity, the fields without a relation attribute are said to belong to the |
213 |
I<primary relation>. This relation has the same name as the entity itself. |
I<primary relation>. This relation has the same name as the entity itself. |
214 |
|
|
215 |
|
=item searchable |
216 |
|
|
217 |
|
If specified, then the field is a candidate for full-text searching. A single full-text |
218 |
|
index will be created for each relation with at least one searchable field in it. |
219 |
|
For best results, this option should only be used for string or text fields. |
220 |
|
|
221 |
|
=item special |
222 |
|
|
223 |
|
This attribute allows the subclass to assign special meaning for certain fields. |
224 |
|
The interpretation is up to the subclass itself. Currently, only entity fields |
225 |
|
can have this attribute. |
226 |
|
|
227 |
=back |
=back |
228 |
|
|
229 |
=head3 Indexes |
=head3 Indexes |
230 |
|
|
231 |
An entity can have multiple alternate indexes associated with it. The fields must |
An entity can have multiple alternate indexes associated with it. The fields in an |
232 |
be from the primary relation. The alternate indexes assist in ordering results |
index must all be from the same relation. The alternate indexes assist in searching |
233 |
from a query. A relationship can have up to two indexes-- a I<to-index> and a |
on fields other than the entity ID. A relationship has at least two indexes-- a I<to-index> and a |
234 |
I<from-index>. These order the results when crossing the relationship. For |
I<from-index> that order the results when crossing the relationship. For |
235 |
example, in the relationship C<HasContig> from C<Genome> to C<Contig>, the |
example, in the relationship C<HasContig> from C<Genome> to C<Contig>, the |
236 |
from-index would order the contigs of a ganome, and the to-index would order |
from-index would order the contigs of a ganome, and the to-index would order |
237 |
the genomes of a contig. A relationship's index must specify only fields in |
the genomes of a contig. In addition, it can have zero or more alternate |
238 |
|
indexes. A relationship's index must specify only fields in |
239 |
the relationship. |
the relationship. |
240 |
|
|
241 |
The indexes for an entity must be listed inside the B<Indexes> tag. The from-index |
The alternate indexes for an entity or relationship must be listed inside the B<Indexes> tag. |
242 |
of a relationship is specified using the B<FromIndex> tag; the to-index is specified |
The from-index of a relationship is specified using the B<FromIndex> tag; the to-index is |
243 |
using the B<ToIndex> tag. |
specified using the B<ToIndex> tag. |
244 |
|
|
245 |
Each index can contain a B<Notes> tag. In addition, it will have an B<IndexFields> |
Each index can contain a B<Notes> tag. In addition, it will have an B<IndexFields> |
246 |
tag containing the B<IndexField> tags. These specify, in order, the fields used in |
tag containing the B<IndexField> tags. These specify, in order, the fields used in |
258 |
|
|
259 |
=back |
=back |
260 |
|
|
261 |
The B<Index>, B<FromIndex>, and B<ToIndex> tags themselves have no attributes. |
The B<FromIndex>, and B<ToIndex> tags have no attributes. The B<Index> tag can |
262 |
|
have a B<Unique> attribute. If specified, the index will be generated as a unique |
263 |
|
index. |
264 |
|
|
265 |
=head3 Object and Field Names |
=head3 Object and Field Names |
266 |
|
|
304 |
|
|
305 |
A relationship is described by the C<Relationship> tag. Within a relationship, |
A relationship is described by the C<Relationship> tag. Within a relationship, |
306 |
there can be a C<Notes> tag, a C<Fields> tag containing the intersection data |
there can be a C<Notes> tag, a C<Fields> tag containing the intersection data |
307 |
fields, a C<FromIndex> tag containing the from-index, and a C<ToIndex> tag containing |
fields, a C<FromIndex> tag containing the from-index, a C<ToIndex> tag containing |
308 |
the to-index. |
the to-index, and an C<Indexes> tag containing the alternate indexes. |
309 |
|
|
310 |
The C<Relationship> tag has the following attributes. |
The C<Relationship> tag has the following attributes. |
311 |
|
|
338 |
|
|
339 |
# Table of information about our datatypes. "sqlType" is the corresponding SQL datatype string. |
# Table of information about our datatypes. "sqlType" is the corresponding SQL datatype string. |
340 |
# "maxLen" is the maximum permissible length of the incoming string data used to populate a field |
# "maxLen" is the maximum permissible length of the incoming string data used to populate a field |
341 |
# of the specified type. "dataGen" is PERL string that will be evaluated if no test data generation |
# of the specified type. "avgLen" is the average byte length for estimating |
342 |
# string is specified in the field definition. "avgLen" is the average byte length for estimating |
# record sizes. "sort" is the key modifier for the sort command, "notes" is a type description, |
343 |
# record sizes. |
# and "indexMod", if non-zero, is the number of characters to use when the field is specified in an |
344 |
my %TypeTable = ( char => { sqlType => 'CHAR(1)', maxLen => 1, avgLen => 1, dataGen => "StringGen('A')" }, |
# index |
345 |
int => { sqlType => 'INTEGER', maxLen => 20, avgLen => 4, dataGen => "IntGen(0, 99999999)" }, |
my %TypeTable = ( char => { sqlType => 'CHAR(1)', maxLen => 1, avgLen => 1, sort => "", |
346 |
string => { sqlType => 'VARCHAR(255)', maxLen => 255, avgLen => 100, dataGen => "StringGen(IntGen(10,250))" }, |
indexMod => 0, notes => "single ASCII character"}, |
347 |
text => { sqlType => 'TEXT', maxLen => 1000000000, avgLen => 500, dataGen => "StringGen(IntGen(80,1000))" }, |
int => { sqlType => 'INTEGER', maxLen => 20, avgLen => 4, sort => "n", |
348 |
date => { sqlType => 'BIGINT', maxLen => 80, avgLen => 8, dataGen => "DateGen(-7, 7, IntGen(0,1400))" }, |
indexMod => 0, notes => "signed 32-bit integer"}, |
349 |
float => { sqlType => 'DOUBLE PRECISION', maxLen => 40, avgLen => 8, dataGen => "FloatGen(0.0, 100.0)" }, |
counter => { sqlType => 'INTEGER UNSIGNED', maxLen => 20, avgLen => 4, sort => "n", |
350 |
boolean => { sqlType => 'SMALLINT', maxLen => 1, avgLen => 1, dataGen => "IntGen(0, 1)" }, |
indexMod => 0, notes => "unsigned 32-bit integer"}, |
351 |
|
string => { sqlType => 'VARCHAR(255)', maxLen => 255, avgLen => 100, sort => "", |
352 |
|
indexMod => 0, notes => "character string, 0 to 255 characters"}, |
353 |
|
text => { sqlType => 'TEXT', maxLen => 1000000000, avgLen => 500, sort => "", |
354 |
|
indexMod => 255, notes => "character string, nearly unlimited length, only first 255 characters are indexed"}, |
355 |
|
date => { sqlType => 'BIGINT', maxLen => 80, avgLen => 8, sort => "n", |
356 |
|
indexMod => 0, notes => "signed, 64-bit integer"}, |
357 |
|
float => { sqlType => 'DOUBLE PRECISION', maxLen => 40, avgLen => 8, sort => "g", |
358 |
|
indexMod => 0, notes => "64-bit double precision floating-point number"}, |
359 |
|
boolean => { sqlType => 'SMALLINT', maxLen => 1, avgLen => 1, sort => "n", |
360 |
|
indexMod => 0, notes => "boolean value: 0 if false, 1 if true"}, |
361 |
'hash-string' => |
'hash-string' => |
362 |
{ sqlType => 'VARCHAR(22)', maxLen => 22, avgLen => 22, dataGen => "SringGen(22)" }, |
{ sqlType => 'VARCHAR(22)', maxLen => 22, avgLen => 22, sort => "", |
363 |
|
indexMod => 0, notes => "string stored in digested form, used for certain types of key fields"}, |
364 |
'id-string' => |
'id-string' => |
365 |
{ sqlType => 'VARCHAR(25)', maxLen => 25, avgLen => 25, dataGen => "SringGen(22)" }, |
{ sqlType => 'VARCHAR(25)', maxLen => 25, avgLen => 25, sort => "", |
366 |
|
indexMod => 0, notes => "character string, 0 to 25 characters"}, |
367 |
'key-string' => |
'key-string' => |
368 |
{ sqlType => 'VARCHAR(40)', maxLen => 40, avgLen => 10, dataGen => "StringGen(IntGen(10,40))" }, |
{ sqlType => 'VARCHAR(40)', maxLen => 40, avgLen => 10, sort => "", |
369 |
|
indexMod => 0, notes => "character string, 0 to 40 characters"}, |
370 |
'name-string' => |
'name-string' => |
371 |
{ sqlType => 'VARCHAR(80)', maxLen => 80, avgLen => 40, dataGen => "StringGen(IntGen(10,80))" }, |
{ sqlType => 'VARCHAR(80)', maxLen => 80, avgLen => 40, sort => "", |
372 |
|
indexMod => 0, notes => "character string, 0 to 80 characters"}, |
373 |
'medium-string' => |
'medium-string' => |
374 |
{ sqlType => 'VARCHAR(160)', maxLen => 160, avgLen => 40, dataGen => "StringGen(IntGen(10,160))" }, |
{ sqlType => 'VARCHAR(160)', maxLen => 160, avgLen => 40, sort => "", |
375 |
|
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. |
384 |
'MM' => 'many-to-many' |
'MM' => 'many-to-many' |
385 |
); |
); |
386 |
|
|
387 |
# Table for interpreting string patterns. |
# Options for XML input and output. |
388 |
|
|
389 |
|
my %XmlOptions = (GroupTags => { Relationships => 'Relationship', |
390 |
|
Entities => 'Entity', |
391 |
|
Fields => 'Field', |
392 |
|
Indexes => 'Index', |
393 |
|
IndexFields => 'IndexField' |
394 |
|
}, |
395 |
|
KeyAttr => { Relationship => 'name', |
396 |
|
Entity => 'name', |
397 |
|
Field => 'name' |
398 |
|
}, |
399 |
|
SuppressEmpty => 1, |
400 |
|
); |
401 |
|
|
402 |
my %PictureTable = ( 'A' => "abcdefghijklmnopqrstuvwxyz", |
my %XmlInOpts = ( |
403 |
'9' => "0123456789", |
ForceArray => ['Field', 'Index', 'IndexField', 'Relationship', 'Entity'], |
404 |
'X' => "abcdefghijklmnopqrstuvwxyz0123456789", |
ForceContent => 1, |
405 |
'V' => "aeiou", |
NormalizeSpace => 2, |
406 |
'K' => "bcdfghjklmnoprstvwxyz" |
); |
407 |
|
my %XmlOutOpts = ( |
408 |
|
RootName => 'Database', |
409 |
|
XMLDecl => 1, |
410 |
); |
); |
411 |
|
|
412 |
|
|
413 |
=head2 Public Methods |
=head2 Public Methods |
414 |
|
|
415 |
=head3 new |
=head3 new |
416 |
|
|
417 |
C<< my $database = ERDB->new($dbh, $metaFileName); >> |
my $database = ERDB->new($dbh, $metaFileName); |
418 |
|
|
419 |
Create a new ERDB object. |
Create a new ERDB object. |
420 |
|
|
434 |
|
|
435 |
sub new { |
sub new { |
436 |
# Get the parameters. |
# Get the parameters. |
437 |
my ($class, $dbh, $metaFileName, $options) = @_; |
my ($class, $dbh, $metaFileName, %options) = @_; |
438 |
# Load the meta-data. |
# Load the meta-data. |
439 |
my $metaData = _LoadMetaData($metaFileName); |
my $metaData = _LoadMetaData($metaFileName); |
440 |
# Create the object. |
# Create the object. |
448 |
|
|
449 |
=head3 ShowMetaData |
=head3 ShowMetaData |
450 |
|
|
451 |
C<< $erdb->ShowMetaData($fileName); >> |
$erdb->ShowMetaData($fileName); |
452 |
|
|
453 |
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 |
454 |
the data to be loaded into the relations. |
the data to be loaded into the relations. |
489 |
|
|
490 |
=head3 DisplayMetaData |
=head3 DisplayMetaData |
491 |
|
|
492 |
C<< my $html = $erdb->DisplayMetaData(); >> |
my $html = $erdb->DisplayMetaData(); |
493 |
|
|
494 |
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 |
495 |
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 |
550 |
my $entityData = $entityList->{$key}; |
my $entityData = $entityList->{$key}; |
551 |
# If there's descriptive text, display it. |
# If there's descriptive text, display it. |
552 |
if (my $notes = $entityData->{Notes}) { |
if (my $notes = $entityData->{Notes}) { |
553 |
$retVal .= "<p>" . _HTMLNote($notes->{content}) . "</p>\n"; |
$retVal .= "<p>" . HTMLNote($notes->{content}) . "</p>\n"; |
554 |
} |
} |
555 |
# Now we want a list of the entity's relationships. First, we set up the relationship subsection. |
# See if we need a list of the entity's relationships. |
556 |
|
my $relCount = keys %{$relationshipList}; |
557 |
|
if ($relCount > 0) { |
558 |
|
# First, we set up the relationship subsection. |
559 |
$retVal .= "<h4>Relationships for <b>$key</b></h4>\n<ul>\n"; |
$retVal .= "<h4>Relationships for <b>$key</b></h4>\n<ul>\n"; |
560 |
# Loop through the relationships. |
# Loop through the relationships. |
561 |
for my $relationship (sort keys %{$relationshipList}) { |
for my $relationship (sort keys %{$relationshipList}) { |
571 |
} |
} |
572 |
# Close off the relationship list. |
# Close off the relationship list. |
573 |
$retVal .= "</ul>\n"; |
$retVal .= "</ul>\n"; |
574 |
|
} |
575 |
# Get the entity's relations. |
# Get the entity's relations. |
576 |
my $relationList = $entityData->{Relations}; |
my $relationList = $entityData->{Relations}; |
577 |
# Create a header for the relation subsection. |
# Create a header for the relation subsection. |
611 |
$retVal .= "</p>\n"; |
$retVal .= "</p>\n"; |
612 |
# If there are notes on this relationship, display them. |
# If there are notes on this relationship, display them. |
613 |
if (my $notes = $relationshipStructure->{Notes}) { |
if (my $notes = $relationshipStructure->{Notes}) { |
614 |
$retVal .= "<p>" . _HTMLNote($notes->{content}) . "</p>\n"; |
$retVal .= "<p>" . HTMLNote($notes->{content}) . "</p>\n"; |
615 |
} |
} |
616 |
# Generate the relationship's relation table. |
# Generate the relationship's relation table. |
617 |
my $htmlString = _ShowRelationTable($key, $relationshipStructure->{Relations}->{$key}); |
my $htmlString = _ShowRelationTable($key, $relationshipStructure->{Relations}->{$key}); |
645 |
|
|
646 |
=head3 DumpMetaData |
=head3 DumpMetaData |
647 |
|
|
648 |
C<< $erdb->DumpMetaData(); >> |
$erdb->DumpMetaData(); |
649 |
|
|
650 |
Return a dump of the metadata structure. |
Return a dump of the metadata structure. |
651 |
|
|
658 |
return Data::Dumper::Dumper($self->{_metaData}); |
return Data::Dumper::Dumper($self->{_metaData}); |
659 |
} |
} |
660 |
|
|
661 |
|
=head3 GenerateWikiData |
662 |
|
|
663 |
|
my @wikiLines = $erdb->GenerateWikiData(); |
664 |
|
|
665 |
|
Build a description of the database for the wiki. The database will be |
666 |
|
organized into a single page, with sections for each entity and relationship. |
667 |
|
The return value is a list of text lines. |
668 |
|
|
669 |
|
=cut |
670 |
|
|
671 |
|
sub GenerateWikiData { |
672 |
|
# Get the parameters. |
673 |
|
my ($self) = @_; |
674 |
|
# We'll build the wiki text in here. |
675 |
|
my @retVal = (); |
676 |
|
# Get the metadata object. |
677 |
|
my $metadata = $self->{_metaData}; |
678 |
|
# Get the title string. This will become the page name. |
679 |
|
my $title = $metadata->{Title}->{content}; |
680 |
|
# Get the entity and relationship lists. |
681 |
|
my $entityList = $metadata->{Entities}; |
682 |
|
my $relationshipList = $metadata->{Relationships}; |
683 |
|
# Start with the introductory text. |
684 |
|
push @retVal, WikiTools::Heading(2, "Introduction"); |
685 |
|
if (my $notes = $metadata->{Notes}) { |
686 |
|
push @retVal, WikiNote($notes->{content}); |
687 |
|
} |
688 |
|
# Start the entity section. |
689 |
|
push @retVal, WikiTools::Heading(2, "Entities"); |
690 |
|
# Loop through the entities. Note that unlike the situation with HTML, we |
691 |
|
# don't need to generate the table of contents manually, just the data |
692 |
|
# itself. |
693 |
|
for my $key (sort keys %$entityList) { |
694 |
|
# Create a header for this entity. |
695 |
|
push @retVal, "", WikiTools::Heading(3, $key); |
696 |
|
# Get the entity data. |
697 |
|
my $entityData = $entityList->{$key}; |
698 |
|
# Plant the notes here, if there are any. |
699 |
|
if (my $notes = $entityData->{Notes}) { |
700 |
|
push @retVal, "", WikiNote($notes->{content}); |
701 |
|
} |
702 |
|
# Get the entity's relations. |
703 |
|
my $relationList = $entityData->{Relations}; |
704 |
|
# Loop through the relations, displaying them. |
705 |
|
for my $relation (sort keys %{$relationList}) { |
706 |
|
my $wikiString = _WikiRelationTable($relation, $relationList->{$relation}); |
707 |
|
push @retVal, $wikiString; |
708 |
|
} |
709 |
|
# Now we list the entity's relationships (if any). First, we build a list |
710 |
|
# of the relationships relevant to this entity. |
711 |
|
my @rels = (); |
712 |
|
for my $rel (sort keys %$relationshipList) { |
713 |
|
my $relStructure = $relationshipList->{$rel}; |
714 |
|
if ($relStructure->{from} eq $key || $relStructure->{to} eq $key) { |
715 |
|
# Get the relationship sentence. |
716 |
|
my $relSentence = _ComputeRelationshipSentence($rel, $relStructure); |
717 |
|
# Linkify it. |
718 |
|
my $linkedRel = WikiTools::LinkMarkup("#$rel", $rel); |
719 |
|
$relSentence =~ s/$rel/$linkedRel/; |
720 |
|
push @rels, $relSentence; |
721 |
|
} |
722 |
|
} |
723 |
|
# Add the relationships as a Wiki list. |
724 |
|
push @retVal, WikiTools::List(@rels); |
725 |
|
} |
726 |
|
# Now the entities are documented. Next we do the relationships. |
727 |
|
push @retVal, WikiTools::Heading(2, "Relationships"); |
728 |
|
for my $key (sort keys %$relationshipList) { |
729 |
|
my $relationshipData = $relationshipList->{$key}; |
730 |
|
# Create the relationship heading. |
731 |
|
push @retVal, WikiTools::Heading(3, $key); |
732 |
|
# Describe the relationship arity. Note there's a bit of trickiness involving recursive |
733 |
|
# many-to-many relationships. In a normal many-to-many we use two sentences to describe |
734 |
|
# the arity (one for each direction). This is a bad idea for a recursive relationship, |
735 |
|
# since both sentences will say the same thing. |
736 |
|
my $arity = $relationshipData->{arity}; |
737 |
|
my $fromEntity = $relationshipData->{from}; |
738 |
|
my $toEntity = $relationshipData->{to}; |
739 |
|
my @listElements = (); |
740 |
|
my $boldCode = WikiTools::BoldCode(); |
741 |
|
if ($arity eq "11") { |
742 |
|
push @listElements, "Each $boldCode$fromEntity$boldCode relates to at most one $boldCode$toEntity$boldCode."; |
743 |
|
} else { |
744 |
|
push @listElements, "Each $boldCode$fromEntity$boldCode relates to multiple $boldCode${toEntity}s$boldCode.\n"; |
745 |
|
if ($arity eq "MM" && $fromEntity ne $toEntity) { |
746 |
|
push @listElements, "Each $boldCode$toEntity$boldCode relates to multiple $boldCode${fromEntity}s$boldCode.\n"; |
747 |
|
} |
748 |
|
} |
749 |
|
push @retVal, WikiTools::List(@listElements); |
750 |
|
# Plant the notes here, if there are any. |
751 |
|
if (my $notes = $relationshipData->{Notes}) { |
752 |
|
push @retVal, "", WikiNote($notes->{content}); |
753 |
|
} |
754 |
|
# Finally, the relationship table. |
755 |
|
my $wikiString = _WikiRelationTable($key, $relationshipData->{Relations}->{$key}); |
756 |
|
push @retVal, $wikiString; |
757 |
|
} |
758 |
|
# All done. Return the lines. |
759 |
|
return @retVal; |
760 |
|
} |
761 |
|
|
762 |
|
|
763 |
|
=head3 CreatePPO |
764 |
|
|
765 |
|
ERDB::CreatePPO($erdbXMLFile, $ppoXMLFile); |
766 |
|
|
767 |
|
Create a PPO XML file from an ERDB data definition XML file. At the |
768 |
|
current time, the PPO XML file can be used to create a database with |
769 |
|
similar functionality. Eventually, the PPO will be able to use the |
770 |
|
created XML to access the live ERDB database. |
771 |
|
|
772 |
|
=over 4 |
773 |
|
|
774 |
|
=item erdbXMLFile |
775 |
|
|
776 |
|
Name of the XML data definition file for the ERDB database. This |
777 |
|
file must exist. |
778 |
|
|
779 |
|
=item ppoXMLFile |
780 |
|
|
781 |
|
Output file for the PPO XML definition. If this file exists, it |
782 |
|
will be overwritten. |
783 |
|
|
784 |
|
=back |
785 |
|
|
786 |
|
=cut |
787 |
|
|
788 |
|
sub CreatePPO { |
789 |
|
# Get the parameters. |
790 |
|
my ($erdbXMLFile, $ppoXMLFile) = @_; |
791 |
|
# First, we want to slurp in the ERDB XML file in its raw form. |
792 |
|
my $xml = ReadMetaXML($erdbXMLFile); |
793 |
|
# Create a variable to hold all of the objects in the PPO project. |
794 |
|
my @objects = (); |
795 |
|
# Get the relationship hash. |
796 |
|
my $relationships = $xml->{Relationships}; |
797 |
|
# Loop through the entities. |
798 |
|
my $entities = $xml->{Entities}; |
799 |
|
for my $entityName (keys %{$entities}) { |
800 |
|
# Get the entity's data structures. |
801 |
|
my $entityObject = $entities->{$entityName}; |
802 |
|
# We put the object's fields in here, according to their type. |
803 |
|
my (@object_refs, @scalars, @indexes, @arrays); |
804 |
|
# Create the ID field for the entity. We get the key type from the |
805 |
|
# entity object and compute the corresponding SQL type. |
806 |
|
my $type = $TypeTable{$entityObject->{keyType}}->{sqlType}; |
807 |
|
push @scalars, { label => 'id', type => $type }; |
808 |
|
# Loop through the entity fields. |
809 |
|
for my $fieldName ( keys %{$entityObject->{Fields}} ) { |
810 |
|
# Get the field object. |
811 |
|
my $fieldObject = $entityObject->{Fields}->{$fieldName}; |
812 |
|
# Convert it to a scalar tag. |
813 |
|
my $scalar = _CreatePPOField($fieldName, $fieldObject); |
814 |
|
# If we have a relation, this field is stored in an array. |
815 |
|
# otherwise, it is a scalar. The array tag has scalars |
816 |
|
# stored as an XML array. In ERDB, there is only ever one, |
817 |
|
# but PPO can have more. |
818 |
|
my $relation = $fieldObject->{relation}; |
819 |
|
if ($relation) { |
820 |
|
push @arrays, { scalar => [$scalar] }; |
821 |
|
} else { |
822 |
|
push @scalars, $scalar; |
823 |
|
} |
824 |
|
} |
825 |
|
# Loop through the relationships. If this entity is the to-entity |
826 |
|
# on a relationship of 1M arity, then it is implemented as a PPO |
827 |
|
# object reference. |
828 |
|
for my $relationshipName (keys %{$relationships}) { |
829 |
|
# Get the relationship data. |
830 |
|
my $relationshipData = $relationships->{$relationshipName}; |
831 |
|
# If we have a from for this entity and an arity of 1M, we |
832 |
|
# have an object reference. |
833 |
|
if ($relationshipData->{to} eq $entityName && |
834 |
|
$relationshipData->{arity} eq '1M') { |
835 |
|
# Build the object reference tag. |
836 |
|
push @object_refs, { label => $relationshipName, |
837 |
|
type => $relationshipData->{from} }; |
838 |
|
} |
839 |
|
} |
840 |
|
# Create the indexes. |
841 |
|
my $indexList = $entityObject->{Indexes}; |
842 |
|
push @indexes, map { _CreatePPOIndex($_) } @{$indexList}; |
843 |
|
# Build the object XML tree. |
844 |
|
my $object = { label => $entityName, |
845 |
|
object_ref => \@object_refs, |
846 |
|
scalar => \@scalars, |
847 |
|
index => \@indexes, |
848 |
|
array => \@arrays |
849 |
|
}; |
850 |
|
# Push the object onto the objects list. |
851 |
|
push @objects, $object; |
852 |
|
} |
853 |
|
# Loop through the relationships, searching for MMs. The 1Ms were |
854 |
|
# already handled by the entity search above. |
855 |
|
for my $relationshipName (keys %{$relationships}) { |
856 |
|
# Get this relationship's object. |
857 |
|
my $relationshipObject = $relationships->{$relationshipName}; |
858 |
|
# Only proceed if it's many-to-many. |
859 |
|
if ($relationshipObject->{arity} eq 'MM') { |
860 |
|
# Create the tag lists for the relationship object. |
861 |
|
my (@object_refs, @scalars, @indexes); |
862 |
|
# The relationship will be created as an object with object |
863 |
|
# references for its links to the participating entities. |
864 |
|
my %links = ( from_link => $relationshipObject->{from}, |
865 |
|
to_link => $relationshipObject->{to} ); |
866 |
|
for my $link (keys %links) { |
867 |
|
# Create an object_ref tag for this piece of the |
868 |
|
# relationship (from or to). |
869 |
|
my $object_ref = { label => $link, |
870 |
|
type => $links{$link} }; |
871 |
|
push @object_refs, $object_ref; |
872 |
|
} |
873 |
|
# Loop through the intersection data fields, creating scalar tags. |
874 |
|
# There are no fancy array tags in a relationship. |
875 |
|
for my $fieldName (keys %{$relationshipObject->{Fields}}) { |
876 |
|
my $fieldObject = $relationshipObject->{Fields}->{$fieldName}; |
877 |
|
push @scalars, _CreatePPOField($fieldName, $fieldObject); |
878 |
|
} |
879 |
|
# Finally, the indexes: currently we cannot support the to-index and |
880 |
|
# from-index in PPO, so we just process the alternate indexes. |
881 |
|
my $indexList = $relationshipObject->{Indexes}; |
882 |
|
push @indexes, map { _CreatePPOIndex($_) } @{$indexList}; |
883 |
|
# Wrap up all the stuff about this relationship. |
884 |
|
my $object = { label => $relationshipName, |
885 |
|
scalar => \@scalars, |
886 |
|
object_ref => \@object_refs, |
887 |
|
index => \@indexes |
888 |
|
}; |
889 |
|
# Push it into the object list. |
890 |
|
push @objects, $object; |
891 |
|
} |
892 |
|
} |
893 |
|
# Compute a title. |
894 |
|
my $title; |
895 |
|
if ($erdbXMLFile =~ /(\/|^)([^\/]+)DBD\.xml/) { |
896 |
|
# Here we have a standard file name we can use for a title. |
897 |
|
$title = $2; |
898 |
|
} else { |
899 |
|
# Here the file name is non-standard, so we carve up the |
900 |
|
# database title. |
901 |
|
$title = $xml->{Title}->{content}; |
902 |
|
$title =~ s/\s\.,//g; |
903 |
|
} |
904 |
|
# Wrap up the XML as a project. |
905 |
|
my $ppoXML = { project => { label => $title, |
906 |
|
object => \@objects }}; |
907 |
|
# Write out the results. |
908 |
|
my $ppoString = XML::Simple::XMLout($ppoXML, |
909 |
|
AttrIndent => 1, |
910 |
|
KeepRoot => 1); |
911 |
|
Tracer::PutFile($ppoXMLFile, [ $ppoString ]); |
912 |
|
} |
913 |
|
|
914 |
|
=head3 FindIndexForEntity |
915 |
|
|
916 |
|
my $indexFound = ERDB::FindIndexForEntity($xml, $entityName, $attributeName); |
917 |
|
|
918 |
|
This method locates the entry in an entity's index list that begins with the |
919 |
|
specified attribute name. If the entity has no index list, one will be |
920 |
|
created. This method works on raw XML, not a live ERDB object. |
921 |
|
|
922 |
|
=over 4 |
923 |
|
|
924 |
|
=item xml |
925 |
|
|
926 |
|
The raw XML structure defining the database. |
927 |
|
|
928 |
|
=item entityName |
929 |
|
|
930 |
|
The name of the relevant entity. |
931 |
|
|
932 |
|
=item attributeName |
933 |
|
|
934 |
|
The name of the attribute relevant to the search. |
935 |
|
|
936 |
|
=item RETURN |
937 |
|
|
938 |
|
The numerical index in the index list of the index entry for the specified entity and |
939 |
|
attribute, or C<undef> if no such index exists. |
940 |
|
|
941 |
|
=back |
942 |
|
|
943 |
|
=cut |
944 |
|
|
945 |
|
sub FindIndexForEntity { |
946 |
|
# Get the parameters. |
947 |
|
my ($xml, $entityName, $attributeName) = @_; |
948 |
|
# Declare the return variable. |
949 |
|
my $retVal; |
950 |
|
# Get the named entity. |
951 |
|
my $entityData = $xml->{Entities}->{$entityName}; |
952 |
|
if (! $entityData) { |
953 |
|
Confess("Entity $entityName not found in DBD structure."); |
954 |
|
} else { |
955 |
|
# Insure it has an index list. |
956 |
|
if (! exists $entityData->{Indexes}) { |
957 |
|
$entityData->{Indexes} = []; |
958 |
|
} else { |
959 |
|
# Search for the desired index. |
960 |
|
my $indexList = $entityData->{Indexes}; |
961 |
|
my $n = scalar @{$indexList}; |
962 |
|
Trace("Searching $n indexes in index list for $entityName.") if T(2); |
963 |
|
# We use an indexed FOR here because we're returning an |
964 |
|
# index number instead of an object. We do THAT so we can |
965 |
|
# delete the index from the list if needed. |
966 |
|
for (my $i = 0; $i < $n && !defined($retVal); $i++) { |
967 |
|
my $index = $indexList->[$i]; |
968 |
|
my $fields = $index->{IndexFields}; |
969 |
|
# Technically this IF should be safe (that is, we are guaranteed |
970 |
|
# the existence of a "$fields->[0]"), because when we load the XML |
971 |
|
# we have SuppressEmpty specified. |
972 |
|
if ($fields->[0]->{name} eq $attributeName) { |
973 |
|
$retVal = $i; |
974 |
|
} |
975 |
|
} |
976 |
|
} |
977 |
|
} |
978 |
|
Trace("Index for $attributeName of $entityName found at position $retVal.") if defined($retVal) && T(3); |
979 |
|
Trace("Index for $attributeName not found in $entityName.") if !defined($retVal) && T(3); |
980 |
|
# Return the result. |
981 |
|
return $retVal; |
982 |
|
} |
983 |
|
|
984 |
=head3 CreateTables |
=head3 CreateTables |
985 |
|
|
986 |
C<< $erdb->CreateTables(); >> |
$erdb->CreateTables(); |
987 |
|
|
988 |
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 |
989 |
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 |
1000 |
# Loop through the relations. |
# Loop through the relations. |
1001 |
for my $relationName (@relNames) { |
for my $relationName (@relNames) { |
1002 |
# Create a table for this relation. |
# Create a table for this relation. |
1003 |
$self->CreateTable($relationName); |
$self->CreateTable($relationName, 1); |
1004 |
Trace("Relation $relationName created.") if T(2); |
Trace("Relation $relationName created.") if T(2); |
1005 |
} |
} |
1006 |
} |
} |
1007 |
|
|
1008 |
=head3 CreateTable |
=head3 CreateTable |
1009 |
|
|
1010 |
C<< $erdb->CreateTable($tableName, $indexFlag, $estimatedRows); >> |
$erdb->CreateTable($tableName, $indexFlag, $estimatedRows); |
1011 |
|
|
1012 |
Create the table for a relation and optionally create its indexes. |
Create the table for a relation and optionally create its indexes. |
1013 |
|
|
1063 |
my $estimation = undef; |
my $estimation = undef; |
1064 |
if ($estimatedRows) { |
if ($estimatedRows) { |
1065 |
$estimation = [$self->EstimateRowSize($relationName), $estimatedRows]; |
$estimation = [$self->EstimateRowSize($relationName), $estimatedRows]; |
1066 |
|
Trace("$estimation->[1] rows of $estimation->[0] bytes each.") if T(3); |
1067 |
} |
} |
1068 |
# Create the table. |
# Create the table. |
1069 |
Trace("Creating table $relationName: $fieldThing") if T(2); |
Trace("Creating table $relationName: $fieldThing") if T(2); |
1070 |
$dbh->create_table(tbl => $relationName, flds => $fieldThing, estimates => $estimation); |
$dbh->create_table(tbl => $relationName, flds => $fieldThing, estimates => $estimation); |
1071 |
Trace("Relation $relationName created in database.") if T(2); |
Trace("Relation $relationName created in database.") if T(2); |
1072 |
# If we want to build the indexes, we do it here. |
# If we want to build the indexes, we do it here. Note that the full-text search |
1073 |
|
# index will not be built until the table has been loaded. |
1074 |
if ($indexFlag) { |
if ($indexFlag) { |
1075 |
$self->CreateIndex($relationName); |
$self->CreateIndex($relationName); |
1076 |
} |
} |
1078 |
|
|
1079 |
=head3 VerifyFields |
=head3 VerifyFields |
1080 |
|
|
1081 |
C<< my $count = $erdb->VerifyFields($relName, \@fieldList); >> |
my $count = $erdb->VerifyFields($relName, \@fieldList); |
1082 |
|
|
1083 |
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 |
1084 |
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. |
1121 |
my $oldString = $fieldList->[$i]; |
my $oldString = $fieldList->[$i]; |
1122 |
if (length($oldString) > $maxLen) { |
if (length($oldString) > $maxLen) { |
1123 |
# Here it's too big, so we truncate it. |
# Here it's too big, so we truncate it. |
1124 |
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); |
1125 |
$fieldList->[$i] = substr $oldString, 0, $maxLen; |
$fieldList->[$i] = substr $oldString, 0, $maxLen; |
1126 |
$retVal++; |
$retVal++; |
1127 |
} |
} |
1133 |
|
|
1134 |
=head3 DigestFields |
=head3 DigestFields |
1135 |
|
|
1136 |
C<< $erdb->DigestFields($relName, $fieldList); >> |
$erdb->DigestFields($relName, $fieldList); |
1137 |
|
|
1138 |
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 |
1139 |
specified relation. |
specified relation. |
1173 |
|
|
1174 |
=head3 DigestKey |
=head3 DigestKey |
1175 |
|
|
1176 |
C<< my $digested = $erdb->DigestKey($keyValue); >> |
my $digested = $erdb->DigestKey($keyValue); |
1177 |
|
|
1178 |
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 |
1179 |
key-based search into a table with key-type hash-string. |
key-based search into a table with key-type hash-string. |
1206 |
|
|
1207 |
=head3 CreateIndex |
=head3 CreateIndex |
1208 |
|
|
1209 |
C<< $erdb->CreateIndex($relationName); >> |
$erdb->CreateIndex($relationName); |
1210 |
|
|
1211 |
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 |
1212 |
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. |
1227 |
for my $indexName (keys %{$indexHash}) { |
for my $indexName (keys %{$indexHash}) { |
1228 |
my $indexData = $indexHash->{$indexName}; |
my $indexData = $indexHash->{$indexName}; |
1229 |
# Get the index's field list. |
# Get the index's field list. |
1230 |
my @fieldList = _FixNames(@{$indexData->{IndexFields}}); |
my @rawFields = @{$indexData->{IndexFields}}; |
1231 |
|
# Get a hash of the relation's field types. |
1232 |
|
my %types = map { $_->{name} => $_->{type} } @{$relationData->{Fields}}; |
1233 |
|
# We need to check for text fields so we can append a length limitation for them. To do |
1234 |
|
# that, we need the relation's field list. |
1235 |
|
my $relFields = $relationData->{Fields}; |
1236 |
|
for (my $i = 0; $i <= $#rawFields; $i++) { |
1237 |
|
# Get the field type. |
1238 |
|
my $field = $rawFields[$i]; |
1239 |
|
my $type = $types{$field}; |
1240 |
|
# Ask if it requires using prefix notation for the index. |
1241 |
|
my $mod = $TypeTable{$type}->{indexMod}; |
1242 |
|
Trace("Field $field ($i) in $relationName has type $type and indexMod $mod.") if T(3); |
1243 |
|
if ($mod) { |
1244 |
|
# Append the prefix length to the field name, |
1245 |
|
$rawFields[$i] .= "($mod)"; |
1246 |
|
} |
1247 |
|
} |
1248 |
|
my @fieldList = _FixNames(@rawFields); |
1249 |
my $flds = join(', ', @fieldList); |
my $flds = join(', ', @fieldList); |
1250 |
# Get the index's uniqueness flag. |
# Get the index's uniqueness flag. |
1251 |
my $unique = (exists $indexData->{Unique} ? $indexData->{Unique} : 'false'); |
my $unique = (exists $indexData->{Unique} ? 'unique' : undef); |
1252 |
# Create the index. |
# Create the index. |
1253 |
my $rv = $dbh->create_index(idx => $indexName, tbl => $relationName, |
my $rv = $dbh->create_index(idx => $indexName, tbl => $relationName, |
1254 |
flds => $flds, unique => $unique); |
flds => $flds, kind => $unique); |
1255 |
if ($rv) { |
if ($rv) { |
1256 |
Trace("Index created: $indexName for $relationName ($flds)") if T(1); |
Trace("Index created: $indexName for $relationName ($flds)") if T(1); |
1257 |
} else { |
} else { |
1260 |
} |
} |
1261 |
} |
} |
1262 |
|
|
1263 |
|
=head3 GetSecondaryFields |
1264 |
|
|
1265 |
|
my %fieldTuples = $erdb->GetSecondaryFields($entityName); |
1266 |
|
|
1267 |
|
This method will return a list of the name and type of each of the secondary |
1268 |
|
fields for a specified entity. Secondary fields are stored in two-column tables |
1269 |
|
in addition to the primary entity table. This enables the field to have no value |
1270 |
|
or to have multiple values. |
1271 |
|
|
1272 |
|
=over 4 |
1273 |
|
|
1274 |
|
=item entityName |
1275 |
|
|
1276 |
|
Name of the entity whose secondary fields are desired. |
1277 |
|
|
1278 |
|
=item RETURN |
1279 |
|
|
1280 |
|
Returns a hash mapping the field names to their field types. |
1281 |
|
|
1282 |
|
=back |
1283 |
|
|
1284 |
|
=cut |
1285 |
|
|
1286 |
|
sub GetSecondaryFields { |
1287 |
|
# Get the parameters. |
1288 |
|
my ($self, $entityName) = @_; |
1289 |
|
# Declare the return variable. |
1290 |
|
my %retVal = (); |
1291 |
|
# Look for the entity. |
1292 |
|
my $table = $self->GetFieldTable($entityName); |
1293 |
|
# Loop through the fields, pulling out the secondaries. |
1294 |
|
for my $field (sort keys %{$table}) { |
1295 |
|
if ($table->{$field}->{relation} ne $entityName) { |
1296 |
|
# Here we have a secondary field. |
1297 |
|
$retVal{$field} = $table->{$field}->{type}; |
1298 |
|
} |
1299 |
|
} |
1300 |
|
# Return the result. |
1301 |
|
return %retVal; |
1302 |
|
} |
1303 |
|
|
1304 |
|
=head3 GetFieldRelationName |
1305 |
|
|
1306 |
|
my $name = $erdb->GetFieldRelationName($objectName, $fieldName); |
1307 |
|
|
1308 |
|
Return the name of the relation containing a specified field. |
1309 |
|
|
1310 |
|
=over 4 |
1311 |
|
|
1312 |
|
=item objectName |
1313 |
|
|
1314 |
|
Name of the entity or relationship containing the field. |
1315 |
|
|
1316 |
|
=item fieldName |
1317 |
|
|
1318 |
|
Name of the relevant field in that entity or relationship. |
1319 |
|
|
1320 |
|
=item RETURN |
1321 |
|
|
1322 |
|
Returns the name of the database relation containing the field, or C<undef> if |
1323 |
|
the field does not exist. |
1324 |
|
|
1325 |
|
=back |
1326 |
|
|
1327 |
|
=cut |
1328 |
|
|
1329 |
|
sub GetFieldRelationName { |
1330 |
|
# Get the parameters. |
1331 |
|
my ($self, $objectName, $fieldName) = @_; |
1332 |
|
# Declare the return variable. |
1333 |
|
my $retVal; |
1334 |
|
# Get the object field table. |
1335 |
|
my $table = $self->GetFieldTable($objectName); |
1336 |
|
# Only proceed if the field exists. |
1337 |
|
if (exists $table->{$fieldName}) { |
1338 |
|
# Determine the name of the relation that contains this field. |
1339 |
|
$retVal = $table->{$fieldName}->{relation}; |
1340 |
|
} |
1341 |
|
# Return the result. |
1342 |
|
return $retVal; |
1343 |
|
} |
1344 |
|
|
1345 |
|
=head3 DeleteValue |
1346 |
|
|
1347 |
|
my $numDeleted = $erdb->DeleteValue($entityName, $id, $fieldName, $fieldValue); |
1348 |
|
|
1349 |
|
Delete secondary field values from the database. This method can be used to delete all |
1350 |
|
values of a specified field for a particular entity instance, or only a single value. |
1351 |
|
|
1352 |
|
Secondary fields are stored in two-column relations separate from an entity's primary |
1353 |
|
table, and as a result a secondary field can legitimately have no value or multiple |
1354 |
|
values. Therefore, it makes sense to talk about deleting secondary fields where it |
1355 |
|
would not make sense for primary fields. |
1356 |
|
|
1357 |
|
=over 4 |
1358 |
|
|
1359 |
|
=item entityName |
1360 |
|
|
1361 |
|
Name of the entity from which the fields are to be deleted. |
1362 |
|
|
1363 |
|
=item id |
1364 |
|
|
1365 |
|
ID of the entity instance to be processed. If the instance is not found, this |
1366 |
|
method will have no effect. If C<undef> is specified, all values for all of |
1367 |
|
the entity instances will be deleted. |
1368 |
|
|
1369 |
|
=item fieldName |
1370 |
|
|
1371 |
|
Name of the field whose values are to be deleted. |
1372 |
|
|
1373 |
|
=item fieldValue (optional) |
1374 |
|
|
1375 |
|
Value to be deleted. If not specified, then all values of the specified field |
1376 |
|
will be deleted for the entity instance. If specified, then only the values which |
1377 |
|
match this parameter will be deleted. |
1378 |
|
|
1379 |
|
=item RETURN |
1380 |
|
|
1381 |
|
Returns the number of rows deleted. |
1382 |
|
|
1383 |
|
=back |
1384 |
|
|
1385 |
|
=cut |
1386 |
|
|
1387 |
|
sub DeleteValue { |
1388 |
|
# Get the parameters. |
1389 |
|
my ($self, $entityName, $id, $fieldName, $fieldValue) = @_; |
1390 |
|
# Declare the return value. |
1391 |
|
my $retVal = 0; |
1392 |
|
# We need to set up an SQL command to do the deletion. First, we |
1393 |
|
# find the name of the field's relation. |
1394 |
|
my $table = $self->GetFieldTable($entityName); |
1395 |
|
my $field = $table->{$fieldName}; |
1396 |
|
my $relation = $field->{relation}; |
1397 |
|
# Make sure this is a secondary field. |
1398 |
|
if ($relation eq $entityName) { |
1399 |
|
Confess("Cannot delete values of $fieldName for $entityName."); |
1400 |
|
} else { |
1401 |
|
# Set up the SQL command to delete all values. |
1402 |
|
my $sql = "DELETE FROM $relation"; |
1403 |
|
# Build the filter. |
1404 |
|
my @filters = (); |
1405 |
|
my @parms = (); |
1406 |
|
# Check for a filter by ID. |
1407 |
|
if (defined $id) { |
1408 |
|
push @filters, "id = ?"; |
1409 |
|
push @parms, $id; |
1410 |
|
} |
1411 |
|
# Check for a filter by value. |
1412 |
|
if (defined $fieldValue) { |
1413 |
|
push @filters, "$fieldName = ?"; |
1414 |
|
push @parms, $fieldValue; |
1415 |
|
} |
1416 |
|
# Append the filters to the command. |
1417 |
|
if (@filters) { |
1418 |
|
$sql .= " WHERE " . join(" AND ", @filters); |
1419 |
|
} |
1420 |
|
# Execute the command. |
1421 |
|
my $dbh = $self->{_dbh}; |
1422 |
|
$retVal = $dbh->SQL($sql, 0, @parms); |
1423 |
|
} |
1424 |
|
# Return the result. |
1425 |
|
return $retVal; |
1426 |
|
} |
1427 |
|
|
1428 |
=head3 LoadTables |
=head3 LoadTables |
1429 |
|
|
1430 |
C<< my $stats = $erdb->LoadTables($directoryName, $rebuild); >> |
my $stats = $erdb->LoadTables($directoryName, $rebuild); |
1431 |
|
|
1432 |
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 |
1433 |
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; |
1487 |
|
|
1488 |
=head3 GetTableNames |
=head3 GetTableNames |
1489 |
|
|
1490 |
C<< my @names = $erdb->GetTableNames; >> |
my @names = $erdb->GetTableNames; |
1491 |
|
|
1492 |
Return a list of the relations required to implement this database. |
Return a list of the relations required to implement this database. |
1493 |
|
|
1504 |
|
|
1505 |
=head3 GetEntityTypes |
=head3 GetEntityTypes |
1506 |
|
|
1507 |
C<< my @names = $erdb->GetEntityTypes; >> |
my @names = $erdb->GetEntityTypes; |
1508 |
|
|
1509 |
Return a list of the entity type names. |
Return a list of the entity type names. |
1510 |
|
|
1519 |
return sort keys %{$entityList}; |
return sort keys %{$entityList}; |
1520 |
} |
} |
1521 |
|
|
1522 |
|
=head3 GetDataTypes |
1523 |
|
|
1524 |
|
my %types = ERDB::GetDataTypes(); |
1525 |
|
|
1526 |
|
Return a table of ERDB data types. The table returned is a hash of hashes. |
1527 |
|
The keys of the big hash are the datatypes. Each smaller hash has several |
1528 |
|
values used to manage the data. The most interesting is the SQL type (key |
1529 |
|
C<sqlType>) and the descriptive node (key C<notes>). |
1530 |
|
|
1531 |
|
Note that changing the values in the smaller hashes will seriously break |
1532 |
|
things, so this data should be treated as read-only. |
1533 |
|
|
1534 |
|
=cut |
1535 |
|
|
1536 |
|
sub GetDataTypes { |
1537 |
|
return %TypeTable; |
1538 |
|
} |
1539 |
|
|
1540 |
|
|
1541 |
=head3 IsEntity |
=head3 IsEntity |
1542 |
|
|
1543 |
C<< my $flag = $erdb->IsEntity($entityName); >> |
my $flag = $erdb->IsEntity($entityName); |
1544 |
|
|
1545 |
Return TRUE if the parameter is an entity name, else FALSE. |
Return TRUE if the parameter is an entity name, else FALSE. |
1546 |
|
|
1567 |
|
|
1568 |
=head3 Get |
=head3 Get |
1569 |
|
|
1570 |
C<< my $query = $erdb->Get(\@objectNames, $filterClause, \@params); >> |
my $query = $erdb->Get(\@objectNames, $filterClause, \@params); |
1571 |
|
|
1572 |
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. |
1573 |
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 |
1575 |
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 |
1576 |
$genus. |
$genus. |
1577 |
|
|
1578 |
C<< $query = $erdb->Get(['Genome'], "Genome(genus) = ?", [$genus]); >> |
$query = $erdb->Get(['Genome'], "Genome(genus) = ?", [$genus]); |
1579 |
|
|
1580 |
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 |
1581 |
parameter representing the parameter value. It would also be possible to code |
parameter representing the parameter value. It would also be possible to code |
1582 |
|
|
1583 |
C<< $query = $erdb->Get(['Genome'], "Genome(genus) = \'$genus\'"); >> |
$query = $erdb->Get(['Genome'], "Genome(genus) = \'$genus\'"); |
1584 |
|
|
1585 |
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 |
1586 |
characters inside the variable C<$genus>. |
characters inside the variable C<$genus>. |
1592 |
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 |
1593 |
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, |
1594 |
|
|
1595 |
C<< $query = $erdb->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >> |
$query = $erdb->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); |
1596 |
|
|
1597 |
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 |
1598 |
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. |
1628 |
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 |
1629 |
particular genus and sorts them by species name. |
particular genus and sorts them by species name. |
1630 |
|
|
1631 |
C<< "Genome(genus) = ? ORDER BY Genome(species)" >> |
"Genome(genus) = ? ORDER BY Genome(species)" |
1632 |
|
|
1633 |
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 |
1634 |
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. |
1641 |
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 |
1642 |
a positive number. So, for example |
a positive number. So, for example |
1643 |
|
|
1644 |
C<< "Genome(genus) = ? ORDER BY Genome(species) LIMIT 10" >> |
"Genome(genus) = ? ORDER BY Genome(species) LIMIT 10" |
1645 |
|
|
1646 |
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 |
1647 |
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 |
1648 |
use |
use |
1649 |
|
|
1650 |
C<< "LIMIT 10" >> |
"LIMIT 10" |
1651 |
|
|
1652 |
=item params |
=item params |
1653 |
|
|
1668 |
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = |
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = |
1669 |
$self->_SetupSQL($objectNames, $filterClause); |
$self->_SetupSQL($objectNames, $filterClause); |
1670 |
# Create the query. |
# Create the query. |
1671 |
my $command = "SELECT DISTINCT " . join(".*, ", @{$mappedNameListRef}) . |
my $command = "SELECT " . join(".*, ", @{$mappedNameListRef}) . |
1672 |
".* $suffix"; |
".* $suffix"; |
1673 |
my $sth = $self->_GetStatementHandle($command, $params); |
my $sth = $self->_GetStatementHandle($command, $params); |
1674 |
# 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 |
1682 |
return $retVal; |
return $retVal; |
1683 |
} |
} |
1684 |
|
|
|
=head3 GetFlat |
|
1685 |
|
|
|
C<< my @list = $erdb->GetFlat(\@objectNames, $filterClause, \@parameterList, $field); >> |
|
1686 |
|
|
1687 |
This is a variation of L</GetAll> that asks for only a single field per record and |
=head3 Search |
1688 |
returns a single flattened list. |
|
1689 |
|
my $query = $erdb->Search($searchExpression, $idx, \@objectNames, $filterClause, \@params); |
1690 |
|
|
1691 |
|
Perform a full text search with filtering. The search will be against a specified object |
1692 |
|
in the object name list. That object will get an extra field containing the search |
1693 |
|
relevance. Note that except for the search expression, the parameters of this method are |
1694 |
|
the same as those for L</Get> and follow the same rules. |
1695 |
|
|
1696 |
=over 4 |
=over 4 |
1697 |
|
|
1698 |
|
=item searchExpression |
1699 |
|
|
1700 |
|
Boolean search expression for the text fields of the target object. The default mode for |
1701 |
|
a Boolean search expression is OR, but we want the default to be AND, so we will |
1702 |
|
add a C<+> operator to each word with no other operator before it. |
1703 |
|
|
1704 |
|
=item idx |
1705 |
|
|
1706 |
|
Index in the I<$objectNames> list of the table to be searched in full-text mode. |
1707 |
|
|
1708 |
=item objectNames |
=item objectNames |
1709 |
|
|
1710 |
List containing the names of the entity and relationship objects to be retrieved. |
List containing the names of the entity and relationship objects to be retrieved. |
1711 |
|
|
1712 |
=item filterClause |
=item filterClause |
1713 |
|
|
1714 |
WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can |
WHERE clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can |
1715 |
be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form |
be parameterized with parameter markers (C<?>). Each field used in the WHERE clause must be |
1716 |
B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the |
specified in the standard form B<I<objectName>(I<fieldName>)>. Any parameters specified |
1717 |
parameter list as additional parameters. The fields in a filter clause can come from primary |
in the filter clause should be added to the parameter list as additional parameters. The |
1718 |
entity relations, relationship relations, or secondary entity relations; however, all of the |
fields in a filter clause can come from primary entity relations, relationship relations, |
1719 |
entities and relationships involved must be included in the list of object names. |
or secondary entity relations; however, all of the entities and relationships involved must |
1720 |
|
be included in the list of object names. |
|
=item parameterList |
|
|
|
|
|
List of the parameters to be substituted in for the parameters marks in the filter clause. |
|
1721 |
|
|
1722 |
=item field |
=item params |
1723 |
|
|
1724 |
Name of the field to be used to get the elements of the list returned. |
Reference to a list of parameter values to be substituted into the filter clause. |
1725 |
|
|
1726 |
=item RETURN |
=item RETURN |
1727 |
|
|
1728 |
Returns a list of values. |
Returns a query object for the specified search. |
1729 |
|
|
1730 |
=back |
=back |
1731 |
|
|
1732 |
=cut |
=cut |
1733 |
#: Return Type @; |
|
1734 |
sub GetFlat { |
sub Search { |
1735 |
# Get the parameters. |
# Get the parameters. |
1736 |
my ($self, $objectNames, $filterClause, $parameterList, $field) = @_; |
my ($self, $searchExpression, $idx, $objectNames, $filterClause, $params) = @_; |
1737 |
# Construct the query. |
# Declare the return variable. |
1738 |
my $query = $self->Get($objectNames, $filterClause, $parameterList); |
my $retVal; |
1739 |
# Create the result list. |
# Create a safety copy of the parameter list. Note we have to be careful to insure |
1740 |
my @retVal = (); |
# a parameter list exists before we copy it. |
1741 |
# Loop through the records, adding the field values found to the result list. |
my @myParams = (); |
1742 |
while (my $row = $query->Fetch()) { |
if (defined $params) { |
1743 |
|
@myParams = @{$params}; |
1744 |
|
} |
1745 |
|
# Get the first object's structure so we have access to the searchable fields. |
1746 |
|
my $object1Name = $objectNames->[$idx]; |
1747 |
|
my $object1Structure = $self->_GetStructure($object1Name); |
1748 |
|
# Get the field list. |
1749 |
|
if (! exists $object1Structure->{searchFields}) { |
1750 |
|
Confess("No searchable index for $object1Name."); |
1751 |
|
} else { |
1752 |
|
# Get the field list. |
1753 |
|
my @fields = @{$object1Structure->{searchFields}}; |
1754 |
|
# Clean the search expression. |
1755 |
|
my $actualKeywords = $self->CleanKeywords($searchExpression); |
1756 |
|
# Prefix a "+" to each uncontrolled word. This converts the default |
1757 |
|
# search mode from OR to AND. |
1758 |
|
$actualKeywords =~ s/(^|\s)(\w|")/$1\+$2/g; |
1759 |
|
Trace("Actual keywords for search are\n$actualKeywords") if T(3); |
1760 |
|
# We need two match expressions, one for the filter clause and one in the |
1761 |
|
# query itself. Both will use a parameter mark, so we need to push the |
1762 |
|
# search expression onto the front of the parameter list twice. |
1763 |
|
unshift @myParams, $actualKeywords, $actualKeywords; |
1764 |
|
# Build the match expression. |
1765 |
|
my @matchFilterFields = map { "$object1Name." . _FixName($_) } @fields; |
1766 |
|
my $matchClause = "MATCH (" . join(", ", @matchFilterFields) . ") AGAINST (? IN BOOLEAN MODE)"; |
1767 |
|
# Process the SQL stuff. |
1768 |
|
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = |
1769 |
|
$self->_SetupSQL($objectNames, $filterClause, $matchClause); |
1770 |
|
# Create the query. Note that the match clause is inserted at the front of |
1771 |
|
# the select fields. |
1772 |
|
my $command = "SELECT $matchClause, " . join(".*, ", @{$mappedNameListRef}) . |
1773 |
|
".* $suffix"; |
1774 |
|
my $sth = $self->_GetStatementHandle($command, \@myParams); |
1775 |
|
# Now we create the relation map, which enables DBQuery to determine the order, name |
1776 |
|
# and mapped name for each object in the query. |
1777 |
|
my @relationMap = _RelationMap($mappedNameHashRef, $mappedNameListRef); |
1778 |
|
# Return the statement object. |
1779 |
|
$retVal = DBQuery::_new($self, $sth, \@relationMap, $object1Name); |
1780 |
|
} |
1781 |
|
return $retVal; |
1782 |
|
} |
1783 |
|
|
1784 |
|
=head3 GetFlat |
1785 |
|
|
1786 |
|
my @list = $erdb->GetFlat(\@objectNames, $filterClause, \@parameterList, $field); |
1787 |
|
|
1788 |
|
This is a variation of L</GetAll> that asks for only a single field per record and |
1789 |
|
returns a single flattened list. |
1790 |
|
|
1791 |
|
=over 4 |
1792 |
|
|
1793 |
|
=item objectNames |
1794 |
|
|
1795 |
|
List containing the names of the entity and relationship objects to be retrieved. |
1796 |
|
|
1797 |
|
=item filterClause |
1798 |
|
|
1799 |
|
WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can |
1800 |
|
be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form |
1801 |
|
B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the |
1802 |
|
parameter list as additional parameters. The fields in a filter clause can come from primary |
1803 |
|
entity relations, relationship relations, or secondary entity relations; however, all of the |
1804 |
|
entities and relationships involved must be included in the list of object names. |
1805 |
|
|
1806 |
|
=item parameterList |
1807 |
|
|
1808 |
|
List of the parameters to be substituted in for the parameters marks in the filter clause. |
1809 |
|
|
1810 |
|
=item field |
1811 |
|
|
1812 |
|
Name of the field to be used to get the elements of the list returned. |
1813 |
|
|
1814 |
|
=item RETURN |
1815 |
|
|
1816 |
|
Returns a list of values. |
1817 |
|
|
1818 |
|
=back |
1819 |
|
|
1820 |
|
=cut |
1821 |
|
#: Return Type @; |
1822 |
|
sub GetFlat { |
1823 |
|
# Get the parameters. |
1824 |
|
my ($self, $objectNames, $filterClause, $parameterList, $field) = @_; |
1825 |
|
# Construct the query. |
1826 |
|
my $query = $self->Get($objectNames, $filterClause, $parameterList); |
1827 |
|
# Create the result list. |
1828 |
|
my @retVal = (); |
1829 |
|
# Loop through the records, adding the field values found to the result list. |
1830 |
|
while (my $row = $query->Fetch()) { |
1831 |
push @retVal, $row->Value($field); |
push @retVal, $row->Value($field); |
1832 |
} |
} |
1833 |
# Return the list created. |
# Return the list created. |
1834 |
return @retVal; |
return @retVal; |
1835 |
} |
} |
1836 |
|
|
1837 |
|
=head3 SpecialFields |
1838 |
|
|
1839 |
|
my %specials = $erdb->SpecialFields($entityName); |
1840 |
|
|
1841 |
|
Return a hash mapping special fields in the specified entity to the value of their |
1842 |
|
C<special> attribute. This enables the subclass to get access to the special field |
1843 |
|
attributes without needed to plumb the internal ERDB data structures. |
1844 |
|
|
1845 |
|
=over 4 |
1846 |
|
|
1847 |
|
=item entityName |
1848 |
|
|
1849 |
|
Name of the entity whose special fields are desired. |
1850 |
|
|
1851 |
|
=item RETURN |
1852 |
|
|
1853 |
|
Returns a hash. The keys of the hash are the special field names, and the values |
1854 |
|
are the values from each special field's C<special> attribute. |
1855 |
|
|
1856 |
|
=back |
1857 |
|
|
1858 |
|
=cut |
1859 |
|
|
1860 |
|
sub SpecialFields { |
1861 |
|
# Get the parameters. |
1862 |
|
my ($self, $entityName) = @_; |
1863 |
|
# Declare the return variable. |
1864 |
|
my %retVal = (); |
1865 |
|
# Find the entity's data structure. |
1866 |
|
my $entityData = $self->{_metaData}->{Entities}->{$entityName}; |
1867 |
|
# Loop through its fields, adding each special field to the return hash. |
1868 |
|
my $fieldHash = $entityData->{Fields}; |
1869 |
|
for my $fieldName (keys %{$fieldHash}) { |
1870 |
|
my $fieldData = $fieldHash->{$fieldName}; |
1871 |
|
if (exists $fieldData->{special}) { |
1872 |
|
$retVal{$fieldName} = $fieldData->{special}; |
1873 |
|
} |
1874 |
|
} |
1875 |
|
# Return the result. |
1876 |
|
return %retVal; |
1877 |
|
} |
1878 |
|
|
1879 |
=head3 Delete |
=head3 Delete |
1880 |
|
|
1881 |
C<< my $stats = $erdb->Delete($entityName, $objectID); >> |
my $stats = $erdb->Delete($entityName, $objectID, %options); |
1882 |
|
|
1883 |
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 |
1884 |
relationship instances dependent on it. The idea of dependence here is recursive. An object is |
relationship instances dependent on it. The definition of I<dependence> is recursive. |
1885 |
always dependent on itself. An object is dependent if it is a 1-to-many or many-to-many |
|
1886 |
relationship connected to a dependent entity or the "to" entity connected to a 1-to-many |
An object is always dependent on itself. An object is dependent if it is a 1-to-many or many-to-many |
1887 |
|
relationship connected to a dependent entity or if it is the "to" entity connected to a 1-to-many |
1888 |
dependent relationship. |
dependent relationship. |
1889 |
|
|
1890 |
=over 4 |
=over 4 |
1898 |
ID of the entity instance to be deleted. If the ID contains a wild card character (C<%>), |
ID of the entity instance to be deleted. If the ID contains a wild card character (C<%>), |
1899 |
then it is presumed to by a LIKE pattern. |
then it is presumed to by a LIKE pattern. |
1900 |
|
|
1901 |
=item testFlag |
=item options |
1902 |
|
|
1903 |
If TRUE, the delete statements will be traced without being executed. |
A hash detailing the options for this delete operation. |
1904 |
|
|
1905 |
=item RETURN |
=item RETURN |
1906 |
|
|
1909 |
|
|
1910 |
=back |
=back |
1911 |
|
|
1912 |
|
The permissible options for this method are as follows. |
1913 |
|
|
1914 |
|
=over 4 |
1915 |
|
|
1916 |
|
=item testMode |
1917 |
|
|
1918 |
|
If TRUE, then the delete statements will be traced, but no changes will be made to the database. |
1919 |
|
|
1920 |
|
=item keepRoot |
1921 |
|
|
1922 |
|
If TRUE, then the entity instances will not be deleted, only the dependent records. |
1923 |
|
|
1924 |
|
=back |
1925 |
|
|
1926 |
=cut |
=cut |
1927 |
#: Return Type $%; |
#: Return Type $%; |
1928 |
sub Delete { |
sub Delete { |
1929 |
# Get the parameters. |
# Get the parameters. |
1930 |
my ($self, $entityName, $objectID, $testFlag) = @_; |
my ($self, $entityName, $objectID, %options) = @_; |
1931 |
# Declare the return variable. |
# Declare the return variable. |
1932 |
my $retVal = Stats->new(); |
my $retVal = Stats->new(); |
1933 |
# Get the DBKernel object. |
# Get the DBKernel object. |
1944 |
# FROM-relationships and entities. |
# FROM-relationships and entities. |
1945 |
my @fromPathList = (); |
my @fromPathList = (); |
1946 |
my @toPathList = (); |
my @toPathList = (); |
1947 |
# This final hash is used to remember what work still needs to be done. We push paths |
# This final list is used to remember what work still needs to be done. We push paths |
1948 |
# onto the list, then pop them off to extend the paths. We prime it with the starting |
# onto the list, then pop them off to extend the paths. We prime it with the starting |
1949 |
# point. Note that we will work hard to insure that the last item on a path in the |
# point. Note that we will work hard to insure that the last item on a path in the |
1950 |
# TODO list is always an entity. |
# to-do list is always an entity. |
1951 |
my @todoList = ([$entityName]); |
my @todoList = ([$entityName]); |
1952 |
while (@todoList) { |
while (@todoList) { |
1953 |
# Get the current path. |
# Get the current path. |
1955 |
# Copy it into a list. |
# Copy it into a list. |
1956 |
my @stackedPath = @{$current}; |
my @stackedPath = @{$current}; |
1957 |
# Pull off the last item on the path. It will always be an entity. |
# Pull off the last item on the path. It will always be an entity. |
1958 |
my $entityName = pop @stackedPath; |
my $myEntityName = pop @stackedPath; |
1959 |
# Add it to the alreadyFound list. |
# Add it to the alreadyFound list. |
1960 |
$alreadyFound{$entityName} = 1; |
$alreadyFound{$myEntityName} = 1; |
1961 |
|
# Figure out if we need to delete this entity. |
1962 |
|
if ($myEntityName ne $entityName || ! $options{keepRoot}) { |
1963 |
# Get the entity data. |
# Get the entity data. |
1964 |
my $entityData = $self->_GetStructure($entityName); |
my $entityData = $self->_GetStructure($myEntityName); |
1965 |
# The first task is to loop through the entity's relation. A DELETE command will |
# Loop through the entity's relations. A DELETE command will be needed for each of them. |
|
# be needed for each of them. |
|
1966 |
my $relations = $entityData->{Relations}; |
my $relations = $entityData->{Relations}; |
1967 |
for my $relation (keys %{$relations}) { |
for my $relation (keys %{$relations}) { |
1968 |
my @augmentedList = (@stackedPath, $relation); |
my @augmentedList = (@stackedPath, $relation); |
1969 |
push @fromPathList, \@augmentedList; |
push @fromPathList, \@augmentedList; |
1970 |
} |
} |
1971 |
|
} |
1972 |
# Now we need to look for relationships connected to this entity. |
# Now we need to look for relationships connected to this entity. |
1973 |
my $relationshipList = $self->{_metaData}->{Relationships}; |
my $relationshipList = $self->{_metaData}->{Relationships}; |
1974 |
for my $relationshipName (keys %{$relationshipList}) { |
for my $relationshipName (keys %{$relationshipList}) { |
1975 |
my $relationship = $relationshipList->{$relationshipName}; |
my $relationship = $relationshipList->{$relationshipName}; |
1976 |
# Check the FROM field. We're only interested if it's us. |
# Check the FROM field. We're only interested if it's us. |
1977 |
if ($relationship->{from} eq $entityName) { |
if ($relationship->{from} eq $myEntityName) { |
1978 |
# Add the path to this relationship. |
# Add the path to this relationship. |
1979 |
my @augmentedList = (@stackedPath, $entityName, $relationshipName); |
my @augmentedList = (@stackedPath, $myEntityName, $relationshipName); |
1980 |
push @fromPathList, \@augmentedList; |
push @fromPathList, \@augmentedList; |
1981 |
# Check the arity. If it's MM we're done. If it's 1M |
# Check the arity. If it's MM we're done. If it's 1M |
1982 |
# and the target hasn't been seen yet, we want to |
# and the target hasn't been seen yet, we want to |
1995 |
} |
} |
1996 |
# Now check the TO field. In this case only the relationship needs |
# Now check the TO field. In this case only the relationship needs |
1997 |
# deletion. |
# deletion. |
1998 |
if ($relationship->{to} eq $entityName) { |
if ($relationship->{to} eq $myEntityName) { |
1999 |
my @augmentedList = (@stackedPath, $entityName, $relationshipName); |
my @augmentedList = (@stackedPath, $myEntityName, $relationshipName); |
2000 |
push @toPathList, \@augmentedList; |
push @toPathList, \@augmentedList; |
2001 |
} |
} |
2002 |
} |
} |
2003 |
} |
} |
2004 |
# Create the first qualifier for the WHERE clause. This selects the |
# Create the first qualifier for the WHERE clause. This selects the |
2005 |
# keys of the primary entity records to be deleted. When we're deleting |
# keys of the primary entity records to be deleted. When we're deleting |
2006 |
# from a dependent table, we construct a join page from the first qualifier |
# from a dependent table, we construct a join path from the first qualifier |
2007 |
# to the table containing the dependent records to delete. |
# to the table containing the dependent records to delete. |
2008 |
my $qualifier = ($objectID =~ /%/ ? "LIKE ?" : "= ?"); |
my $qualifier = ($objectID =~ /%/ ? "LIKE ?" : "= ?"); |
2009 |
# We need to make two passes. The first is through the to-list, and |
# We need to make two passes. The first is through the to-list, and |
2042 |
} |
} |
2043 |
} |
} |
2044 |
# Now we have our desired DELETE statement. |
# Now we have our desired DELETE statement. |
2045 |
if ($testFlag) { |
if ($options{testMode}) { |
2046 |
# Here the user wants to trace without executing. |
# Here the user wants to trace without executing. |
2047 |
Trace($stmt) if T(0); |
Trace($stmt) if T(0); |
2048 |
} else { |
} else { |
2049 |
# Here we can delete. Note that the SQL method dies with a confessing |
# Here we can delete. Note that the SQL method dies with a confession |
2050 |
# if an error occurs, so we just go ahead and do it. |
# if an error occurs, so we just go ahead and do it. |
2051 |
Trace("Executing delete from $target using '$objectID'.") if T(3); |
Trace("Executing delete from $target using '$objectID'.") if T(3); |
2052 |
my $rv = $db->SQL($stmt, 0, $objectID); |
my $rv = $db->SQL($stmt, 0, $objectID); |
2061 |
return $retVal; |
return $retVal; |
2062 |
} |
} |
2063 |
|
|
2064 |
|
=head3 Disconnect |
2065 |
|
|
2066 |
|
$erdb->Disconnect($relationshipName, $originEntityName, $originEntityID); |
2067 |
|
|
2068 |
|
Disconnect an entity instance from all the objects to which it is related. This |
2069 |
|
will delete each relationship instance that connects to the specified entity. |
2070 |
|
|
2071 |
|
=over 4 |
2072 |
|
|
2073 |
|
=item relationshipName |
2074 |
|
|
2075 |
|
Name of the relationship whose instances are to be deleted. |
2076 |
|
|
2077 |
|
=item originEntityName |
2078 |
|
|
2079 |
|
Name of the entity that is to be disconnected. |
2080 |
|
|
2081 |
|
=item originEntityID |
2082 |
|
|
2083 |
|
ID of the entity that is to be disconnected. |
2084 |
|
|
2085 |
|
=back |
2086 |
|
|
2087 |
|
=cut |
2088 |
|
|
2089 |
|
sub Disconnect { |
2090 |
|
# Get the parameters. |
2091 |
|
my ($self, $relationshipName, $originEntityName, $originEntityID) = @_; |
2092 |
|
# Get the relationship descriptor. |
2093 |
|
my $structure = $self->_GetStructure($relationshipName); |
2094 |
|
# Insure we have a relationship. |
2095 |
|
if (! exists $structure->{from}) { |
2096 |
|
Confess("$relationshipName is not a relationship in the database."); |
2097 |
|
} else { |
2098 |
|
# Get the database handle. |
2099 |
|
my $dbh = $self->{_dbh}; |
2100 |
|
# We'll set this value to 1 if we find our entity. |
2101 |
|
my $found = 0; |
2102 |
|
# Loop through the ends of the relationship. |
2103 |
|
for my $dir ('from', 'to') { |
2104 |
|
if ($structure->{$dir} eq $originEntityName) { |
2105 |
|
$found = 1; |
2106 |
|
# Here we want to delete all relationship instances on this side of the |
2107 |
|
# entity instance. |
2108 |
|
Trace("Disconnecting in $dir direction with ID \"$originEntityID\"."); |
2109 |
|
# We do this delete in batches to keep it from dragging down the |
2110 |
|
# server. |
2111 |
|
my $limitClause = ($FIG_Config::delete_limit ? "LIMIT $FIG_Config::delete_limit" : ""); |
2112 |
|
my $done = 0; |
2113 |
|
while (! $done) { |
2114 |
|
# Do the delete. |
2115 |
|
my $rows = $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ? $limitClause", 0, $originEntityID); |
2116 |
|
# See if we're done. We're done if no rows were found or the delete is unlimited. |
2117 |
|
$done = ($rows == 0 || ! $limitClause); |
2118 |
|
} |
2119 |
|
} |
2120 |
|
} |
2121 |
|
# Insure we found the entity on at least one end. |
2122 |
|
if (! $found) { |
2123 |
|
Confess("Entity \"$originEntityName\" does not use $relationshipName."); |
2124 |
|
} |
2125 |
|
} |
2126 |
|
} |
2127 |
|
|
2128 |
|
=head3 DeleteRow |
2129 |
|
|
2130 |
|
$erdb->DeleteRow($relationshipName, $fromLink, $toLink, \%values); |
2131 |
|
|
2132 |
|
Delete a row from a relationship. In most cases, only the from-link and to-link are |
2133 |
|
needed; however, for relationships with intersection data values can be specified |
2134 |
|
for the other fields using a hash. |
2135 |
|
|
2136 |
|
=over 4 |
2137 |
|
|
2138 |
|
=item relationshipName |
2139 |
|
|
2140 |
|
Name of the relationship from which the row is to be deleted. |
2141 |
|
|
2142 |
|
=item fromLink |
2143 |
|
|
2144 |
|
ID of the entity instance in the From direction. |
2145 |
|
|
2146 |
|
=item toLink |
2147 |
|
|
2148 |
|
ID of the entity instance in the To direction. |
2149 |
|
|
2150 |
|
=item values |
2151 |
|
|
2152 |
|
Reference to a hash of other values to be used for filtering the delete. |
2153 |
|
|
2154 |
|
=back |
2155 |
|
|
2156 |
|
=cut |
2157 |
|
|
2158 |
|
sub DeleteRow { |
2159 |
|
# Get the parameters. |
2160 |
|
my ($self, $relationshipName, $fromLink, $toLink, $values) = @_; |
2161 |
|
# Create a hash of all the filter information. |
2162 |
|
my %filter = ('from-link' => $fromLink, 'to-link' => $toLink); |
2163 |
|
if (defined $values) { |
2164 |
|
for my $key (keys %{$values}) { |
2165 |
|
$filter{$key} = $values->{$key}; |
2166 |
|
} |
2167 |
|
} |
2168 |
|
# Build an SQL statement out of the hash. |
2169 |
|
my @filters = (); |
2170 |
|
my @parms = (); |
2171 |
|
for my $key (keys %filter) { |
2172 |
|
push @filters, _FixName($key) . " = ?"; |
2173 |
|
push @parms, $filter{$key}; |
2174 |
|
} |
2175 |
|
Trace("Parms for delete row are " . join(", ", map { "\"$_\"" } @parms) . ".") if T(SQL => 4); |
2176 |
|
my $command = "DELETE FROM $relationshipName WHERE " . |
2177 |
|
join(" AND ", @filters); |
2178 |
|
# Execute it. |
2179 |
|
my $dbh = $self->{_dbh}; |
2180 |
|
$dbh->SQL($command, undef, @parms); |
2181 |
|
} |
2182 |
|
|
2183 |
|
=head3 DeleteLike |
2184 |
|
|
2185 |
|
my $deleteCount = $erdb->DeleteLike($relName, $filter, \@parms); |
2186 |
|
|
2187 |
|
Delete all the relationship rows that satisfy a particular filter condition. Unlike a normal |
2188 |
|
filter, only fields from the relationship itself can be used. |
2189 |
|
|
2190 |
|
=over 4 |
2191 |
|
|
2192 |
|
=item relName |
2193 |
|
|
2194 |
|
Name of the relationship whose records are to be deleted. |
2195 |
|
|
2196 |
|
=item filter |
2197 |
|
|
2198 |
|
A filter clause (L</Get>-style) for the delete query. |
2199 |
|
|
2200 |
|
=item parms |
2201 |
|
|
2202 |
|
Reference to a list of parameters for the filter clause. |
2203 |
|
|
2204 |
|
=item RETURN |
2205 |
|
|
2206 |
|
Returns a count of the number of rows deleted. |
2207 |
|
|
2208 |
|
=back |
2209 |
|
|
2210 |
|
=cut |
2211 |
|
|
2212 |
|
sub DeleteLike { |
2213 |
|
# Get the parameters. |
2214 |
|
my ($self, $objectName, $filter, $parms) = @_; |
2215 |
|
# Declare the return variable. |
2216 |
|
my $retVal; |
2217 |
|
# Insure the parms argument is an array reference if the caller left it off. |
2218 |
|
if (! defined($parms)) { |
2219 |
|
$parms = []; |
2220 |
|
} |
2221 |
|
# Insure we have a relationship. The main reason for this is if we delete an entity |
2222 |
|
# instance we have to yank out a bunch of other stuff with it. |
2223 |
|
if ($self->IsEntity($objectName)) { |
2224 |
|
Confess("Cannot use DeleteLike on $objectName, because it is not a relationship."); |
2225 |
|
} else { |
2226 |
|
# Create the SQL command suffix to get the desierd records. |
2227 |
|
my ($suffix) = $self->_SetupSQL([$objectName], $filter); |
2228 |
|
# Convert it to a DELETE command. |
2229 |
|
my $command = "DELETE $suffix"; |
2230 |
|
# Execute the command. |
2231 |
|
my $dbh = $self->{_dbh}; |
2232 |
|
my $result = $dbh->SQL($command, 0, @{$parms}); |
2233 |
|
# Check the results. Note we convert the "0D0" result to a real zero. |
2234 |
|
# A failure causes an abnormal termination, so the caller isn't going to |
2235 |
|
# worry about it. |
2236 |
|
if (! defined $result) { |
2237 |
|
Confess("Error deleting from $objectName: " . $dbh->errstr()); |
2238 |
|
} elsif ($result == 0) { |
2239 |
|
$retVal = 0; |
2240 |
|
} else { |
2241 |
|
$retVal = $result; |
2242 |
|
} |
2243 |
|
} |
2244 |
|
# Return the result count. |
2245 |
|
return $retVal; |
2246 |
|
} |
2247 |
|
|
2248 |
|
=head3 SortNeeded |
2249 |
|
|
2250 |
|
my $parms = $erdb->SortNeeded($relationName); |
2251 |
|
|
2252 |
|
Return the pipe command for the sort that should be applied to the specified |
2253 |
|
relation when creating the load file. |
2254 |
|
|
2255 |
|
For example, if the load file should be sorted ascending by the first |
2256 |
|
field, this method would return |
2257 |
|
|
2258 |
|
sort -k1 -t"\t" |
2259 |
|
|
2260 |
|
If the first field is numeric, the method would return |
2261 |
|
|
2262 |
|
sort -k1n -t"\t" |
2263 |
|
|
2264 |
|
Unfortunately, due to a bug in the C<sort> command, we cannot eliminate duplicate |
2265 |
|
keys using a sort. |
2266 |
|
|
2267 |
|
=over 4 |
2268 |
|
|
2269 |
|
=item relationName |
2270 |
|
|
2271 |
|
Name of the relation to be examined. |
2272 |
|
|
2273 |
|
=item |
2274 |
|
|
2275 |
|
Returns the sort command to use for sorting the relation, suitable for piping. |
2276 |
|
|
2277 |
|
=back |
2278 |
|
|
2279 |
|
=cut |
2280 |
|
#: Return Type $; |
2281 |
|
sub SortNeeded { |
2282 |
|
# Get the parameters. |
2283 |
|
my ($self, $relationName) = @_; |
2284 |
|
# Declare a descriptor to hold the names of the key fields. |
2285 |
|
my @keyNames = (); |
2286 |
|
# Get the relation structure. |
2287 |
|
my $relationData = $self->_FindRelation($relationName); |
2288 |
|
# Find out if the relation is a primary entity relation, |
2289 |
|
# a relationship relation, or a secondary entity relation. |
2290 |
|
my $entityTable = $self->{_metaData}->{Entities}; |
2291 |
|
my $relationshipTable = $self->{_metaData}->{Relationships}; |
2292 |
|
if (exists $entityTable->{$relationName}) { |
2293 |
|
# Here we have a primary entity relation. |
2294 |
|
push @keyNames, "id"; |
2295 |
|
} elsif (exists $relationshipTable->{$relationName}) { |
2296 |
|
# Here we have a relationship. We sort using the FROM index. |
2297 |
|
my $relationshipData = $relationshipTable->{$relationName}; |
2298 |
|
my $index = $relationData->{Indexes}->{idxFrom}; |
2299 |
|
push @keyNames, @{$index->{IndexFields}}; |
2300 |
|
} else { |
2301 |
|
# Here we have a secondary entity relation, so we have a sort on the ID field. |
2302 |
|
push @keyNames, "id"; |
2303 |
|
} |
2304 |
|
# Now we parse the key names into sort parameters. First, we prime the return |
2305 |
|
# string. |
2306 |
|
my $retVal = "sort -t\"\t\" "; |
2307 |
|
# Get the relation's field list. |
2308 |
|
my @fields = @{$relationData->{Fields}}; |
2309 |
|
# Loop through the keys. |
2310 |
|
for my $keyData (@keyNames) { |
2311 |
|
# Get the key and the ordering. |
2312 |
|
my ($keyName, $ordering); |
2313 |
|
if ($keyData =~ /^([^ ]+) DESC/) { |
2314 |
|
($keyName, $ordering) = ($1, "descending"); |
2315 |
|
} else { |
2316 |
|
($keyName, $ordering) = ($keyData, "ascending"); |
2317 |
|
} |
2318 |
|
# Find the key's position and type. |
2319 |
|
my $fieldSpec; |
2320 |
|
for (my $i = 0; $i <= $#fields && ! $fieldSpec; $i++) { |
2321 |
|
my $thisField = $fields[$i]; |
2322 |
|
if ($thisField->{name} eq $keyName) { |
2323 |
|
# Get the sort modifier for this field type. The modifier |
2324 |
|
# decides whether we're using a character, numeric, or |
2325 |
|
# floating-point sort. |
2326 |
|
my $modifier = $TypeTable{$thisField->{type}}->{sort}; |
2327 |
|
# If the index is descending for this field, denote we want |
2328 |
|
# to reverse the sort order on this field. |
2329 |
|
if ($ordering eq 'descending') { |
2330 |
|
$modifier .= "r"; |
2331 |
|
} |
2332 |
|
# Store the position and modifier into the field spec, which |
2333 |
|
# will stop the inner loop. Note that the field number is |
2334 |
|
# 1-based in the sort command, so we have to increment the |
2335 |
|
# index. |
2336 |
|
$fieldSpec = ($i + 1) . $modifier; |
2337 |
|
} |
2338 |
|
} |
2339 |
|
# Add this field to the sort command. |
2340 |
|
$retVal .= " -k$fieldSpec"; |
2341 |
|
} |
2342 |
|
# Return the result. |
2343 |
|
return $retVal; |
2344 |
|
} |
2345 |
|
|
2346 |
=head3 GetList |
=head3 GetList |
2347 |
|
|
2348 |
C<< my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, \@params); >> |
my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, \@params); |
2349 |
|
|
2350 |
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 |
2351 |
specified filter clause. |
specified filter clause. |
2373 |
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 |
2374 |
particular genus and sorts them by species name. |
particular genus and sorts them by species name. |
2375 |
|
|
2376 |
C<< "Genome(genus) = ? ORDER BY Genome(species)" >> |
"Genome(genus) = ? ORDER BY Genome(species)" |
2377 |
|
|
2378 |
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 |
2379 |
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 |
2385 |
|
|
2386 |
=item RETURN |
=item RETURN |
2387 |
|
|
2388 |
Returns a list of B<DBObject>s that satisfy the query conditions. |
Returns a list of B<ERDBObject>s that satisfy the query conditions. |
2389 |
|
|
2390 |
=back |
=back |
2391 |
|
|
2408 |
|
|
2409 |
=head3 GetCount |
=head3 GetCount |
2410 |
|
|
2411 |
C<< my $count = $erdb->GetCount(\@objectNames, $filter, \@params); >> |
my $count = $erdb->GetCount(\@objectNames, $filter, \@params); |
2412 |
|
|
2413 |
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 |
2414 |
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 |
2459 |
sub GetCount { |
sub GetCount { |
2460 |
# Get the parameters. |
# Get the parameters. |
2461 |
my ($self, $objectNames, $filter, $params) = @_; |
my ($self, $objectNames, $filter, $params) = @_; |
2462 |
|
# Insure the params argument is an array reference if the caller left it off. |
2463 |
|
if (! defined($params)) { |
2464 |
|
$params = []; |
2465 |
|
} |
2466 |
# Declare the return variable. |
# Declare the return variable. |
2467 |
my $retVal; |
my $retVal; |
2468 |
# Find out if we're counting an entity or a relationship. |
# Find out if we're counting an entity or a relationship. |
2501 |
|
|
2502 |
=head3 ComputeObjectSentence |
=head3 ComputeObjectSentence |
2503 |
|
|
2504 |
C<< my $sentence = $erdb->ComputeObjectSentence($objectName); >> |
my $sentence = $erdb->ComputeObjectSentence($objectName); |
2505 |
|
|
2506 |
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. |
2507 |
|
|
2536 |
|
|
2537 |
=head3 DumpRelations |
=head3 DumpRelations |
2538 |
|
|
2539 |
C<< $erdb->DumpRelations($outputDirectory); >> |
$erdb->DumpRelations($outputDirectory); |
2540 |
|
|
2541 |
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. |
2542 |
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. |
2576 |
} |
} |
2577 |
} |
} |
2578 |
|
|
2579 |
|
=head3 InsertValue |
2580 |
|
|
2581 |
|
$erdb->InsertValue($entityID, $fieldName, $value); |
2582 |
|
|
2583 |
|
This method will insert a new value into the database. The value must be one |
2584 |
|
associated with a secondary relation, since primary values cannot be inserted: |
2585 |
|
they occur exactly once. Secondary values, on the other hand, can be missing |
2586 |
|
or multiply-occurring. |
2587 |
|
|
2588 |
|
=over 4 |
2589 |
|
|
2590 |
|
=item entityID |
2591 |
|
|
2592 |
|
ID of the object that is to receive the new value. |
2593 |
|
|
2594 |
|
=item fieldName |
2595 |
|
|
2596 |
|
Field name for the new value-- this includes the entity name, since |
2597 |
|
field names are of the format I<objectName>C<(>I<fieldName>C<)>. |
2598 |
|
|
2599 |
|
=item value |
2600 |
|
|
2601 |
|
New value to be put in the field. |
2602 |
|
|
2603 |
|
=back |
2604 |
|
|
2605 |
|
=cut |
2606 |
|
|
2607 |
|
sub InsertValue { |
2608 |
|
# Get the parameters. |
2609 |
|
my ($self, $entityID, $fieldName, $value) = @_; |
2610 |
|
# Parse the entity name and the real field name. |
2611 |
|
if ($fieldName =~ /^([^(]+)\(([^)]+)\)/) { |
2612 |
|
my $entityName = $1; |
2613 |
|
my $fieldTitle = $2; |
2614 |
|
# Get its descriptor. |
2615 |
|
if (!$self->IsEntity($entityName)) { |
2616 |
|
Confess("$entityName is not a valid entity."); |
2617 |
|
} else { |
2618 |
|
my $entityData = $self->{_metaData}->{Entities}->{$entityName}; |
2619 |
|
# Find the relation containing this field. |
2620 |
|
my $fieldHash = $entityData->{Fields}; |
2621 |
|
if (! exists $fieldHash->{$fieldTitle}) { |
2622 |
|
Confess("$fieldTitle not found in $entityName."); |
2623 |
|
} else { |
2624 |
|
my $relation = $fieldHash->{$fieldTitle}->{relation}; |
2625 |
|
if ($relation eq $entityName) { |
2626 |
|
Confess("Cannot do InsertValue on primary field $fieldTitle of $entityName."); |
2627 |
|
} else { |
2628 |
|
# Now we can create an INSERT statement. |
2629 |
|
my $dbh = $self->{_dbh}; |
2630 |
|
my $fixedName = _FixName($fieldTitle); |
2631 |
|
my $statement = "INSERT INTO $relation (id, $fixedName) VALUES(?, ?)"; |
2632 |
|
# Execute the command. |
2633 |
|
$dbh->SQL($statement, 0, $entityID, $value); |
2634 |
|
} |
2635 |
|
} |
2636 |
|
} |
2637 |
|
} else { |
2638 |
|
Confess("$fieldName is not a valid field name."); |
2639 |
|
} |
2640 |
|
} |
2641 |
|
|
2642 |
=head3 InsertObject |
=head3 InsertObject |
2643 |
|
|
2644 |
C<< my $ok = $erdb->InsertObject($objectType, \%fieldHash); >> |
$erdb->InsertObject($objectType, \%fieldHash); |
2645 |
|
|
2646 |
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 |
2647 |
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. |
2650 |
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 |
2651 |
C<ZP_00210270.1> and C<gi|46206278>. |
C<ZP_00210270.1> and C<gi|46206278>. |
2652 |
|
|
2653 |
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']}); |
2654 |
|
|
2655 |
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 |
2656 |
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>. |
2657 |
|
|
2658 |
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'}); |
2659 |
|
|
2660 |
=over 4 |
=over 4 |
2661 |
|
|
2667 |
|
|
2668 |
Hash of field names to values. |
Hash of field names to values. |
2669 |
|
|
|
=item RETURN |
|
|
|
|
|
Returns 1 if successful, 0 if an error occurred. |
|
|
|
|
2670 |
=back |
=back |
2671 |
|
|
2672 |
=cut |
=cut |
2765 |
$retVal = $sth->execute(@parameterList); |
$retVal = $sth->execute(@parameterList); |
2766 |
if (!$retVal) { |
if (!$retVal) { |
2767 |
my $errorString = $sth->errstr(); |
my $errorString = $sth->errstr(); |
2768 |
Trace("Insert error: $errorString.") if T(0); |
Confess("Error inserting into $relationName: $errorString"); |
2769 |
|
} else { |
2770 |
|
Trace("Insert successful using $parameterList[0].") if T(3); |
2771 |
} |
} |
2772 |
} |
} |
2773 |
} |
} |
2774 |
} |
} |
2775 |
# Return the success indicator. |
# Return a 1 for backward compatability. |
2776 |
return $retVal; |
return 1; |
2777 |
|
} |
2778 |
|
|
2779 |
|
=head3 UpdateEntity |
2780 |
|
|
2781 |
|
$erdb->UpdateEntity($entityName, $id, \%fields); |
2782 |
|
|
2783 |
|
Update the values of an entity. This is an unprotected update, so it should only be |
2784 |
|
done if the database resides on a database server. |
2785 |
|
|
2786 |
|
=over 4 |
2787 |
|
|
2788 |
|
=item entityName |
2789 |
|
|
2790 |
|
Name of the entity to update. (This is the entity type.) |
2791 |
|
|
2792 |
|
=item id |
2793 |
|
|
2794 |
|
ID of the entity to update. If no entity exists with this ID, an error will be thrown. |
2795 |
|
|
2796 |
|
=item fields |
2797 |
|
|
2798 |
|
Reference to a hash mapping field names to their new values. All of the fields named |
2799 |
|
must be in the entity's primary relation, and they cannot any of them be the ID field. |
2800 |
|
|
2801 |
|
=back |
2802 |
|
|
2803 |
|
=cut |
2804 |
|
|
2805 |
|
sub UpdateEntity { |
2806 |
|
# Get the parameters. |
2807 |
|
my ($self, $entityName, $id, $fields) = @_; |
2808 |
|
# Get a list of the field names being updated. |
2809 |
|
my @fieldList = keys %{$fields}; |
2810 |
|
# Verify that the fields exist. |
2811 |
|
my $checker = $self->GetFieldTable($entityName); |
2812 |
|
for my $field (@fieldList) { |
2813 |
|
if ($field eq 'id') { |
2814 |
|
Confess("Cannot update the ID field for entity $entityName."); |
2815 |
|
} elsif ($checker->{$field}->{relation} ne $entityName) { |
2816 |
|
Confess("Cannot find $field in primary relation of $entityName."); |
2817 |
|
} |
2818 |
|
} |
2819 |
|
# Build the SQL statement. |
2820 |
|
my @sets = (); |
2821 |
|
my @valueList = (); |
2822 |
|
for my $field (@fieldList) { |
2823 |
|
push @sets, _FixName($field) . " = ?"; |
2824 |
|
push @valueList, $fields->{$field}; |
2825 |
|
} |
2826 |
|
my $command = "UPDATE $entityName SET " . join(", ", @sets) . " WHERE id = ?"; |
2827 |
|
# Add the ID to the list of binding values. |
2828 |
|
push @valueList, $id; |
2829 |
|
# Call SQL to do the work. |
2830 |
|
my $rows = $self->{_dbh}->SQL($command, 0, @valueList); |
2831 |
|
# Check for errors. |
2832 |
|
if ($rows == 0) { |
2833 |
|
Confess("Entity $id of type $entityName not found."); |
2834 |
|
} |
2835 |
} |
} |
2836 |
|
|
2837 |
=head3 LoadTable |
=head3 LoadTable |
2838 |
|
|
2839 |
C<< my %results = $erdb->LoadTable($fileName, $relationName, $truncateFlag); >> |
my $results = $erdb->LoadTable($fileName, $relationName, %options); |
2840 |
|
|
2841 |
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 |
2842 |
first. |
first. |
2851 |
|
|
2852 |
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. |
2853 |
|
|
2854 |
=item truncateFlag |
=item options |
2855 |
|
|
2856 |
TRUE if the table should be dropped and re-created, else FALSE |
A hash of load options. |
2857 |
|
|
2858 |
=item RETURN |
=item RETURN |
2859 |
|
|
2861 |
|
|
2862 |
=back |
=back |
2863 |
|
|
2864 |
|
The permissible options are as follows. |
2865 |
|
|
2866 |
|
=over 4 |
2867 |
|
|
2868 |
|
=item truncate |
2869 |
|
|
2870 |
|
If TRUE, then the table will be erased before loading. |
2871 |
|
|
2872 |
|
=item mode |
2873 |
|
|
2874 |
|
Mode in which the load should operate, either C<low_priority> or C<concurrent>. |
2875 |
|
This option is only applicable to a MySQL database. |
2876 |
|
|
2877 |
|
=item partial |
2878 |
|
|
2879 |
|
If TRUE, then it is assumed that this is a partial load, and the table will not |
2880 |
|
be analyzed and compacted at the end. |
2881 |
|
|
2882 |
|
=back |
2883 |
|
|
2884 |
=cut |
=cut |
2885 |
sub LoadTable { |
sub LoadTable { |
2886 |
# Get the parameters. |
# Get the parameters. |
2887 |
my ($self, $fileName, $relationName, $truncateFlag) = @_; |
my ($self, $fileName, $relationName, %options) = @_; |
2888 |
# Create the statistical return object. |
# Create the statistical return object. |
2889 |
my $retVal = _GetLoadStats(); |
my $retVal = _GetLoadStats(); |
2890 |
# Trace the fact of the load. |
# Trace the fact of the load. |
2896 |
# Get the relation data. |
# Get the relation data. |
2897 |
my $relation = $self->_FindRelation($relationName); |
my $relation = $self->_FindRelation($relationName); |
2898 |
# Check the truncation flag. |
# Check the truncation flag. |
2899 |
if ($truncateFlag) { |
if ($options{truncate}) { |
2900 |
Trace("Creating table $relationName") if T(2); |
Trace("Creating table $relationName") if T(2); |
2901 |
# 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, |
2902 |
# 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 |
2903 |
# leave extra room. We postulate a minimum row count of 1000 to |
# leave extra room. We postulate a minimum row count of 1000 to |
2904 |
# prevent problems with incoming empty load files. |
# prevent problems with incoming empty load files. |
2905 |
my $rowSize = $self->EstimateRowSize($relationName); |
my $rowSize = $self->EstimateRowSize($relationName); |
2906 |
my $estimate = FIG::max($fileSize * 1.5 / $rowSize, 1000); |
my $estimate = $fileSize * 8 / $rowSize; |
2907 |
|
if ($estimate < 1000) { |
2908 |
|
$estimate = 1000; |
2909 |
|
} |
2910 |
# Re-create the table without its index. |
# Re-create the table without its index. |
2911 |
$self->CreateTable($relationName, 0, $estimate); |
$self->CreateTable($relationName, 0, $estimate); |
2912 |
# If this is a pre-index DBMS, create the index here. |
# If this is a pre-index DBMS, create the index here. |
2922 |
# Load the table. |
# Load the table. |
2923 |
my $rv; |
my $rv; |
2924 |
eval { |
eval { |
2925 |
$rv = $dbh->load_table(file => $fileName, tbl => $relationName); |
$rv = $dbh->load_table(file => $fileName, tbl => $relationName, style => $options{mode}); |
2926 |
}; |
}; |
2927 |
if (!defined $rv) { |
if (!defined $rv) { |
2928 |
$retVal->AddMessage($@) if ($@); |
$retVal->AddMessage($@) if ($@); |
2929 |
$retVal->AddMessage("Table load failed for $relationName using $fileName."); |
$retVal->AddMessage("Table load failed for $relationName using $fileName: " . $dbh->error_message); |
2930 |
Trace("Table load failed for $relationName.") if T(1); |
Trace("Table load failed for $relationName.") if T(1); |
2931 |
} else { |
} else { |
2932 |
# Here we successfully loaded the table. |
# Here we successfully loaded the table. |
2934 |
my $size = -s $fileName; |
my $size = -s $fileName; |
2935 |
Trace("$size bytes loaded into $relationName.") if T(2); |
Trace("$size bytes loaded into $relationName.") if T(2); |
2936 |
# If we're rebuilding, we need to create the table indexes. |
# If we're rebuilding, we need to create the table indexes. |
2937 |
if ($truncateFlag && ! $dbh->{_preIndex}) { |
if ($options{truncate}) { |
2938 |
|
# Indexes are created here for PostGres. For PostGres, indexes are |
2939 |
|
# best built at the end. For MySQL, the reverse is true. |
2940 |
|
if (! $dbh->{_preIndex}) { |
2941 |
eval { |
eval { |
2942 |
$self->CreateIndex($relationName); |
$self->CreateIndex($relationName); |
2943 |
}; |
}; |
2945 |
$retVal->AddMessage($@); |
$retVal->AddMessage($@); |
2946 |
} |
} |
2947 |
} |
} |
2948 |
|
# The full-text index (if any) is always built last, even for MySQL. |
2949 |
|
# First we need to see if this table has a full-text index. Only |
2950 |
|
# primary relations are allowed that privilege. |
2951 |
|
Trace("Checking for full-text index on $relationName.") if T(2); |
2952 |
|
if ($self->_IsPrimary($relationName)) { |
2953 |
|
$self->CreateSearchIndex($relationName); |
2954 |
|
} |
2955 |
|
} |
2956 |
} |
} |
2957 |
# Analyze the table to improve performance. |
# Analyze the table to improve performance. |
2958 |
|
if (! $options{partial}) { |
2959 |
|
Trace("Analyzing and compacting $relationName.") if T(3); |
2960 |
$dbh->vacuum_it($relationName); |
$dbh->vacuum_it($relationName); |
2961 |
|
} |
2962 |
|
Trace("$relationName load completed.") if T(3); |
2963 |
# Return the statistics. |
# Return the statistics. |
2964 |
return $retVal; |
return $retVal; |
2965 |
} |
} |
2966 |
|
|
2967 |
=head3 GenerateEntity |
=head3 CreateSearchIndex |
2968 |
|
|
2969 |
C<< my $fieldHash = $erdb->GenerateEntity($id, $type, \%values); >> |
$erdb->CreateSearchIndex($objectName); |
2970 |
|
|
2971 |
Generate the data for a new entity instance. This method creates a field hash suitable for |
Check for a full-text search index on the specified entity or relationship object, and |
2972 |
passing as a parameter to L</InsertObject>. The ID is specified by the callr, but the rest |
if one is required, rebuild it. |
|
of the fields are generated using information in the database schema. |
|
|
|
|
|
Each data type has a default algorithm for generating random test data. This can be overridden |
|
|
by including a B<DataGen> element in the field. If this happens, the content of the element is |
|
|
executed as a PERL program in the context of this module. The element may make use of a C<$this> |
|
|
variable which contains the field hash as it has been built up to the current point. If any |
|
|
fields are dependent on other fields, the C<pass> attribute can be used to control the order |
|
|
in which the fields are generated. A field with a high data pass number will be generated after |
|
|
a field with a lower one. If any external values are needed, they should be passed in via the |
|
|
optional third parameter, which will be available to the data generation script under the name |
|
|
C<$value>. Several useful utility methods are provided for generating random values, including |
|
|
L</IntGen>, L</StringGen>, L</FloatGen>, and L</DateGen>. Note that dates are stored and generated |
|
|
in the form of a timestamp number rather than a string. |
|
2973 |
|
|
2974 |
=over 4 |
=over 4 |
2975 |
|
|
2976 |
=item id |
=item objectName |
2977 |
|
|
2978 |
ID to assign to the new entity. |
Name of the entity or relationship to be indexed. |
2979 |
|
|
2980 |
=item type |
=back |
2981 |
|
|
2982 |
Type name for the new entity. |
=cut |
2983 |
|
|
2984 |
=item values |
sub CreateSearchIndex { |
2985 |
|
# Get the parameters. |
2986 |
|
my ($self, $objectName) = @_; |
2987 |
|
# Get the relation's entity/relationship structure. |
2988 |
|
my $structure = $self->_GetStructure($objectName); |
2989 |
|
# Get the database handle. |
2990 |
|
my $dbh = $self->{_dbh}; |
2991 |
|
Trace("Checking for search fields in $objectName.") if T(3); |
2992 |
|
# Check for a searchable fields list. |
2993 |
|
if (exists $structure->{searchFields}) { |
2994 |
|
# Here we know that we need to create a full-text search index. |
2995 |
|
# Get an SQL-formatted field name list. |
2996 |
|
my $fields = join(", ", _FixNames(@{$structure->{searchFields}})); |
2997 |
|
# Create the index. If it already exists, it will be dropped. |
2998 |
|
$dbh->create_index(tbl => $objectName, idx => "search_idx", |
2999 |
|
flds => $fields, kind => 'fulltext'); |
3000 |
|
Trace("Index created for $fields in $objectName.") if T(2); |
3001 |
|
} |
3002 |
|
} |
3003 |
|
|
3004 |
Hash containing additional values that might be needed by the data generation methods (optional). |
=head3 DropRelation |
3005 |
|
|
3006 |
|
$erdb->DropRelation($relationName); |
3007 |
|
|
3008 |
|
Physically drop a relation from the database. |
3009 |
|
|
3010 |
|
=over 4 |
3011 |
|
|
3012 |
|
=item relationName |
3013 |
|
|
3014 |
|
Name of the relation to drop. If it does not exist, this method will have |
3015 |
|
no effect. |
3016 |
|
|
3017 |
=back |
=back |
3018 |
|
|
3019 |
=cut |
=cut |
3020 |
|
|
3021 |
sub GenerateEntity { |
sub DropRelation { |
3022 |
# Get the parameters. |
# Get the parameters. |
3023 |
my ($self, $id, $type, $values) = @_; |
my ($self, $relationName) = @_; |
3024 |
# Create the return hash. |
# Get the database handle. |
3025 |
my $this = { id => $id }; |
my $dbh = $self->{_dbh}; |
3026 |
# Get the metadata structure. |
# Drop the relation. The method used here has no effect if the relation |
3027 |
my $metadata = $self->{_metaData}; |
# does not exist. |
3028 |
# Get this entity's list of fields. |
Trace("Invoking DB Kernel to drop $relationName.") if T(3); |
3029 |
if (!exists $metadata->{Entities}->{$type}) { |
$dbh->drop_table(tbl => $relationName); |
3030 |
Confess("Unrecognized entity type $type in GenerateEntity."); |
} |
3031 |
} else { |
|
3032 |
my $entity = $metadata->{Entities}->{$type}; |
=head3 MatchSqlPattern |
3033 |
my $fields = $entity->{Fields}; |
|
3034 |
# Generate data from the fields. |
my $matched = ERDB::MatchSqlPattern($value, $pattern); |
3035 |
_GenerateFields($this, $fields, $type, $values); |
|
3036 |
|
Determine whether or not a specified value matches an SQL pattern. An SQL |
3037 |
|
pattern has two wild card characters: C<%> that matches multiple characters, |
3038 |
|
and C<_> that matches a single character. These can be escaped using a |
3039 |
|
backslash (C<\>). We pull this off by converting the SQL pattern to a |
3040 |
|
PERL regular expression. As per SQL rules, the match is case-insensitive. |
3041 |
|
|
3042 |
|
=over 4 |
3043 |
|
|
3044 |
|
=item value |
3045 |
|
|
3046 |
|
Value to be matched against the pattern. Note that an undefined or empty |
3047 |
|
value will not match anything. |
3048 |
|
|
3049 |
|
=item pattern |
3050 |
|
|
3051 |
|
SQL pattern against which to match the value. An undefined or empty pattern will |
3052 |
|
match everything. |
3053 |
|
|
3054 |
|
=item RETURN |
3055 |
|
|
3056 |
|
Returns TRUE if the value and pattern match, else FALSE. |
3057 |
|
|
3058 |
|
=back |
3059 |
|
|
3060 |
|
=cut |
3061 |
|
|
3062 |
|
sub MatchSqlPattern { |
3063 |
|
# Get the parameters. |
3064 |
|
my ($value, $pattern) = @_; |
3065 |
|
# Declare the return variable. |
3066 |
|
my $retVal; |
3067 |
|
# Insure we have a pattern. |
3068 |
|
if (! defined($pattern) || $pattern eq "") { |
3069 |
|
$retVal = 1; |
3070 |
|
} else { |
3071 |
|
# Break the pattern into pieces around the wildcard characters. Because we |
3072 |
|
# use parentheses in the split function's delimiter expression, we'll get |
3073 |
|
# list elements for the delimiters as well as the rest of the string. |
3074 |
|
my @pieces = split /([_%]|\\[_%])/, $pattern; |
3075 |
|
# Check some fast special cases. |
3076 |
|
if ($pattern eq '%') { |
3077 |
|
# A null pattern matches everything. |
3078 |
|
$retVal = 1; |
3079 |
|
} elsif (@pieces == 1) { |
3080 |
|
# No wildcards, so we have a literal comparison. Note we're case-insensitive. |
3081 |
|
$retVal = (lc($value) eq lc($pattern)); |
3082 |
|
} elsif (@pieces == 2 && $pieces[1] eq '%') { |
3083 |
|
# A wildcard at the end, so we have a substring match. This is also case-insensitive. |
3084 |
|
$retVal = (lc(substr($value, 0, length($pieces[0]))) eq lc($pieces[0])); |
3085 |
|
} else { |
3086 |
|
# Okay, we have to do it the hard way. Convert each piece to a PERL pattern. |
3087 |
|
my $realPattern = ""; |
3088 |
|
for my $piece (@pieces) { |
3089 |
|
# Determine the type of piece. |
3090 |
|
if ($piece eq "") { |
3091 |
|
# Empty pieces are ignored. |
3092 |
|
} elsif ($piece eq "%") { |
3093 |
|
# Here we have a multi-character wildcard. Note that it can match |
3094 |
|
# zero or more characters. |
3095 |
|
$realPattern .= ".*" |
3096 |
|
} elsif ($piece eq "_") { |
3097 |
|
# Here we have a single-character wildcard. |
3098 |
|
$realPattern .= "."; |
3099 |
|
} elsif ($piece eq "\\%" || $piece eq "\\_") { |
3100 |
|
# This is an escape sequence (which is a rare thing, actually). |
3101 |
|
$realPattern .= substr($piece, 1, 1); |
3102 |
|
} else { |
3103 |
|
# Here we have raw text. |
3104 |
|
$realPattern .= quotemeta($piece); |
3105 |
} |
} |
3106 |
# Return the hash created. |
} |
3107 |
return $this; |
# Do the match. |
3108 |
|
$retVal = ($value =~ /^$realPattern$/i ? 1 : 0); |
3109 |
|
} |
3110 |
|
} |
3111 |
|
# Return the result. |
3112 |
|
return $retVal; |
3113 |
} |
} |
3114 |
|
|
3115 |
=head3 GetEntity |
=head3 GetEntity |
3116 |
|
|
3117 |
C<< my $entityObject = $erdb->GetEntity($entityType, $ID); >> |
my $entityObject = $erdb->GetEntity($entityType, $ID); |
3118 |
|
|
3119 |
Return an object describing the entity instance with a specified ID. |
Return an object describing the entity instance with a specified ID. |
3120 |
|
|
3130 |
|
|
3131 |
=item RETURN |
=item RETURN |
3132 |
|
|
3133 |
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 |
3134 |
instance is found with the specified key. |
instance is found with the specified key. |
3135 |
|
|
3136 |
=back |
=back |
3148 |
return $retVal; |
return $retVal; |
3149 |
} |
} |
3150 |
|
|
3151 |
|
=head3 GetChoices |
3152 |
|
|
3153 |
|
my @values = $erdb->GetChoices($entityName, $fieldName); |
3154 |
|
|
3155 |
|
Return a list of all the values for the specified field that are represented in the |
3156 |
|
specified entity. |
3157 |
|
|
3158 |
|
Note that if the field is not indexed, then this will be a very slow operation. |
3159 |
|
|
3160 |
|
=over 4 |
3161 |
|
|
3162 |
|
=item entityName |
3163 |
|
|
3164 |
|
Name of an entity in the database. |
3165 |
|
|
3166 |
|
=item fieldName |
3167 |
|
|
3168 |
|
Name of a field belonging to the entity. This is a raw field name without |
3169 |
|
the standard parenthesized notation used in most calls. |
3170 |
|
|
3171 |
|
=item RETURN |
3172 |
|
|
3173 |
|
Returns a list of the distinct values for the specified field in the database. |
3174 |
|
|
3175 |
|
=back |
3176 |
|
|
3177 |
|
=cut |
3178 |
|
|
3179 |
|
sub GetChoices { |
3180 |
|
# Get the parameters. |
3181 |
|
my ($self, $entityName, $fieldName) = @_; |
3182 |
|
# Declare the return variable. |
3183 |
|
my @retVal; |
3184 |
|
# Get the entity data structure. |
3185 |
|
my $entityData = $self->_GetStructure($entityName); |
3186 |
|
# Get the field. |
3187 |
|
my $fieldHash = $entityData->{Fields}; |
3188 |
|
if (! exists $fieldHash->{$fieldName}) { |
3189 |
|
Confess("$fieldName not found in $entityName."); |
3190 |
|
} else { |
3191 |
|
# Get the name of the relation containing the field. |
3192 |
|
my $relation = $fieldHash->{$fieldName}->{relation}; |
3193 |
|
# Fix up the field name. |
3194 |
|
my $realName = _FixName($fieldName); |
3195 |
|
# Get the database handle. |
3196 |
|
my $dbh = $self->{_dbh}; |
3197 |
|
# Query the database. |
3198 |
|
my $results = $dbh->SQL("SELECT DISTINCT $realName FROM $relation"); |
3199 |
|
# Clean the results. They are stored as a list of lists, and we just want the one list. |
3200 |
|
@retVal = sort map { $_->[0] } @{$results}; |
3201 |
|
} |
3202 |
|
# Return the result. |
3203 |
|
return @retVal; |
3204 |
|
} |
3205 |
|
|
3206 |
=head3 GetEntityValues |
=head3 GetEntityValues |
3207 |
|
|
3208 |
C<< my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); >> |
my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); |
3209 |
|
|
3210 |
Return a list of values from a specified entity instance. |
Return a list of values from a specified entity instance. If the entity instance |
3211 |
|
does not exist, an empty list is returned. |
3212 |
|
|
3213 |
=over 4 |
=over 4 |
3214 |
|
|
3249 |
|
|
3250 |
=head3 GetAll |
=head3 GetAll |
3251 |
|
|
3252 |
C<< my @list = $erdb->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >> |
my @list = $erdb->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); |
3253 |
|
|
3254 |
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 |
3255 |
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 |
3263 |
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 |
3264 |
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 |
3265 |
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 |
3266 |
feature ID followed by all of its aliases. |
feature ID followed by all of its essentiality determinations. |
3267 |
|
|
3268 |
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)']); |
3269 |
|
|
3270 |
=over 4 |
=over 4 |
3271 |
|
|
3336 |
push @retVal, \@rowData; |
push @retVal, \@rowData; |
3337 |
$fetched++; |
$fetched++; |
3338 |
} |
} |
3339 |
|
Trace("$fetched rows returned in GetAll.") if T(SQL => 4); |
3340 |
# Return the resulting list. |
# Return the resulting list. |
3341 |
return @retVal; |
return @retVal; |
3342 |
} |
} |
3343 |
|
|
3344 |
|
=head3 Exists |
3345 |
|
|
3346 |
|
my $found = $sprout->Exists($entityName, $entityID); |
3347 |
|
|
3348 |
|
Return TRUE if an entity exists, else FALSE. |
3349 |
|
|
3350 |
|
=over 4 |
3351 |
|
|
3352 |
|
=item entityName |
3353 |
|
|
3354 |
|
Name of the entity type (e.g. C<Feature>) relevant to the existence check. |
3355 |
|
|
3356 |
|
=item entityID |
3357 |
|
|
3358 |
|
ID of the entity instance whose existence is to be checked. |
3359 |
|
|
3360 |
|
=item RETURN |
3361 |
|
|
3362 |
|
Returns TRUE if the entity instance exists, else FALSE. |
3363 |
|
|
3364 |
|
=back |
3365 |
|
|
3366 |
|
=cut |
3367 |
|
#: Return Type $; |
3368 |
|
sub Exists { |
3369 |
|
# Get the parameters. |
3370 |
|
my ($self, $entityName, $entityID) = @_; |
3371 |
|
# Check for the entity instance. |
3372 |
|
Trace("Checking existence of $entityName with ID=$entityID.") if T(4); |
3373 |
|
my $testInstance = $self->GetEntity($entityName, $entityID); |
3374 |
|
# Return an existence indicator. |
3375 |
|
my $retVal = ($testInstance ? 1 : 0); |
3376 |
|
return $retVal; |
3377 |
|
} |
3378 |
|
|
3379 |
=head3 EstimateRowSize |
=head3 EstimateRowSize |
3380 |
|
|
3381 |
C<< my $rowSize = $erdb->EstimateRowSize($relName); >> |
my $rowSize = $erdb->EstimateRowSize($relName); |
3382 |
|
|
3383 |
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 |
3384 |
up the average length for each data type. |
up the average length for each data type. |
3410 |
my $fieldLen = $TypeTable{$fieldData->{type}}->{avgLen}; |
my $fieldLen = $TypeTable{$fieldData->{type}}->{avgLen}; |
3411 |
$retVal += $fieldLen; |
$retVal += $fieldLen; |
3412 |
} |
} |
3413 |
# Return the result. |
# Return the result. |
3414 |
return $retVal; |
return $retVal; |
3415 |
|
} |
3416 |
|
|
3417 |
|
=head3 GetFieldTable |
3418 |
|
|
3419 |
|
my $fieldHash = $self->GetFieldTable($objectnName); |
3420 |
|
|
3421 |
|
Get the field structure for a specified entity or relationship. |
3422 |
|
|
3423 |
|
=over 4 |
3424 |
|
|
3425 |
|
=item objectName |
3426 |
|
|
3427 |
|
Name of the desired entity or relationship. |
3428 |
|
|
3429 |
|
=item RETURN |
3430 |
|
|
3431 |
|
The table containing the field descriptors for the specified object. |
3432 |
|
|
3433 |
|
=back |
3434 |
|
|
3435 |
|
=cut |
3436 |
|
|
3437 |
|
sub GetFieldTable { |
3438 |
|
# Get the parameters. |
3439 |
|
my ($self, $objectName) = @_; |
3440 |
|
# Get the descriptor from the metadata. |
3441 |
|
my $objectData = $self->_GetStructure($objectName); |
3442 |
|
# Return the object's field table. |
3443 |
|
return $objectData->{Fields}; |
3444 |
|
} |
3445 |
|
|
3446 |
|
=head3 SplitKeywords |
3447 |
|
|
3448 |
|
my @keywords = ERDB::SplitKeywords($keywordString); |
3449 |
|
|
3450 |
|
This method returns a list of the positive keywords in the specified |
3451 |
|
keyword string. All of the operators will have been stripped off, |
3452 |
|
and if the keyword is preceded by a minus operator (C<->), it will |
3453 |
|
not be in the list returned. The idea here is to get a list of the |
3454 |
|
keywords the user wants to see. The list will be processed to remove |
3455 |
|
duplicates. |
3456 |
|
|
3457 |
|
It is possible to create a string that confuses this method. For example |
3458 |
|
|
3459 |
|
frog toad -frog |
3460 |
|
|
3461 |
|
would return both C<frog> and C<toad>. If this is a problem we can deal |
3462 |
|
with it later. |
3463 |
|
|
3464 |
|
=over 4 |
3465 |
|
|
3466 |
|
=item keywordString |
3467 |
|
|
3468 |
|
The keyword string to be parsed. |
3469 |
|
|
3470 |
|
=item RETURN |
3471 |
|
|
3472 |
|
Returns a list of the words in the keyword string the user wants to |
3473 |
|
see. |
3474 |
|
|
3475 |
|
=back |
3476 |
|
|
3477 |
|
=cut |
3478 |
|
|
3479 |
|
sub SplitKeywords { |
3480 |
|
# Get the parameters. |
3481 |
|
my ($keywordString) = @_; |
3482 |
|
# Make a safety copy of the string. (This helps during debugging.) |
3483 |
|
my $workString = $keywordString; |
3484 |
|
# Convert operators we don't care about to spaces. |
3485 |
|
$workString =~ tr/+"()<>/ /; |
3486 |
|
# Split the rest of the string along space boundaries. Note that we |
3487 |
|
# eliminate any words that are zero length or begin with a minus sign. |
3488 |
|
my @wordList = grep { $_ && substr($_, 0, 1) ne "-" } split /\s+/, $workString; |
3489 |
|
# Use a hash to remove duplicates. |
3490 |
|
my %words = map { $_ => 1 } @wordList; |
3491 |
|
# Return the result. |
3492 |
|
return sort keys %words; |
3493 |
|
} |
3494 |
|
|
3495 |
|
=head3 ValidateFieldName |
3496 |
|
|
3497 |
|
my $okFlag = ERDB::ValidateFieldName($fieldName); |
3498 |
|
|
3499 |
|
Return TRUE if the specified field name is valid, else FALSE. Valid field names must |
3500 |
|
be hyphenated words subject to certain restrictions. |
3501 |
|
|
3502 |
|
=over 4 |
3503 |
|
|
3504 |
|
=item fieldName |
3505 |
|
|
3506 |
|
Field name to be validated. |
3507 |
|
|
3508 |
|
=item RETURN |
3509 |
|
|
3510 |
|
Returns TRUE if the field name is valid, else FALSE. |
3511 |
|
|
3512 |
|
=back |
3513 |
|
|
3514 |
|
=cut |
3515 |
|
|
3516 |
|
sub ValidateFieldName { |
3517 |
|
# Get the parameters. |
3518 |
|
my ($fieldName) = @_; |
3519 |
|
# Declare the return variable. The field name is valid until we hear |
3520 |
|
# differently. |
3521 |
|
my $retVal = 1; |
3522 |
|
# Compute the maximum name length. |
3523 |
|
my $maxLen = $TypeTable{'name-string'}->{maxLen}; |
3524 |
|
# Look for bad stuff in the name. |
3525 |
|
if ($fieldName =~ /--/) { |
3526 |
|
# Here we have a doubled minus sign. |
3527 |
|
Trace("Field name $fieldName has a doubled hyphen.") if T(1); |
3528 |
|
$retVal = 0; |
3529 |
|
} elsif ($fieldName !~ /^[A-Za-z]/) { |
3530 |
|
# Here the field name is missing the initial letter. |
3531 |
|
Trace("Field name $fieldName does not begin with a letter.") if T(1); |
3532 |
|
$retVal = 0; |
3533 |
|
} elsif (length($fieldName) > $maxLen) { |
3534 |
|
# Here the field name is too long. |
3535 |
|
Trace("Maximum field name length is $maxLen. Field name must be truncated to " . substr($fieldName,0, $maxLen) . "."); |
3536 |
|
} else { |
3537 |
|
# Strip out the minus signs. Everything remaining must be a letter, |
3538 |
|
# underscore, or digit. |
3539 |
|
my $strippedName = $fieldName; |
3540 |
|
$strippedName =~ s/-//g; |
3541 |
|
if ($strippedName !~ /^(\w|\d)+$/) { |
3542 |
|
Trace("Field name $fieldName contains illegal characters.") if T(1); |
3543 |
|
$retVal = 0; |
3544 |
|
} |
3545 |
|
} |
3546 |
|
# Return the result. |
3547 |
|
return $retVal; |
3548 |
|
} |
3549 |
|
|
3550 |
|
=head3 ReadMetaXML |
3551 |
|
|
3552 |
|
my $rawMetaData = ERDB::ReadDBD($fileName); |
3553 |
|
|
3554 |
|
This method reads a raw database definition XML file and returns it. |
3555 |
|
Normally, the metadata used by the ERDB system has been processed and |
3556 |
|
modified to make it easier to load and retrieve the data; however, |
3557 |
|
this method can be used to get the data in its raw form. |
3558 |
|
|
3559 |
|
=over 4 |
3560 |
|
|
3561 |
|
=item fileName |
3562 |
|
|
3563 |
|
Name of the XML file to read. |
3564 |
|
|
3565 |
|
=item RETURN |
3566 |
|
|
3567 |
|
Returns a hash reference containing the raw XML data from the specified file. |
3568 |
|
|
3569 |
|
=back |
3570 |
|
|
3571 |
|
=cut |
3572 |
|
|
3573 |
|
sub ReadMetaXML { |
3574 |
|
# Get the parameters. |
3575 |
|
my ($fileName) = @_; |
3576 |
|
# Read the XML. |
3577 |
|
my $retVal = XML::Simple::XMLin($fileName, %XmlOptions, %XmlInOpts); |
3578 |
|
Trace("XML metadata loaded from file $fileName.") if T(1); |
3579 |
|
# Return the result. |
3580 |
|
return $retVal; |
3581 |
|
} |
3582 |
|
|
3583 |
|
=head3 GetEntityFieldHash |
3584 |
|
|
3585 |
|
my $fieldHashRef = ERDB::GetEntityFieldHash($structure, $entityName); |
3586 |
|
|
3587 |
|
Get the field hash of the named entity in the specified raw XML structure. |
3588 |
|
The field hash may not exist, in which case we need to create it. |
3589 |
|
|
3590 |
|
=over 4 |
3591 |
|
|
3592 |
|
=item structure |
3593 |
|
|
3594 |
|
Raw XML structure defininng the database. This is not the run-time XML used by |
3595 |
|
an ERDB object, since that has all sorts of optimizations built-in. |
3596 |
|
|
3597 |
|
=item entityName |
3598 |
|
|
3599 |
|
Name of the entity whose field structure is desired. |
3600 |
|
|
3601 |
|
=item RETURN |
3602 |
|
|
3603 |
|
Returns the field hash used to define the entity's fields. |
3604 |
|
|
3605 |
|
=back |
3606 |
|
|
3607 |
|
=cut |
3608 |
|
|
3609 |
|
sub GetEntityFieldHash { |
3610 |
|
# Get the parameters. |
3611 |
|
my ($structure, $entityName) = @_; |
3612 |
|
# Get the entity structure. |
3613 |
|
my $entityData = $structure->{Entities}->{$entityName}; |
3614 |
|
# Look for a field structure. |
3615 |
|
my $retVal = $entityData->{Fields}; |
3616 |
|
# If it doesn't exist, create it. |
3617 |
|
if (! defined($retVal)) { |
3618 |
|
$entityData->{Fields} = {}; |
3619 |
|
$retVal = $entityData->{Fields}; |
3620 |
|
} |
3621 |
|
# Return the result. |
3622 |
|
return $retVal; |
3623 |
|
} |
3624 |
|
|
3625 |
|
=head3 WriteMetaXML |
3626 |
|
|
3627 |
|
ERDB::WriteMetaXML($structure, $fileName); |
3628 |
|
|
3629 |
|
Write the metadata XML to a file. This method is the reverse of L</ReadMetaXML>, and is |
3630 |
|
used to update the database definition. It must be used with care, however, since it |
3631 |
|
will only work on a raw structure, not on the processed structure created by an ERDB |
3632 |
|
constructor. |
3633 |
|
|
3634 |
|
=over 4 |
3635 |
|
|
3636 |
|
=item structure |
3637 |
|
|
3638 |
|
XML structure to be written to the file. |
3639 |
|
|
3640 |
|
=item fileName |
3641 |
|
|
3642 |
|
Name of the output file to which the updated XML should be stored. |
3643 |
|
|
3644 |
|
=back |
3645 |
|
|
3646 |
|
=cut |
3647 |
|
|
3648 |
|
sub WriteMetaXML { |
3649 |
|
# Get the parameters. |
3650 |
|
my ($structure, $fileName) = @_; |
3651 |
|
# Compute the output. |
3652 |
|
my $fileString = XML::Simple::XMLout($structure, %XmlOptions, %XmlOutOpts); |
3653 |
|
# Write it to the file. |
3654 |
|
my $xmlOut = Open(undef, ">$fileName"); |
3655 |
|
print $xmlOut $fileString; |
3656 |
|
} |
3657 |
|
|
3658 |
|
|
3659 |
|
=head3 HTMLNote |
3660 |
|
|
3661 |
|
Convert a note or comment to HTML by replacing some bulletin-board codes with HTML. The codes |
3662 |
|
supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph. |
3663 |
|
Except for C<[p]>, all the codes are closed by slash-codes. So, for |
3664 |
|
example, C<[b]Feature[/b]> displays the string C<Feature> in boldface. |
3665 |
|
|
3666 |
|
my $realHtml = ERDB::HTMLNote($dataString); |
3667 |
|
|
3668 |
|
=over 4 |
3669 |
|
|
3670 |
|
=item dataString |
3671 |
|
|
3672 |
|
String to convert to HTML. |
3673 |
|
|
3674 |
|
=item RETURN |
3675 |
|
|
3676 |
|
An HTML string derived from the input string. |
3677 |
|
|
3678 |
|
=back |
3679 |
|
|
3680 |
|
=cut |
3681 |
|
|
3682 |
|
sub HTMLNote { |
3683 |
|
# Get the parameter. |
3684 |
|
my ($dataString) = @_; |
3685 |
|
# HTML-escape the text. |
3686 |
|
my $retVal = CGI::escapeHTML($dataString); |
3687 |
|
# Substitute the bulletin board codes. |
3688 |
|
$retVal =~ s!\[(/?[bi])\]!<$1>!g; |
3689 |
|
$retVal =~ s!\[p\]!</p><p>!g; |
3690 |
|
$retVal =~ s!\[link\s+([^\]]+)\]!<a href="$1">!g; |
3691 |
|
$retVal =~ s!\[/link\]!</a>!g; |
3692 |
|
# Return the result. |
3693 |
|
return $retVal; |
3694 |
|
} |
3695 |
|
|
3696 |
|
=head3 WikiNote |
3697 |
|
|
3698 |
|
Convert a note or comment to Wiki text by replacing some bulletin-board codes with HTML. The codes |
3699 |
|
supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph. |
3700 |
|
Except for C<[p]>, all the codes are closed by slash-codes. So, for |
3701 |
|
example, C<[b]Feature[/b]> displays the string C<Feature> in boldface. |
3702 |
|
|
3703 |
|
my $wikiText = ERDB::WikiNote($dataString); |
3704 |
|
|
3705 |
|
=over 4 |
3706 |
|
|
3707 |
|
=item dataString |
3708 |
|
|
3709 |
|
String to convert to Wiki text. |
3710 |
|
|
3711 |
|
=item RETURN |
3712 |
|
|
3713 |
|
An Wiki text string derived from the input string. |
3714 |
|
|
3715 |
|
=back |
3716 |
|
|
3717 |
|
=cut |
3718 |
|
|
3719 |
|
sub WikiNote { |
3720 |
|
# Get the parameter. |
3721 |
|
my ($dataString) = @_; |
3722 |
|
# HTML-escape the text. |
3723 |
|
my $retVal = CGI::escapeHTML($dataString); |
3724 |
|
# Substitute the bulletin board codes. |
3725 |
|
my $italic = WikiTools::ItalicCode(); |
3726 |
|
$retVal =~ s/\[\/?i\]/$italic/g; |
3727 |
|
my $bold = WikiTools::BoldCode(); |
3728 |
|
$retVal =~ s/\[\/?b\]/$bold/g; |
3729 |
|
# Paragraph breaks are the same no matter which Wiki you're using. |
3730 |
|
$retVal =~ s!\[p\]!\n\n!g; |
3731 |
|
# Now we do the links, which are complicated by the need to know two |
3732 |
|
# things: the target URL and the text. |
3733 |
|
while ($retVal =~ /\[link\s+([^\]]+)\]([^\[]+)\[\/link\]/g) { |
3734 |
|
# Replace the matched string with the Wiki markup for links. Note that |
3735 |
|
# $-[0] is the starting position of the match for the entire expression, |
3736 |
|
# and $+[0] is past the ending position. |
3737 |
|
substr $retVal, $-[0], $+[0] - $-[0], WikiTools::LinkMarkup($1, $2); |
3738 |
|
} |
3739 |
|
# Return the result. |
3740 |
|
return $retVal; |
3741 |
|
} |
3742 |
|
|
3743 |
|
=head3 BeginTran |
3744 |
|
|
3745 |
|
$erdb->BeginTran(); |
3746 |
|
|
3747 |
|
Start a database transaction. |
3748 |
|
|
3749 |
|
=cut |
3750 |
|
|
3751 |
|
sub BeginTran { |
3752 |
|
my ($self) = @_; |
3753 |
|
$self->{_dbh}->begin_tran(); |
3754 |
|
|
3755 |
|
} |
3756 |
|
|
3757 |
|
=head3 CommitTran |
3758 |
|
|
3759 |
|
$erdb->CommitTran(); |
3760 |
|
|
3761 |
|
Commit an active database transaction. |
3762 |
|
|
3763 |
|
=cut |
3764 |
|
|
3765 |
|
sub CommitTran { |
3766 |
|
my ($self) = @_; |
3767 |
|
$self->{_dbh}->commit_tran(); |
3768 |
|
} |
3769 |
|
|
3770 |
|
=head3 RollbackTran |
3771 |
|
|
3772 |
|
$erdb->RollbackTran(); |
3773 |
|
|
3774 |
|
Roll back an active database transaction. |
3775 |
|
|
3776 |
|
=cut |
3777 |
|
|
3778 |
|
sub RollbackTran { |
3779 |
|
my ($self) = @_; |
3780 |
|
$self->{_dbh}->roll_tran(); |
3781 |
} |
} |
3782 |
|
|
3783 |
=head3 GetFieldTable |
=head3 UpdateField |
3784 |
|
|
3785 |
C<< my $fieldHash = $self->GetFieldTable($objectnName); >> |
my $count = $erdb->UpdateField($objectNames, $fieldName, $oldValue, $newValue, $filter, $parms); |
3786 |
|
|
3787 |
Get the field structure for a specified entity or relationship. |
Update all occurrences of a specific field value to a new value. The number of rows changed will be |
3788 |
|
returned. |
3789 |
|
|
3790 |
=over 4 |
=over 4 |
3791 |
|
|
3792 |
=item objectName |
=item fieldName |
3793 |
|
|
3794 |
Name of the desired entity or relationship. |
Name of the field in standard I<objectName>C<(>I<fieldName>C<)> format. |
3795 |
|
|
3796 |
|
=item oldValue |
3797 |
|
|
3798 |
|
Value to be modified. All occurrences of this value in the named field will be replaced by the |
3799 |
|
new value. |
3800 |
|
|
3801 |
|
=item newValue |
3802 |
|
|
3803 |
|
New value to be substituted for the old value when it's found. |
3804 |
|
|
3805 |
|
=item filter |
3806 |
|
|
3807 |
|
A standard ERDB filter clause (see L</Get>). The filter will be applied before any substitutions take place. |
3808 |
|
|
3809 |
|
=item parms |
3810 |
|
|
3811 |
|
Reference to a list of parameter values in the filter. |
3812 |
|
|
3813 |
=item RETURN |
=item RETURN |
3814 |
|
|
3815 |
The table containing the field descriptors for the specified object. |
Returns the number of rows modified. |
3816 |
|
|
3817 |
=back |
=back |
3818 |
|
|
3819 |
=cut |
=cut |
3820 |
|
|
3821 |
sub GetFieldTable { |
sub UpdateField { |
3822 |
# Get the parameters. |
# Get the parameters. |
3823 |
my ($self, $objectName) = @_; |
my ($self, $fieldName, $oldValue, $newValue, $filter, $parms) = @_; |
3824 |
# Get the descriptor from the metadata. |
# Get the object and field names from the field name parameter. |
3825 |
my $objectData = $self->_GetStructure($objectName); |
$fieldName =~ /^([^(]+)\(([^)]+)\)/; |
3826 |
# Return the object's field table. |
my $objectName = $1; |
3827 |
return $objectData->{Fields}; |
my $realFieldName = _FixName($2); |
3828 |
|
# Add the old value to the filter. Note we allow the possibility that no |
3829 |
|
# filter was specified. |
3830 |
|
my $realFilter = "$fieldName = ?"; |
3831 |
|
if ($filter) { |
3832 |
|
$realFilter .= " AND $filter"; |
3833 |
} |
} |
3834 |
|
# Format the query filter. |
3835 |
|
my ($suffix, $mappedNameListRef, $mappedNameHashRef) = |
3836 |
|
$self->_SetupSQL([$objectName], $realFilter); |
3837 |
|
# Create the query. Since there is only one object name, the mapped-name data is not |
3838 |
|
# necessary. Neither is the FROM clause. |
3839 |
|
$suffix =~ s/^FROM.+WHERE\s+//; |
3840 |
|
# Create the update statement. |
3841 |
|
my $command = "UPDATE $objectName SET $realFieldName = ? WHERE $suffix"; |
3842 |
|
# Get the database handle. |
3843 |
|
my $dbh = $self->{_dbh}; |
3844 |
|
# Add the old and new values to the parameter list. Note we allow the possibility that |
3845 |
|
# there are no user-supplied parameters. |
3846 |
|
my @params = ($newValue, $oldValue); |
3847 |
|
if (defined $parms) { |
3848 |
|
push @params, @{$parms}; |
3849 |
|
} |
3850 |
|
# Execute the update. |
3851 |
|
my $retVal = $dbh->SQL($command, 0, @params); |
3852 |
|
# Make the funky zero a real zero. |
3853 |
|
if ($retVal == 0) { |
3854 |
|
$retVal = 0; |
3855 |
|
} |
3856 |
|
# Return the result. |
3857 |
|
return $retVal; |
3858 |
|
} |
3859 |
|
|
3860 |
|
|
3861 |
=head2 Data Mining Methods |
=head2 Data Mining Methods |
3862 |
|
|
3863 |
=head3 GetUsefulCrossValues |
=head3 GetUsefulCrossValues |
3864 |
|
|
3865 |
C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >> |
my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); |
3866 |
|
|
3867 |
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 |
3868 |
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 |
3923 |
|
|
3924 |
=head3 FindColumn |
=head3 FindColumn |
3925 |
|
|
3926 |
C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >> |
my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); |
3927 |
|
|
3928 |
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 |
3929 |
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 |
3981 |
|
|
3982 |
=head3 ParseColumns |
=head3 ParseColumns |
3983 |
|
|
3984 |
C<< my @columns = ERDB->ParseColumns($line); >> |
my @columns = ERDB::ParseColumns($line); |
3985 |
|
|
3986 |
Convert the specified data line to a list of columns. |
Convert the specified data line to a list of columns. |
3987 |
|
|
4002 |
|
|
4003 |
sub ParseColumns { |
sub ParseColumns { |
4004 |
# Get the parameters. |
# Get the parameters. |
4005 |
my ($self, $line) = @_; |
my ($line) = @_; |
4006 |
# Chop off the line-end. |
# Chop off the line-end. |
4007 |
chomp $line; |
chomp $line; |
4008 |
# Split it into a list. |
# Split it into a list. |
4011 |
return @retVal; |
return @retVal; |
4012 |
} |
} |
4013 |
|
|
4014 |
|
=head2 Virtual Methods |
4015 |
|
|
4016 |
|
=head3 _CreatePPOIndex |
4017 |
|
|
4018 |
|
my $index = ERDB::_CreatePPOIndex($indexObject); |
4019 |
|
|
4020 |
|
Convert the XML for an ERDB index to the XML structure for a PPO |
4021 |
|
index. |
4022 |
|
|
4023 |
|
=over 4 |
4024 |
|
|
4025 |
|
=item indexObject |
4026 |
|
|
4027 |
|
ERDB XML structure for an index. |
4028 |
|
|
4029 |
|
=item RETURN |
4030 |
|
|
4031 |
|
PPO XML structure for the same index. |
4032 |
|
|
4033 |
|
=back |
4034 |
|
|
4035 |
|
=cut |
4036 |
|
|
4037 |
|
sub _CreatePPOIndex { |
4038 |
|
# Get the parameters. |
4039 |
|
my ($indexObject) = @_; |
4040 |
|
# The incoming index contains a list of the index fields in the IndexFields |
4041 |
|
# member. We loop through it to create the index tags. |
4042 |
|
my @fields = map { { label => _FixName($_->{name}) } } @{$indexObject->{IndexFields}}; |
4043 |
|
# Wrap the fields in attribute tags. |
4044 |
|
my $retVal = { attribute => \@fields }; |
4045 |
|
# Return the result. |
4046 |
|
return $retVal; |
4047 |
|
} |
4048 |
|
|
4049 |
|
=head3 _CreatePPOField |
4050 |
|
|
4051 |
|
my $fieldXML = ERDB::_CreatePPOField($fieldName, $fieldObject); |
4052 |
|
|
4053 |
|
Convert the ERDB XML structure for a field to a PPO scalar XML structure. |
4054 |
|
|
4055 |
|
=over 4 |
4056 |
|
|
4057 |
|
=item fieldName |
4058 |
|
|
4059 |
|
Name of the scalar field. |
4060 |
|
|
4061 |
|
=item fieldObject |
4062 |
|
|
4063 |
|
ERDB XML structure describing the field. |
4064 |
|
|
4065 |
|
=item RETURN |
4066 |
|
|
4067 |
|
Returns a PPO XML structure for the same field. |
4068 |
|
|
4069 |
|
=back |
4070 |
|
|
4071 |
|
=cut |
4072 |
|
|
4073 |
|
sub _CreatePPOField { |
4074 |
|
# Get the parameters. |
4075 |
|
my ($fieldName, $fieldObject) = @_; |
4076 |
|
# Get the field type. |
4077 |
|
my $type = $TypeTable{$fieldObject->{type}}->{sqlType}; |
4078 |
|
# Fix up the field name. |
4079 |
|
$fieldName = _FixName($fieldName); |
4080 |
|
# Build the scalar tag. |
4081 |
|
my $retVal = { label => $fieldName, type => $type }; |
4082 |
|
# Return the result. |
4083 |
|
return $retVal; |
4084 |
|
} |
4085 |
|
|
4086 |
|
=head3 CleanKeywords |
4087 |
|
|
4088 |
|
my $cleanedString = $erdb->CleanKeywords($searchExpression); |
4089 |
|
|
4090 |
|
Clean up a search expression or keyword list. This is a virtual method that may |
4091 |
|
be overridden by the subclass. The base-class method removes extra spaces |
4092 |
|
and converts everything to lower case. |
4093 |
|
|
4094 |
|
=over 4 |
4095 |
|
|
4096 |
|
=item searchExpression |
4097 |
|
|
4098 |
|
Search expression or keyword list to clean. Note that a search expression may |
4099 |
|
contain boolean operators which need to be preserved. This includes leading |
4100 |
|
minus signs. |
4101 |
|
|
4102 |
|
=item RETURN |
4103 |
|
|
4104 |
|
Cleaned expression or keyword list. |
4105 |
|
|
4106 |
|
=back |
4107 |
|
|
4108 |
|
=cut |
4109 |
|
|
4110 |
|
sub CleanKeywords { |
4111 |
|
# Get the parameters. |
4112 |
|
my ($self, $searchExpression) = @_; |
4113 |
|
# Lower-case the expression and copy it into the return variable. Note that we insure we |
4114 |
|
# don't accidentally end up with an undefined value. |
4115 |
|
my $retVal = lc($searchExpression || ""); |
4116 |
|
# Remove extra spaces. |
4117 |
|
$retVal =~ s/\s+/ /g; |
4118 |
|
$retVal =~ s/(^\s+)|(\s+$)//g; |
4119 |
|
# Return the result. |
4120 |
|
return $retVal; |
4121 |
|
} |
4122 |
|
|
4123 |
|
=head3 GetSourceObject |
4124 |
|
|
4125 |
|
my $source = $erdb->GetSourceObject($entityName); |
4126 |
|
|
4127 |
|
Return the object to be used in loading special attributes of the specified entity. The |
4128 |
|
algorithm for loading special attributes is stored in the C<DataGen> elements of the |
4129 |
|
XML |
4130 |
|
|
4131 |
=head2 Internal Utility Methods |
=head2 Internal Utility Methods |
4132 |
|
|
4133 |
=head3 SetupSQL |
=head3 _RelationMap |
4134 |
|
|
4135 |
|
my @relationMap = _RelationMap($mappedNameHashRef, $mappedNameListRef); |
4136 |
|
|
4137 |
|
Create the relation map for an SQL query. The relation map is used by B<ERDBObject> |
4138 |
|
to determine how to interpret the results of the query. |
4139 |
|
|
4140 |
|
=over 4 |
4141 |
|
|
4142 |
|
=item mappedNameHashRef |
4143 |
|
|
4144 |
|
Reference to a hash that maps modified object names to real object names. |
4145 |
|
|
4146 |
|
=item mappedNameListRef |
4147 |
|
|
4148 |
|
Reference to a list of modified object names in the order they appear in the |
4149 |
|
SELECT list. |
4150 |
|
|
4151 |
|
=item RETURN |
4152 |
|
|
4153 |
|
Returns a list of 2-tuples. Each tuple consists of an object name as used in the |
4154 |
|
query followed by the actual name of that object. This enables the B<ERDBObject> to |
4155 |
|
determine the order of the tables in the query and which object name belongs to each |
4156 |
|
mapped object name. Most of the time these two values are the same; however, if a |
4157 |
|
relation occurs twice in the query, the relation name in the field list and WHERE |
4158 |
|
clause will use a mapped name (generally the actual relation name with a numeric |
4159 |
|
suffix) that does not match the actual relation name. |
4160 |
|
|
4161 |
|
=back |
4162 |
|
|
4163 |
|
=cut |
4164 |
|
|
4165 |
|
sub _RelationMap { |
4166 |
|
# Get the parameters. |
4167 |
|
my ($mappedNameHashRef, $mappedNameListRef) = @_; |
4168 |
|
# Declare the return variable. |
4169 |
|
my @retVal = (); |
4170 |
|
# Build the map. |
4171 |
|
for my $mappedName (@{$mappedNameListRef}) { |
4172 |
|
push @retVal, [$mappedName, $mappedNameHashRef->{$mappedName}]; |
4173 |
|
} |
4174 |
|
# Return it. |
4175 |
|
return @retVal; |
4176 |
|
} |
4177 |
|
|
4178 |
|
|
4179 |
|
=head3 _SetupSQL |
4180 |
|
|
4181 |
Process a list of object names and a filter clause so that they can be used to |
Process a list of object names and a filter clause so that they can be used to |
4182 |
build an SQL statement. This method takes in a reference to a list of object names |
build an SQL statement. This method takes in a reference to a list of object names |
4196 |
A string containing the WHERE clause for the query (without the C<WHERE>) and also |
A string containing the WHERE clause for the query (without the C<WHERE>) and also |
4197 |
optionally the C<ORDER BY> and C<LIMIT> clauses. |
optionally the C<ORDER BY> and C<LIMIT> clauses. |
4198 |
|
|
4199 |
|
=item matchClause |
4200 |
|
|
4201 |
|
An optional full-text search clause. If specified, it will be inserted at the |
4202 |
|
front of the WHERE clause. It should already be SQL-formatted; that is, the |
4203 |
|
field names should be in the form I<table>C<.>I<fieldName>. |
4204 |
|
|
4205 |
=item RETURN |
=item RETURN |
4206 |
|
|
4207 |
Returns a three-element list. The first element is the SQL statement suffix, beginning |
Returns a three-element list. The first element is the SQL statement suffix, beginning |
4214 |
=cut |
=cut |
4215 |
|
|
4216 |
sub _SetupSQL { |
sub _SetupSQL { |
4217 |
my ($self, $objectNames, $filterClause) = @_; |
my ($self, $objectNames, $filterClause, $matchClause) = @_; |
4218 |
# Adjust the list of object names to account for multiple occurrences of the |
# Adjust the list of object names to account for multiple occurrences of the |
4219 |
# same object. We start with a hash table keyed on object name that will |
# same object. We start with a hash table keyed on object name that will |
4220 |
# return the object suffix. The first time an object is encountered it will |
# return the object suffix. The first time an object is encountered it will |
4263 |
# FROM name1, name2, ... nameN |
# FROM name1, name2, ... nameN |
4264 |
# |
# |
4265 |
my $suffix = "FROM " . join(', ', @fromList); |
my $suffix = "FROM " . join(', ', @fromList); |
4266 |
|
# Now for the WHERE. First, we need a place for the filter string. |
4267 |
|
my $filterString = ""; |
4268 |
|
# We will also keep a list of conditions to add to the WHERE clause in order to link |
4269 |
|
# entities and relationships as well as primary relations to secondary ones. |
4270 |
|
my @joinWhere = (); |
4271 |
# Check for a filter clause. |
# Check for a filter clause. |
4272 |
if ($filterClause) { |
if ($filterClause) { |
4273 |
# Here we have one, so we convert its field names and add it to the query. First, |
# Here we have one, so we convert its field names and add it to the query. First, |
4274 |
# We create a copy of the filter string we can work with. |
# We create a copy of the filter string we can work with. |
4275 |
my $filterString = $filterClause; |
$filterString = $filterClause; |
4276 |
# Next, we sort the object names by length. This helps protect us from finding |
# Next, we sort the object names by length. This helps protect us from finding |
4277 |
# object names inside other object names when we're doing our search and replace. |
# object names inside other object names when we're doing our search and replace. |
4278 |
my @sortedNames = sort { length($b) - length($a) } @mappedNameList; |
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 = (); |
|
4279 |
# The final preparatory step is to create a hash table of relation names. The |
# The final preparatory step is to create a hash table of relation names. The |
4280 |
# table begins with the relation names already in the SELECT command. We may |
# table begins with the relation names already in the SELECT command. We may |
4281 |
# need to add relations later if there is filtering on a field in a secondary |
# need to add relations later if there is filtering on a field in a secondary |
4343 |
} |
} |
4344 |
} |
} |
4345 |
} |
} |
4346 |
|
} |
4347 |
# The next step is to join the objects together. We only need to do this if there |
# The next step is to join the objects together. We only need to do this if there |
4348 |
# is more than one object in the object list. We start with the first object and |
# is more than one object in the object list. We start with the first object and |
4349 |
# run through the objects after it. Note also that we make a safety copy of the |
# run through the objects after it. Note also that we make a safety copy of the |
4350 |
# list before running through it. |
# list before running through it, because we shift off the first object before |
4351 |
|
# processing the rest. |
4352 |
my @mappedObjectList = @mappedNameList; |
my @mappedObjectList = @mappedNameList; |
4353 |
my $lastMappedObject = shift @mappedObjectList; |
my $lastMappedObject = shift @mappedObjectList; |
4354 |
# Get the join table. |
# Get the join table. |
4377 |
# here is we want the filter clause to be empty if there's no WHERE filter. |
# here is we want the filter clause to be empty if there's no WHERE filter. |
4378 |
# We'll put the ORDER BY / LIMIT clauses in the following variable. |
# We'll put the ORDER BY / LIMIT clauses in the following variable. |
4379 |
my $orderClause = ""; |
my $orderClause = ""; |
4380 |
|
# This is only necessary if we have a filter string in which the ORDER BY |
4381 |
|
# and LIMIT clauses can live. |
4382 |
|
if ($filterString) { |
4383 |
# Locate the ORDER BY or LIMIT verbs (if any). We use a non-greedy |
# Locate the ORDER BY or LIMIT verbs (if any). We use a non-greedy |
4384 |
# operator so that we find the first occurrence of either verb. |
# operator so that we find the first occurrence of either verb. |
4385 |
if ($filterString =~ m/^(.*?)\s*(ORDER BY|LIMIT)/g) { |
if ($filterString =~ m/^(.*?)\s*(ORDER BY|LIMIT)/g) { |
4388 |
$orderClause = $2 . substr($filterString, $pos); |
$orderClause = $2 . substr($filterString, $pos); |
4389 |
$filterString = $1; |
$filterString = $1; |
4390 |
} |
} |
4391 |
# Add the filter and the join clauses (if any) to the SELECT command. |
} |
4392 |
|
# All the things that are supposed to be in the WHERE clause of the |
4393 |
|
# SELECT command need to be put into @joinWhere so we can string them |
4394 |
|
# together. We begin with the match clause. This is important, |
4395 |
|
# because the match clause's parameter mark must precede any parameter |
4396 |
|
# marks in the filter string. |
4397 |
|
if ($matchClause) { |
4398 |
|
push @joinWhere, $matchClause; |
4399 |
|
} |
4400 |
|
# Add the filter string. We put it in parentheses to avoid operator |
4401 |
|
# precedence problems with the match clause or the joins. |
4402 |
if ($filterString) { |
if ($filterString) { |
4403 |
Trace("Filter string is \"$filterString\".") if T(4); |
Trace("Filter string is \"$filterString\".") if T(4); |
4404 |
push @joinWhere, "($filterString)"; |
push @joinWhere, "($filterString)"; |
4405 |
} |
} |
4406 |
|
# String it all together into a big filter clause. |
4407 |
if (@joinWhere) { |
if (@joinWhere) { |
4408 |
$suffix .= " WHERE " . join(' AND ', @joinWhere); |
$suffix .= " WHERE " . join(' AND ', @joinWhere); |
4409 |
} |
} |
4410 |
# Add the sort or limit clause (if any) to the SELECT command. |
# Add the sort or limit clause (if any). |
4411 |
if ($orderClause) { |
if ($orderClause) { |
4412 |
$suffix .= " $orderClause"; |
$suffix .= " $orderClause"; |
4413 |
} |
} |
|
} |
|
4414 |
# Return the suffix, the mapped name list, and the mapped name hash. |
# Return the suffix, the mapped name list, and the mapped name hash. |
4415 |
return ($suffix, \@mappedNameList, \%mappedNameHash); |
return ($suffix, \@mappedNameList, \%mappedNameHash); |
4416 |
} |
} |
4417 |
|
|
4418 |
=head3 GetStatementHandle |
=head3 _GetStatementHandle |
4419 |
|
|
4420 |
This method will prepare and execute an SQL query, returning the statement handle. |
This method will prepare and execute an SQL query, returning the statement handle. |
4421 |
The main reason for doing this here is so that everybody who does SQL queries gets |
The main reason for doing this here is so that everybody who does SQL queries gets |
4445 |
sub _GetStatementHandle { |
sub _GetStatementHandle { |
4446 |
# Get the parameters. |
# Get the parameters. |
4447 |
my ($self, $command, $params) = @_; |
my ($self, $command, $params) = @_; |
4448 |
|
Confess("Invalid parameter list.") if (! defined($params) || ref($params) ne 'ARRAY'); |
4449 |
# Trace the query. |
# Trace the query. |
4450 |
Trace("SQL query: $command") if T(SQL => 3); |
Trace("SQL query: $command") if T(SQL => 3); |
4451 |
Trace("PARMS: '" . (join "', '", @{$params}) . "'") if (T(SQL => 4) && (@{$params} > 0)); |
Trace("PARMS: '" . (join "', '", @{$params}) . "'") if (T(SQL => 4) && (@{$params} > 0)); |
4454 |
# Prepare the command. |
# Prepare the command. |
4455 |
my $sth = $dbh->prepare_command($command); |
my $sth = $dbh->prepare_command($command); |
4456 |
# Execute it with the parameters bound in. |
# Execute it with the parameters bound in. |
4457 |
$sth->execute(@{$params}) || Confess("SELECT error" . $sth->errstr()); |
$sth->execute(@{$params}) || Confess("SELECT error: " . $sth->errstr()); |
4458 |
# Return the statement handle. |
# Return the statement handle. |
4459 |
return $sth; |
return $sth; |
4460 |
} |
} |
4461 |
|
|
4462 |
=head3 GetLoadStats |
=head3 _GetLoadStats |
4463 |
|
|
4464 |
Return a blank statistics object for use by the load methods. |
Return a blank statistics object for use by the load methods. |
4465 |
|
|
4471 |
return Stats->new(); |
return Stats->new(); |
4472 |
} |
} |
4473 |
|
|
4474 |
=head3 GenerateFields |
=head3 _DumpRelation |
|
|
|
|
Generate field values from a field structure and store in a specified table. The field names |
|
|
are first sorted by pass count, certain pre-defined fields are removed from the list, and |
|
|
then we rip through them evaluation the data generation string. Fields in the primary relation |
|
|
are stored as scalars; fields in secondary relations are stored as value lists. |
|
|
|
|
|
This is a static method. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item this |
|
|
|
|
|
Hash table into which the field values should be placed. |
|
|
|
|
|
=item fields |
|
|
|
|
|
Field structure from which the field descriptors should be taken. |
|
|
|
|
|
=item type |
|
|
|
|
|
Type name of the object whose fields are being generated. |
|
|
|
|
|
=item values (optional) |
|
|
|
|
|
Reference to a value structure from which additional values can be taken. |
|
|
|
|
|
=item from (optiona) |
|
|
|
|
|
Reference to the source entity instance if relationship data is being generated. |
|
|
|
|
|
=item to (optional) |
|
|
|
|
|
Reference to the target entity instance if relationship data is being generated. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub _GenerateFields { |
|
|
# Get the parameters. |
|
|
my ($this, $fields, $type, $values, $from, $to) = @_; |
|
|
# Sort the field names by pass number. |
|
|
my @fieldNames = sort { $fields->{$a}->{DataGen}->{pass} <=> $fields->{$b}->{DataGen}->{pass} } keys %{$fields}; |
|
|
# Loop through the field names, generating data. |
|
|
for my $name (@fieldNames) { |
|
|
# Only proceed if this field needs to be generated. |
|
|
if (!exists $this->{$name}) { |
|
|
# Get this field's data generation descriptor. |
|
|
my $fieldDescriptor = $fields->{$name}; |
|
|
my $data = $fieldDescriptor->{DataGen}; |
|
|
# Get the code to generate the field value. |
|
|
my $codeString = $data->{content}; |
|
|
# Determine whether or not this field is in the primary relation. |
|
|
if ($fieldDescriptor->{relation} eq $type) { |
|
|
# Here we have a primary relation field. Store the field value as |
|
|
# a scalar. |
|
|
$this->{$name} = eval($codeString); |
|
|
} else { |
|
|
# Here we have a secondary relation field. Create a null list |
|
|
# and push the desired number of field values onto it. |
|
|
my @fieldValues = (); |
|
|
my $count = IntGen(0,$data->{testCount}); |
|
|
for (my $i = 0; $i < $count; $i++) { |
|
|
my $newValue = eval($codeString); |
|
|
push @fieldValues, $newValue; |
|
|
} |
|
|
# Store the value list in the main hash. |
|
|
$this->{$name} = \@fieldValues; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
=head3 DumpRelation |
|
4475 |
|
|
4476 |
Dump the specified relation's to the specified output file in tab-delimited format. |
Dump the specified relation to the specified output file in tab-delimited format. |
4477 |
|
|
4478 |
This is an instance method. |
This is an instance method. |
4479 |
|
|
4521 |
close DTXOUT; |
close DTXOUT; |
4522 |
} |
} |
4523 |
|
|
4524 |
=head3 GetStructure |
=head3 _GetStructure |
4525 |
|
|
4526 |
Get the data structure for a specified entity or relationship. |
Get the data structure for a specified entity or relationship. |
4527 |
|
|
4560 |
return $retVal; |
return $retVal; |
4561 |
} |
} |
4562 |
|
|
4563 |
=head3 GetRelationTable |
|
4564 |
|
|
4565 |
|
=head3 _GetRelationTable |
4566 |
|
|
4567 |
Get the list of relations for a specified entity or relationship. |
Get the list of relations for a specified entity or relationship. |
4568 |
|
|
4591 |
return $objectData->{Relations}; |
return $objectData->{Relations}; |
4592 |
} |
} |
4593 |
|
|
4594 |
=head3 ValidateFieldNames |
=head3 _ValidateFieldNames |
4595 |
|
|
4596 |
Determine whether or not the field names are valid. A description of the problems with the names |
Determine whether or not the field names are valid. A description of the problems with the names |
4597 |
will be written to the standard error output. If there is an error, this method will abort. This is |
will be written to the standard error output. If there is an error, this method will abort. This is |
4618 |
for my $object (values %{$metadata->{$section}}) { |
for my $object (values %{$metadata->{$section}}) { |
4619 |
# Loop through the object's fields. |
# Loop through the object's fields. |
4620 |
for my $fieldName (keys %{$object->{Fields}}) { |
for my $fieldName (keys %{$object->{Fields}}) { |
4621 |
# Now we make some initial validations. |
# If this field name is invalid, set the return value to zero |
4622 |
if ($fieldName =~ /--/) { |
# so we know we encountered an error. |
4623 |
# Here we have a doubled minus sign. |
if (! ValidateFieldName($fieldName)) { |
|
print STDERR "Field name $fieldName has a doubled hyphen.\n"; |
|
|
$retVal = 0; |
|
|
} elsif ($fieldName !~ /^[A-Za-z]/) { |
|
|
# Here the field name is missing the initial letter. |
|
|
print STDERR "Field name $fieldName does not begin with a letter.\n"; |
|
|
$retVal = 0; |
|
|
} else { |
|
|
# Strip out the minus signs. Everything remaining must be a letter |
|
|
# or digit. |
|
|
my $strippedName = $fieldName; |
|
|
$strippedName =~ s/-//g; |
|
|
if ($strippedName !~ /^[A-Za-z0-9]+$/) { |
|
|
print STDERR "Field name $fieldName contains illegal characters.\n"; |
|
4624 |
$retVal = 0; |
$retVal = 0; |
4625 |
} |
} |
4626 |
} |
} |
4627 |
} |
} |
4628 |
} |
} |
|
} |
|
4629 |
# If an error was found, fail. |
# If an error was found, fail. |
4630 |
if ($retVal == 0) { |
if ($retVal == 0) { |
4631 |
Confess("Errors found in field names."); |
Confess("Errors found in field names."); |
4632 |
} |
} |
4633 |
} |
} |
4634 |
|
|
4635 |
=head3 LoadRelation |
=head3 _LoadRelation |
4636 |
|
|
4637 |
Load a relation from the data in a tab-delimited disk file. The load will only take place if a disk |
Load a relation from the data in a tab-delimited disk file. The load will only take place if a disk |
4638 |
file with the same name as the relation exists in the specified directory. |
file with the same name as the relation exists in the specified directory. |
4683 |
# be a null string. |
# be a null string. |
4684 |
if ($fileName ne "") { |
if ($fileName ne "") { |
4685 |
# Load the relation from the file. |
# Load the relation from the file. |
4686 |
$retVal = $self->LoadTable($fileName, $relationName, $rebuild); |
$retVal = $self->LoadTable($fileName, $relationName, truncate => $rebuild); |
4687 |
} elsif ($rebuild) { |
} elsif ($rebuild) { |
4688 |
# 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. |
4689 |
$self->CreateTable($relationName, 1); |
$self->CreateTable($relationName, 1); |
4692 |
return $retVal; |
return $retVal; |
4693 |
} |
} |
4694 |
|
|
4695 |
=head3 LoadMetaData |
|
4696 |
|
=head3 _LoadMetaData |
4697 |
|
|
4698 |
|
my $metadata = ERDB::_LoadMetaData($filename); |
4699 |
|
|
4700 |
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. |
4701 |
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 |
4720 |
sub _LoadMetaData { |
sub _LoadMetaData { |
4721 |
# Get the parameters. |
# Get the parameters. |
4722 |
my ($filename) = @_; |
my ($filename) = @_; |
4723 |
Trace("Reading Sprout DBD from $filename.") if T(2); |
Trace("Reading DBD from $filename.") if T(2); |
4724 |
# Slurp the XML file into a variable. Extensive use of options is used to insure we |
# Slurp the XML file into a variable. Extensive use of options is used to insure we |
4725 |
# get the exact structure we want. |
# get the exact structure we want. |
4726 |
my $metadata = XML::Simple::XMLin($filename, |
my $metadata = ReadMetaXML($filename); |
|
GroupTags => { Relationships => 'Relationship', |
|
|
Entities => 'Entity', |
|
|
Fields => 'Field', |
|
|
Indexes => 'Index', |
|
|
IndexFields => 'IndexField'}, |
|
|
KeyAttr => { Relationship => 'name', |
|
|
Entity => 'name', |
|
|
Field => 'name'}, |
|
|
ForceArray => ['Field', 'Index', 'IndexField'], |
|
|
ForceContent => 1, |
|
|
NormalizeSpace => 2 |
|
|
); |
|
|
Trace("XML metadata loaded from file $filename.") if T(1); |
|
4727 |
# Before we go any farther, we need to validate the field and object names. If an error is found, |
# Before we go any farther, we need to validate the field and object names. If an error is found, |
4728 |
# the method below will fail. |
# the method below will fail. |
4729 |
_ValidateFieldNames($metadata); |
_ValidateFieldNames($metadata); |
4846 |
if ($found == 0) { |
if ($found == 0) { |
4847 |
push @{$indexList}, { IndexFields => [ {name => 'id', order => 'ascending'} ] }; |
push @{$indexList}, { IndexFields => [ {name => 'id', order => 'ascending'} ] }; |
4848 |
} |
} |
4849 |
# Now we need to convert the relation's index list to an index table. We begin by creating |
# Attach all the indexes to the relation. |
4850 |
# an empty table in the relation structure. |
_ProcessIndexes($indexList, $relation); |
|
$relation->{Indexes} = { }; |
|
|
# Loop through the indexes. |
|
|
my $count = 0; |
|
|
for my $index (@{$indexList}) { |
|
|
# Add this index to the index table. |
|
|
_AddIndex("idx$relationName$count", $relation, $index); |
|
|
# Increment the counter so that the next index has a different name. |
|
|
$count++; |
|
|
} |
|
4851 |
} |
} |
4852 |
# Finally, we add the relation structure to the entity. |
# Finally, we add the relation structure to the entity. |
4853 |
$entityStructure->{Relations} = $relationTable; |
$entityStructure->{Relations} = $relationTable; |
4861 |
_FixupFields($relationshipStructure, $relationshipName, 2, 3); |
_FixupFields($relationshipStructure, $relationshipName, 2, 3); |
4862 |
# Format a description for the FROM field. |
# Format a description for the FROM field. |
4863 |
my $fromEntity = $relationshipStructure->{from}; |
my $fromEntity = $relationshipStructure->{from}; |
4864 |
my $fromComment = "<b>id</b> of the source <b><a href=\"#$fromEntity\">$fromEntity</a></b>."; |
my $fromComment = "[b]id[/b] of the source [b][link #$fromEntity]$fromEntity\[/link][/b]."; |
4865 |
# Get the FROM entity's key type. |
# Get the FROM entity's key type. |
4866 |
my $fromType = $entityList->{$fromEntity}->{keyType}; |
my $fromType = $entityList->{$fromEntity}->{keyType}; |
4867 |
# Add the FROM field. |
# Add the FROM field. |
4871 |
PrettySort => 1}); |
PrettySort => 1}); |
4872 |
# Format a description for the TO field. |
# Format a description for the TO field. |
4873 |
my $toEntity = $relationshipStructure->{to}; |
my $toEntity = $relationshipStructure->{to}; |
4874 |
my $toComment = "<b>id</b> of the target <b><a href=\"#$toEntity\">$toEntity</a></b>."; |
my $toComment = "[b]id[/b] of the target [b][link #$toEntity]$toEntity\[/link][/b]."; |
4875 |
# Get the TO entity's key type. |
# Get the TO entity's key type. |
4876 |
my $toType = $entityList->{$toEntity}->{keyType}; |
my $toType = $entityList->{$toEntity}->{keyType}; |
4877 |
# Add the TO field. |
# Add the TO field. |
4883 |
my $thisRelation = { Fields => _ReOrderRelationTable($relationshipStructure->{Fields}), |
my $thisRelation = { Fields => _ReOrderRelationTable($relationshipStructure->{Fields}), |
4884 |
Indexes => { } }; |
Indexes => { } }; |
4885 |
$relationshipStructure->{Relations} = { $relationshipName => $thisRelation }; |
$relationshipStructure->{Relations} = { $relationshipName => $thisRelation }; |
4886 |
|
|
4887 |
|
# Add the alternate indexes (if any). This MUST be done before the FROM and |
4888 |
|
# TO indexes, because it erases the relation's index list. |
4889 |
|
if (exists $relationshipStructure->{Indexes}) { |
4890 |
|
_ProcessIndexes($relationshipStructure->{Indexes}, $thisRelation); |
4891 |
|
} |
4892 |
|
# Add the relation to the master table. |
4893 |
# Create the FROM and TO indexes. |
# Create the FROM and TO indexes. |
4894 |
_CreateRelationshipIndex("From", $relationshipName, $relationshipStructure); |
_CreateRelationshipIndex("From", $relationshipName, $relationshipStructure); |
4895 |
_CreateRelationshipIndex("To", $relationshipName, $relationshipStructure); |
_CreateRelationshipIndex("To", $relationshipName, $relationshipStructure); |
|
# Add the relation to the master table. |
|
4896 |
$masterRelationTable{$relationshipName} = $thisRelation; |
$masterRelationTable{$relationshipName} = $thisRelation; |
4897 |
} |
} |
4898 |
# Now store the master relation table in the metadata structure. |
# Now store the master relation table in the metadata structure. |
5006 |
return $metadata; |
return $metadata; |
5007 |
} |
} |
5008 |
|
|
5009 |
=head3 SortNeeded |
=head3 _CreateRelationshipIndex |
|
|
|
|
C<< my $flag = $erdb->SortNeeded($relationName); >> |
|
|
|
|
|
Return TRUE if the specified relation should be sorted during loading to remove duplicate keys, |
|
|
else FALSE. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item relationName |
|
|
|
|
|
Name of the relation to be examined. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
Returns TRUE if the relation needs a sort, else FALSE. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
#: Return Type $; |
|
|
sub SortNeeded { |
|
|
# Get the parameters. |
|
|
my ($self, $relationName) = @_; |
|
|
# Declare the return variable. |
|
|
my $retVal = 0; |
|
|
# Find out if the relation is a primary entity relation. |
|
|
my $entityTable = $self->{_metaData}->{Entities}; |
|
|
if (exists $entityTable->{$relationName}) { |
|
|
my $keyType = $entityTable->{$relationName}->{keyType}; |
|
|
Trace("Relation $relationName found in entity table with key type $keyType.") if T(3); |
|
|
# If the key is not a hash string, we must do the sort. |
|
|
if ($keyType ne 'hash-string') { |
|
|
$retVal = 1; |
|
|
} |
|
|
} |
|
|
# Return the result. |
|
|
return $retVal; |
|
|
} |
|
|
|
|
|
=head3 CreateRelationshipIndex |
|
5010 |
|
|
5011 |
Create an index for a relationship's relation. |
Create an index for a relationship's relation. |
5012 |
|
|
5047 |
if ($relationshipStructure->{arity} eq "1M" && $indexKey eq "To") { |
if ($relationshipStructure->{arity} eq "1M" && $indexKey eq "To") { |
5048 |
$newIndex->{Unique} = 'true'; |
$newIndex->{Unique} = 'true'; |
5049 |
} |
} |
5050 |
# Add the index to the relation. |
# Add the index to the relation. |
5051 |
_AddIndex("idx$relationshipName$indexKey", $relationStructure, $newIndex); |
_AddIndex("idx$indexKey", $relationStructure, $newIndex); |
5052 |
|
} |
5053 |
|
|
5054 |
|
=head3 _ProcessIndexes |
5055 |
|
|
5056 |
|
ERDB::_ProcessIndexes($indexList, $relation); |
5057 |
|
|
5058 |
|
Build the data structures for the specified indexes in the specified relation. |
5059 |
|
|
5060 |
|
=over 4 |
5061 |
|
|
5062 |
|
=item indexList |
5063 |
|
|
5064 |
|
Reference to a list of indexes. Each index is a hash reference containing an optional |
5065 |
|
C<Notes> value that describes the index and an C<IndexFields> value that is a reference |
5066 |
|
to a list of index field structures. An index field structure, in turn, is a reference |
5067 |
|
to a hash that contains a C<name> attribute for the field name and an C<order> |
5068 |
|
attribute that specifies either C<ascending> or C<descending>. In this sense the |
5069 |
|
index list encapsulates the XML C<Indexes> structure in the database definition. |
5070 |
|
|
5071 |
|
=item relation |
5072 |
|
|
5073 |
|
The structure that describes the current relation. The new index descriptors will |
5074 |
|
be stored in the structure's C<Indexes> member. Any previous data in the structure |
5075 |
|
will be lost. |
5076 |
|
|
5077 |
|
=back |
5078 |
|
|
5079 |
|
=cut |
5080 |
|
|
5081 |
|
sub _ProcessIndexes { |
5082 |
|
# Get the parameters. |
5083 |
|
my ($indexList, $relation) = @_; |
5084 |
|
# Now we need to convert the relation's index list to an index table. We begin by creating |
5085 |
|
# an empty table in the relation structure. |
5086 |
|
$relation->{Indexes} = { }; |
5087 |
|
# Loop through the indexes. |
5088 |
|
my $count = 0; |
5089 |
|
for my $index (@{$indexList}) { |
5090 |
|
# Add this index to the index table. |
5091 |
|
_AddIndex("idx$count", $relation, $index); |
5092 |
|
# Increment the counter so that the next index has a different name. |
5093 |
|
$count++; |
5094 |
|
} |
5095 |
} |
} |
5096 |
|
|
5097 |
=head3 AddIndex |
=head3 _AddIndex |
5098 |
|
|
5099 |
Add an index to a relation structure. |
Add an index to a relation structure. |
5100 |
|
|
5140 |
$relationStructure->{Indexes}->{$indexName} = $newIndex; |
$relationStructure->{Indexes}->{$indexName} = $newIndex; |
5141 |
} |
} |
5142 |
|
|
5143 |
=head3 FixupFields |
=head3 _FixupFields |
5144 |
|
|
5145 |
This method fixes the field list for an entity or relationship. It will add the caller-specified |
This method fixes the field list for an entity or relationship. It will add the caller-specified |
5146 |
relation name to fields that do not have a name and set the C<PrettySort> value as specified. |
relation name to fields that do not have a name and set the C<PrettySort> value as specified. |
5178 |
# Here it doesn't, so we create a new one. |
# Here it doesn't, so we create a new one. |
5179 |
$structure->{Fields} = { }; |
$structure->{Fields} = { }; |
5180 |
} else { |
} else { |
5181 |
# Here we have a field list. Loop through its fields. |
# Here we have a field list. We need to track the searchable fields, so we |
5182 |
|
# create a list for stashing them. |
5183 |
|
my @textFields = (); |
5184 |
|
# Loop through the fields. |
5185 |
my $fieldStructures = $structure->{Fields}; |
my $fieldStructures = $structure->{Fields}; |
5186 |
for my $fieldName (keys %{$fieldStructures}) { |
for my $fieldName (keys %{$fieldStructures}) { |
5187 |
Trace("Processing field $fieldName of $defaultRelationName.") if T(4); |
Trace("Processing field $fieldName of $defaultRelationName.") if T(4); |
5190 |
my $type = $fieldData->{type}; |
my $type = $fieldData->{type}; |
5191 |
# Plug in a relation name if it is needed. |
# Plug in a relation name if it is needed. |
5192 |
Tracer::MergeOptions($fieldData, { relation => $defaultRelationName }); |
Tracer::MergeOptions($fieldData, { relation => $defaultRelationName }); |
5193 |
# Plug in a data generator if we need one. |
# Check for searchability. |
5194 |
if (!exists $fieldData->{DataGen}) { |
if ($fieldData->{searchable}) { |
5195 |
# The data generator will use the default for the field's type. |
# Only allow this for a primary relation. |
5196 |
$fieldData->{DataGen} = { content => $TypeTable{$type}->{dataGen} }; |
if ($fieldData->{relation} ne $defaultRelationName) { |
5197 |
|
Confess("Field $fieldName of $defaultRelationName is in secondary relations and cannot be searchable."); |
5198 |
|
} else { |
5199 |
|
push @textFields, $fieldName; |
5200 |
|
} |
5201 |
} |
} |
|
# Plug in the defaults for the optional data generation parameters. |
|
|
Tracer::MergeOptions($fieldData->{DataGen}, { testCount => 1, pass => 0 }); |
|
5202 |
# Add the PrettySortValue. |
# Add the PrettySortValue. |
5203 |
$fieldData->{PrettySort} = (($type eq "text") ? $textPrettySortValue : $prettySortValue); |
$fieldData->{PrettySort} = (($type eq "text") ? $textPrettySortValue : $prettySortValue); |
5204 |
} |
} |
5205 |
|
# If there are searchable fields, remember the fact. |
5206 |
|
if (@textFields) { |
5207 |
|
$structure->{searchFields} = \@textFields; |
5208 |
|
} |
5209 |
} |
} |
5210 |
} |
} |
5211 |
|
|
5212 |
=head3 FixName |
=head3 _FixName |
5213 |
|
|
5214 |
Fix the incoming field name so that it is a legal SQL column name. |
Fix the incoming field name so that it is a legal SQL column name. |
5215 |
|
|
5238 |
return $fieldName; |
return $fieldName; |
5239 |
} |
} |
5240 |
|
|
5241 |
=head3 FixNames |
=head3 _FixNames |
5242 |
|
|
5243 |
Fix all the field names in a list. |
Fix all the field names in a list. |
5244 |
|
|
5269 |
return @result; |
return @result; |
5270 |
} |
} |
5271 |
|
|
5272 |
=head3 AddField |
=head3 _AddField |
5273 |
|
|
5274 |
Add a field to a field list. |
Add a field to a field list. |
5275 |
|
|
5304 |
$fieldList->{$fieldName} = $fieldStructure; |
$fieldList->{$fieldName} = $fieldStructure; |
5305 |
} |
} |
5306 |
|
|
5307 |
=head3 ReOrderRelationTable |
=head3 _ReOrderRelationTable |
5308 |
|
|
5309 |
This method will take a relation table and re-sort it according to the implicit ordering of the |
This method will take a relation table and re-sort it according to the implicit ordering of the |
5310 |
C<PrettySort> property. Instead of a hash based on field names, it will return a list of fields. |
C<PrettySort> property. Instead of a hash based on field names, it will return a list of fields. |
5365 |
|
|
5366 |
} |
} |
5367 |
|
|
5368 |
=head3 IsPrimary |
=head3 _IsPrimary |
5369 |
|
|
5370 |
Return TRUE if a specified relation is a primary relation, else FALSE. A relation is primary |
Return TRUE if a specified relation is a primary relation, else FALSE. A relation is primary |
5371 |
if it has the same name as an entity or relationship. |
if it has the same name as an entity or relationship. |
5401 |
return $retVal; |
return $retVal; |
5402 |
} |
} |
5403 |
|
|
5404 |
=head3 FindRelation |
=head3 _FindRelation |
5405 |
|
|
5406 |
Return the descriptor for the specified relation. |
Return the descriptor for the specified relation. |
5407 |
|
|
5432 |
|
|
5433 |
=head2 HTML Documentation Utility Methods |
=head2 HTML Documentation Utility Methods |
5434 |
|
|
5435 |
=head3 ComputeRelationshipSentence |
=head3 _ComputeRelationshipSentence |
5436 |
|
|
5437 |
The relationship sentence consists of the relationship name between the names of the |
The relationship sentence consists of the relationship name between the names of the |
5438 |
two related entities and an arity indicator. |
two related entities and an arity indicator. |
5462 |
# Get the parameters. |
# Get the parameters. |
5463 |
my ($relationshipName, $relationshipStructure) = @_; |
my ($relationshipName, $relationshipStructure) = @_; |
5464 |
# Format the relationship sentence. |
# Format the relationship sentence. |
5465 |
my $result = "$relationshipStructure->{from} <b>$relationshipName</b> $relationshipStructure->{to}"; |
my $result = "$relationshipStructure->{from} $relationshipName $relationshipStructure->{to}"; |
5466 |
# Compute the arity. |
# Compute the arity. |
5467 |
my $arityCode = $relationshipStructure->{arity}; |
my $arityCode = $relationshipStructure->{arity}; |
5468 |
my $arity = $ArityTable{$arityCode}; |
my $arity = $ArityTable{$arityCode}; |
5470 |
return $result; |
return $result; |
5471 |
} |
} |
5472 |
|
|
5473 |
=head3 ComputeRelationshipHeading |
=head3 _ComputeRelationshipHeading |
5474 |
|
|
5475 |
The relationship heading is the L<relationship sentence|/ComputeRelationshipSentence> with the entity |
The relationship heading is the L<relationship sentence|/ComputeRelationshipSentence> with the entity |
5476 |
names hyperlinked to the appropriate entity sections of the document. |
names hyperlinked to the appropriate entity sections of the document. |
5507 |
return $result; |
return $result; |
5508 |
} |
} |
5509 |
|
|
5510 |
=head3 ShowRelationTable |
=head3 _WikiRelationTable |
5511 |
|
|
5512 |
|
Generate the Wiki text for a particular relation. The relation's data will be formatted as a |
5513 |
|
table with three columns-- the field name, the field type, and the field description. |
5514 |
|
|
5515 |
|
This is a static method. |
5516 |
|
|
5517 |
|
=over 4 |
5518 |
|
|
5519 |
|
=item relationName |
5520 |
|
|
5521 |
|
Name of the relation being formatted. |
5522 |
|
|
5523 |
|
=item relationData |
5524 |
|
|
5525 |
|
Hash containing the relation's fields and indexes. |
5526 |
|
|
5527 |
|
=item RETURN |
5528 |
|
|
5529 |
|
Returns a Wiki string that can be used to display the relation name and all of its fields. |
5530 |
|
|
5531 |
|
=back |
5532 |
|
|
5533 |
|
=cut |
5534 |
|
|
5535 |
|
sub _WikiRelationTable { |
5536 |
|
# Get the parameters. |
5537 |
|
my ($relationName, $relationData) = @_; |
5538 |
|
# We'll create a list of lists in here, then call WikiTools::Table to |
5539 |
|
# convert it into a table. |
5540 |
|
my @rows = (); |
5541 |
|
# Push in the header row. |
5542 |
|
push @rows, [qw(Field Type Description)]; |
5543 |
|
# Loop through the fields. |
5544 |
|
for my $field (@{$relationData->{Fields}}) { |
5545 |
|
# Create this field's row. We always have a name and type. |
5546 |
|
my @row = ($field->{name}, $field->{type}); |
5547 |
|
# If we have a description, add it as the third column. |
5548 |
|
if (exists $field->{Notes}) { |
5549 |
|
push @row, WikiNote($field->{Notes}->{content}); |
5550 |
|
} |
5551 |
|
# Push this row onto the table list. |
5552 |
|
push @rows, \@row; |
5553 |
|
} |
5554 |
|
# Store the rows as a Wiki table. |
5555 |
|
my $retVal = WikiTools::Table(@rows); |
5556 |
|
# Now we show the relation's indexes. These are formatted as another |
5557 |
|
# table. |
5558 |
|
@rows = (); |
5559 |
|
# Push in the header row. |
5560 |
|
push @rows, [qw(Index Unique Fields Notes)]; |
5561 |
|
# Get the index hash. |
5562 |
|
my $indexTable = $relationData->{Indexes}; |
5563 |
|
# Loop through the indexes. For an entity, there is always at least one index. |
5564 |
|
# For a relationship, there are at least two. The upshot is we don't need to |
5565 |
|
# worry about accidentally generating a frivolous table here. |
5566 |
|
for my $indexName (sort keys %$indexTable) { |
5567 |
|
my $indexData = $indexTable->{$indexName}; |
5568 |
|
# Determine whether or not the index is unique. |
5569 |
|
my $unique = ((exists $indexData->{Unique} && $indexData->{Unique} eq "true") ? |
5570 |
|
"yes" : ""); |
5571 |
|
# Get the field list. |
5572 |
|
my $fields = join(', ', @{$indexData->{IndexFields}}); |
5573 |
|
# Get the note text. |
5574 |
|
my $description = ""; |
5575 |
|
if (my $note = $indexData->{Notes}) { |
5576 |
|
$description = WikiNote($note->{content}); |
5577 |
|
} |
5578 |
|
# Format this row. |
5579 |
|
my @row = ($indexName, $unique, $fields, $description); |
5580 |
|
push @rows, \@row; |
5581 |
|
} |
5582 |
|
# Add the index list to the result. |
5583 |
|
$retVal .= "\n\n" . WikiTools::Table(@rows); |
5584 |
|
} |
5585 |
|
|
5586 |
|
|
5587 |
|
=head3 _ShowRelationTable |
5588 |
|
|
5589 |
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 |
5590 |
table with three columns-- the field name, the field type, and the field description. |
table with three columns-- the field name, the field type, and the field description. |
5634 |
$htmlString .= "<li><b>Index $fullName</b>\n<ul>\n"; |
$htmlString .= "<li><b>Index $fullName</b>\n<ul>\n"; |
5635 |
# Add any note text. |
# Add any note text. |
5636 |
if (my $note = $indexData->{Notes}) { |
if (my $note = $indexData->{Notes}) { |
5637 |
$htmlString .= "<li>" . _HTMLNote($note->{content}) . "</li>\n"; |
$htmlString .= "<li>" . HTMLNote($note->{content}) . "</li>\n"; |
5638 |
} |
} |
5639 |
# Add the fiield list. |
# Add the fiield list. |
5640 |
$htmlString .= "<li><i>" . join(', ', @{$indexData->{IndexFields}}) . "</i></li>\n"; |
$htmlString .= "<li><i>" . join(', ', @{$indexData->{IndexFields}}) . "</i></li>\n"; |
5645 |
$htmlString .= "</ul>\n"; |
$htmlString .= "</ul>\n"; |
5646 |
} |
} |
5647 |
|
|
5648 |
=head3 OpenFieldTable |
=head3 _OpenFieldTable |
5649 |
|
|
5650 |
This method creates the header string for the field table generated by L</ShowMetaData>. |
This method creates the header string for the field table generated by L</ShowMetaData>. |
5651 |
|
|
5670 |
return _OpenTable($tablename, 'Field', 'Type', 'Description'); |
return _OpenTable($tablename, 'Field', 'Type', 'Description'); |
5671 |
} |
} |
5672 |
|
|
5673 |
=head3 OpenTable |
=head3 _OpenTable |
5674 |
|
|
5675 |
This method creates the header string for an HTML table. |
This method creates the header string for an HTML table. |
5676 |
|
|
5700 |
# Compute the number of columns. |
# Compute the number of columns. |
5701 |
my $colCount = @colNames; |
my $colCount = @colNames; |
5702 |
# Generate the title row. |
# Generate the title row. |
5703 |
my $htmlString = "<p><table border=\"2\"><tr><td colspan=\"$colCount\" align=\"center\">$tablename</td></tr>\n"; |
my $htmlString = "<table border=\"2\"><tr><td colspan=\"$colCount\" align=\"center\">$tablename</td></tr>\n"; |
5704 |
# Loop through the columns, adding the column header rows. |
# Loop through the columns, adding the column header rows. |
5705 |
$htmlString .= "<tr>"; |
$htmlString .= "<tr>"; |
5706 |
for my $colName (@colNames) { |
for my $colName (@colNames) { |
5710 |
return $htmlString; |
return $htmlString; |
5711 |
} |
} |
5712 |
|
|
5713 |
=head3 CloseTable |
=head3 _CloseTable |
5714 |
|
|
5715 |
This method returns the HTML for closing a table. |
This method returns the HTML for closing a table. |
5716 |
|
|
5719 |
=cut |
=cut |
5720 |
|
|
5721 |
sub _CloseTable { |
sub _CloseTable { |
5722 |
return "</table></p>\n"; |
return "</table>\n"; |
5723 |
} |
} |
5724 |
|
|
5725 |
=head3 ShowField |
=head3 _ShowField |
5726 |
|
|
5727 |
This method returns the HTML for displaying a row of field information in a field table. |
This method returns the HTML for displaying a row of field information in a field table. |
5728 |
|
|
5749 |
my $htmlString = "<tr><th align=\"left\">$fieldData->{name}</th><td>$fieldData->{type}</td>"; |
my $htmlString = "<tr><th align=\"left\">$fieldData->{name}</th><td>$fieldData->{type}</td>"; |
5750 |
# If we have content, add it as a third column. |
# If we have content, add it as a third column. |
5751 |
if (exists $fieldData->{Notes}) { |
if (exists $fieldData->{Notes}) { |
5752 |
$htmlString .= "<td>" . _HTMLNote($fieldData->{Notes}->{content}) . "</td>"; |
$htmlString .= "<td>" . HTMLNote($fieldData->{Notes}->{content}) . "</td>"; |
5753 |
} |
} |
5754 |
# Close off the row. |
# Close off the row. |
5755 |
$htmlString .= "</tr>\n"; |
$htmlString .= "</tr>\n"; |
5757 |
return $htmlString; |
return $htmlString; |
5758 |
} |
} |
5759 |
|
|
|
=head3 HTMLNote |
|
|
|
|
|
Convert a note or comment to HTML by replacing some bulletin-board codes with HTML. The codes |
|
|
supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph. |
|
|
Except for C<[p]>, all the codes are closed by slash-codes. So, for |
|
|
example, C<[b]Feature[/b]> displays the string C<Feature> in boldface. |
|
|
|
|
|
This is a static method. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item dataString |
|
|
|
|
|
String to convert to HTML. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
An HTML string derived from the input string. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub _HTMLNote { |
|
|
# Get the parameter. |
|
|
my ($dataString) = @_; |
|
|
# Substitute the codes. |
|
|
$dataString =~ s!\[(/?[bi])\]!<$1>!g; |
|
|
$dataString =~ s!\[p\]!</p><p>!g; |
|
|
# Return the result. |
|
|
return $dataString; |
|
|
} |
|
|
|
|
|
=head2 Data Generation Utilities |
|
|
|
|
|
=head3 IntGen |
|
|
|
|
|
C<< my $integer = IntGen($min, $max); >> |
|
|
|
|
|
Returns a random number between the specified minimum and maximum (inclusive). |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item min |
|
|
|
|
|
Minimum permissible return value. |
|
|
|
|
|
=item max |
|
|
|
|
|
Maximum permissible return value. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
Returns a value no lower than the minimum and no greater than the maximum. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub IntGen { |
|
|
# Get the parameters. |
|
|
my ($min, $max) = @_; |
|
|
# Determine the range of possible values. Note we put some space well above the |
|
|
# maximum value to give it a fighting chance of apppearing in the list. |
|
|
my $span = $max + 0.99 - $min; |
|
|
# Create an integer in the range. |
|
|
my $retVal = $min + int(rand($span)); |
|
|
# Return the result. |
|
|
return $retVal; |
|
|
} |
|
|
|
|
|
=head3 RandChar |
|
|
|
|
|
C<< my $char = RandChar($sourceString); >> |
|
|
|
|
|
Select a random character from a string. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item sourceString |
|
|
|
|
|
String from which the random character should be selected. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
Returns a single character from the incoming string. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub RandChar { |
|
|
# Get the parameter. |
|
|
my ($sourceString) = @_; |
|
|
# Select a random character. |
|
|
my $retVal = IntGen(0, (length $sourceString) - 1); |
|
|
# Return it. |
|
|
return substr($sourceString, $retVal, 1); |
|
|
} |
|
|
|
|
|
=head3 RandChars |
|
|
|
|
|
C<< my $string = RandChars($sourceString, $length); >> |
|
|
|
|
|
Create a string from characters taken from a source string. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item sourceString |
|
|
|
|
|
String from which the random characters should be selected. |
|
|
|
|
|
=item length |
|
|
|
|
|
Number of characters to put in the output string. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
Returns a string of the specified length consisting of characters taken from the |
|
|
source string. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub RandChars { |
|
|
# Get the parameters. |
|
|
my ($sourceString, $length) = @_; |
|
|
# Call RandChar repeatedly to generate the string. |
|
|
my $retVal = ""; |
|
|
for (my $i = 0; $i < $length; $i++) { |
|
|
$retVal .= RandChar($sourceString); |
|
|
} |
|
|
# Return the result. |
|
|
return $retVal; |
|
|
} |
|
|
|
|
|
=head3 RandParam |
|
|
|
|
|
C<< my $value = RandParam($parm1, $parm2, ... $parmN); >> |
|
|
|
|
|
Return a randomly-selected value from the parameter list. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item parm1, parm2, ... parmN |
|
|
|
|
|
List of values of which one will be selected. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
Returns a randomly-chosen value from the specified list. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub RandParam { |
|
|
# Get the parameter. |
|
|
my @parms = @_; |
|
|
# Choose a random parameter from the list. |
|
|
my $chosenIndex = IntGen(0, $#parms); |
|
|
return $parms[$chosenIndex]; |
|
|
} |
|
|
|
|
|
=head3 StringGen |
|
|
|
|
|
C<< my $string = StringGen($pattern1, $pattern2, ... $patternN); >> |
|
|
|
|
|
Returns a random string derived from a randomly-chosen format pattern. The pattern |
|
|
can either be a number (indicating the number of characters desired, or the letter |
|
|
C<P> followed by a picture. The picture should contain C<A> when a letter is desired, |
|
|
C<9> when a digit is desired, C<V> when a vowel is desired, C<K> when a consonant is |
|
|
desired, and C<X> when a letter or a digit is desired. Any other character will be |
|
|
translated as a literal. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item pattern1, pattern2, ... patternN |
|
|
|
|
|
List of patterns to be used to generate string values. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
A single string generated from a pattern. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub StringGen { |
|
|
# Get the parameters. |
|
|
my @patterns = @_; |
|
|
# Choose the appropriate pattern. |
|
|
my $chosenPattern = RandParam(@patterns); |
|
|
# Declare the return variable. |
|
|
my $retVal = ""; |
|
|
# Determine whether this is a count or a picture pattern. |
|
|
if ($chosenPattern =~ m/^\d+/) { |
|
|
# Here we have a count. Get the string of source characters. |
|
|
my $letterString = $PictureTable{'X'}; |
|
|
my $stringLen = length $letterString; |
|
|
# Save the number of characters we have to generate. |
|
|
my $charsLeft = $chosenPattern; |
|
|
# Loop until the return variable is full. |
|
|
while ($charsLeft > 0) { |
|
|
# Generate a random position in the soruce string. |
|
|
my $stringIndex = IntGen(0, $stringLen - 1); |
|
|
# Compute the number of characters to pull out of the source string. |
|
|
my $chunkSize = $stringLen - $stringIndex; |
|
|
if ($chunkSize > $charsLeft) { $chunkSize = $charsLeft; } |
|
|
# Stuff this chunk into the return value. |
|
|
$retVal .= substr($letterString, $stringIndex, $chunkSize); |
|
|
# Record the data moved. |
|
|
$charsLeft -= $chunkSize; |
|
|
} |
|
|
} elsif ($chosenPattern =~ m/^P/) { |
|
|
# Here we have a picture string. We will move through the picture one |
|
|
# character at a time generating data. |
|
|
for (my $i = 1; $i < length $chosenPattern; $i++) { |
|
|
# Get this picture character. |
|
|
my $chr = substr($chosenPattern, $i, 1); |
|
|
# Check to see if the picture char is one we recognize. |
|
|
if (exists $PictureTable{$chr}) { |
|
|
# Choose a random character from the available values for this |
|
|
# picture character. |
|
|
$retVal .= RandChar($PictureTable{$chr}); |
|
|
} else { |
|
|
# Copy in the picture character as a literal. |
|
|
$retVal .= $chr; |
|
|
} |
|
|
} |
|
|
} else { |
|
|
# Here we have neither a picture string or a letter count, so we treat |
|
|
# the string as a literal. |
|
|
$retVal = $chosenPattern; |
|
|
} |
|
|
# Return the string formed. |
|
|
return $retVal; |
|
|
} |
|
|
|
|
|
=head3 DateGen |
|
|
|
|
|
C<< my $date = DateGen($startDayOffset, $endDayOffset, $minutes); >> |
|
|
|
|
|
Return a numeric timestamp within the specified range of days with the specified minute |
|
|
value. The range of days is specified relevant to the current day. Thus, the call |
|
|
|
|
|
C<< my $date = DateGen(-1, 5, 720); >> |
|
|
|
|
|
will return a timestamp at noon (72 minutes past midnight) sometime during the week that |
|
|
began on the preceding day. If you want a random minute of the day, simply combine with |
|
|
a call to L</IntGen>, as follows. |
|
|
|
|
|
C<< my $date = DateGen(-1, 5, IntGen(0, 1439)); >> |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item startDayOffset |
|
|
|
|
|
The earliest day that can be returned, relative to the current day. |
|
|
|
|
|
=item endDayOffset |
|
|
|
|
|
The latest day that can be returned, related to the current day. |
|
|
|
|
|
=item minutes |
|
|
|
|
|
Number of minutes into the selected day that should be used. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub DateGen { |
|
|
# Get the parameters. |
|
|
my ($startDayOffset, $endDayOffset, $minutes) = @_; |
|
|
# Get midnight of the current day. |
|
|
my $now = time(); |
|
|
my ($sec, $min, $hour) = localtime($now); |
|
|
my $today = $now - (($hour * 60 + $min) * 60 + $sec); |
|
|
# Compute the day we want. |
|
|
my $newDay = IntGen($startDayOffset, $endDayOffset) * 86400 + $today; |
|
|
# Add the minutes. |
|
|
my $retVal = $newDay + $minutes * 60; |
|
|
# Return the result. |
|
|
return $retVal; |
|
|
} |
|
|
|
|
|
=head3 FloatGen |
|
|
|
|
|
C<< my $number = FloatGen($min, $max); >> |
|
|
|
|
|
Return a random floating-point number greater than or equal to the specified minimum and |
|
|
less than the specified maximum. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item min |
|
|
|
|
|
Minimum permissible value for the number returned. |
|
|
|
|
|
=item max |
|
|
|
|
|
Maximum permissible value for the number returned. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
Returns a floating-point number anywhere in the specified range. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub FloatGen { |
|
|
# Get the parameters. |
|
|
my ($min, $max) = @_; |
|
|
# Generate the result. |
|
|
my $retVal = rand($max - $min) + $min; |
|
|
return $retVal; |
|
|
} |
|
|
|
|
|
=head3 ListGen |
|
|
|
|
|
C<< my @list = ListGen($pattern, $count); >> |
|
|
|
|
|
Return a list containing a fixed number of randomly-generated strings. |
|
|
|
|
|
=over 4 |
|
|
|
|
|
=item pattern |
|
|
|
|
|
A pattern (in the form expected by L</StringGen>) that should be used to generate the |
|
|
strings in the list. |
|
|
|
|
|
=item count |
|
|
|
|
|
The number of list entries to generate. |
|
|
|
|
|
=item RETURN |
|
|
|
|
|
Returns a list consisting of the specified number of strings. |
|
|
|
|
|
=back |
|
|
|
|
|
=cut |
|
|
|
|
|
sub ListGen { |
|
|
# Get the parameters. |
|
|
my ($pattern, $count) = @_; |
|
|
# Generate the list. |
|
|
my @retVal = (); |
|
|
for (my $i = 0; $i < $count; $i++) { |
|
|
push @retVal, StringGen($pattern); |
|
|
} |
|
|
# Return it. |
|
|
return @retVal; |
|
|
} |
|
|
|
|
5760 |
1; |
1; |