[Bio] / Sprout / ERDB.pm Repository:
ViewVC logotype

Annotation of /Sprout/ERDB.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.71 - (view) (download) (as text)

1 : parrello 1.1 package ERDB;
2 :    
3 : parrello 1.10 use strict;
4 :     use Tracer;
5 : olson 1.14 use DBrtns;
6 : parrello 1.10 use Data::Dumper;
7 :     use XML::Simple;
8 :     use DBQuery;
9 :     use DBObject;
10 :     use Stats;
11 :     use Time::HiRes qw(gettimeofday);
12 : parrello 1.42 use Digest::MD5 qw(md5_base64);
13 : parrello 1.19 use FIG;
14 : parrello 1.1
15 :     =head1 Entity-Relationship Database Package
16 :    
17 :     =head2 Introduction
18 :    
19 :     The Entity-Relationship Database Package allows the client to create an easily-configurable
20 :     database of Entities connected by Relationships. Each entity is represented by one or more
21 :     relations in an underlying SQL database. Each relationship is represented by a single
22 :     relation that connects two entities.
23 :    
24 :     Although this package is designed for general use, all examples are derived from the
25 :     Sprout database, which is the first database implemented using this package.
26 :    
27 :     Each entity has at least one relation, the I<primary relation>, that has the same name as
28 :     the entity. The primary relation contains a field named C<id> that contains the unique
29 :     identifier of each entity instance. An entity may have additional relations that contain
30 :     fields which are optional or can occur more than once. For example, the B<FEATURE> entity
31 :     has a B<feature-type> attribute that occurs exactly once for each feature. This attribute
32 :     is implemented by a C<feature_type> column in the primary relation C<Feature>. In addition,
33 :     however, a feature may have zero or more aliases. These are implemented using a C<FeatureAlias>
34 :     relation that contains two fields-- the feature ID (C<id>) and the alias name (C<alias>).
35 :     The B<FEATURE> entity also contains an optional virulence number. This is implemented
36 :     as a separate relation C<FeatureVirulence> which contains an ID (C<id>) and a virulence number
37 : parrello 1.8 (C<virulence>). If the virulence of a feature I<ABC> is known to be 6, there will be one row in
38 :     the C<FeatureVirulence> relation possessing the value I<ABC> as its ID and 6 as its virulence
39 :     number. If the virulence of I<ABC> is not known, there will not be any rows for it in
40 :     C<FeatureVirulence>.
41 : parrello 1.1
42 :     Entities are connected by binary relationships implemented using single relations possessing the
43 :     same name as the relationship itself and that has an I<arity> of 1-to-1 (C<11>), 1-to-many (C<1M>),
44 :     or many-to-many (C<MM>). Each relationship's relation contains a C<from-link> field that contains the
45 :     ID of the source entity and a C<to-link> field that contains the ID of the target entity. The name
46 :     of the relationship is generally a verb phrase with the source entity as the subject and the
47 :     target entity as the object. So, for example, the B<ComesFrom> relationship connects the B<GENOME>
48 :     and B<SOURCE> entities, and indicates that a particular source organization participated in the
49 :     mapping of the genome. A source organization frequently participates in the mapping
50 :     of many genomes, and many source organizations can cooperate in the mapping of a single genome, so
51 :     this relationship has an arity of many-to-many (C<MM>). The relation that implements the B<ComesFrom>
52 :     relationship is called C<ComesFrom> and contains two fields-- C<from-link>, which contains a genome ID,
53 :     and C<to-link>, which contains a source ID.
54 :    
55 :     A relationship may itself have attributes. These attributes, known as I<intersection data attributes>,
56 :     are implemented as additional fields in the relationship's relation. So, for example, the
57 :     B<IsMadeUpOf> relationship connects the B<Contig> entity to the B<Sequence> entity, and is used
58 :     to determine which sequences make up a contig. The relationship has as an attribute the
59 :     B<start-position>, which indicates where in the contig that the sequence begins. This attribute
60 :     is implemented as the C<start_position> field in the C<IsMadeUpOf> relation.
61 :    
62 :     The database itself is described by an XML file using the F<ERDatabase.xsd> schema. In addition to
63 :     all the data required to define the entities, relationships, and attributes, the schema provides
64 :     space for notes describing the data and what it means. These notes are used by L</ShowMetaData>
65 :     to generate documentation for the database.
66 :    
67 :     Finally, every entity and relationship object has a flag indicating if it is new or old. The object
68 :     is considered I<old> if it was loaded by the L</LoadTables> method. It is considered I<new> if it
69 :     was inserted by the L</InsertObject> method.
70 :    
71 :     To facilitate testing, the ERDB module supports automatic generation of test data. This process
72 : parrello 1.5 is described in the L</GenerateEntity> and L</GenerateConnection> methods, though it is not yet
73 :     fully implemented.
74 : parrello 1.1
75 : parrello 1.8 =head2 XML Database Description
76 :    
77 :     =head3 Data Types
78 :    
79 :     The ERDB system supports the following data types. Note that there are numerous string
80 :     types depending on the maximum length. Some database packages limit the total number of
81 :     characters you have in an index key; to insure the database works in all environments,
82 :     the type of string should be the shortest one possible that supports all the known values.
83 :    
84 :     =over 4
85 :    
86 :     =item char
87 :    
88 :     single ASCII character
89 :    
90 :     =item int
91 :    
92 :     32-bit signed integer
93 :    
94 : parrello 1.60 =item counter
95 :    
96 :     32-bit unsigned integer
97 :    
98 : parrello 1.8 =item date
99 :    
100 :     64-bit unsigned integer, representing a PERL date/time value
101 :    
102 :     =item text
103 :    
104 :     long string; Text fields cannot be used in indexes or sorting and do not support the
105 :     normal syntax of filter clauses, but can be up to a billion character in length
106 :    
107 :     =item float
108 :    
109 :     double-precision floating-point number
110 :    
111 :     =item boolean
112 :    
113 :     single-bit numeric value; The value is stored as a 16-bit signed integer (for
114 :     compatability with certain database packages), but the only values supported are
115 :     0 and 1.
116 :    
117 : parrello 1.44 =item id-string
118 :    
119 :     variable-length string, maximum 25 characters
120 :    
121 : parrello 1.8 =item key-string
122 :    
123 :     variable-length string, maximum 40 characters
124 :    
125 :     =item name-string
126 :    
127 :     variable-length string, maximum 80 characters
128 :    
129 :     =item medium-string
130 :    
131 :     variable-length string, maximum 160 characters
132 :    
133 :     =item string
134 :    
135 :     variable-length string, maximum 255 characters
136 :    
137 : parrello 1.42 =item hash-string
138 :    
139 :     variable-length string, maximum 22 characters
140 :    
141 : parrello 1.8 =back
142 :    
143 : parrello 1.42 The hash-string data type has a special meaning. The actual key passed into the loader will
144 :     be a string, but it will be digested into a 22-character MD5 code to save space. Although the
145 :     MD5 algorithm is not perfect, it is extremely unlikely two strings will have the same
146 :     digest. Therefore, it is presumed the keys will be unique. When the database is actually
147 :     in use, the hashed keys will be presented rather than the original values. For this reason,
148 :     they should not be used for entities where the key is meaningful.
149 :    
150 : parrello 1.8 =head3 Global Tags
151 :    
152 :     The entire database definition must be inside a B<Database> tag. The display name of
153 :     the database is given by the text associated with the B<Title> tag. The display name
154 :     is only used in the automated documentation. It has no other effect. The entities and
155 :     relationships are listed inside the B<Entities> and B<Relationships> tags,
156 :     respectively. None of these tags have attributes.
157 :    
158 : parrello 1.10 <Database>
159 :     <Title>... display title here...</Title>
160 :     <Entities>
161 :     ... entity definitions here ...
162 :     </Entities>
163 :     <Relationships>
164 :     ... relationship definitions here...
165 :     </Relationships>
166 :     </Database>
167 : parrello 1.8
168 :     Entities, relationships, indexes, and fields all allow a text tag called B<Notes>.
169 :     The text inside the B<Notes> tag contains comments that will appear when the database
170 :     documentation is generated. Within a B<Notes> tag, you may use C<[i]> and C<[/i]> for
171 :     italics, C<[b]> and C<[/b]> for bold, and C<[p]> for a new paragraph.
172 :    
173 :     =head3 Fields
174 :    
175 :     Both entities and relationships have fields described by B<Field> tags. A B<Field>
176 :     tag can have B<Notes> associated with it. The complete set of B<Field> tags for an
177 :     object mus be inside B<Fields> tags.
178 :    
179 : parrello 1.10 <Entity ... >
180 :     <Fields>
181 :     ... Field tags ...
182 :     </Fields>
183 :     </Entity>
184 : parrello 1.8
185 :     The attributes for the B<Field> tag are as follows.
186 :    
187 :     =over 4
188 :    
189 :     =item name
190 :    
191 :     Name of the field. The field name should contain only letters, digits, and hyphens (C<->),
192 :     and the first character should be a letter. Most underlying databases are case-insensitive
193 : parrello 1.70 with the respect to field names, so a best practice is to use lower-case letters only. Finally,
194 :     the name C<search-relevance> has special meaning for full-text searches and should not be
195 :     used as a field name.
196 : parrello 1.8
197 :     =item type
198 :    
199 :     Data type of the field. The legal data types are given above.
200 :    
201 :     =item relation
202 :    
203 :     Name of the relation containing the field. This should only be specified for entity
204 :     fields. The ERDB system does not support optional fields or multi-occurring fields
205 :     in the primary relation of an entity. Instead, they are put into secondary relations.
206 :     So, for example, in the C<Genome> entity, the C<group-name> field indicates a special
207 :     grouping used to select a subset of the genomes. A given genome may not be in any
208 :     groups or may be in multiple groups. Therefore, C<group-name> specifies a relation
209 :     value. The relation name specified must be a valid table name. By convention, it is
210 :     usually the entity name followed by a qualifying word (e.g. C<GenomeGroup>). In an
211 :     entity, the fields without a relation attribute are said to belong to the
212 :     I<primary relation>. This relation has the same name as the entity itself.
213 :    
214 : parrello 1.70 =item searchable
215 :    
216 :     If specified, then the field is a candidate for full-text searching. A single full-text
217 :     index will be created for each relation with at least one searchable field in it.
218 :     For best results, this option should only be used for string or text fields.
219 :    
220 : parrello 1.8 =back
221 :    
222 :     =head3 Indexes
223 :    
224 :     An entity can have multiple alternate indexes associated with it. The fields must
225 :     be from the primary relation. The alternate indexes assist in ordering results
226 :     from a query. A relationship can have up to two indexes-- a I<to-index> and a
227 :     I<from-index>. These order the results when crossing the relationship. For
228 :     example, in the relationship C<HasContig> from C<Genome> to C<Contig>, the
229 :     from-index would order the contigs of a ganome, and the to-index would order
230 :     the genomes of a contig. A relationship's index must specify only fields in
231 :     the relationship.
232 :    
233 :     The indexes for an entity must be listed inside the B<Indexes> tag. The from-index
234 :     of a relationship is specified using the B<FromIndex> tag; the to-index is specified
235 :     using the B<ToIndex> tag.
236 :    
237 :     Each index can contain a B<Notes> tag. In addition, it will have an B<IndexFields>
238 :     tag containing the B<IndexField> tags. These specify, in order, the fields used in
239 :     the index. The attributes of an B<IndexField> tag are as follows.
240 :    
241 :     =over 4
242 :    
243 :     =item name
244 :    
245 :     Name of the field.
246 :    
247 :     =item order
248 :    
249 :     Sort order of the field-- C<ascending> or C<descending>.
250 :    
251 :     =back
252 :    
253 :     The B<Index>, B<FromIndex>, and B<ToIndex> tags themselves have no attributes.
254 :    
255 :     =head3 Object and Field Names
256 :    
257 :     By convention entity and relationship names use capital casing (e.g. C<Genome> or
258 :     C<HasRegionsIn>. Most underlying databases, however, are aggressively case-insensitive
259 :     with respect to relation names, converting them internally to all-upper case or
260 :     all-lower case.
261 :    
262 :     If syntax or parsing errors occur when you try to load or use an ERDB database, the
263 :     most likely reason is that one of your objects has an SQL reserved word as its name.
264 :     The list of SQL reserved words keeps increasing; however, most are unlikely to show
265 :     up as a noun or declarative verb phrase. The exceptions are C<Group>, C<User>,
266 :     C<Table>, C<Index>, C<Object>, C<Date>, C<Number>, C<Update>, C<Time>, C<Percent>,
267 :     C<Memo>, C<Order>, and C<Sum>. This problem can crop up in field names as well.
268 :    
269 :     Every entity has a field called C<id> that acts as its primary key. Every relationship
270 :     has fields called C<from-link> and C<to-link> that contain copies of the relevant
271 :     entity IDs. These are essentially ERDB's reserved words, and should not be used
272 :     for user-defined field names.
273 :    
274 :     =head3 Entities
275 :    
276 :     An entity is described by the B<Entity> tag. The entity can contain B<Notes>, an
277 :     B<Indexes> tag containing one or more secondary indexes, and a B<Fields> tag
278 :     containing one or more fields. The attributes of the B<Entity> tag are as follows.
279 :    
280 :     =over 4
281 :    
282 :     =item name
283 :    
284 :     Name of the entity. The entity name, by convention, uses capital casing (e.g. C<Genome>
285 :     or C<GroupBlock>) and should be a noun or noun phrase.
286 :    
287 :     =item keyType
288 :    
289 :     Data type of the primary key. The primary key is always named C<id>.
290 :    
291 :     =back
292 :    
293 :     =head3 Relationships
294 :    
295 :     A relationship is described by the C<Relationship> tag. Within a relationship,
296 :     there can be a C<Notes> tag, a C<Fields> tag containing the intersection data
297 :     fields, a C<FromIndex> tag containing the from-index, and a C<ToIndex> tag containing
298 :     the to-index.
299 :    
300 :     The C<Relationship> tag has the following attributes.
301 :    
302 :     =over 4
303 :    
304 :     =item name
305 :    
306 :     Name of the relationship. The relationship name, by convention, uses capital casing
307 :     (e.g. C<ContainsRegionIn> or C<HasContig>), and should be a declarative verb
308 :     phrase, designed to fit between the from-entity and the to-entity (e.g.
309 :     Block C<ContainsRegionIn> Genome).
310 :    
311 :     =item from
312 :    
313 :     Name of the entity from which the relationship starts.
314 :    
315 :     =item to
316 :    
317 :     Name of the entity to which the relationship proceeds.
318 :    
319 :     =item arity
320 :    
321 :     Relationship type: C<1M> for one-to-many and C<MM> for many-to-many.
322 :    
323 :     =back
324 :    
325 : parrello 1.1 =cut
326 :    
327 :     # GLOBALS
328 :    
329 :     # Table of information about our datatypes. "sqlType" is the corresponding SQL datatype string.
330 :     # "maxLen" is the maximum permissible length of the incoming string data used to populate a field
331 :     # of the specified type. "dataGen" is PERL string that will be evaluated if no test data generation
332 : parrello 1.18 # string is specified in the field definition. "avgLen" is the average byte length for estimating
333 : parrello 1.60 # record sizes. "sort" is the key modifier for the sort command.
334 :     my %TypeTable = ( char => { sqlType => 'CHAR(1)', maxLen => 1, avgLen => 1, sort => "", dataGen => "StringGen('A')" },
335 :     int => { sqlType => 'INTEGER', maxLen => 20, avgLen => 4, sort => "n", dataGen => "IntGen(0, 99999999)" },
336 :     counter => { sqlType => 'INTEGER UNSIGNED', maxLen => 20, avgLen => 4, sort => "n", dataGen => "IntGen(0, 99999999)" },
337 :     string => { sqlType => 'VARCHAR(255)', maxLen => 255, avgLen => 100, sort => "", dataGen => "StringGen(IntGen(10,250))" },
338 :     text => { sqlType => 'TEXT', maxLen => 1000000000, avgLen => 500, sort => "", dataGen => "StringGen(IntGen(80,1000))" },
339 :     date => { sqlType => 'BIGINT', maxLen => 80, avgLen => 8, sort => "n", dataGen => "DateGen(-7, 7, IntGen(0,1400))" },
340 :     float => { sqlType => 'DOUBLE PRECISION', maxLen => 40, avgLen => 8, sort => "g", dataGen => "FloatGen(0.0, 100.0)" },
341 :     boolean => { sqlType => 'SMALLINT', maxLen => 1, avgLen => 1, sort => "n", dataGen => "IntGen(0, 1)" },
342 : parrello 1.42 'hash-string' =>
343 : parrello 1.60 { sqlType => 'VARCHAR(22)', maxLen => 22, avgLen => 22, sort => "", dataGen => "SringGen(22)" },
344 : parrello 1.44 'id-string' =>
345 : parrello 1.60 { sqlType => 'VARCHAR(25)', maxLen => 25, avgLen => 25, sort => "", dataGen => "SringGen(22)" },
346 : parrello 1.10 'key-string' =>
347 : parrello 1.60 { sqlType => 'VARCHAR(40)', maxLen => 40, avgLen => 10, sort => "", dataGen => "StringGen(IntGen(10,40))" },
348 : parrello 1.10 'name-string' =>
349 : parrello 1.60 { sqlType => 'VARCHAR(80)', maxLen => 80, avgLen => 40, sort => "", dataGen => "StringGen(IntGen(10,80))" },
350 : parrello 1.10 'medium-string' =>
351 : parrello 1.60 { sqlType => 'VARCHAR(160)', maxLen => 160, avgLen => 40, sort => "", dataGen => "StringGen(IntGen(10,160))" },
352 : parrello 1.10 );
353 : parrello 1.1
354 :     # Table translating arities into natural language.
355 :     my %ArityTable = ( '11' => 'one-to-one',
356 : parrello 1.10 '1M' => 'one-to-many',
357 :     'MM' => 'many-to-many'
358 :     );
359 : parrello 1.1
360 :     # Table for interpreting string patterns.
361 :    
362 :     my %PictureTable = ( 'A' => "abcdefghijklmnopqrstuvwxyz",
363 : parrello 1.10 '9' => "0123456789",
364 :     'X' => "abcdefghijklmnopqrstuvwxyz0123456789",
365 :     'V' => "aeiou",
366 :     'K' => "bcdfghjklmnoprstvwxyz"
367 :     );
368 : parrello 1.1
369 :     =head2 Public Methods
370 :    
371 :     =head3 new
372 :    
373 : parrello 1.5 C<< my $database = ERDB->new($dbh, $metaFileName); >>
374 : parrello 1.1
375 :     Create a new ERDB object.
376 :    
377 :     =over 4
378 :    
379 :     =item dbh
380 :    
381 :     DBKernel database object for the target database.
382 :    
383 :     =item metaFileName
384 :    
385 :     Name of the XML file containing the metadata.
386 :    
387 :     =back
388 :    
389 :     =cut
390 :    
391 :     sub new {
392 : parrello 1.10 # Get the parameters.
393 :     my ($class, $dbh, $metaFileName, $options) = @_;
394 :     # Load the meta-data.
395 :     my $metaData = _LoadMetaData($metaFileName);
396 :     # Create the object.
397 :     my $self = { _dbh => $dbh,
398 :     _metaData => $metaData
399 :     };
400 :     # Bless and return it.
401 :     bless $self, $class;
402 :     return $self;
403 : parrello 1.1 }
404 :    
405 :     =head3 ShowMetaData
406 :    
407 : parrello 1.18 C<< $erdb->ShowMetaData($fileName); >>
408 : parrello 1.1
409 :     This method outputs a description of the database. This description can be used to help users create
410 :     the data to be loaded into the relations.
411 :    
412 :     =over 4
413 :    
414 :     =item filename
415 :    
416 :     The name of the output file.
417 :    
418 :     =back
419 :    
420 :     =cut
421 :    
422 :     sub ShowMetaData {
423 : parrello 1.10 # Get the parameters.
424 :     my ($self, $filename) = @_;
425 :     # Get the metadata and the title string.
426 :     my $metadata = $self->{_metaData};
427 :     # Get the title string.
428 :     my $title = $metadata->{Title};
429 :     # Get the entity and relationship lists.
430 :     my $entityList = $metadata->{Entities};
431 :     my $relationshipList = $metadata->{Relationships};
432 :     # Open the output file.
433 :     open(HTMLOUT, ">$filename") || Confess("Could not open MetaData display file $filename: $!");
434 :     Trace("Building MetaData table of contents.") if T(4);
435 :     # Write the HTML heading stuff.
436 :     print HTMLOUT "<html>\n<head>\n<title>$title</title>\n";
437 :     print HTMLOUT "</head>\n<body>\n";
438 : parrello 1.45 # Write the documentation.
439 :     print HTMLOUT $self->DisplayMetaData();
440 :     # Close the document.
441 :     print HTMLOUT "</body>\n</html>\n";
442 :     # Close the file.
443 :     close HTMLOUT;
444 :     }
445 :    
446 :     =head3 DisplayMetaData
447 :    
448 :     C<< my $html = $erdb->DisplayMetaData(); >>
449 :    
450 :     Return an HTML description of the database. This description can be used to help users create
451 :     the data to be loaded into the relations and form queries. The output is raw includable HTML
452 :     without any HEAD or BODY tags.
453 :    
454 :     =over 4
455 :    
456 :     =item filename
457 :    
458 :     The name of the output file.
459 :    
460 :     =back
461 :    
462 :     =cut
463 :    
464 :     sub DisplayMetaData {
465 :     # Get the parameters.
466 :     my ($self) = @_;
467 :     # Get the metadata and the title string.
468 :     my $metadata = $self->{_metaData};
469 :     # Get the title string.
470 :     my $title = $metadata->{Title};
471 :     # Get the entity and relationship lists.
472 :     my $entityList = $metadata->{Entities};
473 :     my $relationshipList = $metadata->{Relationships};
474 :     # Declare the return variable.
475 :     my $retVal = "";
476 :     # Open the output file.
477 :     Trace("Building MetaData table of contents.") if T(4);
478 : parrello 1.10 # Here we do the table of contents. It starts as an unordered list of section names. Each
479 :     # section contains an ordered list of entity or relationship subsections.
480 : parrello 1.45 $retVal .= "<ul>\n<li><a href=\"#EntitiesSection\">Entities</a>\n<ol>\n";
481 : parrello 1.10 # Loop through the Entities, displaying a list item for each.
482 :     foreach my $key (sort keys %{$entityList}) {
483 :     # Display this item.
484 : parrello 1.45 $retVal .= "<li><a href=\"#$key\">$key</a></li>\n";
485 : parrello 1.10 }
486 :     # Close off the entity section and start the relationship section.
487 : parrello 1.45 $retVal .= "</ol></li>\n<li><a href=\"#RelationshipsSection\">Relationships</a>\n<ol>\n";
488 : parrello 1.10 # Loop through the Relationships.
489 :     foreach my $key (sort keys %{$relationshipList}) {
490 :     # Display this item.
491 :     my $relationshipTitle = _ComputeRelationshipSentence($key, $relationshipList->{$key});
492 : parrello 1.45 $retVal .= "<li><a href=\"#$key\">$relationshipTitle</a></li>\n";
493 : parrello 1.10 }
494 :     # Close off the relationship section and list the join table section.
495 : parrello 1.45 $retVal .= "</ol></li>\n<li><a href=\"#JoinTable\">Join Table</a></li>\n";
496 : parrello 1.10 # Close off the table of contents itself.
497 : parrello 1.45 $retVal .= "</ul>\n";
498 : parrello 1.10 # Now we start with the actual data. Denote we're starting the entity section.
499 : parrello 1.45 $retVal .= "<a name=\"EntitiesSection\"></a><h2>Entities</h2>\n";
500 : parrello 1.10 # Loop through the entities.
501 :     for my $key (sort keys %{$entityList}) {
502 :     Trace("Building MetaData entry for $key entity.") if T(4);
503 :     # Create the entity header. It contains a bookmark and the entity name.
504 : parrello 1.45 $retVal .= "<a name=\"$key\"></a><h3>$key</h3>\n";
505 : parrello 1.10 # Get the entity data.
506 :     my $entityData = $entityList->{$key};
507 :     # If there's descriptive text, display it.
508 :     if (my $notes = $entityData->{Notes}) {
509 : parrello 1.45 $retVal .= "<p>" . _HTMLNote($notes->{content}) . "</p>\n";
510 : parrello 1.10 }
511 :     # Now we want a list of the entity's relationships. First, we set up the relationship subsection.
512 : parrello 1.45 $retVal .= "<h4>Relationships for <b>$key</b></h4>\n<ul>\n";
513 : parrello 1.10 # Loop through the relationships.
514 :     for my $relationship (sort keys %{$relationshipList}) {
515 :     # Get the relationship data.
516 :     my $relationshipStructure = $relationshipList->{$relationship};
517 :     # Only use the relationship if if has this entity in its FROM or TO fields.
518 :     if ($relationshipStructure->{from} eq $key || $relationshipStructure->{to} eq $key) {
519 :     # Get the relationship sentence and append the arity.
520 :     my $relationshipDescription = _ComputeRelationshipSentence($relationship, $relationshipStructure);
521 :     # Display the relationship data.
522 : parrello 1.45 $retVal .= "<li><a href=\"#$relationship\">$relationshipDescription</a></li>\n";
523 : parrello 1.10 }
524 :     }
525 :     # Close off the relationship list.
526 : parrello 1.45 $retVal .= "</ul>\n";
527 : parrello 1.10 # Get the entity's relations.
528 :     my $relationList = $entityData->{Relations};
529 :     # Create a header for the relation subsection.
530 : parrello 1.45 $retVal .= "<h4>Relations for <b>$key</b></h4>\n";
531 : parrello 1.10 # Loop through the relations, displaying them.
532 :     for my $relation (sort keys %{$relationList}) {
533 :     my $htmlString = _ShowRelationTable($relation, $relationList->{$relation});
534 : parrello 1.45 $retVal .= $htmlString;
535 : parrello 1.10 }
536 :     }
537 :     # Denote we're starting the relationship section.
538 : parrello 1.45 $retVal .= "<a name=\"RelationshipsSection\"></a><h2>Relationships</h2>\n";
539 : parrello 1.10 # Loop through the relationships.
540 :     for my $key (sort keys %{$relationshipList}) {
541 :     Trace("Building MetaData entry for $key relationship.") if T(4);
542 :     # Get the relationship's structure.
543 :     my $relationshipStructure = $relationshipList->{$key};
544 :     # Create the relationship header.
545 :     my $headerText = _ComputeRelationshipHeading($key, $relationshipStructure);
546 : parrello 1.45 $retVal .= "<h3><a name=\"$key\"></a>$headerText</h3>\n";
547 : parrello 1.10 # Get the entity names.
548 :     my $fromEntity = $relationshipStructure->{from};
549 :     my $toEntity = $relationshipStructure->{to};
550 :     # Describe the relationship arity. Note there's a bit of trickiness involving recursive
551 :     # many-to-many relationships. In a normal many-to-many we use two sentences to describe
552 :     # the arity (one for each direction). This is a bad idea for a recursive relationship,
553 :     # since both sentences will say the same thing.
554 :     my $arity = $relationshipStructure->{arity};
555 :     if ($arity eq "11") {
556 : parrello 1.45 $retVal .= "<p>Each <b>$fromEntity</b> relates to at most one <b>$toEntity</b>.\n";
557 : parrello 1.10 } else {
558 : parrello 1.45 $retVal .= "<p>Each <b>$fromEntity</b> relates to multiple <b>$toEntity</b>s.\n";
559 : parrello 1.10 if ($arity eq "MM" && $fromEntity ne $toEntity) {
560 : parrello 1.45 $retVal .= "Each <b>$toEntity</b> relates to multiple <b>$fromEntity</b>s.\n";
561 : parrello 1.10 }
562 :     }
563 : parrello 1.45 $retVal .= "</p>\n";
564 : parrello 1.10 # If there are notes on this relationship, display them.
565 :     if (my $notes = $relationshipStructure->{Notes}) {
566 : parrello 1.45 $retVal .= "<p>" . _HTMLNote($notes->{content}) . "</p>\n";
567 : parrello 1.10 }
568 :     # Generate the relationship's relation table.
569 :     my $htmlString = _ShowRelationTable($key, $relationshipStructure->{Relations}->{$key});
570 : parrello 1.45 $retVal .= $htmlString;
571 : parrello 1.10 }
572 :     Trace("Building MetaData join table.") if T(4);
573 :     # Denote we're starting the join table.
574 : parrello 1.45 $retVal .= "<a name=\"JoinTable\"></a><h3>Join Table</h3>\n";
575 : parrello 1.10 # Create a table header.
576 : parrello 1.45 $retVal .= _OpenTable("Join Table", "Source", "Target", "Join Condition");
577 : parrello 1.10 # Loop through the joins.
578 :     my $joinTable = $metadata->{Joins};
579 :     my @joinKeys = keys %{$joinTable};
580 :     for my $joinKey (sort @joinKeys) {
581 :     # Separate out the source, the target, and the join clause.
582 :     $joinKey =~ m!^([^/]+)/(.+)$!;
583 :     my ($sourceRelation, $targetRelation) = ($1, $2);
584 : parrello 1.30 Trace("Join with key $joinKey is from $sourceRelation to $targetRelation.") if T(Joins => 4);
585 : parrello 1.10 my $source = $self->ComputeObjectSentence($sourceRelation);
586 :     my $target = $self->ComputeObjectSentence($targetRelation);
587 :     my $clause = $joinTable->{$joinKey};
588 :     # Display them in a table row.
589 : parrello 1.45 $retVal .= "<tr><td>$source</td><td>$target</td><td>$clause</td></tr>\n";
590 : parrello 1.10 }
591 :     # Close the table.
592 : parrello 1.45 $retVal .= _CloseTable();
593 :     Trace("Built MetaData HTML.") if T(3);
594 :     # Return the HTML.
595 :     return $retVal;
596 : parrello 1.1 }
597 :    
598 :     =head3 DumpMetaData
599 :    
600 : parrello 1.18 C<< $erdb->DumpMetaData(); >>
601 : parrello 1.1
602 :     Return a dump of the metadata structure.
603 :    
604 :     =cut
605 :    
606 :     sub DumpMetaData {
607 : parrello 1.10 # Get the parameters.
608 :     my ($self) = @_;
609 :     # Dump the meta-data.
610 :     return Data::Dumper::Dumper($self->{_metaData});
611 : parrello 1.1 }
612 :    
613 :     =head3 CreateTables
614 :    
615 : parrello 1.18 C<< $erdb->CreateTables(); >>
616 : parrello 1.1
617 :     This method creates the tables for the database from the metadata structure loaded by the
618 :     constructor. It is expected this function will only be used on rare occasions, when the
619 : parrello 1.2 user needs to start with an empty database. Otherwise, the L</LoadTables> method can be
620 : parrello 1.1 used by itself with the truncate flag turned on.
621 :    
622 :     =cut
623 :    
624 :     sub CreateTables {
625 : parrello 1.10 # Get the parameters.
626 :     my ($self) = @_;
627 : parrello 1.23 # Get the relation names.
628 :     my @relNames = $self->GetTableNames();
629 :     # Loop through the relations.
630 :     for my $relationName (@relNames) {
631 :     # Create a table for this relation.
632 :     $self->CreateTable($relationName);
633 :     Trace("Relation $relationName created.") if T(2);
634 : parrello 1.10 }
635 : parrello 1.1 }
636 :    
637 :     =head3 CreateTable
638 :    
639 : parrello 1.18 C<< $erdb->CreateTable($tableName, $indexFlag, $estimatedRows); >>
640 : parrello 1.1
641 :     Create the table for a relation and optionally create its indexes.
642 :    
643 :     =over 4
644 :    
645 :     =item relationName
646 :    
647 :     Name of the relation (which will also be the table name).
648 :    
649 : parrello 1.18 =item indexFlag
650 : parrello 1.1
651 :     TRUE if the indexes for the relation should be created, else FALSE. If FALSE,
652 :     L</CreateIndexes> must be called later to bring the indexes into existence.
653 :    
654 : parrello 1.18 =item estimatedRows (optional)
655 :    
656 :     If specified, the estimated maximum number of rows for the relation. This
657 :     information allows the creation of tables using storage engines that are
658 :     faster but require size estimates, such as MyISAM.
659 :    
660 : parrello 1.1 =back
661 :    
662 :     =cut
663 :    
664 :     sub CreateTable {
665 : parrello 1.10 # Get the parameters.
666 : parrello 1.18 my ($self, $relationName, $indexFlag, $estimatedRows) = @_;
667 : parrello 1.10 # Get the database handle.
668 :     my $dbh = $self->{_dbh};
669 :     # Get the relation data and determine whether or not the relation is primary.
670 :     my $relationData = $self->_FindRelation($relationName);
671 :     my $rootFlag = $self->_IsPrimary($relationName);
672 :     # Create a list of the field data.
673 :     my @fieldList;
674 :     for my $fieldData (@{$relationData->{Fields}}) {
675 :     # Assemble the field name and type.
676 :     my $fieldName = _FixName($fieldData->{name});
677 :     my $fieldString = "$fieldName $TypeTable{$fieldData->{type}}->{sqlType} NOT NULL ";
678 :     # Push the result into the field list.
679 :     push @fieldList, $fieldString;
680 :     }
681 :     # If this is a root table, add the "new_record" flag. It defaults to 0, so
682 :     if ($rootFlag) {
683 :     push @fieldList, "new_record $TypeTable{boolean}->{sqlType} NOT NULL DEFAULT 0";
684 :     }
685 :     # Convert the field list into a comma-delimited string.
686 :     my $fieldThing = join(', ', @fieldList);
687 :     # Insure the table is not already there.
688 :     $dbh->drop_table(tbl => $relationName);
689 :     Trace("Table $relationName dropped.") if T(2);
690 : parrello 1.18 # If there are estimated rows, create an estimate so we can take advantage of
691 :     # faster DB technologies.
692 :     my $estimation = undef;
693 :     if ($estimatedRows) {
694 :     $estimation = [$self->EstimateRowSize($relationName), $estimatedRows];
695 :     }
696 : parrello 1.10 # Create the table.
697 :     Trace("Creating table $relationName: $fieldThing") if T(2);
698 : parrello 1.18 $dbh->create_table(tbl => $relationName, flds => $fieldThing, estimates => $estimation);
699 : parrello 1.10 Trace("Relation $relationName created in database.") if T(2);
700 : parrello 1.70 # If we want to build the indexes, we do it here. Note that the full-text search
701 :     # index will not be built until the table has been loaded.
702 : parrello 1.10 if ($indexFlag) {
703 :     $self->CreateIndex($relationName);
704 :     }
705 : parrello 1.1 }
706 :    
707 : parrello 1.31 =head3 VerifyFields
708 :    
709 :     C<< my $count = $erdb->VerifyFields($relName, \@fieldList); >>
710 :    
711 :     Run through the list of proposed field values, insuring that all the character fields are
712 :     below the maximum length. If any fields are too long, they will be truncated in place.
713 :    
714 :     =over 4
715 :    
716 :     =item relName
717 :    
718 :     Name of the relation for which the specified fields are destined.
719 :    
720 :     =item fieldList
721 :    
722 :     Reference to a list, in order, of the fields to be put into the relation.
723 :    
724 :     =item RETURN
725 :    
726 :     Returns the number of fields truncated.
727 :    
728 :     =back
729 :    
730 :     =cut
731 :    
732 :     sub VerifyFields {
733 :     # Get the parameters.
734 :     my ($self, $relName, $fieldList) = @_;
735 :     # Initialize the return value.
736 :     my $retVal = 0;
737 :     # Get the relation definition.
738 :     my $relData = $self->_FindRelation($relName);
739 :     # Get the list of field descriptors.
740 :     my $fieldTypes = $relData->{Fields};
741 :     my $fieldCount = scalar @{$fieldTypes};
742 :     # Loop through the two lists.
743 :     for (my $i = 0; $i < $fieldCount; $i++) {
744 :     # Get the type of the current field.
745 :     my $fieldType = $fieldTypes->[$i]->{type};
746 :     # If it's a character field, verify the length.
747 :     if ($fieldType =~ /string/) {
748 :     my $maxLen = $TypeTable{$fieldType}->{maxLen};
749 :     my $oldString = $fieldList->[$i];
750 :     if (length($oldString) > $maxLen) {
751 :     # Here it's too big, so we truncate it.
752 :     Trace("Truncating field $i in relation $relName to $maxLen characters from \"$oldString\".") if T(1);
753 :     $fieldList->[$i] = substr $oldString, 0, $maxLen;
754 :     $retVal++;
755 :     }
756 :     }
757 :     }
758 :     # Return the truncation count.
759 :     return $retVal;
760 :     }
761 :    
762 : parrello 1.42 =head3 DigestFields
763 :    
764 :     C<< $erdb->DigestFields($relName, $fieldList); >>
765 :    
766 :     Digest the strings in the field list that correspond to data type C<hash-string> in the
767 :     specified relation.
768 :    
769 :     =over 4
770 :    
771 :     =item relName
772 :    
773 :     Name of the relation to which the fields belong.
774 :    
775 :     =item fieldList
776 :    
777 :     List of field contents to be loaded into the relation.
778 :    
779 :     =back
780 :    
781 :     =cut
782 :     #: Return Type ;
783 :     sub DigestFields {
784 :     # Get the parameters.
785 :     my ($self, $relName, $fieldList) = @_;
786 :     # Get the relation definition.
787 :     my $relData = $self->_FindRelation($relName);
788 :     # Get the list of field descriptors.
789 :     my $fieldTypes = $relData->{Fields};
790 :     my $fieldCount = scalar @{$fieldTypes};
791 :     # Loop through the two lists.
792 :     for (my $i = 0; $i < $fieldCount; $i++) {
793 :     # Get the type of the current field.
794 :     my $fieldType = $fieldTypes->[$i]->{type};
795 :     # If it's a hash string, digest it in place.
796 :     if ($fieldType eq 'hash-string') {
797 : parrello 1.46 $fieldList->[$i] = $self->DigestKey($fieldList->[$i]);
798 : parrello 1.42 }
799 :     }
800 :     }
801 :    
802 : parrello 1.46 =head3 DigestKey
803 :    
804 :     C<< my $digested = $erdb->DigestKey($keyValue); >>
805 :    
806 :     Return the digested value of a symbolic key. The digested value can then be plugged into a
807 :     key-based search into a table with key-type hash-string.
808 :    
809 :     Currently the digesting process is independent of the database structure, but that may not
810 :     always be the case, so this is an instance method instead of a static method.
811 :    
812 :     =over 4
813 :    
814 :     =item keyValue
815 :    
816 :     Key value to digest.
817 :    
818 :     =item RETURN
819 :    
820 : parrello 1.56 Digested value of the key.
821 : parrello 1.46
822 :     =back
823 :    
824 :     =cut
825 :    
826 :     sub DigestKey {
827 :     # Get the parameters.
828 :     my ($self, $keyValue) = @_;
829 :     # Compute the digest.
830 :     my $retVal = md5_base64($keyValue);
831 :     # Return the result.
832 :     return $retVal;
833 :     }
834 :    
835 : parrello 1.1 =head3 CreateIndex
836 :    
837 : parrello 1.18 C<< $erdb->CreateIndex($relationName); >>
838 : parrello 1.1
839 :     Create the indexes for a relation. If a table is being loaded from a large source file (as
840 : parrello 1.12 is the case in L</LoadTable>), it is sometimes best to create the indexes after the load.
841 :     If that is the case, then L</CreateTable> should be called with the index flag set to
842 :     FALSE, and this method used after the load to create the indexes for the table.
843 : parrello 1.1
844 :     =cut
845 :    
846 :     sub CreateIndex {
847 : parrello 1.10 # Get the parameters.
848 :     my ($self, $relationName) = @_;
849 :     # Get the relation's descriptor.
850 :     my $relationData = $self->_FindRelation($relationName);
851 :     # Get the database handle.
852 :     my $dbh = $self->{_dbh};
853 :     # Now we need to create this relation's indexes. We do this by looping through its index table.
854 :     my $indexHash = $relationData->{Indexes};
855 :     for my $indexName (keys %{$indexHash}) {
856 :     my $indexData = $indexHash->{$indexName};
857 :     # Get the index's field list.
858 :     my @fieldList = _FixNames(@{$indexData->{IndexFields}});
859 :     my $flds = join(', ', @fieldList);
860 :     # Get the index's uniqueness flag.
861 : parrello 1.70 my $unique = (exists $indexData->{Unique} ? 'unique' : undef);
862 : parrello 1.10 # Create the index.
863 : parrello 1.24 my $rv = $dbh->create_index(idx => $indexName, tbl => $relationName,
864 : parrello 1.70 flds => $flds, kind => $unique);
865 : parrello 1.24 if ($rv) {
866 :     Trace("Index created: $indexName for $relationName ($flds)") if T(1);
867 :     } else {
868 :     Confess("Error creating index $indexName for $relationName using ($flds): " . $dbh->error_message());
869 :     }
870 : parrello 1.10 }
871 : parrello 1.1 }
872 :    
873 :     =head3 LoadTables
874 :    
875 : parrello 1.18 C<< my $stats = $erdb->LoadTables($directoryName, $rebuild); >>
876 : parrello 1.1
877 :     This method will load the database tables from a directory. The tables must already have been created
878 :     in the database. (This can be done by calling L</CreateTables>.) The caller passes in a directory name;
879 :     all of the relations to be loaded must have a file in the directory with the same name as the relation
880 :     (optionally with a suffix of C<.dtx>). Each file must be a tab-delimited table of field values. Each
881 :     line of the file will be loaded as a row of the target relation table. The field values should be in
882 :     the same order as the fields in the relation tables generated by L</ShowMetaData>. The old data is
883 :     erased before the new data is loaded in.
884 :    
885 :     A certain amount of translation automatically takes place. Ctrl-M characters are deleted, and
886 :     tab and new-line characters inside a field are escaped as C<\t> and C<\n>, respectively. Dates must
887 :     be entered as a Unix timestamp, that is, as an integer number of seconds since the base epoch.
888 :    
889 :     =over 4
890 :    
891 :     =item directoryName
892 :    
893 :     Name of the directory containing the relation files to be loaded.
894 :    
895 :     =item rebuild
896 :    
897 :     TRUE if the tables should be dropped and rebuilt, else FALSE. This is, unfortunately, the
898 :     only way to erase existing data in the tables, since the TRUNCATE command is not supported
899 :     by all of the DB engines we use.
900 :    
901 :     =item RETURN
902 :    
903 :     Returns a statistical object describing the number of records read and a list of the error messages.
904 :    
905 :     =back
906 :    
907 :     =cut
908 :    
909 :     sub LoadTables {
910 : parrello 1.10 # Get the parameters.
911 :     my ($self, $directoryName, $rebuild) = @_;
912 :     # Start the timer.
913 :     my $startTime = gettimeofday;
914 :     # Clean any trailing slash from the directory name.
915 :     $directoryName =~ s!/\\$!!;
916 :     # Declare the return variable.
917 :     my $retVal = Stats->new();
918 : parrello 1.23 # Get the relation names.
919 :     my @relNames = $self->GetTableNames();
920 :     for my $relationName (@relNames) {
921 :     # Try to load this relation.
922 :     my $result = $self->_LoadRelation($directoryName, $relationName, $rebuild);
923 : parrello 1.10 # Accumulate the statistics.
924 :     $retVal->Accumulate($result);
925 :     }
926 :     # Add the duration of the load to the statistical object.
927 :     $retVal->Add('duration', gettimeofday - $startTime);
928 :     # Return the accumulated statistics.
929 :     return $retVal;
930 : parrello 1.1 }
931 :    
932 : parrello 1.23
933 : parrello 1.1 =head3 GetTableNames
934 :    
935 : parrello 1.18 C<< my @names = $erdb->GetTableNames; >>
936 : parrello 1.1
937 :     Return a list of the relations required to implement this database.
938 :    
939 :     =cut
940 :    
941 :     sub GetTableNames {
942 : parrello 1.10 # Get the parameters.
943 :     my ($self) = @_;
944 :     # Get the relation list from the metadata.
945 :     my $relationTable = $self->{_metaData}->{RelationTable};
946 :     # Return the relation names.
947 :     return keys %{$relationTable};
948 : parrello 1.1 }
949 :    
950 :     =head3 GetEntityTypes
951 :    
952 : parrello 1.18 C<< my @names = $erdb->GetEntityTypes; >>
953 : parrello 1.1
954 :     Return a list of the entity type names.
955 :    
956 :     =cut
957 :    
958 :     sub GetEntityTypes {
959 : parrello 1.10 # Get the database object.
960 :     my ($self) = @_;
961 :     # Get the entity list from the metadata object.
962 :     my $entityList = $self->{_metaData}->{Entities};
963 :     # Return the list of entity names in alphabetical order.
964 :     return sort keys %{$entityList};
965 : parrello 1.1 }
966 :    
967 : parrello 1.20 =head3 IsEntity
968 :    
969 :     C<< my $flag = $erdb->IsEntity($entityName); >>
970 :    
971 :     Return TRUE if the parameter is an entity name, else FALSE.
972 :    
973 :     =over 4
974 :    
975 :     =item entityName
976 :    
977 :     Object name to be tested.
978 :    
979 :     =item RETURN
980 :    
981 :     Returns TRUE if the specified string is an entity name, else FALSE.
982 :    
983 :     =back
984 :    
985 :     =cut
986 :    
987 :     sub IsEntity {
988 :     # Get the parameters.
989 :     my ($self, $entityName) = @_;
990 :     # Test to see if it's an entity.
991 :     return exists $self->{_metaData}->{Entities}->{$entityName};
992 :     }
993 :    
994 : parrello 1.1 =head3 Get
995 :    
996 : parrello 1.45 C<< my $query = $erdb->Get(\@objectNames, $filterClause, \@params); >>
997 : parrello 1.1
998 :     This method returns a query object for entities of a specified type using a specified filter.
999 :     The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each
1000 :     field name represented in the form B<I<objectName>(I<fieldName>)>. For example, the
1001 :     following call requests all B<Genome> objects for the genus specified in the variable
1002 :     $genus.
1003 :    
1004 : parrello 1.45 C<< $query = $erdb->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>
1005 : parrello 1.1
1006 :     The WHERE clause contains a single question mark, so there is a single additional
1007 :     parameter representing the parameter value. It would also be possible to code
1008 :    
1009 : parrello 1.18 C<< $query = $erdb->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>
1010 : parrello 1.1
1011 :     however, this version of the call would generate a syntax error if there were any quote
1012 :     characters inside the variable C<$genus>.
1013 :    
1014 :     The use of the strange parenthesized notation for field names enables us to distinguish
1015 :     hyphens contained within field names from minus signs that participate in the computation
1016 :     of the WHERE clause. All of the methods that manipulate fields will use this same notation.
1017 :    
1018 :     It is possible to specify multiple entity and relationship names in order to retrieve more than
1019 :     one object's data at the same time, which allows highly complex joined queries. For example,
1020 :    
1021 : parrello 1.45 C<< $query = $erdb->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>
1022 : parrello 1.1
1023 :     If multiple names are specified, then the query processor will automatically determine a
1024 :     join path between the entities and relationships. The algorithm used is very simplistic.
1025 : parrello 1.39 In particular, if a relationship is recursive, the path is determined by the order in which
1026 :     the entity and the relationship appear. For example, consider a recursive relationship
1027 :     B<IsParentOf> which relates B<People> objects to other B<People> objects. If the join path is
1028 : parrello 1.1 coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however,
1029 :     the join path is C<['IsParentOf', 'People']>, then the people returned will be children.
1030 :    
1031 : parrello 1.39 If an entity or relationship is mentioned twice, the name for the second occurrence will
1032 :     be suffixed with C<2>, the third occurrence will be suffixed with C<3>, and so forth. So,
1033 :     for example, if we have C<['Feature', 'HasContig', 'Contig', 'HasContig']>, then the
1034 :     B<to-link> field of the first B<HasContig> is specified as C<HasContig(to-link)>, while
1035 :     the B<to-link> field of the second B<HasContig> is specified as C<HasContig2(to-link)>.
1036 :    
1037 : parrello 1.1 =over 4
1038 :    
1039 :     =item objectNames
1040 :    
1041 :     List containing the names of the entity and relationship objects to be retrieved.
1042 :    
1043 :     =item filterClause
1044 :    
1045 :     WHERE clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
1046 :     be parameterized with parameter markers (C<?>). Each field used in the WHERE clause must be
1047 :     specified in the standard form B<I<objectName>(I<fieldName>)>. Any parameters specified
1048 :     in the filter clause should be added to the parameter list as additional parameters. The
1049 :     fields in a filter clause can come from primary entity relations, relationship relations,
1050 :     or secondary entity relations; however, all of the entities and relationships involved must
1051 :     be included in the list of object names.
1052 :    
1053 :     The filter clause can also specify a sort order. To do this, simply follow the filter string
1054 :     with an ORDER BY clause. For example, the following filter string gets all genomes for a
1055 :     particular genus and sorts them by species name.
1056 :    
1057 :     C<< "Genome(genus) = ? ORDER BY Genome(species)" >>
1058 :    
1059 : parrello 1.30 Note that the case is important. Only an uppercase "ORDER BY" with a single space will
1060 :     be processed. The idea is to make it less likely to find the verb by accident.
1061 :    
1062 : parrello 1.1 The rules for field references in a sort order are the same as those for field references in the
1063 :     filter clause in general; however, odd things may happen if a sort field is from a secondary
1064 :     relation.
1065 :    
1066 : parrello 1.39 Finally, you can limit the number of rows returned by adding a LIMIT clause. The LIMIT must
1067 :     be the last thing in the filter clause, and it contains only the word "LIMIT" followed by
1068 :     a positive number. So, for example
1069 :    
1070 :     C<< "Genome(genus) = ? ORDER BY Genome(species) LIMIT 10" >>
1071 :    
1072 :     will only return the first ten genomes for the specified genus. The ORDER BY clause is not
1073 :     required. For example, to just get the first 10 genomes in the B<Genome> table, you could
1074 :     use
1075 :    
1076 :     C<< "LIMIT 10" >>
1077 :    
1078 : parrello 1.45 =item params
1079 : parrello 1.1
1080 : parrello 1.45 Reference to a list of parameter values to be substituted into the filter clause.
1081 : parrello 1.1
1082 :     =item RETURN
1083 :    
1084 :     Returns a B<DBQuery> that can be used to iterate through all of the results.
1085 :    
1086 :     =back
1087 :    
1088 :     =cut
1089 :    
1090 :     sub Get {
1091 : parrello 1.10 # Get the parameters.
1092 : parrello 1.45 my ($self, $objectNames, $filterClause, $params) = @_;
1093 :     # Process the SQL stuff.
1094 :     my ($suffix, $mappedNameListRef, $mappedNameHashRef) =
1095 :     $self->_SetupSQL($objectNames, $filterClause);
1096 :     # Create the query.
1097 :     my $command = "SELECT DISTINCT " . join(".*, ", @{$mappedNameListRef}) .
1098 :     ".* $suffix";
1099 :     my $sth = $self->_GetStatementHandle($command, $params);
1100 : parrello 1.39 # Now we create the relation map, which enables DBQuery to determine the order, name
1101 :     # and mapped name for each object in the query.
1102 :     my @relationMap = ();
1103 : parrello 1.45 for my $mappedName (@{$mappedNameListRef}) {
1104 :     push @relationMap, [$mappedName, $mappedNameHashRef->{$mappedName}];
1105 : parrello 1.39 }
1106 : parrello 1.10 # Return the statement object.
1107 : parrello 1.39 my $retVal = DBQuery::_new($self, $sth, \@relationMap);
1108 : parrello 1.10 return $retVal;
1109 : parrello 1.1 }
1110 :    
1111 : parrello 1.70 =head3 Search
1112 :    
1113 :     C<< my $query = $erdb->Search($searchExpression, $idx, \@objectNames, $filterClause, \@params); >>
1114 :    
1115 :     Perform a full text search with filtering. The search will be against a specified object
1116 :     in the object name list. That object will get an extra field containing the search
1117 :     relevance. Note that except for the search expression, the parameters of this method are
1118 :     the same as those for L</Get> and follow the same rules.
1119 :    
1120 :     =over 4
1121 :    
1122 :     =item searchExpression
1123 :    
1124 :     Boolean search expression for the text fields of the target object.
1125 :    
1126 :     =item idx
1127 :    
1128 :     Index in the I<$objectNames> list of the table to be searched in full-text mode.
1129 :    
1130 :     =item objectNames
1131 :    
1132 :     List containing the names of the entity and relationship objects to be retrieved.
1133 :    
1134 :     =item filterClause
1135 :    
1136 :     WHERE clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
1137 :     be parameterized with parameter markers (C<?>). Each field used in the WHERE clause must be
1138 :     specified in the standard form B<I<objectName>(I<fieldName>)>. Any parameters specified
1139 :     in the filter clause should be added to the parameter list as additional parameters. The
1140 :     fields in a filter clause can come from primary entity relations, relationship relations,
1141 :     or secondary entity relations; however, all of the entities and relationships involved must
1142 :     be included in the list of object names.
1143 :    
1144 :     =item params
1145 :    
1146 :     Reference to a list of parameter values to be substituted into the filter clause.
1147 :    
1148 :     =item RETURN
1149 :    
1150 :     Returns a query object for the specified search.
1151 :    
1152 :     =back
1153 :    
1154 :     =cut
1155 :    
1156 :     sub Search {
1157 :     # Get the parameters.
1158 :     my ($self, $searchExpression, $idx, $objectNames, $filterClause, $params) = @_;
1159 :     # Declare the return variable.
1160 :     my $retVal;
1161 :     # Create a safety copy of the parameter list.
1162 :     my @myParams = @{$params};
1163 :     # Get the first object's structure so we have access to the searchable fields.
1164 :     my $object1Name = $objectNames->[$idx];
1165 :     my $object1Structure = $self->_GetStructure($object1Name);
1166 :     # Get the field list.
1167 :     if (! exists $object1Structure->{searchFields}) {
1168 :     Confess("No searchable index for $object1Name.");
1169 :     } else {
1170 :     # Get the field list.
1171 :     my @fields = @{$object1Structure->{searchFields}};
1172 : parrello 1.71 # Clean the search expression.
1173 :     my $actualKeywords = $self->CleanKeywords($searchExpression);
1174 : parrello 1.70 # We need two match expressions, one for the filter clause and one in the
1175 :     # query itself. Both will use a parameter mark, so we need to push the
1176 :     # search expression onto the front of the parameter list twice.
1177 : parrello 1.71 unshift @myParams, $actualKeywords, $actualKeywords;
1178 : parrello 1.70 # Build the match expression.
1179 :     my @matchFilterFields = map { "$object1Name." . _FixName($_) } @fields;
1180 :     my $matchClause = "MATCH (" . join(", ", @matchFilterFields) . ") AGAINST (? IN BOOLEAN MODE)";
1181 :     # Process the SQL stuff.
1182 :     my ($suffix, $mappedNameListRef, $mappedNameHashRef) =
1183 :     $self->_SetupSQL($objectNames, $filterClause, $matchClause);
1184 :     # Create the query. Note that the match clause is inserted at the front of
1185 :     # the select fields.
1186 :     my $command = "SELECT DISTINCT $matchClause, " . join(".*, ", @{$mappedNameListRef}) .
1187 :     ".* $suffix";
1188 :     my $sth = $self->_GetStatementHandle($command, \@myParams);
1189 :     # Now we create the relation map, which enables DBQuery to determine the order, name
1190 :     # and mapped name for each object in the query.
1191 :     my @relationMap = _RelationMap($mappedNameHashRef, $mappedNameListRef);
1192 :     # Return the statement object.
1193 :     $retVal = DBQuery::_new($self, $sth, \@relationMap, $object1Name);
1194 :     }
1195 :     return $retVal;
1196 :     }
1197 :    
1198 : parrello 1.45 =head3 GetFlat
1199 :    
1200 :     C<< my @list = $erdb->GetFlat(\@objectNames, $filterClause, \@parameterList, $field); >>
1201 :    
1202 :     This is a variation of L</GetAll> that asks for only a single field per record and
1203 :     returns a single flattened list.
1204 :    
1205 :     =over 4
1206 :    
1207 :     =item objectNames
1208 :    
1209 :     List containing the names of the entity and relationship objects to be retrieved.
1210 :    
1211 :     =item filterClause
1212 :    
1213 :     WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
1214 :     be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form
1215 :     B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the
1216 :     parameter list as additional parameters. The fields in a filter clause can come from primary
1217 :     entity relations, relationship relations, or secondary entity relations; however, all of the
1218 :     entities and relationships involved must be included in the list of object names.
1219 :    
1220 :     =item parameterList
1221 :    
1222 :     List of the parameters to be substituted in for the parameters marks in the filter clause.
1223 :    
1224 :     =item field
1225 :    
1226 :     Name of the field to be used to get the elements of the list returned.
1227 :    
1228 :     =item RETURN
1229 :    
1230 :     Returns a list of values.
1231 :    
1232 :     =back
1233 :    
1234 :     =cut
1235 :     #: Return Type @;
1236 :     sub GetFlat {
1237 :     # Get the parameters.
1238 :     my ($self, $objectNames, $filterClause, $parameterList, $field) = @_;
1239 :     # Construct the query.
1240 :     my $query = $self->Get($objectNames, $filterClause, $parameterList);
1241 :     # Create the result list.
1242 :     my @retVal = ();
1243 :     # Loop through the records, adding the field values found to the result list.
1244 :     while (my $row = $query->Fetch()) {
1245 :     push @retVal, $row->Value($field);
1246 :     }
1247 :     # Return the list created.
1248 :     return @retVal;
1249 :     }
1250 :    
1251 : parrello 1.32 =head3 Delete
1252 :    
1253 :     C<< my $stats = $erdb->Delete($entityName, $objectID); >>
1254 :    
1255 :     Delete an entity instance from the database. The instance is deleted along with all entity and
1256 :     relationship instances dependent on it. The idea of dependence here is recursive. An object is
1257 :     always dependent on itself. An object is dependent if it is a 1-to-many or many-to-many
1258 :     relationship connected to a dependent entity or the "to" entity connected to a 1-to-many
1259 :     dependent relationship.
1260 :    
1261 :     =over 4
1262 :    
1263 :     =item entityName
1264 :    
1265 :     Name of the entity type for the instance being deleted.
1266 :    
1267 :     =item objectID
1268 :    
1269 :     ID of the entity instance to be deleted. If the ID contains a wild card character (C<%>),
1270 :     then it is presumed to by a LIKE pattern.
1271 :    
1272 :     =item testFlag
1273 :    
1274 :     If TRUE, the delete statements will be traced without being executed.
1275 :    
1276 :     =item RETURN
1277 :    
1278 :     Returns a statistics object indicating how many records of each particular table were
1279 :     deleted.
1280 :    
1281 :     =back
1282 :    
1283 :     =cut
1284 :     #: Return Type $%;
1285 :     sub Delete {
1286 :     # Get the parameters.
1287 :     my ($self, $entityName, $objectID, $testFlag) = @_;
1288 :     # Declare the return variable.
1289 :     my $retVal = Stats->new();
1290 :     # Get the DBKernel object.
1291 :     my $db = $self->{_dbh};
1292 :     # We're going to generate all the paths branching out from the starting entity. One of
1293 :     # the things we have to be careful about is preventing loops. We'll use a hash to
1294 :     # determine if we've hit a loop.
1295 :     my %alreadyFound = ();
1296 : parrello 1.33 # These next lists will serve as our result stack. We start by pushing object lists onto
1297 : parrello 1.32 # the stack, and then popping them off to do the deletes. This means the deletes will
1298 :     # start with the longer paths before getting to the shorter ones. That, in turn, makes
1299 :     # sure we don't delete records that might be needed to forge relationships back to the
1300 : parrello 1.33 # original item. We have two lists-- one for TO-relationships, and one for
1301 :     # FROM-relationships and entities.
1302 :     my @fromPathList = ();
1303 :     my @toPathList = ();
1304 : parrello 1.32 # This final hash is used to remember what work still needs to be done. We push paths
1305 :     # onto the list, then pop them off to extend the paths. We prime it with the starting
1306 :     # point. Note that we will work hard to insure that the last item on a path in the
1307 :     # TODO list is always an entity.
1308 :     my @todoList = ([$entityName]);
1309 :     while (@todoList) {
1310 :     # Get the current path.
1311 :     my $current = pop @todoList;
1312 :     # Copy it into a list.
1313 :     my @stackedPath = @{$current};
1314 :     # Pull off the last item on the path. It will always be an entity.
1315 :     my $entityName = pop @stackedPath;
1316 :     # Add it to the alreadyFound list.
1317 :     $alreadyFound{$entityName} = 1;
1318 :     # Get the entity data.
1319 :     my $entityData = $self->_GetStructure($entityName);
1320 :     # The first task is to loop through the entity's relation. A DELETE command will
1321 :     # be needed for each of them.
1322 :     my $relations = $entityData->{Relations};
1323 :     for my $relation (keys %{$relations}) {
1324 :     my @augmentedList = (@stackedPath, $relation);
1325 : parrello 1.33 push @fromPathList, \@augmentedList;
1326 : parrello 1.32 }
1327 :     # Now we need to look for relationships connected to this entity.
1328 :     my $relationshipList = $self->{_metaData}->{Relationships};
1329 :     for my $relationshipName (keys %{$relationshipList}) {
1330 :     my $relationship = $relationshipList->{$relationshipName};
1331 :     # Check the FROM field. We're only interested if it's us.
1332 :     if ($relationship->{from} eq $entityName) {
1333 :     # Add the path to this relationship.
1334 :     my @augmentedList = (@stackedPath, $entityName, $relationshipName);
1335 : parrello 1.33 push @fromPathList, \@augmentedList;
1336 : parrello 1.32 # Check the arity. If it's MM we're done. If it's 1M
1337 :     # and the target hasn't been seen yet, we want to
1338 :     # stack the entity for future processing.
1339 :     if ($relationship->{arity} eq '1M') {
1340 :     my $toEntity = $relationship->{to};
1341 :     if (! exists $alreadyFound{$toEntity}) {
1342 :     # Here we have a new entity that's dependent on
1343 :     # the current entity, so we need to stack it.
1344 :     my @stackList = (@augmentedList, $toEntity);
1345 : parrello 1.33 push @fromPathList, \@stackList;
1346 : parrello 1.34 } else {
1347 :     Trace("$toEntity ignored because it occurred previously.") if T(4);
1348 : parrello 1.32 }
1349 :     }
1350 :     }
1351 :     # Now check the TO field. In this case only the relationship needs
1352 : parrello 1.33 # deletion.
1353 : parrello 1.32 if ($relationship->{to} eq $entityName) {
1354 :     my @augmentedList = (@stackedPath, $entityName, $relationshipName);
1355 : parrello 1.33 push @toPathList, \@augmentedList;
1356 : parrello 1.32 }
1357 :     }
1358 :     }
1359 :     # Create the first qualifier for the WHERE clause. This selects the
1360 :     # keys of the primary entity records to be deleted. When we're deleting
1361 :     # from a dependent table, we construct a join page from the first qualifier
1362 :     # to the table containing the dependent records to delete.
1363 :     my $qualifier = ($objectID =~ /%/ ? "LIKE ?" : "= ?");
1364 : parrello 1.33 # We need to make two passes. The first is through the to-list, and
1365 :     # the second through the from-list. The from-list is second because
1366 :     # the to-list may need to pass through some of the entities the
1367 :     # from-list would delete.
1368 :     my %stackList = ( from_link => \@fromPathList, to_link => \@toPathList );
1369 :     # Now it's time to do the deletes. We do it in two passes.
1370 :     for my $keyName ('to_link', 'from_link') {
1371 :     # Get the list for this key.
1372 :     my @pathList = @{$stackList{$keyName}};
1373 : parrello 1.34 Trace(scalar(@pathList) . " entries in path list for $keyName.") if T(3);
1374 : parrello 1.33 # Loop through this list.
1375 :     while (my $path = pop @pathList) {
1376 :     # Get the table whose rows are to be deleted.
1377 :     my @pathTables = @{$path};
1378 : parrello 1.37 # Start the DELETE statement. We need to call DBKernel because the
1379 :     # syntax of a DELETE-USING varies among DBMSs.
1380 : parrello 1.33 my $target = $pathTables[$#pathTables];
1381 : parrello 1.37 my $stmt = $db->SetUsing(@pathTables);
1382 : parrello 1.33 # Now start the WHERE. The first thing is the ID field from the starting table. That
1383 :     # starting table will either be the entity relation or one of the entity's
1384 :     # sub-relations.
1385 :     $stmt .= " WHERE $pathTables[0].id $qualifier";
1386 :     # Now we run through the remaining entities in the path, connecting them up.
1387 :     for (my $i = 1; $i <= $#pathTables; $i += 2) {
1388 :     # Connect the current relationship to the preceding entity.
1389 :     my ($entity, $rel) = @pathTables[$i-1,$i];
1390 :     # The style of connection depends on the direction of the relationship.
1391 : parrello 1.35 $stmt .= " AND $entity.id = $rel.$keyName";
1392 : parrello 1.32 if ($i + 1 <= $#pathTables) {
1393 :     # Here there's a next entity, so connect that to the relationship's
1394 :     # to-link.
1395 :     my $entity2 = $pathTables[$i+1];
1396 : parrello 1.35 $stmt .= " AND $rel.to_link = $entity2.id";
1397 : parrello 1.32 }
1398 :     }
1399 : parrello 1.33 # Now we have our desired DELETE statement.
1400 :     if ($testFlag) {
1401 :     # Here the user wants to trace without executing.
1402 :     Trace($stmt) if T(0);
1403 :     } else {
1404 :     # Here we can delete. Note that the SQL method dies with a confessing
1405 :     # if an error occurs, so we just go ahead and do it.
1406 : parrello 1.36 Trace("Executing delete from $target using '$objectID'.") if T(3);
1407 :     my $rv = $db->SQL($stmt, 0, $objectID);
1408 : parrello 1.33 # Accumulate the statistics for this delete. The only rows deleted
1409 :     # are from the target table, so we use its name to record the
1410 :     # statistic.
1411 :     $retVal->Add($target, $rv);
1412 :     }
1413 : parrello 1.32 }
1414 :     }
1415 :     # Return the result.
1416 :     return $retVal;
1417 :     }
1418 :    
1419 : parrello 1.70 =head3 SortNeeded
1420 :    
1421 :     C<< my $parms = $erdb->SortNeeded($relationName); >>
1422 :    
1423 :     Return the pipe command for the sort that should be applied to the specified
1424 :     relation when creating the load file.
1425 :    
1426 :     For example, if the load file should be sorted ascending by the first
1427 :     field, this method would return
1428 :    
1429 :     sort -k1 -t"\t"
1430 :    
1431 :     If the first field is numeric, the method would return
1432 :    
1433 :     sort -k1n -t"\t"
1434 :    
1435 :     Unfortunately, due to a bug in the C<sort> command, we cannot eliminate duplicate
1436 :     keys using a sort.
1437 :    
1438 :     =over 4
1439 :    
1440 :     =item relationName
1441 :    
1442 :     Name of the relation to be examined.
1443 :    
1444 :     =item
1445 :    
1446 :     Returns the sort command to use for sorting the relation, suitable for piping.
1447 :    
1448 :     =back
1449 :    
1450 :     =cut
1451 :     #: Return Type $;
1452 :     sub SortNeeded {
1453 :     # Get the parameters.
1454 :     my ($self, $relationName) = @_;
1455 :     # Declare a descriptor to hold the names of the key fields.
1456 :     my @keyNames = ();
1457 :     # Get the relation structure.
1458 :     my $relationData = $self->_FindRelation($relationName);
1459 :     # Find out if the relation is a primary entity relation,
1460 :     # a relationship relation, or a secondary entity relation.
1461 :     my $entityTable = $self->{_metaData}->{Entities};
1462 :     my $relationshipTable = $self->{_metaData}->{Relationships};
1463 :     if (exists $entityTable->{$relationName}) {
1464 :     # Here we have a primary entity relation.
1465 :     push @keyNames, "id";
1466 :     } elsif (exists $relationshipTable->{$relationName}) {
1467 :     # Here we have a relationship. We sort using the FROM index.
1468 :     my $relationshipData = $relationshipTable->{$relationName};
1469 :     my $index = $relationData->{Indexes}->{"idx${relationName}From"};
1470 :     push @keyNames, @{$index->{IndexFields}};
1471 :     } else {
1472 :     # Here we have a secondary entity relation, so we have a sort on the ID field.
1473 :     push @keyNames, "id";
1474 :     }
1475 :     # Now we parse the key names into sort parameters. First, we prime the return
1476 :     # string.
1477 :     my $retVal = "sort -t\"\t\" ";
1478 :     # Get the relation's field list.
1479 :     my @fields = @{$relationData->{Fields}};
1480 :     # Loop through the keys.
1481 :     for my $keyData (@keyNames) {
1482 :     # Get the key and the ordering.
1483 :     my ($keyName, $ordering);
1484 :     if ($keyData =~ /^([^ ]+) DESC/) {
1485 :     ($keyName, $ordering) = ($1, "descending");
1486 :     } else {
1487 :     ($keyName, $ordering) = ($keyData, "ascending");
1488 :     }
1489 :     # Find the key's position and type.
1490 :     my $fieldSpec;
1491 :     for (my $i = 0; $i <= $#fields && ! $fieldSpec; $i++) {
1492 :     my $thisField = $fields[$i];
1493 :     if ($thisField->{name} eq $keyName) {
1494 :     # Get the sort modifier for this field type. The modifier
1495 :     # decides whether we're using a character, numeric, or
1496 :     # floating-point sort.
1497 :     my $modifier = $TypeTable{$thisField->{type}}->{sort};
1498 :     # If the index is descending for this field, denote we want
1499 :     # to reverse the sort order on this field.
1500 :     if ($ordering eq 'descending') {
1501 :     $modifier .= "r";
1502 :     }
1503 :     # Store the position and modifier into the field spec, which
1504 :     # will stop the inner loop. Note that the field number is
1505 :     # 1-based in the sort command, so we have to increment the
1506 :     # index.
1507 :     $fieldSpec = ($i + 1) . $modifier;
1508 :     }
1509 :     }
1510 :     # Add this field to the sort command.
1511 :     $retVal .= " -k$fieldSpec";
1512 :     }
1513 :     # Return the result.
1514 :     return $retVal;
1515 :     }
1516 :    
1517 : parrello 1.6 =head3 GetList
1518 :    
1519 : parrello 1.45 C<< my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, \@params); >>
1520 : parrello 1.6
1521 :     Return a list of object descriptors for the specified objects as determined by the
1522 :     specified filter clause.
1523 :    
1524 :     This method is essentially the same as L</Get> except it returns a list of objects rather
1525 : parrello 1.7 than a query object that can be used to get the results one record at a time.
1526 : parrello 1.6
1527 :     =over 4
1528 :    
1529 :     =item objectNames
1530 :    
1531 :     List containing the names of the entity and relationship objects to be retrieved.
1532 :    
1533 :     =item filterClause
1534 :    
1535 :     WHERE clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
1536 :     be parameterized with parameter markers (C<?>). Each field used in the WHERE clause must be
1537 :     specified in the standard form B<I<objectName>(I<fieldName>)>. Any parameters specified
1538 :     in the filter clause should be added to the parameter list as additional parameters. The
1539 :     fields in a filter clause can come from primary entity relations, relationship relations,
1540 :     or secondary entity relations; however, all of the entities and relationships involved must
1541 :     be included in the list of object names.
1542 :    
1543 :     The filter clause can also specify a sort order. To do this, simply follow the filter string
1544 :     with an ORDER BY clause. For example, the following filter string gets all genomes for a
1545 :     particular genus and sorts them by species name.
1546 :    
1547 :     C<< "Genome(genus) = ? ORDER BY Genome(species)" >>
1548 :    
1549 :     The rules for field references in a sort order are the same as those for field references in the
1550 :     filter clause in general; however, odd things may happen if a sort field is from a secondary
1551 :     relation.
1552 :    
1553 : parrello 1.45 =item params
1554 : parrello 1.6
1555 : parrello 1.45 Reference to a list of parameter values to be substituted into the filter clause.
1556 : parrello 1.6
1557 :     =item RETURN
1558 :    
1559 :     Returns a list of B<DBObject>s that satisfy the query conditions.
1560 :    
1561 :     =back
1562 :    
1563 :     =cut
1564 :     #: Return Type @%
1565 :     sub GetList {
1566 :     # Get the parameters.
1567 : parrello 1.45 my ($self, $objectNames, $filterClause, $params) = @_;
1568 : parrello 1.10 # Declare the return variable.
1569 :     my @retVal = ();
1570 :     # Perform the query.
1571 : parrello 1.45 my $query = $self->Get($objectNames, $filterClause, $params);
1572 : parrello 1.10 # Loop through the results.
1573 :     while (my $object = $query->Fetch) {
1574 :     push @retVal, $object;
1575 :     }
1576 : parrello 1.6 # Return the result.
1577 :     return @retVal;
1578 :     }
1579 :    
1580 : parrello 1.45 =head3 GetCount
1581 :    
1582 :     C<< my $count = $erdb->GetCount(\@objectNames, $filter, \@params); >>
1583 :    
1584 :     Return the number of rows found by a specified query. This method would
1585 :     normally be used to count the records in a single table. For example, in a
1586 :     genetics database
1587 :    
1588 :     my $count = $erdb->GetCount(['Genome'], 'Genome(genus-species) LIKE ?', ['homo %']);
1589 :    
1590 :     would return the number of genomes for the genus I<homo>. It is conceivable, however,
1591 :     to use it to return records based on a join. For example,
1592 :    
1593 : parrello 1.47 my $count = $erdb->GetCount(['HasFeature', 'Genome'], 'Genome(genus-species) LIKE ?',
1594 : parrello 1.45 ['homo %']);
1595 :    
1596 :     would return the number of features for genomes in the genus I<homo>. Note that
1597 :     only the rows from the first table are counted. If the above command were
1598 :    
1599 :     my $count = $erdb->GetCount(['Genome', 'Feature'], 'Genome(genus-species) LIKE ?',
1600 :     ['homo %']);
1601 :    
1602 :     it would return the number of genomes, not the number of genome/feature pairs.
1603 :    
1604 :     =over 4
1605 :    
1606 :     =item objectNames
1607 :    
1608 :     Reference to a list of the objects (entities and relationships) included in the
1609 :     query.
1610 :    
1611 :     =item filter
1612 :    
1613 :     A filter clause for restricting the query. The rules are the same as for the L</Get>
1614 :     method.
1615 :    
1616 :     =item params
1617 :    
1618 :     Reference to a list of the parameter values to be substituted for the parameter marks
1619 :     in the filter.
1620 :    
1621 :     =item RETURN
1622 :    
1623 :     Returns a count of the number of records in the first table that would satisfy
1624 :     the query.
1625 :    
1626 :     =back
1627 :    
1628 :     =cut
1629 :    
1630 :     sub GetCount {
1631 :     # Get the parameters.
1632 :     my ($self, $objectNames, $filter, $params) = @_;
1633 : parrello 1.66 # Insure the params argument is an array reference if the caller left it off.
1634 :     if (! defined($params)) {
1635 :     $params = [];
1636 :     }
1637 : parrello 1.45 # Declare the return variable.
1638 :     my $retVal;
1639 : parrello 1.47 # Find out if we're counting an entity or a relationship.
1640 :     my $countedField;
1641 :     if ($self->IsEntity($objectNames->[0])) {
1642 :     $countedField = "id";
1643 :     } else {
1644 :     # For a relationship we count the to-link because it's usually more
1645 :     # numerous. Note we're automatically converting to the SQL form
1646 :     # of the field name (to_link vs. to-link).
1647 :     $countedField = "to_link";
1648 :     }
1649 : parrello 1.45 # Create the SQL command suffix to get the desired records.
1650 :     my ($suffix, $mappedNameListRef, $mappedNameHashRef) = $self->_SetupSQL($objectNames,
1651 :     $filter);
1652 :     # Prefix it with text telling it we want a record count.
1653 :     my $firstObject = $mappedNameListRef->[0];
1654 : parrello 1.47 my $command = "SELECT COUNT($firstObject.$countedField) $suffix";
1655 : parrello 1.45 # Prepare and execute the command.
1656 :     my $sth = $self->_GetStatementHandle($command, $params);
1657 :     # Get the count value.
1658 :     ($retVal) = $sth->fetchrow_array();
1659 :     # Check for a problem.
1660 :     if (! defined($retVal)) {
1661 :     if ($sth->err) {
1662 :     # Here we had an SQL error.
1663 :     Confess("Error retrieving row count: " . $sth->errstr());
1664 :     } else {
1665 :     # Here we have no result.
1666 :     Confess("No result attempting to retrieve row count.");
1667 :     }
1668 :     }
1669 :     # Return the result.
1670 :     return $retVal;
1671 :     }
1672 :    
1673 : parrello 1.1 =head3 ComputeObjectSentence
1674 :    
1675 : parrello 1.18 C<< my $sentence = $erdb->ComputeObjectSentence($objectName); >>
1676 : parrello 1.1
1677 :     Check an object name, and if it is a relationship convert it to a relationship sentence.
1678 :    
1679 :     =over 4
1680 :    
1681 :     =item objectName
1682 :    
1683 :     Name of the entity or relationship.
1684 :    
1685 :     =item RETURN
1686 :    
1687 :     Returns a string containing the entity name or a relationship sentence.
1688 :    
1689 :     =back
1690 :    
1691 :     =cut
1692 :    
1693 :     sub ComputeObjectSentence {
1694 : parrello 1.10 # Get the parameters.
1695 :     my ($self, $objectName) = @_;
1696 :     # Set the default return value.
1697 :     my $retVal = $objectName;
1698 :     # Look for the object as a relationship.
1699 :     my $relTable = $self->{_metaData}->{Relationships};
1700 :     if (exists $relTable->{$objectName}) {
1701 :     # Get the relationship sentence.
1702 :     $retVal = _ComputeRelationshipSentence($objectName, $relTable->{$objectName});
1703 :     }
1704 :     # Return the result.
1705 :     return $retVal;
1706 : parrello 1.1 }
1707 :    
1708 :     =head3 DumpRelations
1709 :    
1710 : parrello 1.18 C<< $erdb->DumpRelations($outputDirectory); >>
1711 : parrello 1.1
1712 :     Write the contents of all the relations to tab-delimited files in the specified directory.
1713 :     Each file will have the same name as the relation dumped, with an extension of DTX.
1714 :    
1715 :     =over 4
1716 :    
1717 :     =item outputDirectory
1718 :    
1719 :     Name of the directory into which the relation files should be dumped.
1720 :    
1721 :     =back
1722 :    
1723 :     =cut
1724 :    
1725 :     sub DumpRelations {
1726 : parrello 1.10 # Get the parameters.
1727 :     my ($self, $outputDirectory) = @_;
1728 :     # Now we need to run through all the relations. First, we loop through the entities.
1729 :     my $metaData = $self->{_metaData};
1730 :     my $entities = $metaData->{Entities};
1731 :     for my $entityName (keys %{$entities}) {
1732 :     my $entityStructure = $entities->{$entityName};
1733 :     # Get the entity's relations.
1734 :     my $relationList = $entityStructure->{Relations};
1735 :     # Loop through the relations, dumping them.
1736 :     for my $relationName (keys %{$relationList}) {
1737 :     my $relation = $relationList->{$relationName};
1738 :     $self->_DumpRelation($outputDirectory, $relationName, $relation);
1739 :     }
1740 :     }
1741 :     # Next, we loop through the relationships.
1742 :     my $relationships = $metaData->{Relationships};
1743 :     for my $relationshipName (keys %{$relationships}) {
1744 :     my $relationshipStructure = $relationships->{$relationshipName};
1745 :     # Dump this relationship's relation.
1746 :     $self->_DumpRelation($outputDirectory, $relationshipName, $relationshipStructure->{Relations}->{$relationshipName});
1747 :     }
1748 : parrello 1.1 }
1749 :    
1750 : parrello 1.51 =head3 InsertValue
1751 :    
1752 :     C<< $erdb->InsertValue($entityID, $fieldName, $value); >>
1753 :    
1754 :     This method will insert a new value into the database. The value must be one
1755 :     associated with a secondary relation, since primary values cannot be inserted:
1756 :     they occur exactly once. Secondary values, on the other hand, can be missing
1757 :     or multiply-occurring.
1758 :    
1759 :     =over 4
1760 :    
1761 :     =item entityID
1762 :    
1763 :     ID of the object that is to receive the new value.
1764 :    
1765 :     =item fieldName
1766 :    
1767 :     Field name for the new value-- this includes the entity name, since
1768 :     field names are of the format I<objectName>C<(>I<fieldName>C<)>.
1769 :    
1770 :     =item value
1771 :    
1772 :     New value to be put in the field.
1773 :    
1774 :     =back
1775 :    
1776 :     =cut
1777 :    
1778 :     sub InsertValue {
1779 :     # Get the parameters.
1780 :     my ($self, $entityID, $fieldName, $value) = @_;
1781 :     # Parse the entity name and the real field name.
1782 :     if ($fieldName =~ /^([^(]+)\(([^)]+)\)/) {
1783 :     my $entityName = $1;
1784 :     my $fieldTitle = $2;
1785 :     # Get its descriptor.
1786 :     if (!$self->IsEntity($entityName)) {
1787 :     Confess("$entityName is not a valid entity.");
1788 :     } else {
1789 :     my $entityData = $self->{_metaData}->{Entities}->{$entityName};
1790 :     # Find the relation containing this field.
1791 :     my $fieldHash = $entityData->{Fields};
1792 : parrello 1.52 if (! exists $fieldHash->{$fieldTitle}) {
1793 : parrello 1.51 Confess("$fieldTitle not found in $entityName.");
1794 :     } else {
1795 :     my $relation = $fieldHash->{$fieldTitle}->{relation};
1796 :     if ($relation eq $entityName) {
1797 :     Confess("Cannot do InsertValue on primary field $fieldTitle of $entityName.");
1798 :     } else {
1799 :     # Now we can create an INSERT statement.
1800 :     my $dbh = $self->{_dbh};
1801 :     my $fixedName = _FixName($fieldTitle);
1802 : parrello 1.53 my $statement = "INSERT INTO $relation (id, $fixedName) VALUES(?, ?)";
1803 : parrello 1.51 # Execute the command.
1804 :     $dbh->SQL($statement, 0, $entityID, $value);
1805 :     }
1806 :     }
1807 :     }
1808 :     } else {
1809 :     Confess("$fieldName is not a valid field name.");
1810 :     }
1811 :     }
1812 :    
1813 : parrello 1.1 =head3 InsertObject
1814 :    
1815 : parrello 1.18 C<< my $ok = $erdb->InsertObject($objectType, \%fieldHash); >>
1816 : parrello 1.1
1817 :     Insert an object into the database. The object is defined by a type name and then a hash
1818 :     of field names to values. Field values in the primary relation are represented by scalars.
1819 :     (Note that for relationships, the primary relation is the B<only> relation.)
1820 :     Field values for the other relations comprising the entity are always list references. For
1821 :     example, the following line inserts an inactive PEG feature named C<fig|188.1.peg.1> with aliases
1822 :     C<ZP_00210270.1> and C<gi|46206278>.
1823 :    
1824 : parrello 1.18 C<< $erdb->InsertObject('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >>
1825 : parrello 1.1
1826 :     The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
1827 :     property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
1828 :    
1829 : parrello 1.57 C<< $erdb->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>
1830 : parrello 1.1
1831 :     =over 4
1832 :    
1833 :     =item newObjectType
1834 :    
1835 :     Type name of the object to insert.
1836 :    
1837 :     =item fieldHash
1838 :    
1839 :     Hash of field names to values.
1840 :    
1841 :     =item RETURN
1842 :    
1843 :     Returns 1 if successful, 0 if an error occurred.
1844 :    
1845 :     =back
1846 :    
1847 :     =cut
1848 :    
1849 :     sub InsertObject {
1850 : parrello 1.10 # Get the parameters.
1851 :     my ($self, $newObjectType, $fieldHash) = @_;
1852 :     # Denote that so far we appear successful.
1853 :     my $retVal = 1;
1854 :     # Get the database handle.
1855 :     my $dbh = $self->{_dbh};
1856 :     # Get the relation list.
1857 :     my $relationTable = $self->_GetRelationTable($newObjectType);
1858 :     # Loop through the relations. We'll build insert statements for each one. If a relation is
1859 :     # secondary, we may end up generating multiple insert statements. If an error occurs, we
1860 :     # stop the loop.
1861 :     my @relationList = keys %{$relationTable};
1862 :     for (my $i = 0; $retVal && $i <= $#relationList; $i++) {
1863 :     my $relationName = $relationList[$i];
1864 :     my $relationDefinition = $relationTable->{$relationName};
1865 :     # Get the relation's fields. For each field we will collect a value in the corresponding
1866 :     # position of the @valueList array. If one of the fields is missing, we will add it to the
1867 :     # @missing list.
1868 :     my @fieldList = @{$relationDefinition->{Fields}};
1869 :     my @fieldNameList = ();
1870 :     my @valueList = ();
1871 :     my @missing = ();
1872 :     my $recordCount = 1;
1873 :     for my $fieldDescriptor (@fieldList) {
1874 :     # Get the field name and save it. Note we need to fix it up so the hyphens
1875 :     # are converted to underscores.
1876 :     my $fieldName = $fieldDescriptor->{name};
1877 :     push @fieldNameList, _FixName($fieldName);
1878 :     # Look for the named field in the incoming structure. Note that we are looking
1879 :     # for the real field name, not the fixed-up one!
1880 :     if (exists $fieldHash->{$fieldName}) {
1881 :     # Here we found the field. Stash it in the value list.
1882 :     my $value = $fieldHash->{$fieldName};
1883 :     push @valueList, $value;
1884 :     # If the value is a list, we may need to increment the record count.
1885 :     if (ref $value eq "ARRAY") {
1886 :     my $thisCount = @{$value};
1887 :     if ($recordCount == 1) {
1888 :     # Here we have our first list, so we save its count.
1889 :     $recordCount = $thisCount;
1890 :     } elsif ($recordCount != $thisCount) {
1891 :     # Here we have a second list, so its length has to match the
1892 :     # previous lists.
1893 :     Trace("Field $value in new $newObjectType object has an invalid list length $thisCount. Expected $recordCount.") if T(0);
1894 :     $retVal = 0;
1895 :     }
1896 :     }
1897 :     } else {
1898 :     # Here the field is not present. Flag it as missing.
1899 :     push @missing, $fieldName;
1900 :     }
1901 :     }
1902 :     # If we are the primary relation, add the new-record flag.
1903 :     if ($relationName eq $newObjectType) {
1904 :     push @valueList, 1;
1905 :     push @fieldNameList, "new_record";
1906 :     }
1907 :     # Only proceed if there are no missing fields.
1908 :     if (@missing > 0) {
1909 :     Trace("Relation $relationName for $newObjectType skipped due to missing fields: " .
1910 :     join(' ', @missing)) if T(1);
1911 :     } else {
1912 :     # Build the INSERT statement.
1913 :     my $statement = "INSERT INTO $relationName (" . join (', ', @fieldNameList) .
1914 :     ") VALUES (";
1915 :     # Create a marker list of the proper size and put it in the statement.
1916 :     my @markers = ();
1917 :     while (@markers < @fieldNameList) { push @markers, '?'; }
1918 :     $statement .= join(', ', @markers) . ")";
1919 :     # We have the insert statement, so prepare it.
1920 :     my $sth = $dbh->prepare_command($statement);
1921 :     Trace("Insert statement prepared: $statement") if T(3);
1922 :     # Now we loop through the values. If a value is scalar, we use it unmodified. If it's
1923 :     # a list, we use the current element. The values are stored in the @parameterList array.
1924 :     my $done = 0;
1925 :     for (my $i = 0; $i < $recordCount; $i++) {
1926 :     # Clear the parameter list array.
1927 :     my @parameterList = ();
1928 :     # Loop through the values.
1929 :     for my $value (@valueList) {
1930 :     # Check to see if this is a scalar value.
1931 :     if (ref $value eq "ARRAY") {
1932 :     # Here we have a list value. Pull the current entry.
1933 :     push @parameterList, $value->[$i];
1934 :     } else {
1935 :     # Here we have a scalar value. Use it unmodified.
1936 :     push @parameterList, $value;
1937 :     }
1938 :     }
1939 :     # Execute the INSERT statement with the specified parameter list.
1940 :     $retVal = $sth->execute(@parameterList);
1941 :     if (!$retVal) {
1942 :     my $errorString = $sth->errstr();
1943 :     Trace("Insert error: $errorString.") if T(0);
1944 :     }
1945 :     }
1946 :     }
1947 :     }
1948 :     # Return the success indicator.
1949 :     return $retVal;
1950 : parrello 1.1 }
1951 :    
1952 :     =head3 LoadTable
1953 :    
1954 : parrello 1.18 C<< my %results = $erdb->LoadTable($fileName, $relationName, $truncateFlag); >>
1955 : parrello 1.1
1956 : parrello 1.9 Load data from a tab-delimited file into a specified table, optionally re-creating the table
1957 :     first.
1958 : parrello 1.1
1959 :     =over 4
1960 :    
1961 :     =item fileName
1962 :    
1963 :     Name of the file from which the table data should be loaded.
1964 :    
1965 :     =item relationName
1966 :    
1967 :     Name of the relation to be loaded. This is the same as the table name.
1968 :    
1969 :     =item truncateFlag
1970 :    
1971 :     TRUE if the table should be dropped and re-created, else FALSE
1972 :    
1973 :     =item RETURN
1974 :    
1975 : parrello 1.28 Returns a statistical object containing a list of the error messages.
1976 : parrello 1.1
1977 :     =back
1978 :    
1979 :     =cut
1980 :     sub LoadTable {
1981 : parrello 1.10 # Get the parameters.
1982 :     my ($self, $fileName, $relationName, $truncateFlag) = @_;
1983 :     # Create the statistical return object.
1984 :     my $retVal = _GetLoadStats();
1985 :     # Trace the fact of the load.
1986 :     Trace("Loading table $relationName from $fileName") if T(2);
1987 :     # Get the database handle.
1988 :     my $dbh = $self->{_dbh};
1989 : parrello 1.22 # Get the input file size.
1990 :     my $fileSize = -s $fileName;
1991 : parrello 1.10 # Get the relation data.
1992 :     my $relation = $self->_FindRelation($relationName);
1993 :     # Check the truncation flag.
1994 :     if ($truncateFlag) {
1995 :     Trace("Creating table $relationName") if T(2);
1996 : parrello 1.19 # Compute the row count estimate. We take the size of the load file,
1997 :     # divide it by the estimated row size, and then multiply by 1.5 to
1998 :     # leave extra room. We postulate a minimum row count of 1000 to
1999 :     # prevent problems with incoming empty load files.
2000 :     my $rowSize = $self->EstimateRowSize($relationName);
2001 :     my $estimate = FIG::max($fileSize * 1.5 / $rowSize, 1000);
2002 : parrello 1.10 # Re-create the table without its index.
2003 : parrello 1.19 $self->CreateTable($relationName, 0, $estimate);
2004 : parrello 1.10 # If this is a pre-index DBMS, create the index here.
2005 :     if ($dbh->{_preIndex}) {
2006 :     eval {
2007 :     $self->CreateIndex($relationName);
2008 :     };
2009 :     if ($@) {
2010 :     $retVal->AddMessage($@);
2011 :     }
2012 :     }
2013 :     }
2014 : parrello 1.3 # Load the table.
2015 : parrello 1.10 my $rv;
2016 :     eval {
2017 : parrello 1.20 $rv = $dbh->load_table(file => $fileName, tbl => $relationName);
2018 : parrello 1.10 };
2019 :     if (!defined $rv) {
2020 : parrello 1.3 $retVal->AddMessage($@) if ($@);
2021 : parrello 1.20 $retVal->AddMessage("Table load failed for $relationName using $fileName.");
2022 : parrello 1.10 Trace("Table load failed for $relationName.") if T(1);
2023 :     } else {
2024 : parrello 1.22 # Here we successfully loaded the table.
2025 :     $retVal->Add("tables");
2026 :     my $size = -s $fileName;
2027 :     Trace("$size bytes loaded into $relationName.") if T(2);
2028 : parrello 1.10 # If we're rebuilding, we need to create the table indexes.
2029 : parrello 1.70 if ($truncateFlag) {
2030 :     # Indexes are created here for PostGres. For PostGres, indexes are
2031 :     # best built at the end. For MySQL, the reverse is true.
2032 :     if (! $dbh->{_preIndex}) {
2033 :     eval {
2034 :     $self->CreateIndex($relationName);
2035 :     };
2036 :     if ($@) {
2037 :     $retVal->AddMessage($@);
2038 :     }
2039 :     }
2040 :     # The full-text index (if any) is always built last, even for MySQL.
2041 :     # First we need to see if this table has a full-text index. Only
2042 :     # primary relations are allowed that privilege.
2043 :     if ($self->_IsPrimary($relationName)) {
2044 :     # Get the relation's entity/relationship structure.
2045 :     my $structure = $self->_GetStructure($relationName);
2046 :     # Check for a searchable fields list.
2047 :     if (exists $structure->{searchFields}) {
2048 :     # Here we know that we need to create a full-text search index.
2049 :     # Get an SQL-formatted field name list.
2050 :     my $fields = join(", ", $self->_FixNames(@{$structure->{searchFields}}));
2051 :     # Create the index.
2052 :     $dbh->create_index(tbl => $relationName, idx => "search_idx_$relationName",
2053 :     flds => $fields, kind => 'fulltext');
2054 :     }
2055 : parrello 1.10 }
2056 :     }
2057 :     }
2058 : parrello 1.20 # Analyze the table to improve performance.
2059 : parrello 1.61 Trace("Analyzing and compacting $relationName.") if T(3);
2060 : olson 1.16 $dbh->vacuum_it($relationName);
2061 : parrello 1.61 Trace("$relationName load completed.") if T(3);
2062 : parrello 1.10 # Return the statistics.
2063 :     return $retVal;
2064 : parrello 1.1 }
2065 :    
2066 :     =head3 GenerateEntity
2067 :    
2068 : parrello 1.18 C<< my $fieldHash = $erdb->GenerateEntity($id, $type, \%values); >>
2069 : parrello 1.1
2070 :     Generate the data for a new entity instance. This method creates a field hash suitable for
2071 :     passing as a parameter to L</InsertObject>. The ID is specified by the callr, but the rest
2072 :     of the fields are generated using information in the database schema.
2073 :    
2074 :     Each data type has a default algorithm for generating random test data. This can be overridden
2075 :     by including a B<DataGen> element in the field. If this happens, the content of the element is
2076 :     executed as a PERL program in the context of this module. The element may make use of a C<$this>
2077 :     variable which contains the field hash as it has been built up to the current point. If any
2078 :     fields are dependent on other fields, the C<pass> attribute can be used to control the order
2079 :     in which the fields are generated. A field with a high data pass number will be generated after
2080 :     a field with a lower one. If any external values are needed, they should be passed in via the
2081 :     optional third parameter, which will be available to the data generation script under the name
2082 :     C<$value>. Several useful utility methods are provided for generating random values, including
2083 :     L</IntGen>, L</StringGen>, L</FloatGen>, and L</DateGen>. Note that dates are stored and generated
2084 :     in the form of a timestamp number rather than a string.
2085 :    
2086 :     =over 4
2087 :    
2088 :     =item id
2089 :    
2090 :     ID to assign to the new entity.
2091 :    
2092 :     =item type
2093 :    
2094 :     Type name for the new entity.
2095 :    
2096 :     =item values
2097 :    
2098 :     Hash containing additional values that might be needed by the data generation methods (optional).
2099 :    
2100 :     =back
2101 :    
2102 :     =cut
2103 :    
2104 :     sub GenerateEntity {
2105 : parrello 1.10 # Get the parameters.
2106 :     my ($self, $id, $type, $values) = @_;
2107 :     # Create the return hash.
2108 :     my $this = { id => $id };
2109 :     # Get the metadata structure.
2110 :     my $metadata = $self->{_metaData};
2111 :     # Get this entity's list of fields.
2112 :     if (!exists $metadata->{Entities}->{$type}) {
2113 :     Confess("Unrecognized entity type $type in GenerateEntity.");
2114 :     } else {
2115 :     my $entity = $metadata->{Entities}->{$type};
2116 :     my $fields = $entity->{Fields};
2117 :     # Generate data from the fields.
2118 :     _GenerateFields($this, $fields, $type, $values);
2119 :     }
2120 :     # Return the hash created.
2121 :     return $this;
2122 : parrello 1.1 }
2123 :    
2124 : parrello 1.6 =head3 GetEntity
2125 :    
2126 : parrello 1.18 C<< my $entityObject = $erdb->GetEntity($entityType, $ID); >>
2127 : parrello 1.6
2128 :     Return an object describing the entity instance with a specified ID.
2129 :    
2130 :     =over 4
2131 :    
2132 :     =item entityType
2133 :    
2134 :     Entity type name.
2135 :    
2136 :     =item ID
2137 :    
2138 :     ID of the desired entity.
2139 :    
2140 :     =item RETURN
2141 :    
2142 :     Returns a B<DBObject> representing the desired entity instance, or an undefined value if no
2143 :     instance is found with the specified key.
2144 :    
2145 :     =back
2146 :    
2147 :     =cut
2148 :    
2149 :     sub GetEntity {
2150 : parrello 1.10 # Get the parameters.
2151 :     my ($self, $entityType, $ID) = @_;
2152 :     # Create a query.
2153 : parrello 1.45 my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);
2154 : parrello 1.10 # Get the first (and only) object.
2155 :     my $retVal = $query->Fetch();
2156 :     # Return the result.
2157 :     return $retVal;
2158 : parrello 1.6 }
2159 :    
2160 : parrello 1.69 =head3 GetChoices
2161 :    
2162 :     C<< my @values = $erdb->GetChoices($entityName, $fieldName); >>
2163 :    
2164 :     Return a list of all the values for the specified field that are represented in the
2165 :     specified entity.
2166 :    
2167 :     Note that if the field is not indexed, then this will be a very slow operation.
2168 :    
2169 :     =over 4
2170 :    
2171 :     =item entityName
2172 :    
2173 :     Name of an entity in the database.
2174 :    
2175 :     =item fieldName
2176 :    
2177 :     Name of a field belonging to the entity. This is a raw field name without
2178 :     the standard parenthesized notation used in most calls.
2179 :    
2180 :     =item RETURN
2181 :    
2182 :     Returns a list of the distinct values for the specified field in the database.
2183 :    
2184 :     =back
2185 :    
2186 :     =cut
2187 :    
2188 :     sub GetChoices {
2189 :     # Get the parameters.
2190 :     my ($self, $entityName, $fieldName) = @_;
2191 :     # Declare the return variable.
2192 :     my @retVal;
2193 :     # Get the entity data structure.
2194 :     my $entityData = $self->_GetStructure($entityName);
2195 :     # Get the field.
2196 :     my $fieldHash = $entityData->{Fields};
2197 :     if (! exists $fieldHash->{$fieldName}) {
2198 :     Confess("$fieldName not found in $entityName.");
2199 :     } else {
2200 :     # Get the name of the relation containing the field.
2201 :     my $relation = $fieldHash->{$fieldName}->{relation};
2202 :     # Fix up the field name.
2203 :     my $realName = _FixName($fieldName);
2204 :     # Get the database handle.
2205 :     my $dbh = $self->{_dbh};
2206 :     # Query the database.
2207 :     my $results = $dbh->SQL("SELECT DISTINCT $realName FROM $relation");
2208 :     # Clean the results. They are stored as a list of lists, and we just want the one list.
2209 :     @retVal = sort map { $_->[0] } @{$results};
2210 :     }
2211 :     # Return the result.
2212 :     return @retVal;
2213 :     }
2214 :    
2215 : parrello 1.6 =head3 GetEntityValues
2216 :    
2217 : parrello 1.18 C<< my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); >>
2218 : parrello 1.6
2219 : parrello 1.68 Return a list of values from a specified entity instance. If the entity instance
2220 :     does not exist, an empty list is returned.
2221 : parrello 1.6
2222 :     =over 4
2223 :    
2224 :     =item entityType
2225 :    
2226 :     Entity type name.
2227 :    
2228 :     =item ID
2229 :    
2230 :     ID of the desired entity.
2231 :    
2232 :     =item fields
2233 :    
2234 :     List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.
2235 :    
2236 :     =item RETURN
2237 :    
2238 :     Returns a flattened list of the values of the specified fields for the specified entity.
2239 :    
2240 :     =back
2241 :    
2242 :     =cut
2243 :    
2244 :     sub GetEntityValues {
2245 : parrello 1.10 # Get the parameters.
2246 :     my ($self, $entityType, $ID, $fields) = @_;
2247 :     # Get the specified entity.
2248 :     my $entity = $self->GetEntity($entityType, $ID);
2249 :     # Declare the return list.
2250 :     my @retVal = ();
2251 :     # If we found the entity, push the values into the return list.
2252 :     if ($entity) {
2253 :     push @retVal, $entity->Values($fields);
2254 :     }
2255 :     # Return the result.
2256 :     return @retVal;
2257 : parrello 1.6 }
2258 : parrello 1.1
2259 : parrello 1.7 =head3 GetAll
2260 :    
2261 : parrello 1.18 C<< my @list = $erdb->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >>
2262 : parrello 1.7
2263 :     Return a list of values taken from the objects returned by a query. The first three
2264 :     parameters correspond to the parameters of the L</Get> method. The final parameter is
2265 :     a list of the fields desired from each record found by the query. The field name
2266 :     syntax is the standard syntax used for fields in the B<ERDB> system--
2267 :     B<I<objectName>(I<fieldName>)>-- where I<objectName> is the name of the relevant entity
2268 :     or relationship and I<fieldName> is the name of the field.
2269 :    
2270 :     The list returned will be a list of lists. Each element of the list will contain
2271 :     the values returned for the fields specified in the fourth parameter. If one of the
2272 :     fields specified returns multiple values, they are flattened in with the rest. For
2273 :     example, the following call will return a list of the features in a particular
2274 :     spreadsheet cell, and each feature will be represented by a list containing the
2275 :     feature ID followed by all of its aliases.
2276 :    
2277 : parrello 1.18 C<< $query = $erdb->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >>
2278 : parrello 1.7
2279 :     =over 4
2280 :    
2281 :     =item objectNames
2282 :    
2283 :     List containing the names of the entity and relationship objects to be retrieved.
2284 :    
2285 :     =item filterClause
2286 :    
2287 :     WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
2288 :     be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form
2289 :     B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the
2290 :     parameter list as additional parameters. The fields in a filter clause can come from primary
2291 :     entity relations, relationship relations, or secondary entity relations; however, all of the
2292 :     entities and relationships involved must be included in the list of object names.
2293 :    
2294 :     =item parameterList
2295 :    
2296 :     List of the parameters to be substituted in for the parameters marks in the filter clause.
2297 :    
2298 :     =item fields
2299 :    
2300 :     List of the fields to be returned in each element of the list returned.
2301 :    
2302 :     =item count
2303 :    
2304 :     Maximum number of records to return. If omitted or 0, all available records will be returned.
2305 :    
2306 :     =item RETURN
2307 :    
2308 :     Returns a list of list references. Each element of the return list contains the values for the
2309 :     fields specified in the B<fields> parameter.
2310 :    
2311 :     =back
2312 :    
2313 :     =cut
2314 :     #: Return Type @@;
2315 :     sub GetAll {
2316 : parrello 1.10 # Get the parameters.
2317 :     my ($self, $objectNames, $filterClause, $parameterList, $fields, $count) = @_;
2318 :     # Translate the parameters from a list reference to a list. If the parameter
2319 :     # list is a scalar we convert it into a singleton list.
2320 :     my @parmList = ();
2321 :     if (ref $parameterList eq "ARRAY") {
2322 : parrello 1.45 Trace("GetAll parm list is an array.") if T(4);
2323 : parrello 1.10 @parmList = @{$parameterList};
2324 :     } else {
2325 : parrello 1.45 Trace("GetAll parm list is a scalar: $parameterList.") if T(4);
2326 : parrello 1.10 push @parmList, $parameterList;
2327 :     }
2328 : parrello 1.30 # Insure the counter has a value.
2329 :     if (!defined $count) {
2330 :     $count = 0;
2331 :     }
2332 :     # Add the row limit to the filter clause.
2333 :     if ($count > 0) {
2334 :     $filterClause .= " LIMIT $count";
2335 :     }
2336 : parrello 1.10 # Create the query.
2337 : parrello 1.45 my $query = $self->Get($objectNames, $filterClause, \@parmList);
2338 : parrello 1.10 # Set up a counter of the number of records read.
2339 :     my $fetched = 0;
2340 :     # Loop through the records returned, extracting the fields. Note that if the
2341 :     # counter is non-zero, we stop when the number of records read hits the count.
2342 :     my @retVal = ();
2343 :     while (($count == 0 || $fetched < $count) && (my $row = $query->Fetch())) {
2344 :     my @rowData = $row->Values($fields);
2345 :     push @retVal, \@rowData;
2346 :     $fetched++;
2347 :     }
2348 : parrello 1.54 Trace("$fetched rows returned in GetAll.") if T(SQL => 4);
2349 : parrello 1.10 # Return the resulting list.
2350 :     return @retVal;
2351 : parrello 1.7 }
2352 :    
2353 : parrello 1.55 =head3 Exists
2354 :    
2355 :     C<< my $found = $sprout->Exists($entityName, $entityID); >>
2356 :    
2357 :     Return TRUE if an entity exists, else FALSE.
2358 :    
2359 :     =over 4
2360 :    
2361 :     =item entityName
2362 :    
2363 :     Name of the entity type (e.g. C<Feature>) relevant to the existence check.
2364 :    
2365 :     =item entityID
2366 :    
2367 :     ID of the entity instance whose existence is to be checked.
2368 :    
2369 :     =item RETURN
2370 :    
2371 :     Returns TRUE if the entity instance exists, else FALSE.
2372 :    
2373 :     =back
2374 :    
2375 :     =cut
2376 :     #: Return Type $;
2377 :     sub Exists {
2378 :     # Get the parameters.
2379 :     my ($self, $entityName, $entityID) = @_;
2380 :     # Check for the entity instance.
2381 :     Trace("Checking existence of $entityName with ID=$entityID.") if T(4);
2382 :     my $testInstance = $self->GetEntity($entityName, $entityID);
2383 :     # Return an existence indicator.
2384 :     my $retVal = ($testInstance ? 1 : 0);
2385 :     return $retVal;
2386 :     }
2387 :    
2388 : parrello 1.18 =head3 EstimateRowSize
2389 :    
2390 :     C<< my $rowSize = $erdb->EstimateRowSize($relName); >>
2391 :    
2392 :     Estimate the row size of the specified relation. The estimated row size is computed by adding
2393 :     up the average length for each data type.
2394 :    
2395 :     =over 4
2396 :    
2397 :     =item relName
2398 :    
2399 :     Name of the relation whose estimated row size is desired.
2400 :    
2401 :     =item RETURN
2402 :    
2403 :     Returns an estimate of the row size for the specified relation.
2404 :    
2405 :     =back
2406 :    
2407 :     =cut
2408 :     #: Return Type $;
2409 :     sub EstimateRowSize {
2410 :     # Get the parameters.
2411 :     my ($self, $relName) = @_;
2412 :     # Declare the return variable.
2413 :     my $retVal = 0;
2414 :     # Find the relation descriptor.
2415 :     my $relation = $self->_FindRelation($relName);
2416 :     # Get the list of fields.
2417 :     for my $fieldData (@{$relation->{Fields}}) {
2418 :     # Get the field type and add its length.
2419 :     my $fieldLen = $TypeTable{$fieldData->{type}}->{avgLen};
2420 :     $retVal += $fieldLen;
2421 :     }
2422 :     # Return the result.
2423 :     return $retVal;
2424 :     }
2425 :    
2426 : parrello 1.38 =head3 GetFieldTable
2427 :    
2428 :     C<< my $fieldHash = $self->GetFieldTable($objectnName); >>
2429 :    
2430 :     Get the field structure for a specified entity or relationship.
2431 :    
2432 :     =over 4
2433 :    
2434 :     =item objectName
2435 :    
2436 :     Name of the desired entity or relationship.
2437 :    
2438 :     =item RETURN
2439 :    
2440 :     The table containing the field descriptors for the specified object.
2441 :    
2442 :     =back
2443 :    
2444 :     =cut
2445 :    
2446 :     sub GetFieldTable {
2447 :     # Get the parameters.
2448 :     my ($self, $objectName) = @_;
2449 :     # Get the descriptor from the metadata.
2450 :     my $objectData = $self->_GetStructure($objectName);
2451 :     # Return the object's field table.
2452 :     return $objectData->{Fields};
2453 :     }
2454 :    
2455 : parrello 1.48 =head2 Data Mining Methods
2456 :    
2457 : parrello 1.38 =head3 GetUsefulCrossValues
2458 :    
2459 :     C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>
2460 :    
2461 :     Return a list of the useful attributes that would be returned by a B<Cross> call
2462 :     from an entity of the source entity type through the specified relationship. This
2463 :     means it will return the fields of the target entity type and the intersection data
2464 :     fields in the relationship. Only primary table fields are returned. In other words,
2465 :     the field names returned will be for fields where there is always one and only one
2466 :     value.
2467 :    
2468 :     =over 4
2469 :    
2470 :     =item sourceEntity
2471 :    
2472 :     Name of the entity from which the relationship crossing will start.
2473 :    
2474 :     =item relationship
2475 :    
2476 :     Name of the relationship being crossed.
2477 :    
2478 :     =item RETURN
2479 :    
2480 :     Returns a list of field names in Sprout field format (I<objectName>C<(>I<fieldName>C<)>.
2481 :    
2482 :     =back
2483 :    
2484 :     =cut
2485 :     #: Return Type @;
2486 :     sub GetUsefulCrossValues {
2487 :     # Get the parameters.
2488 :     my ($self, $sourceEntity, $relationship) = @_;
2489 :     # Declare the return variable.
2490 :     my @retVal = ();
2491 :     # Determine the target entity for the relationship. This is whichever entity is not
2492 :     # the source entity. So, if the source entity is the FROM, we'll get the name of
2493 :     # the TO, and vice versa.
2494 :     my $relStructure = $self->_GetStructure($relationship);
2495 :     my $targetEntityType = ($relStructure->{from} eq $sourceEntity ? "to" : "from");
2496 :     my $targetEntity = $relStructure->{$targetEntityType};
2497 :     # Get the field table for the entity.
2498 :     my $entityFields = $self->GetFieldTable($targetEntity);
2499 :     # The field table is a hash. The hash key is the field name. The hash value is a structure.
2500 :     # For the entity fields, the key aspect of the target structure is that the {relation} value
2501 :     # must match the entity name.
2502 :     my @fieldList = map { "$targetEntity($_)" } grep { $entityFields->{$_}->{relation} eq $targetEntity }
2503 :     keys %{$entityFields};
2504 :     # Push the fields found onto the return variable.
2505 :     push @retVal, sort @fieldList;
2506 :     # Get the field table for the relationship.
2507 :     my $relationshipFields = $self->GetFieldTable($relationship);
2508 :     # Here we have a different rule. We want all the fields other than "from-link" and "to-link".
2509 :     # This may end up being an empty set.
2510 :     my @fieldList2 = map { "$relationship($_)" } grep { $_ ne "from-link" && $_ ne "to-link" }
2511 :     keys %{$relationshipFields};
2512 :     # Push these onto the return list.
2513 :     push @retVal, sort @fieldList2;
2514 :     # Return the result.
2515 :     return @retVal;
2516 :     }
2517 :    
2518 : parrello 1.48 =head3 FindColumn
2519 :    
2520 :     C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >>
2521 :    
2522 :     Return the location a desired column in a data mining header line. The data
2523 :     mining header line is a tab-separated list of column names. The column
2524 :     identifier is either the numerical index of a column or the actual column
2525 :     name.
2526 :    
2527 :     =over 4
2528 :    
2529 :     =item headerLine
2530 :    
2531 :     The header line from a data mining command, which consists of a tab-separated
2532 :     list of column names.
2533 :    
2534 :     =item columnIdentifier
2535 :    
2536 :     Either the ordinal number of the desired column (1-based), or the name of the
2537 :     desired column.
2538 :    
2539 :     =item RETURN
2540 :    
2541 :     Returns the array index (0-based) of the desired column.
2542 :    
2543 :     =back
2544 :    
2545 :     =cut
2546 :    
2547 :     sub FindColumn {
2548 :     # Get the parameters.
2549 :     my ($headerLine, $columnIdentifier) = @_;
2550 :     # Declare the return variable.
2551 :     my $retVal;
2552 :     # Split the header line into column names.
2553 :     my @headers = ParseColumns($headerLine);
2554 :     # Determine whether we have a number or a name.
2555 :     if ($columnIdentifier =~ /^\d+$/) {
2556 :     # Here we have a number. Subtract 1 and validate the result.
2557 :     $retVal = $columnIdentifier - 1;
2558 :     if ($retVal < 0 || $retVal > $#headers) {
2559 :     Confess("Invalid column identifer \"$columnIdentifier\": value out of range.");
2560 :     }
2561 :     } else {
2562 :     # Here we have a name. We need to find it in the list.
2563 :     for (my $i = 0; $i <= $#headers && ! defined($retVal); $i++) {
2564 :     if ($headers[$i] eq $columnIdentifier) {
2565 :     $retVal = $i;
2566 :     }
2567 :     }
2568 :     if (! defined($retVal)) {
2569 :     Confess("Invalid column identifier \"$columnIdentifier\": value not found.");
2570 :     }
2571 :     }
2572 :     # Return the result.
2573 :     return $retVal;
2574 :     }
2575 :    
2576 :     =head3 ParseColumns
2577 :    
2578 : parrello 1.50 C<< my @columns = ERDB::ParseColumns($line); >>
2579 : parrello 1.48
2580 :     Convert the specified data line to a list of columns.
2581 :    
2582 :     =over 4
2583 :    
2584 :     =item line
2585 :    
2586 :     A data mining input, consisting of a tab-separated list of columns terminated by a
2587 :     new-line.
2588 :    
2589 :     =item RETURN
2590 :    
2591 :     Returns a list consisting of the column values.
2592 :    
2593 :     =back
2594 :    
2595 :     =cut
2596 :    
2597 :     sub ParseColumns {
2598 :     # Get the parameters.
2599 : parrello 1.50 my ($line) = @_;
2600 : parrello 1.48 # Chop off the line-end.
2601 :     chomp $line;
2602 :     # Split it into a list.
2603 :     my @retVal = split(/\t/, $line);
2604 :     # Return the result.
2605 :     return @retVal;
2606 :     }
2607 :    
2608 : parrello 1.71 =head2 Virtual Methods
2609 :    
2610 :     =head3 CleanKeywords
2611 :    
2612 :     C<< my $cleanedString = $erdb->CleanKeywords($searchExpression); >>
2613 :    
2614 :     Clean up a search expression or keyword list. This is a virtual method that may
2615 :     be overridden by the subclass. The base-class method removes extra spaces
2616 :     and converts everything to lower case.
2617 :    
2618 :     =over 4
2619 :    
2620 :     =item searchExpression
2621 :    
2622 :     Search expression or keyword list to clean. Note that a search expression may
2623 :     contain boolean operators which need to be preserved. This includes leading
2624 :     minus signs.
2625 :    
2626 :     =item RETURN
2627 :    
2628 :     Cleaned expression or keyword list.
2629 :    
2630 :     =back
2631 :    
2632 :     =cut
2633 :    
2634 :     sub CleanKeywords {
2635 :     # Get the parameters.
2636 :     my ($self, $searchExpression) = @_;
2637 :     # Lower-case the expression and copy it into the return variable. Note that we insure we
2638 :     # don't accidentally end up with an undefined value.
2639 :     my $retVal = lc($searchExpression || "");
2640 :     # Remove extra spaces.
2641 :     $retVal =~ s/\s+/ /g;
2642 :     $retVal =~ s/(^\s+)|(\s+$)//g;
2643 :     # Return the result.
2644 :     return $retVal;
2645 :     }
2646 :    
2647 : parrello 1.1 =head2 Internal Utility Methods
2648 :    
2649 : parrello 1.70 =head3 _RelationMap
2650 :    
2651 :     C<< my @relationMap = _RelationMap($mappedNameHashRef, $mappedNameListRef); >>
2652 :    
2653 :     Create the relation map for an SQL query. The relation map is used by B<DBObject>
2654 :     to determine how to interpret the results of the query.
2655 :    
2656 :     =over 4
2657 :    
2658 :     =item mappedNameHashRef
2659 :    
2660 :     Reference to a hash that maps modified object names to real object names.
2661 :    
2662 :     =item mappedNameListRef
2663 :    
2664 :     Reference to a list of modified object names in the order they appear in the
2665 :     SELECT list.
2666 :    
2667 :     =item RETURN
2668 :    
2669 :     Returns a list of 2-tuples. Each tuple consists of an object name as used in the
2670 :     query followed by the actual name of that object. This enables the B<DBObject> to
2671 :     determine the order of the tables in the query and which object name belongs to each
2672 :     mapped object name. Most of the time these two values are the same; however, if a
2673 :     relation occurs twice in the query, the relation name in the field list and WHERE
2674 :     clause will use a mapped name (generally the actual relation name with a numeric
2675 :     suffix) that does not match the actual relation name.
2676 :    
2677 :     =back
2678 :    
2679 :     =cut
2680 :    
2681 :     sub _RelationMap {
2682 :     # Get the parameters.
2683 :     my ($mappedNameHashRef, $mappedNameListRef) = @_;
2684 :     # Declare the return variable.
2685 :     my @retVal = ();
2686 :     # Build the map.
2687 :     for my $mappedName (@{$mappedNameListRef}) {
2688 :     push @retVal, [$mappedName, $mappedNameHashRef->{$mappedName}];
2689 :     }
2690 :     # Return it.
2691 :     return @retVal;
2692 :     }
2693 :    
2694 :    
2695 :     =head3 _SetupSQL
2696 : parrello 1.45
2697 :     Process a list of object names and a filter clause so that they can be used to
2698 :     build an SQL statement. This method takes in a reference to a list of object names
2699 :     and a filter clause. It will return a corrected filter clause, a list of mapped
2700 :     names and the mapped name hash.
2701 :    
2702 :     This is an instance method.
2703 :    
2704 :     =over 4
2705 :    
2706 :     =item objectNames
2707 :    
2708 :     Reference to a list of the object names to be included in the query.
2709 :    
2710 :     =item filterClause
2711 :    
2712 :     A string containing the WHERE clause for the query (without the C<WHERE>) and also
2713 :     optionally the C<ORDER BY> and C<LIMIT> clauses.
2714 :    
2715 : parrello 1.70 =item matchClause
2716 :    
2717 :     An optional full-text search clause. If specified, it will be inserted at the
2718 :     front of the WHERE clause. It should already be SQL-formatted; that is, the
2719 :     field names should be in the form I<table>C<.>I<fieldName>.
2720 :    
2721 : parrello 1.45 =item RETURN
2722 :    
2723 :     Returns a three-element list. The first element is the SQL statement suffix, beginning
2724 :     with the FROM clause. The second element is a reference to a list of the names to be
2725 :     used in retrieving the fields. The third element is a hash mapping the names to the
2726 :     objects they represent.
2727 :    
2728 :     =back
2729 :    
2730 :     =cut
2731 :    
2732 :     sub _SetupSQL {
2733 : parrello 1.70 my ($self, $objectNames, $filterClause, $matchClause) = @_;
2734 : parrello 1.45 # Adjust the list of object names to account for multiple occurrences of the
2735 :     # same object. We start with a hash table keyed on object name that will
2736 :     # return the object suffix. The first time an object is encountered it will
2737 :     # not be found in the hash. The next time the hash will map the object name
2738 :     # to 2, then 3, and so forth.
2739 :     my %objectHash = ();
2740 :     # This list will contain the object names as they are to appear in the
2741 :     # FROM list.
2742 :     my @fromList = ();
2743 :     # This list contains the suffixed object name for each object. It is exactly
2744 :     # parallel to the list in the $objectNames parameter.
2745 :     my @mappedNameList = ();
2746 :     # Finally, this hash translates from a mapped name to its original object name.
2747 :     my %mappedNameHash = ();
2748 :     # Now we create the lists. Note that for every single name we push something into
2749 :     # @fromList and @mappedNameList. This insures that those two arrays are exactly
2750 :     # parallel to $objectNames.
2751 :     for my $objectName (@{$objectNames}) {
2752 :     # Get the next suffix for this object.
2753 :     my $suffix = $objectHash{$objectName};
2754 :     if (! $suffix) {
2755 :     # Here we are seeing the object for the first time. The object name
2756 :     # is used as is.
2757 :     push @mappedNameList, $objectName;
2758 :     push @fromList, $objectName;
2759 :     $mappedNameHash{$objectName} = $objectName;
2760 :     # Denote the next suffix will be 2.
2761 :     $objectHash{$objectName} = 2;
2762 :     } else {
2763 :     # Here we've seen the object before. We construct a new name using
2764 :     # the suffix from the hash and update the hash.
2765 :     my $mappedName = "$objectName$suffix";
2766 :     $objectHash{$objectName} = $suffix + 1;
2767 :     # The FROM list has the object name followed by the mapped name. This
2768 :     # tells SQL it's still the same table, but we're using a different name
2769 :     # for it to avoid confusion.
2770 :     push @fromList, "$objectName $mappedName";
2771 :     # The mapped-name list contains the real mapped name.
2772 :     push @mappedNameList, $mappedName;
2773 :     # Finally, enable us to get back from the mapped name to the object name.
2774 :     $mappedNameHash{$mappedName} = $objectName;
2775 :     }
2776 :     }
2777 :     # Begin the SELECT suffix. It starts with
2778 :     #
2779 :     # FROM name1, name2, ... nameN
2780 :     #
2781 :     my $suffix = "FROM " . join(', ', @fromList);
2782 : parrello 1.70 # Now for the WHERE. First, we need a place for the filter string.
2783 :     my $filterString = "";
2784 :     # We will also keep a list of conditions to add to the WHERE clause in order to link
2785 :     # entities and relationships as well as primary relations to secondary ones.
2786 :     my @joinWhere = ();
2787 : parrello 1.45 # Check for a filter clause.
2788 :     if ($filterClause) {
2789 :     # Here we have one, so we convert its field names and add it to the query. First,
2790 :     # We create a copy of the filter string we can work with.
2791 : parrello 1.70 $filterString = $filterClause;
2792 : parrello 1.45 # Next, we sort the object names by length. This helps protect us from finding
2793 :     # object names inside other object names when we're doing our search and replace.
2794 :     my @sortedNames = sort { length($b) - length($a) } @mappedNameList;
2795 :     # The final preparatory step is to create a hash table of relation names. The
2796 :     # table begins with the relation names already in the SELECT command. We may
2797 :     # need to add relations later if there is filtering on a field in a secondary
2798 :     # relation. The secondary relations are the ones that contain multiply-
2799 :     # occurring or optional fields.
2800 :     my %fromNames = map { $_ => 1 } @sortedNames;
2801 :     # We are ready to begin. We loop through the object names, replacing each
2802 :     # object name's field references by the corresponding SQL field reference.
2803 :     # Along the way, if we find a secondary relation, we will need to add it
2804 :     # to the FROM clause.
2805 :     for my $mappedName (@sortedNames) {
2806 :     # Get the length of the object name plus 2. This is the value we add to the
2807 :     # size of the field name to determine the size of the field reference as a
2808 :     # whole.
2809 :     my $nameLength = 2 + length $mappedName;
2810 :     # Get the real object name for this mapped name.
2811 :     my $objectName = $mappedNameHash{$mappedName};
2812 :     Trace("Processing $mappedName for object $objectName.") if T(4);
2813 :     # Get the object's field list.
2814 :     my $fieldList = $self->GetFieldTable($objectName);
2815 :     # Find the field references for this object.
2816 :     while ($filterString =~ m/$mappedName\(([^)]*)\)/g) {
2817 :     # At this point, $1 contains the field name, and the current position
2818 :     # is set immediately after the final parenthesis. We pull out the name of
2819 :     # the field and the position and length of the field reference as a whole.
2820 :     my $fieldName = $1;
2821 :     my $len = $nameLength + length $fieldName;
2822 :     my $pos = pos($filterString) - $len;
2823 :     # Insure the field exists.
2824 :     if (!exists $fieldList->{$fieldName}) {
2825 :     Confess("Field $fieldName not found for object $objectName.");
2826 :     } else {
2827 :     Trace("Processing $fieldName at position $pos.") if T(4);
2828 :     # Get the field's relation.
2829 :     my $relationName = $fieldList->{$fieldName}->{relation};
2830 :     # Now we have a secondary relation. We need to insure it matches the
2831 :     # mapped name of the primary relation. First we peel off the suffix
2832 :     # from the mapped name.
2833 :     my $mappingSuffix = substr $mappedName, length($objectName);
2834 :     # Put the mapping suffix onto the relation name to get the
2835 :     # mapped relation name.
2836 :     my $mappedRelationName = "$relationName$mappingSuffix";
2837 :     # Insure the relation is in the FROM clause.
2838 :     if (!exists $fromNames{$mappedRelationName}) {
2839 :     # Add the relation to the FROM clause.
2840 :     if ($mappedRelationName eq $relationName) {
2841 :     # The name is un-mapped, so we add it without
2842 :     # any frills.
2843 :     $suffix .= ", $relationName";
2844 :     push @joinWhere, "$objectName.id = $relationName.id";
2845 :     } else {
2846 :     # Here we have a mapping situation.
2847 :     $suffix .= ", $relationName $mappedRelationName";
2848 :     push @joinWhere, "$mappedRelationName.id = $mappedName.id";
2849 :     }
2850 :     # Denote we have this relation available for future fields.
2851 :     $fromNames{$mappedRelationName} = 1;
2852 :     }
2853 :     # Form an SQL field reference from the relation name and the field name.
2854 :     my $sqlReference = "$mappedRelationName." . _FixName($fieldName);
2855 :     # Put it into the filter string in place of the old value.
2856 :     substr($filterString, $pos, $len) = $sqlReference;
2857 :     # Reposition the search.
2858 :     pos $filterString = $pos + length $sqlReference;
2859 :     }
2860 :     }
2861 :     }
2862 : parrello 1.70 }
2863 :     # The next step is to join the objects together. We only need to do this if there
2864 :     # is more than one object in the object list. We start with the first object and
2865 :     # run through the objects after it. Note also that we make a safety copy of the
2866 :     # list before running through it, because we shift off the first object before
2867 :     # processing the rest.
2868 :     my @mappedObjectList = @mappedNameList;
2869 :     my $lastMappedObject = shift @mappedObjectList;
2870 :     # Get the join table.
2871 :     my $joinTable = $self->{_metaData}->{Joins};
2872 :     # Loop through the object list.
2873 :     for my $thisMappedObject (@mappedObjectList) {
2874 :     # Look for a join using the real object names.
2875 :     my $lastObject = $mappedNameHash{$lastMappedObject};
2876 :     my $thisObject = $mappedNameHash{$thisMappedObject};
2877 :     my $joinKey = "$lastObject/$thisObject";
2878 :     if (!exists $joinTable->{$joinKey}) {
2879 :     # Here there's no join, so we throw an error.
2880 :     Confess("No join exists to connect from $lastMappedObject to $thisMappedObject.");
2881 :     } else {
2882 :     # Get the join clause.
2883 :     my $unMappedJoin = $joinTable->{$joinKey};
2884 :     # Fix the names.
2885 :     $unMappedJoin =~ s/$lastObject/$lastMappedObject/;
2886 :     $unMappedJoin =~ s/$thisObject/$thisMappedObject/;
2887 :     push @joinWhere, $unMappedJoin;
2888 :     # Save this object as the last object for the next iteration.
2889 :     $lastMappedObject = $thisMappedObject;
2890 :     }
2891 :     }
2892 :     # Now we need to handle the whole ORDER BY / LIMIT thing. The important part
2893 :     # here is we want the filter clause to be empty if there's no WHERE filter.
2894 :     # We'll put the ORDER BY / LIMIT clauses in the following variable.
2895 :     my $orderClause = "";
2896 :     # This is only necessary if we have a filter string in which the ORDER BY
2897 :     # and LIMIT clauses can live.
2898 :     if ($filterString) {
2899 : parrello 1.45 # Locate the ORDER BY or LIMIT verbs (if any). We use a non-greedy
2900 :     # operator so that we find the first occurrence of either verb.
2901 :     if ($filterString =~ m/^(.*?)\s*(ORDER BY|LIMIT)/g) {
2902 :     # Here we have an ORDER BY or LIMIT verb. Split it off of the filter string.
2903 :     my $pos = pos $filterString;
2904 :     $orderClause = $2 . substr($filterString, $pos);
2905 :     $filterString = $1;
2906 :     }
2907 : parrello 1.70 }
2908 :     # All the things that are supposed to be in the WHERE clause of the
2909 :     # SELECT command need to be put into @joinWhere so we can string them
2910 :     # together. We begin with the match clause. This is important,
2911 :     # because the match clause's parameter mark must precede any parameter
2912 :     # marks in the filter string.
2913 :     if ($matchClause) {
2914 :     push @joinWhere, $matchClause;
2915 :     }
2916 :     # Add the filter string. We put it in parentheses to avoid operator
2917 :     # precedence problems with the match clause or the joins.
2918 :     if ($filterString) {
2919 :     Trace("Filter string is \"$filterString\".") if T(4);
2920 :     push @joinWhere, "($filterString)";
2921 :     }
2922 :     # String it all together into a big filter clause.
2923 :     if (@joinWhere) {
2924 :     $suffix .= " WHERE " . join(' AND ', @joinWhere);
2925 :     }
2926 :     # Add the sort or limit clause (if any).
2927 :     if ($orderClause) {
2928 :     $suffix .= " $orderClause";
2929 : parrello 1.45 }
2930 :     # Return the suffix, the mapped name list, and the mapped name hash.
2931 :     return ($suffix, \@mappedNameList, \%mappedNameHash);
2932 :     }
2933 :    
2934 : parrello 1.70 =head3 _GetStatementHandle
2935 : parrello 1.45
2936 :     This method will prepare and execute an SQL query, returning the statement handle.
2937 :     The main reason for doing this here is so that everybody who does SQL queries gets
2938 :     the benefit of tracing.
2939 :    
2940 :     This is an instance method.
2941 :    
2942 :     =over 4
2943 :    
2944 :     =item command
2945 :    
2946 :     Command to prepare and execute.
2947 :    
2948 :     =item params
2949 :    
2950 :     Reference to a list of the values to be substituted in for the parameter marks.
2951 :    
2952 :     =item RETURN
2953 :    
2954 :     Returns a prepared and executed statement handle from which the caller can extract
2955 :     results.
2956 :    
2957 :     =back
2958 :    
2959 :     =cut
2960 :    
2961 :     sub _GetStatementHandle {
2962 :     # Get the parameters.
2963 :     my ($self, $command, $params) = @_;
2964 :     # Trace the query.
2965 :     Trace("SQL query: $command") if T(SQL => 3);
2966 :     Trace("PARMS: '" . (join "', '", @{$params}) . "'") if (T(SQL => 4) && (@{$params} > 0));
2967 :     # Get the database handle.
2968 :     my $dbh = $self->{_dbh};
2969 :     # Prepare the command.
2970 :     my $sth = $dbh->prepare_command($command);
2971 :     # Execute it with the parameters bound in.
2972 :     $sth->execute(@{$params}) || Confess("SELECT error" . $sth->errstr());
2973 :     # Return the statement handle.
2974 :     return $sth;
2975 :     }
2976 :    
2977 : parrello 1.70 =head3 _GetLoadStats
2978 : parrello 1.1
2979 :     Return a blank statistics object for use by the load methods.
2980 :    
2981 :     This is a static method.
2982 :    
2983 :     =cut
2984 :    
2985 : parrello 1.29 sub _GetLoadStats{
2986 : parrello 1.28 return Stats->new();
2987 : parrello 1.1 }
2988 :    
2989 : parrello 1.70 =head3 _GenerateFields
2990 : parrello 1.1
2991 :     Generate field values from a field structure and store in a specified table. The field names
2992 :     are first sorted by pass count, certain pre-defined fields are removed from the list, and
2993 :     then we rip through them evaluation the data generation string. Fields in the primary relation
2994 :     are stored as scalars; fields in secondary relations are stored as value lists.
2995 :    
2996 :     This is a static method.
2997 :    
2998 :     =over 4
2999 :    
3000 :     =item this
3001 :    
3002 :     Hash table into which the field values should be placed.
3003 :    
3004 :     =item fields
3005 :    
3006 :     Field structure from which the field descriptors should be taken.
3007 :    
3008 :     =item type
3009 :    
3010 :     Type name of the object whose fields are being generated.
3011 :    
3012 :     =item values (optional)
3013 :    
3014 :     Reference to a value structure from which additional values can be taken.
3015 :    
3016 :     =item from (optiona)
3017 :    
3018 :     Reference to the source entity instance if relationship data is being generated.
3019 :    
3020 :     =item to (optional)
3021 :    
3022 :     Reference to the target entity instance if relationship data is being generated.
3023 :    
3024 :     =back
3025 :    
3026 :     =cut
3027 :    
3028 :     sub _GenerateFields {
3029 : parrello 1.10 # Get the parameters.
3030 :     my ($this, $fields, $type, $values, $from, $to) = @_;
3031 :     # Sort the field names by pass number.
3032 :     my @fieldNames = sort { $fields->{$a}->{DataGen}->{pass} <=> $fields->{$b}->{DataGen}->{pass} } keys %{$fields};
3033 :     # Loop through the field names, generating data.
3034 :     for my $name (@fieldNames) {
3035 :     # Only proceed if this field needs to be generated.
3036 :     if (!exists $this->{$name}) {
3037 :     # Get this field's data generation descriptor.
3038 :     my $fieldDescriptor = $fields->{$name};
3039 :     my $data = $fieldDescriptor->{DataGen};
3040 :     # Get the code to generate the field value.
3041 :     my $codeString = $data->{content};
3042 :     # Determine whether or not this field is in the primary relation.
3043 :     if ($fieldDescriptor->{relation} eq $type) {
3044 :     # Here we have a primary relation field. Store the field value as
3045 :     # a scalar.
3046 :     $this->{$name} = eval($codeString);
3047 :     } else {
3048 :     # Here we have a secondary relation field. Create a null list
3049 :     # and push the desired number of field values onto it.
3050 :     my @fieldValues = ();
3051 :     my $count = IntGen(0,$data->{testCount});
3052 :     for (my $i = 0; $i < $count; $i++) {
3053 :     my $newValue = eval($codeString);
3054 :     push @fieldValues, $newValue;
3055 :     }
3056 :     # Store the value list in the main hash.
3057 :     $this->{$name} = \@fieldValues;
3058 :     }
3059 :     }
3060 :     }
3061 : parrello 1.1 }
3062 :    
3063 : parrello 1.70 =head3 _DumpRelation
3064 : parrello 1.1
3065 :     Dump the specified relation's to the specified output file in tab-delimited format.
3066 :    
3067 :     This is an instance method.
3068 :    
3069 :     =over 4
3070 :    
3071 :     =item outputDirectory
3072 :    
3073 :     Directory to contain the output file.
3074 :    
3075 :     =item relationName
3076 :    
3077 :     Name of the relation to dump.
3078 :    
3079 :     =item relation
3080 :    
3081 :     Structure describing the relation to be dumped.
3082 :    
3083 :     =back
3084 :    
3085 :     =cut
3086 :    
3087 :     sub _DumpRelation {
3088 : parrello 1.10 # Get the parameters.
3089 :     my ($self, $outputDirectory, $relationName, $relation) = @_;
3090 :     # Open the output file.
3091 :     my $fileName = "$outputDirectory/$relationName.dtx";
3092 :     open(DTXOUT, ">$fileName") || Confess("Could not open dump file $fileName: $!");
3093 :     # Create a query for the specified relation.
3094 :     my $dbh = $self->{_dbh};
3095 :     my $query = $dbh->prepare_command("SELECT * FROM $relationName");
3096 :     # Execute the query.
3097 :     $query->execute() || Confess("SELECT error dumping $relationName.");
3098 :     # Loop through the results.
3099 :     while (my @row = $query->fetchrow) {
3100 :     # Escape any tabs or new-lines in the row text.
3101 :     for my $field (@row) {
3102 :     $field =~ s/\n/\\n/g;
3103 :     $field =~ s/\t/\\t/g;
3104 :     }
3105 :     # Tab-join the row and write it to the output file.
3106 :     my $rowText = join("\t", @row);
3107 :     print DTXOUT "$rowText\n";
3108 :     }
3109 :     # Close the output file.
3110 :     close DTXOUT;
3111 : parrello 1.1 }
3112 :    
3113 : parrello 1.70 =head3 _GetStructure
3114 : parrello 1.1
3115 :     Get the data structure for a specified entity or relationship.
3116 :    
3117 :     This is an instance method.
3118 :    
3119 :     =over 4
3120 :    
3121 :     =item objectName
3122 :    
3123 :     Name of the desired entity or relationship.
3124 :    
3125 :     =item RETURN
3126 :    
3127 :     The descriptor for the specified object.
3128 :    
3129 :     =back
3130 :    
3131 :     =cut
3132 :    
3133 :     sub _GetStructure {
3134 : parrello 1.10 # Get the parameters.
3135 :     my ($self, $objectName) = @_;
3136 :     # Get the metadata structure.
3137 :     my $metadata = $self->{_metaData};
3138 :     # Declare the variable to receive the descriptor.
3139 :     my $retVal;
3140 :     # Get the descriptor from the metadata.
3141 :     if (exists $metadata->{Entities}->{$objectName}) {
3142 :     $retVal = $metadata->{Entities}->{$objectName};
3143 :     } elsif (exists $metadata->{Relationships}->{$objectName}) {
3144 :     $retVal = $metadata->{Relationships}->{$objectName};
3145 :     } else {
3146 :     Confess("Object $objectName not found in database.");
3147 :     }
3148 :     # Return the descriptor.
3149 :     return $retVal;
3150 : parrello 1.1 }
3151 :    
3152 : parrello 1.70
3153 :    
3154 :     =head3 _GetRelationTable
3155 : parrello 1.1
3156 :     Get the list of relations for a specified entity or relationship.
3157 :    
3158 :     This is an instance method.
3159 :    
3160 :     =over 4
3161 :    
3162 :     =item objectName
3163 :    
3164 :     Name of the desired entity or relationship.
3165 :    
3166 :     =item RETURN
3167 :    
3168 :     A table containing the relations for the specified object.
3169 :    
3170 :     =back
3171 :    
3172 :     =cut
3173 :    
3174 :     sub _GetRelationTable {
3175 : parrello 1.10 # Get the parameters.
3176 :     my ($self, $objectName) = @_;
3177 :     # Get the descriptor from the metadata.
3178 :     my $objectData = $self->_GetStructure($objectName);
3179 :     # Return the object's relation list.
3180 :     return $objectData->{Relations};
3181 : parrello 1.1 }
3182 :    
3183 : parrello 1.70 =head3 _ValidateFieldNames
3184 : parrello 1.1
3185 :     Determine whether or not the field names are valid. A description of the problems with the names
3186 :     will be written to the standard error output. If there is an error, this method will abort. This is
3187 :     a static method.
3188 :    
3189 :     =over 4
3190 :    
3191 :     =item metadata
3192 :    
3193 :     Metadata structure loaded from the XML data definition.
3194 :    
3195 :     =back
3196 :    
3197 :     =cut
3198 :    
3199 :     sub _ValidateFieldNames {
3200 : parrello 1.10 # Get the object.
3201 :     my ($metadata) = @_;
3202 :     # Declare the return value. We assume success.
3203 :     my $retVal = 1;
3204 :     # Loop through the sections of the database definition.
3205 :     for my $section ('Entities', 'Relationships') {
3206 :     # Loop through the objects in this section.
3207 :     for my $object (values %{$metadata->{$section}}) {
3208 :     # Loop through the object's fields.
3209 :     for my $fieldName (keys %{$object->{Fields}}) {
3210 :     # Now we make some initial validations.
3211 :     if ($fieldName =~ /--/) {
3212 :     # Here we have a doubled minus sign.
3213 :     print STDERR "Field name $fieldName has a doubled hyphen.\n";
3214 :     $retVal = 0;
3215 :     } elsif ($fieldName !~ /^[A-Za-z]/) {
3216 :     # Here the field name is missing the initial letter.
3217 :     print STDERR "Field name $fieldName does not begin with a letter.\n";
3218 :     $retVal = 0;
3219 :     } else {
3220 :     # Strip out the minus signs. Everything remaining must be a letter
3221 :     # or digit.
3222 :     my $strippedName = $fieldName;
3223 :     $strippedName =~ s/-//g;
3224 :     if ($strippedName !~ /^[A-Za-z0-9]+$/) {
3225 :     print STDERR "Field name $fieldName contains illegal characters.\n";
3226 :     $retVal = 0;
3227 :     }
3228 :     }
3229 :     }
3230 :     }
3231 :     }
3232 :     # If an error was found, fail.
3233 :     if ($retVal == 0) {
3234 :     Confess("Errors found in field names.");
3235 :     }
3236 : parrello 1.1 }
3237 :    
3238 : parrello 1.70 =head3 _LoadRelation
3239 : parrello 1.1
3240 :     Load a relation from the data in a tab-delimited disk file. The load will only take place if a disk
3241 :     file with the same name as the relation exists in the specified directory.
3242 :    
3243 :     This is an instance method.
3244 :    
3245 :     =over 4
3246 :    
3247 :     =item dbh
3248 :    
3249 :     DBKernel object for accessing the database.
3250 :    
3251 :     =item directoryName
3252 :    
3253 :     Name of the directory containing the tab-delimited data files.
3254 :    
3255 :     =item relationName
3256 :    
3257 :     Name of the relation to load.
3258 :    
3259 :     =item rebuild
3260 :    
3261 :     TRUE if the table should be dropped and re-created before loading.
3262 :    
3263 :     =item RETURN
3264 :    
3265 :     Returns a statistical object describing the number of records read and a list of error messages.
3266 :    
3267 :     =back
3268 :    
3269 :     =cut
3270 :    
3271 :     sub _LoadRelation {
3272 : parrello 1.10 # Get the parameters.
3273 :     my ($self, $directoryName, $relationName, $rebuild) = @_;
3274 :     # Create the file name.
3275 :     my $fileName = "$directoryName/$relationName";
3276 :     # If the file doesn't exist, try adding the .dtx suffix.
3277 :     if (! -e $fileName) {
3278 :     $fileName .= ".dtx";
3279 :     if (! -e $fileName) {
3280 :     $fileName = "";
3281 :     }
3282 :     }
3283 :     # Create the return object.
3284 :     my $retVal = _GetLoadStats();
3285 :     # If a file exists to load the table, its name will be in $fileName. Otherwise, $fileName will
3286 :     # be a null string.
3287 :     if ($fileName ne "") {
3288 :     # Load the relation from the file.
3289 :     $retVal = $self->LoadTable($fileName, $relationName, $rebuild);
3290 :     } elsif ($rebuild) {
3291 :     # Here we are rebuilding, but no file exists, so we just re-create the table.
3292 :     $self->CreateTable($relationName, 1);
3293 :     }
3294 :     # Return the statistics from the load.
3295 :     return $retVal;
3296 : parrello 1.1 }
3297 :    
3298 : parrello 1.70 =head3 _LoadMetaData
3299 : parrello 1.1
3300 :     This method loads the data describing this database from an XML file into a metadata structure.
3301 :     The resulting structure is a set of nested hash tables containing all the information needed to
3302 :     load or use the database. The schema for the XML file is F<ERDatabase.xml>.
3303 :    
3304 :     This is a static method.
3305 :    
3306 :     =over 4
3307 :    
3308 :     =item filename
3309 :    
3310 :     Name of the file containing the database definition.
3311 :    
3312 :     =item RETURN
3313 :    
3314 :     Returns a structure describing the database.
3315 :    
3316 :     =back
3317 :    
3318 :     =cut
3319 :    
3320 :     sub _LoadMetaData {
3321 : parrello 1.10 # Get the parameters.
3322 :     my ($filename) = @_;
3323 : parrello 1.15 Trace("Reading Sprout DBD from $filename.") if T(2);
3324 : parrello 1.10 # Slurp the XML file into a variable. Extensive use of options is used to insure we
3325 :     # get the exact structure we want.
3326 :     my $metadata = XML::Simple::XMLin($filename,
3327 :     GroupTags => { Relationships => 'Relationship',
3328 :     Entities => 'Entity',
3329 :     Fields => 'Field',
3330 :     Indexes => 'Index',
3331 :     IndexFields => 'IndexField'},
3332 :     KeyAttr => { Relationship => 'name',
3333 :     Entity => 'name',
3334 :     Field => 'name'},
3335 :     ForceArray => ['Field', 'Index', 'IndexField'],
3336 :     ForceContent => 1,
3337 :     NormalizeSpace => 2
3338 :     );
3339 :     Trace("XML metadata loaded from file $filename.") if T(1);
3340 :     # Before we go any farther, we need to validate the field and object names. If an error is found,
3341 :     # the method below will fail.
3342 :     _ValidateFieldNames($metadata);
3343 :     # Next we need to create a hash table for finding relations. The entities and relationships are
3344 :     # implemented as one or more database relations.
3345 :     my %masterRelationTable = ();
3346 :     # Loop through the entities.
3347 :     my $entityList = $metadata->{Entities};
3348 :     for my $entityName (keys %{$entityList}) {
3349 :     my $entityStructure = $entityList->{$entityName};
3350 :     #
3351 : parrello 1.12 # The first step is to create all the entity's default values. For C<Field> elements,
3352 : parrello 1.10 # the relation name must be added where it is not specified. For relationships,
3353 :     # the B<from-link> and B<to-link> fields must be inserted, and for entities an B<id>
3354 :     # field must be added to each relation. Finally, each field will have a C<PrettySort> attribute
3355 :     # added that can be used to pull the implicit fields to the top when displaying the field
3356 :     # documentation. The PrettySort values are 1-based and indicate in which pass through a
3357 :     # relation's data the field should be displayed-- 1 for the first pass, 2 for the second,
3358 :     # and so on.
3359 :     #
3360 :     # Fix up this entity.
3361 :     _FixupFields($entityStructure, $entityName, 2, 3);
3362 :     # Add the ID field.
3363 :     _AddField($entityStructure, 'id', { type => $entityStructure->{keyType},
3364 :     relation => $entityName,
3365 :     Notes => { content => "Unique identifier for this \[b\]$entityName\[/b\]." },
3366 :     PrettySort => 1});
3367 :     #
3368 :     # The current field list enables us to quickly find the relation containing a particular field.
3369 :     # We also need a list that tells us the fields in each relation. We do this by creating a
3370 :     # Relations structure in the entity structure and collating the fields into it based on their
3371 :     # C<relation> property. There is one tricky bit, which is that every relation has to have the
3372 :     # C<id> field in it. Note also that the field list is put into a C<Fields> member of the
3373 :     # relation's structure so that it looks more like the entity and relationship structures.
3374 :     #
3375 :     # First we need to create the relations list.
3376 :     my $relationTable = { };
3377 :     # Loop through the fields. We use a list of field names to prevent a problem with
3378 :     # the hash table cursor losing its place during the loop.
3379 :     my $fieldList = $entityStructure->{Fields};
3380 :     my @fieldNames = keys %{$fieldList};
3381 :     for my $fieldName (@fieldNames) {
3382 :     my $fieldData = $fieldList->{$fieldName};
3383 :     # Get the current field's relation name.
3384 :     my $relationName = $fieldData->{relation};
3385 :     # Insure the relation exists.
3386 :     if (!exists $relationTable->{$relationName}) {
3387 :     $relationTable->{$relationName} = { Fields => { } };
3388 :     }
3389 :     # Add the field to the relation's field structure.
3390 :     $relationTable->{$relationName}->{Fields}->{$fieldName} = $fieldData;
3391 :     }
3392 :     # Now that we've organized all our fields by relation name we need to do some serious
3393 :     # housekeeping. We must add the C<id> field to every relation and convert each relation
3394 :     # to a list of fields. First, we need the ID field itself.
3395 :     my $idField = $fieldList->{id};
3396 :     # Loop through the relations.
3397 :     for my $relationName (keys %{$relationTable}) {
3398 :     my $relation = $relationTable->{$relationName};
3399 :     # Get the relation's field list.
3400 :     my $relationFieldList = $relation->{Fields};
3401 :     # Add the ID field to it. If the field's already there, it will not make any
3402 :     # difference.
3403 :     $relationFieldList->{id} = $idField;
3404 :     # Convert the field set from a hash into a list using the pretty-sort number.
3405 :     $relation->{Fields} = _ReOrderRelationTable($relationFieldList);
3406 :     # Add the relation to the master table.
3407 :     $masterRelationTable{$relationName} = $relation;
3408 :     }
3409 :     # The indexes come next. The primary relation will have a unique-keyed index based on the ID field.
3410 :     # The other relations must have at least one index that begins with the ID field. In addition, the
3411 :     # metadata may require alternate indexes. We do those alternate indexes first. To begin, we need to
3412 :     # get the entity's field list and index list.
3413 :     my $indexList = $entityStructure->{Indexes};
3414 :     # Loop through the indexes.
3415 :     for my $indexData (@{$indexList}) {
3416 :     # We need to find this index's fields. All of them should belong to the same relation.
3417 :     # The ID field is an exception, since it's in all relations.
3418 :     my $relationName = '0';
3419 :     for my $fieldDescriptor (@{$indexData->{IndexFields}}) {
3420 :     # Get this field's name.
3421 :     my $fieldName = $fieldDescriptor->{name};
3422 :     # Only proceed if it is NOT the ID field.
3423 :     if ($fieldName ne 'id') {
3424 :     # Find the relation containing the current index field.
3425 :     my $thisName = $fieldList->{$fieldName}->{relation};
3426 :     if ($relationName eq '0') {
3427 :     # Here we're looking at the first field, so we save its relation name.
3428 :     $relationName = $thisName;
3429 :     } elsif ($relationName ne $thisName) {
3430 :     # Here we have a field mismatch.
3431 :     Confess("Mixed index: field $fieldName does not belong to relation $relationName.");
3432 :     }
3433 :     }
3434 :     }
3435 :     # Now $relationName is the name of the relation that contains this index. Add the index structure
3436 :     # to the relation.
3437 :     push @{$relationTable->{$relationName}->{Indexes}}, $indexData;
3438 :     }
3439 :     # Now each index has been put in a relation. We need to add the primary index for the primary
3440 :     # relation.
3441 :     push @{$relationTable->{$entityName}->{Indexes}},
3442 :     { IndexFields => [ {name => 'id', order => 'ascending'} ], Unique => 'true',
3443 :     Notes => { content => "Primary index for $entityName." }
3444 :     };
3445 :     # The next step is to insure that each relation has at least one index that begins with the ID field.
3446 :     # After that, we convert each relation's index list to an index table. We first need to loop through
3447 :     # the relations.
3448 :     for my $relationName (keys %{$relationTable}) {
3449 :     my $relation = $relationTable->{$relationName};
3450 :     # Get the relation's index list.
3451 :     my $indexList = $relation->{Indexes};
3452 :     # Insure this relation has an ID index.
3453 :     my $found = 0;
3454 :     for my $index (@{$indexList}) {
3455 :     if ($index->{IndexFields}->[0]->{name} eq "id") {
3456 :     $found = 1;
3457 :     }
3458 :     }
3459 :     if ($found == 0) {
3460 :     push @{$indexList}, { IndexFields => [ {name => 'id', order => 'ascending'} ] };
3461 :     }
3462 :     # Now we need to convert the relation's index list to an index table. We begin by creating
3463 :     # an empty table in the relation structure.
3464 :     $relation->{Indexes} = { };
3465 :     # Loop through the indexes.
3466 :     my $count = 0;
3467 :     for my $index (@{$indexList}) {
3468 :     # Add this index to the index table.
3469 :     _AddIndex("idx$relationName$count", $relation, $index);
3470 :     # Increment the counter so that the next index has a different name.
3471 :     $count++;
3472 :     }
3473 :     }
3474 :     # Finally, we add the relation structure to the entity.
3475 :     $entityStructure->{Relations} = $relationTable;
3476 :     }
3477 :     # Loop through the relationships. Relationships actually turn out to be much simpler than entities.
3478 :     # For one thing, there is only a single constituent relation.
3479 :     my $relationshipList = $metadata->{Relationships};
3480 :     for my $relationshipName (keys %{$relationshipList}) {
3481 :     my $relationshipStructure = $relationshipList->{$relationshipName};
3482 :     # Fix up this relationship.
3483 :     _FixupFields($relationshipStructure, $relationshipName, 2, 3);
3484 :     # Format a description for the FROM field.
3485 :     my $fromEntity = $relationshipStructure->{from};
3486 :     my $fromComment = "<b>id</b> of the source <b><a href=\"#$fromEntity\">$fromEntity</a></b>.";
3487 :     # Get the FROM entity's key type.
3488 :     my $fromType = $entityList->{$fromEntity}->{keyType};
3489 :     # Add the FROM field.
3490 :     _AddField($relationshipStructure, 'from-link', { type => $fromType,
3491 :     relation => $relationshipName,
3492 :     Notes => { content => $fromComment },
3493 :     PrettySort => 1});
3494 :     # Format a description for the TO field.
3495 :     my $toEntity = $relationshipStructure->{to};
3496 :     my $toComment = "<b>id</b> of the target <b><a href=\"#$toEntity\">$toEntity</a></b>.";
3497 :     # Get the TO entity's key type.
3498 :     my $toType = $entityList->{$toEntity}->{keyType};
3499 :     # Add the TO field.
3500 :     _AddField($relationshipStructure, 'to-link', { type=> $toType,
3501 :     relation => $relationshipName,
3502 :     Notes => { content => $toComment },
3503 :     PrettySort => 1});
3504 :     # Create an index-free relation from the fields.
3505 :     my $thisRelation = { Fields => _ReOrderRelationTable($relationshipStructure->{Fields}),
3506 :     Indexes => { } };
3507 :     $relationshipStructure->{Relations} = { $relationshipName => $thisRelation };
3508 :     # Create the FROM and TO indexes.
3509 :     _CreateRelationshipIndex("From", $relationshipName, $relationshipStructure);
3510 :     _CreateRelationshipIndex("To", $relationshipName, $relationshipStructure);
3511 :     # Add the relation to the master table.
3512 :     $masterRelationTable{$relationshipName} = $thisRelation;
3513 :     }
3514 :     # Now store the master relation table in the metadata structure.
3515 :     $metadata->{RelationTable} = \%masterRelationTable;
3516 :     # Our final task is to create the join table. The join table is a hash that describes all
3517 :     # the join clauses for traveling through the relationships. The join clause is an equality
3518 :     # condition that can be put into a WHERE clause in order to join two objects. Two relationships
3519 :     # can be joined if they share an entity in common; and an entity can be joined to a relationship
3520 :     # if the entity is at either end of the relationship.
3521 :     my %joinTable = ();
3522 :     # Loop through the entities.
3523 :     for my $entityName (keys %{$entityList}) {
3524 :     # Build three lists of the relationships connected to this entity. One will be
3525 :     # for relationships from the entity, one for relationships to the entity, and
3526 :     # one for recursive relationships.
3527 :     my @fromList = ();
3528 :     my @toList = ();
3529 :     my @bothList = ();
3530 : parrello 1.21 Trace("Join table build for $entityName.") if T(metadata => 4);
3531 : parrello 1.10 for my $relationshipName (keys %{$relationshipList}) {
3532 :     my $relationship = $relationshipList->{$relationshipName};
3533 :     # Determine if this relationship has our entity in one of its link fields.
3534 :     my $fromEntity = $relationship->{from};
3535 :     my $toEntity = $relationship->{to};
3536 : parrello 1.41 Trace("Join check for relationship $relationshipName from $fromEntity to $toEntity.") if T(Joins => 4);
3537 : parrello 1.10 if ($fromEntity eq $entityName) {
3538 :     if ($toEntity eq $entityName) {
3539 :     # Here the relationship is recursive.
3540 :     push @bothList, $relationshipName;
3541 : parrello 1.21 Trace("Relationship $relationshipName put in both-list.") if T(metadata => 4);
3542 : parrello 1.10 } else {
3543 :     # Here the relationship comes from the entity.
3544 :     push @fromList, $relationshipName;
3545 : parrello 1.21 Trace("Relationship $relationshipName put in from-list.") if T(metadata => 4);
3546 : parrello 1.10 }
3547 :     } elsif ($toEntity eq $entityName) {
3548 :     # Here the relationship goes to the entity.
3549 :     push @toList, $relationshipName;
3550 : parrello 1.21 Trace("Relationship $relationshipName put in to-list.") if T(metadata => 4);
3551 : parrello 1.10 }
3552 :     }
3553 :     # Create the nonrecursive joins. Note that we build two hashes for running
3554 :     # through the nonrecursive relationships since we'll have an outer loop
3555 :     # and an inner loop, and we can't do two "each" iterations on the same
3556 :     # hash table at the same time.
3557 :     my %directRelationships = ( from => \@fromList, to => \@toList );
3558 :     my %otherRelationships = ( from => \@fromList, to => \@toList );
3559 :     for my $linkType (keys %directRelationships) {
3560 :     my $relationships = $directRelationships{$linkType};
3561 :     # Loop through all the relationships.
3562 :     for my $relationshipName (@{$relationships}) {
3563 :     # Create joins between the entity and this relationship.
3564 :     my $linkField = "$relationshipName.${linkType}_link";
3565 :     my $joinClause = "$entityName.id = $linkField";
3566 : parrello 1.21 Trace("Entity join clause is $joinClause for $entityName and $relationshipName.") if T(metadata => 4);
3567 : parrello 1.10 $joinTable{"$entityName/$relationshipName"} = $joinClause;
3568 :     $joinTable{"$relationshipName/$entityName"} = $joinClause;
3569 :     # Create joins between this relationship and the other relationships.
3570 :     for my $otherType (keys %otherRelationships) {
3571 :     my $otherships = $otherRelationships{$otherType};
3572 :     for my $otherName (@{$otherships}) {
3573 :     # Get the key for this join.
3574 :     my $joinKey = "$otherName/$relationshipName";
3575 :     # Check for a duplicate or a self-join.
3576 :     if (exists $joinTable{$joinKey}) {
3577 :     # Here we have a duplicate, which means that the join
3578 :     # path is ambiguous. We delete the join from the join
3579 :     # table to prevent it from being used.
3580 :     delete $joinTable{$joinKey};
3581 :     Trace("Deleting ambiguous join $joinKey.") if T(4);
3582 :     } elsif ($otherName ne $relationshipName) {
3583 :     # Here we have a valid join. Note that joins between a
3584 :     # relationship and itself are prohibited.
3585 :     my $relJoinClause = "$otherName.${otherType}_link = $linkField";
3586 :     $joinTable{$joinKey} = $relJoinClause;
3587 : parrello 1.21 Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(metadata => 4);
3588 : parrello 1.10 }
3589 :     }
3590 :     }
3591 :     # Create joins between this relationship and the recursive relationships.
3592 :     # We don't need to check for ambiguous joins here, because a recursive
3593 :     # relationship can only be ambiguous with another recursive relationship,
3594 :     # and the incoming relationship from the outer loop is never recursive.
3595 :     for my $otherName (@bothList) {
3596 : parrello 1.21 Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(metadata => 4);
3597 : parrello 1.10 # Join from the left.
3598 :     $joinTable{"$relationshipName/$otherName"} =
3599 :     "$linkField = $otherName.from_link";
3600 :     # Join from the right.
3601 :     $joinTable{"$otherName/$relationshipName"} =
3602 :     "$otherName.to_link = $linkField";
3603 :     }
3604 :     }
3605 :     }
3606 :     # Create entity joins for the recursive relationships. Unlike the non-recursive
3607 :     # joins, the direction makes a difference with the recursive joins. This can give
3608 :     # rise to situations where we can't create the path we want; however, it is always
3609 :     # possible to get the same effect using multiple queries.
3610 :     for my $relationshipName (@bothList) {
3611 : parrello 1.21 Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(metadata => 4);
3612 : parrello 1.10 # Join to the entity from each direction.
3613 :     $joinTable{"$entityName/$relationshipName"} =
3614 :     "$entityName.id = $relationshipName.from_link";
3615 :     $joinTable{"$relationshipName/$entityName"} =
3616 :     "$relationshipName.to_link = $entityName.id";
3617 :     }
3618 :     }
3619 :     # Add the join table to the structure.
3620 :     $metadata->{Joins} = \%joinTable;
3621 :     # Return the slurped and fixed-up structure.
3622 :     return $metadata;
3623 : parrello 1.1 }
3624 :    
3625 : parrello 1.70 =head3 _CreateRelationshipIndex
3626 : parrello 1.1
3627 :     Create an index for a relationship's relation.
3628 :    
3629 :     This is a static method.
3630 :    
3631 :     =over 4
3632 :    
3633 :     =item indexKey
3634 :    
3635 :     Type of index: either C<"From"> or C<"To">.
3636 :    
3637 :     =item relationshipName
3638 :    
3639 :     Name of the relationship.
3640 :    
3641 :     =item relationshipStructure
3642 :    
3643 :     Structure describing the relationship that the index will sort.
3644 :    
3645 :     =back
3646 :    
3647 :     =cut
3648 :    
3649 :     sub _CreateRelationshipIndex {
3650 : parrello 1.10 # Get the parameters.
3651 :     my ($indexKey, $relationshipName, $relationshipStructure) = @_;
3652 :     # Get the target relation.
3653 :     my $relationStructure = $relationshipStructure->{Relations}->{$relationshipName};
3654 :     # Create a descriptor for the link field that goes at the beginning of this index.
3655 :     my $firstField = { name => lcfirst $indexKey . '-link', order => 'ascending' };
3656 :     # Get the target index descriptor.
3657 :     my $newIndex = $relationshipStructure->{$indexKey . "Index"};
3658 :     # Add the first field to the index's field list. Due to the craziness of PERL, if the
3659 :     # index descriptor does not exist, it will be created automatically so we can add
3660 :     # the field to it.
3661 :     unshift @{$newIndex->{IndexFields}}, $firstField;
3662 : parrello 1.12 # If this is a one-to-many relationship, the "To" index is unique.
3663 :     if ($relationshipStructure->{arity} eq "1M" && $indexKey eq "To") {
3664 :     $newIndex->{Unique} = 'true';
3665 :     }
3666 : parrello 1.10 # Add the index to the relation.
3667 :     _AddIndex("idx$relationshipName$indexKey", $relationStructure, $newIndex);
3668 : parrello 1.1 }
3669 :    
3670 : parrello 1.70 =head3 _AddIndex
3671 : parrello 1.1
3672 :     Add an index to a relation structure.
3673 :    
3674 :     This is a static method.
3675 :    
3676 :     =over 4
3677 :    
3678 :     =item indexName
3679 :    
3680 :     Name to give to the new index.
3681 :    
3682 :     =item relationStructure
3683 :    
3684 :     Relation structure to which the new index should be added.
3685 :    
3686 :     =item newIndex
3687 :    
3688 :     New index to add.
3689 :    
3690 :     =back
3691 :    
3692 :     =cut
3693 :    
3694 :     sub _AddIndex {
3695 : parrello 1.10 # Get the parameters.
3696 :     my ($indexName, $relationStructure, $newIndex) = @_;
3697 :     # We want to re-do the index's field list. Instead of an object for each field,
3698 :     # we want a string consisting of the field name optionally followed by the token DESC.
3699 :     my @fieldList = ( );
3700 :     for my $field (@{$newIndex->{IndexFields}}) {
3701 :     # Create a string containing the field name.
3702 :     my $fieldString = $field->{name};
3703 :     # Add the ordering token if needed.
3704 :     if ($field->{order} eq "descending") {
3705 :     $fieldString .= " DESC";
3706 :     }
3707 :     # Push the result onto the field list.
3708 :     push @fieldList, $fieldString;
3709 :     }
3710 :     # Store the field list just created as the new index field list.
3711 :     $newIndex->{IndexFields} = \@fieldList;
3712 :     # Add the index to the relation's index list.
3713 :     $relationStructure->{Indexes}->{$indexName} = $newIndex;
3714 : parrello 1.1 }
3715 :    
3716 : parrello 1.70 =head3 _FixupFields
3717 : parrello 1.1
3718 :     This method fixes the field list for an entity or relationship. It will add the caller-specified
3719 :     relation name to fields that do not have a name and set the C<PrettySort> value as specified.
3720 :    
3721 :     This is a static method.
3722 :    
3723 :     =over 4
3724 :    
3725 :     =item structure
3726 :    
3727 :     Entity or relationship structure to be fixed up.
3728 :    
3729 :     =item defaultRelationName
3730 :    
3731 :     Default relation name to be added to the fields.
3732 :    
3733 :     =item prettySortValue
3734 :    
3735 :     C<PrettySort> value for the relation's normal fields.
3736 :    
3737 :     =item textPrettySortValue
3738 :    
3739 :     C<PrettySort> value for the relation's text fields. This value can be set to one greater than the
3740 :     normal pretty sort value so that text fields go at the end of each relation.
3741 :    
3742 :     =back
3743 :    
3744 :     =cut
3745 :    
3746 :     sub _FixupFields {
3747 : parrello 1.10 # Get the parameters.
3748 :     my ($structure, $defaultRelationName, $prettySortValue, $textPrettySortValue) = @_;
3749 :     # Insure the structure has a field list.
3750 :     if (!exists $structure->{Fields}) {
3751 :     # Here it doesn't, so we create a new one.
3752 :     $structure->{Fields} = { };
3753 :     } else {
3754 : parrello 1.70 # Here we have a field list. We need to track the searchable fields, so we
3755 :     # create a list for stashing them.
3756 :     my @textFields = ();
3757 :     # Loop through the fields.
3758 : parrello 1.10 my $fieldStructures = $structure->{Fields};
3759 :     for my $fieldName (keys %{$fieldStructures}) {
3760 : parrello 1.8 Trace("Processing field $fieldName of $defaultRelationName.") if T(4);
3761 : parrello 1.10 my $fieldData = $fieldStructures->{$fieldName};
3762 :     # Get the field type.
3763 :     my $type = $fieldData->{type};
3764 :     # Plug in a relation name if it is needed.
3765 :     Tracer::MergeOptions($fieldData, { relation => $defaultRelationName });
3766 :     # Plug in a data generator if we need one.
3767 :     if (!exists $fieldData->{DataGen}) {
3768 :     # The data generator will use the default for the field's type.
3769 :     $fieldData->{DataGen} = { content => $TypeTable{$type}->{dataGen} };
3770 :     }
3771 : parrello 1.70 # Check for searchability.
3772 :     if ($fieldData->{searchable}) {
3773 :     # Only allow this for a primary relation.
3774 :     if ($fieldData->{relation} ne $defaultRelationName) {
3775 :     Confess("Field $fieldName of $defaultRelationName is in secondary relations and cannot be searchable.");
3776 :     } else {
3777 :     push @textFields, $fieldName;
3778 :     }
3779 :     }
3780 : parrello 1.10 # Plug in the defaults for the optional data generation parameters.
3781 :     Tracer::MergeOptions($fieldData->{DataGen}, { testCount => 1, pass => 0 });
3782 :     # Add the PrettySortValue.
3783 :     $fieldData->{PrettySort} = (($type eq "text") ? $textPrettySortValue : $prettySortValue);
3784 :     }
3785 : parrello 1.70 # If there are searchable fields, remember the fact.
3786 :     if (@textFields) {
3787 :     $structure->{searchFields} = \@textFields;
3788 :     }
3789 : parrello 1.10 }
3790 : parrello 1.1 }
3791 :    
3792 : parrello 1.70 =head3 _FixName
3793 : parrello 1.1
3794 :     Fix the incoming field name so that it is a legal SQL column name.
3795 :    
3796 :     This is a static method.
3797 :    
3798 :     =over 4
3799 :    
3800 :     =item fieldName
3801 :    
3802 :     Field name to fix.
3803 :    
3804 :     =item RETURN
3805 :    
3806 :     Returns the fixed-up field name.
3807 :    
3808 :     =back
3809 :    
3810 :     =cut
3811 :    
3812 :     sub _FixName {
3813 : parrello 1.10 # Get the parameter.
3814 :     my ($fieldName) = @_;
3815 :     # Replace its minus signs with underscores.
3816 :     $fieldName =~ s/-/_/g;
3817 :     # Return the result.
3818 :     return $fieldName;
3819 : parrello 1.1 }
3820 :    
3821 : parrello 1.70 =head3 _FixNames
3822 : parrello 1.1
3823 :     Fix all the field names in a list.
3824 :    
3825 :     This is a static method.
3826 :    
3827 :     =over 4
3828 :    
3829 :     =item field1, field2, field3, ... fieldn
3830 :    
3831 :     List of field names to fix.
3832 :    
3833 :     =item RETURN
3834 :    
3835 :     Returns a list of fixed-up versions of the incoming field names.
3836 :    
3837 :     =back
3838 :    
3839 :     =cut
3840 :    
3841 :     sub _FixNames {
3842 : parrello 1.10 # Create the result list.
3843 :     my @result = ( );
3844 :     # Loop through the incoming parameters.
3845 :     for my $field (@_) {
3846 :     push @result, _FixName($field);
3847 :     }
3848 :     # Return the result.
3849 :     return @result;
3850 : parrello 1.1 }
3851 :    
3852 : parrello 1.70 =head3 _AddField
3853 : parrello 1.1
3854 :     Add a field to a field list.
3855 :    
3856 :     This is a static method.
3857 :    
3858 :     =over 4
3859 :    
3860 :     =item structure
3861 :    
3862 :     Structure (usually an entity or relationship) that is to contain the field.
3863 :    
3864 :     =item fieldName
3865 :    
3866 :     Name of the new field.
3867 :    
3868 :     =item fieldData
3869 :    
3870 :     Structure containing the data to put in the field.
3871 :    
3872 :     =back
3873 :    
3874 :     =cut
3875 :    
3876 :     sub _AddField {
3877 : parrello 1.10 # Get the parameters.
3878 :     my ($structure, $fieldName, $fieldData) = @_;
3879 :     # Create the field structure by copying the incoming data.
3880 :     my $fieldStructure = {%{$fieldData}};
3881 :     # Get a reference to the field list itself.
3882 :     my $fieldList = $structure->{Fields};
3883 :     # Add the field to the field list.
3884 :     $fieldList->{$fieldName} = $fieldStructure;
3885 : parrello 1.1 }
3886 :    
3887 : parrello 1.70 =head3 _ReOrderRelationTable
3888 : parrello 1.1
3889 :     This method will take a relation table and re-sort it according to the implicit ordering of the
3890 :     C<PrettySort> property. Instead of a hash based on field names, it will return a list of fields.
3891 :     This requires creating a new hash that contains the field name in the C<name> property but doesn't
3892 :     have the C<PrettySort> property, and then inserting that new hash into the field list.
3893 :    
3894 :     This is a static method.
3895 :    
3896 :     =over 4
3897 :    
3898 :     =item relationTable
3899 :    
3900 :     Relation hash to be reformatted into a list.
3901 :    
3902 :     =item RETURN
3903 :    
3904 :     A list of field hashes.
3905 :    
3906 :     =back
3907 :    
3908 :     =cut
3909 :    
3910 :     sub _ReOrderRelationTable {
3911 : parrello 1.10 # Get the parameters.
3912 :     my ($relationTable) = @_;
3913 :     # Create the return list.
3914 :     my @resultList;
3915 :     # Rather than copy all the fields in a single pass, we make multiple passes and only copy
3916 :     # fields whose PrettySort value matches the current pass number. This process continues
3917 :     # until we process all the fields in the relation.
3918 :     my $fieldsLeft = (values %{$relationTable});
3919 :     for (my $sortPass = 1; $fieldsLeft > 0; $sortPass++) {
3920 :     # Loop through the fields. Note that we lexically sort the fields. This makes field name
3921 :     # secondary to pretty-sort number in the final ordering.
3922 :     for my $fieldName (sort keys %{$relationTable}) {
3923 :     # Get this field's data.
3924 :     my $fieldData = $relationTable->{$fieldName};
3925 :     # Verify the sort pass.
3926 :     if ($fieldData->{PrettySort} == $sortPass) {
3927 :     # Here we're in the correct pass. Denote we've found a field.
3928 :     $fieldsLeft--;
3929 :     # The next step is to create the field structure. This done by copying all
3930 :     # of the field elements except PrettySort and adding the name.
3931 :     my %thisField;
3932 :     for my $property (keys %{$fieldData}) {
3933 :     if ($property ne 'PrettySort') {
3934 :     $thisField{$property} = $fieldData->{$property};
3935 :     }
3936 :     }
3937 :     $thisField{name} = $fieldName;
3938 :     # Now we add this field to the end of the result list.
3939 :     push @resultList, \%thisField;
3940 :     }
3941 :     }
3942 :     }
3943 :     # Return a reference to the result list.
3944 :     return \@resultList;
3945 : parrello 1.1
3946 :     }
3947 :    
3948 : parrello 1.70 =head3 _IsPrimary
3949 : parrello 1.1
3950 :     Return TRUE if a specified relation is a primary relation, else FALSE. A relation is primary
3951 :     if it has the same name as an entity or relationship.
3952 :    
3953 :     This is an instance method.
3954 :    
3955 :     =over 4
3956 :    
3957 :     =item relationName
3958 :    
3959 :     Name of the relevant relation.
3960 :    
3961 :     =item RETURN
3962 :    
3963 :     Returns TRUE for a primary relation, else FALSE.
3964 :    
3965 :     =back
3966 :    
3967 :     =cut
3968 :    
3969 :     sub _IsPrimary {
3970 : parrello 1.10 # Get the parameters.
3971 :     my ($self, $relationName) = @_;
3972 :     # Check for the relation in the entity table.
3973 :     my $entityTable = $self->{_metaData}->{Entities};
3974 :     my $retVal = exists $entityTable->{$relationName};
3975 :     if (! $retVal) {
3976 :     # Check for it in the relationship table.
3977 :     my $relationshipTable = $self->{_metaData}->{Relationships};
3978 :     $retVal = exists $relationshipTable->{$relationName};
3979 :     }
3980 :     # Return the determination indicator.
3981 :     return $retVal;
3982 : parrello 1.1 }
3983 :    
3984 : parrello 1.70 =head3 _FindRelation
3985 : parrello 1.1
3986 :     Return the descriptor for the specified relation.
3987 :    
3988 :     This is an instance method.
3989 :    
3990 :     =over 4
3991 :    
3992 :     =item relationName
3993 :    
3994 :     Name of the relation whose descriptor is to be returned.
3995 :    
3996 :     =item RETURN
3997 :    
3998 :     Returns the object that describes the relation's indexes and fields.
3999 :    
4000 :     =back
4001 :    
4002 :     =cut
4003 :     sub _FindRelation {
4004 : parrello 1.10 # Get the parameters.
4005 :     my ($self, $relationName) = @_;
4006 :     # Get the relation's structure from the master relation table in the metadata structure.
4007 :     my $metaData = $self->{_metaData};
4008 :     my $retVal = $metaData->{RelationTable}->{$relationName};
4009 :     # Return it to the caller.
4010 :     return $retVal;
4011 : parrello 1.1 }
4012 :    
4013 :     =head2 HTML Documentation Utility Methods
4014 :    
4015 : parrello 1.70 =head3 _ComputeRelationshipSentence
4016 : parrello 1.1
4017 :     The relationship sentence consists of the relationship name between the names of the
4018 :     two related entities and an arity indicator.
4019 :    
4020 :     This is a static method.
4021 :    
4022 :     =over 4
4023 :    
4024 :     =item relationshipName
4025 :    
4026 :     Name of the relationship.
4027 :    
4028 :     =item relationshipStructure
4029 :    
4030 :     Relationship structure containing the relationship's description and properties.
4031 :    
4032 :     =item RETURN
4033 :    
4034 :     Returns a string containing the entity names on either side of the relationship name and an
4035 :     indicator of the arity.
4036 :    
4037 :     =back
4038 :    
4039 :     =cut
4040 :    
4041 :     sub _ComputeRelationshipSentence {
4042 : parrello 1.10 # Get the parameters.
4043 :     my ($relationshipName, $relationshipStructure) = @_;
4044 :     # Format the relationship sentence.
4045 :     my $result = "$relationshipStructure->{from} <b>$relationshipName</b> $relationshipStructure->{to}";
4046 :     # Compute the arity.
4047 :     my $arityCode = $relationshipStructure->{arity};
4048 :     my $arity = $ArityTable{$arityCode};
4049 :     $result .= " ($arity)";
4050 :     return $result;
4051 : parrello 1.1 }
4052 :    
4053 : parrello 1.70 =head3 _ComputeRelationshipHeading
4054 : parrello 1.1
4055 :     The relationship heading is the L<relationship sentence|/ComputeRelationshipSentence> with the entity
4056 :     names hyperlinked to the appropriate entity sections of the document.
4057 :    
4058 :     This is a static method.
4059 :    
4060 :     =over 4
4061 :    
4062 :     =item relationshipName
4063 :    
4064 :     Name of the relationship.
4065 :    
4066 :     =item relationshipStructure
4067 :    
4068 :     Relationship structure containing the relationship's description and properties.
4069 :    
4070 :     =item RETURN
4071 :    
4072 :     Returns a string containing the entity names on either side of the relationship name with the entity
4073 :     names hyperlinked.
4074 :    
4075 :     =back
4076 :    
4077 :     =cut
4078 :    
4079 :     sub _ComputeRelationshipHeading {
4080 : parrello 1.10 # Get the parameters.
4081 :     my ($relationshipName, $relationshipStructure) = @_;
4082 :     # Get the FROM and TO entity names.
4083 :     my $fromEntity = $relationshipStructure->{from};
4084 :     my $toEntity = $relationshipStructure->{to};
4085 :     # Format a relationship sentence with hyperlinks in it.
4086 :     my $result = "<a href=\"#$fromEntity\">$fromEntity</a> $relationshipName <a href=\"#$toEntity\">$toEntity</a>";
4087 :     return $result;
4088 : parrello 1.1 }
4089 :    
4090 : parrello 1.70 =head3 _ShowRelationTable
4091 : parrello 1.1
4092 :     Generate the HTML string for a particular relation. The relation's data will be formatted as an HTML
4093 :     table with three columns-- the field name, the field type, and the field description.
4094 :    
4095 :     This is a static method.
4096 :    
4097 :     =over 4
4098 :    
4099 :     =item relationName
4100 :    
4101 :     Name of the relation being formatted.
4102 :    
4103 :     =item relationData
4104 :    
4105 :     Hash containing the relation's fields and indexes.
4106 :    
4107 :     =item RETURN
4108 :    
4109 :     Returns an HTML string that can be used to display the relation name and all of its fields.
4110 :    
4111 :     =back
4112 :    
4113 :     =cut
4114 :    
4115 :     sub _ShowRelationTable {
4116 : parrello 1.10 # Get the parameters.
4117 :     my ($relationName, $relationData) = @_;
4118 :     # Start the relation's field table.
4119 :     my $htmlString = _OpenFieldTable($relationName);
4120 :     # Loop through the fields.
4121 :     for my $field (@{$relationData->{Fields}}) {
4122 :     $htmlString .= _ShowField($field);
4123 :     }
4124 :     # Close this relation's field table.
4125 :     $htmlString .= &_CloseTable;
4126 :     # Now we show the relation's indexes.
4127 :     $htmlString .= "<ul>\n";
4128 :     my $indexTable = $relationData->{Indexes};
4129 :     for my $indexName (sort keys %{$indexTable}) {
4130 :     my $indexData = $indexTable->{$indexName};
4131 :     # Determine whether or not the index is unique.
4132 :     my $fullName = $indexName;
4133 :     if (exists $indexData->{Unique} && $indexData->{Unique} eq "true") {
4134 :     $fullName .= " (unique)";
4135 :     }
4136 :     # Start an HTML list item for this index.
4137 :     $htmlString .= "<li><b>Index $fullName</b>\n<ul>\n";
4138 :     # Add any note text.
4139 :     if (my $note = $indexData->{Notes}) {
4140 :     $htmlString .= "<li>" . _HTMLNote($note->{content}) . "</li>\n";
4141 :     }
4142 :     # Add the fiield list.
4143 :     $htmlString .= "<li><i>" . join(', ', @{$indexData->{IndexFields}}) . "</i></li>\n";
4144 :     # Close this entry.
4145 :     $htmlString .= "</ul></li>\n";
4146 :     }
4147 :     # Close off the index list.
4148 :     $htmlString .= "</ul>\n";
4149 : parrello 1.1 }
4150 :    
4151 : parrello 1.70 =head3 _OpenFieldTable
4152 : parrello 1.1
4153 :     This method creates the header string for the field table generated by L</ShowMetaData>.
4154 :    
4155 :     This is a static method.
4156 :    
4157 :     =over 4
4158 :    
4159 :     =item tablename
4160 :    
4161 :     Name of the table whose fields will be displayed.
4162 :    
4163 :     =item RETURN
4164 :    
4165 :     Returns a string containing the HTML for a field table's header.
4166 :    
4167 :     =back
4168 :    
4169 :     =cut
4170 :    
4171 :     sub _OpenFieldTable {
4172 : parrello 1.10 my ($tablename) = @_;
4173 :     return _OpenTable($tablename, 'Field', 'Type', 'Description');
4174 : parrello 1.1 }
4175 :    
4176 : parrello 1.70 =head3 _OpenTable
4177 : parrello 1.1
4178 :     This method creates the header string for an HTML table.
4179 :    
4180 :     This is a static method.
4181 :    
4182 :     =over 4
4183 :    
4184 :     =item tablename
4185 :    
4186 :     Title of the table.
4187 :    
4188 :     =item colName1, colName2, ..., colNameN
4189 :    
4190 :     List of column names.
4191 :    
4192 :     =item RETURN
4193 :    
4194 :     Returns a string containing the HTML for the desired table's header.
4195 :    
4196 :     =back
4197 :    
4198 :     =cut
4199 :    
4200 :     sub _OpenTable {
4201 : parrello 1.10 # Get the parameters.
4202 :     my ($tablename, @colNames) = @_;
4203 :     # Compute the number of columns.
4204 :     my $colCount = @colNames;
4205 :     # Generate the title row.
4206 :     my $htmlString = "<p><table border=\"2\"><tr><td colspan=\"$colCount\" align=\"center\">$tablename</td></tr>\n";
4207 :     # Loop through the columns, adding the column header rows.
4208 :     $htmlString .= "<tr>";
4209 :     for my $colName (@colNames) {
4210 :     $htmlString .= "<th>$colName</th>";
4211 :     }
4212 :     $htmlString .= "</tr>\n";
4213 :     return $htmlString;
4214 : parrello 1.1 }
4215 :    
4216 : parrello 1.70 =head3 _CloseTable
4217 : parrello 1.1
4218 :     This method returns the HTML for closing a table.
4219 :    
4220 :     This is a static method.
4221 :    
4222 :     =cut
4223 :    
4224 :     sub _CloseTable {
4225 : parrello 1.10 return "</table></p>\n";
4226 : parrello 1.1 }
4227 :    
4228 : parrello 1.70 =head3 _ShowField
4229 : parrello 1.1
4230 :     This method returns the HTML for displaying a row of field information in a field table.
4231 :    
4232 :     This is a static method.
4233 :    
4234 :     =over 4
4235 :    
4236 :     =item fieldData
4237 :    
4238 :     Table of data about the field.
4239 :    
4240 :     =item RETURN
4241 :    
4242 :     Returns an HTML string for a table row that shows the field's name, type, and description.
4243 :    
4244 :     =back
4245 :    
4246 :     =cut
4247 :    
4248 :     sub _ShowField {
4249 : parrello 1.10 # Get the parameters.
4250 :     my ($fieldData) = @_;
4251 :     # Create the HTML string.
4252 :     my $htmlString = "<tr><th align=\"left\">$fieldData->{name}</th><td>$fieldData->{type}</td>";
4253 :     # If we have content, add it as a third column.
4254 :     if (exists $fieldData->{Notes}) {
4255 :     $htmlString .= "<td>" . _HTMLNote($fieldData->{Notes}->{content}) . "</td>";
4256 :     }
4257 :     # Close off the row.
4258 :     $htmlString .= "</tr>\n";
4259 :     # Return the result.
4260 :     return $htmlString;
4261 : parrello 1.1 }
4262 :    
4263 : parrello 1.70 =head3 _HTMLNote
4264 : parrello 1.1
4265 :     Convert a note or comment to HTML by replacing some bulletin-board codes with HTML. The codes
4266 :     supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph.
4267 :     Except for C<[p]>, all the codes are closed by slash-codes. So, for
4268 :     example, C<[b]Feature[/b]> displays the string C<Feature> in boldface.
4269 :    
4270 :     This is a static method.
4271 :    
4272 :     =over 4
4273 :    
4274 :     =item dataString
4275 :    
4276 :     String to convert to HTML.
4277 :    
4278 :     =item RETURN
4279 :    
4280 :     An HTML string derived from the input string.
4281 :    
4282 :     =back
4283 :    
4284 :     =cut
4285 :    
4286 :     sub _HTMLNote {
4287 : parrello 1.10 # Get the parameter.
4288 :     my ($dataString) = @_;
4289 :     # Substitute the codes.
4290 :     $dataString =~ s!\[(/?[bi])\]!<$1>!g;
4291 :     $dataString =~ s!\[p\]!</p><p>!g;
4292 :     # Return the result.
4293 :     return $dataString;
4294 : parrello 1.1 }
4295 :    
4296 :     =head2 Data Generation Utilities
4297 :    
4298 :     =head3 IntGen
4299 :    
4300 :     C<< my $integer = IntGen($min, $max); >>
4301 :    
4302 :     Returns a random number between the specified minimum and maximum (inclusive).
4303 :    
4304 :     =over 4
4305 :    
4306 :     =item min
4307 :    
4308 :     Minimum permissible return value.
4309 :    
4310 :     =item max
4311 :    
4312 :     Maximum permissible return value.
4313 :    
4314 :     =item RETURN
4315 :    
4316 :     Returns a value no lower than the minimum and no greater than the maximum.
4317 :    
4318 :     =back
4319 :    
4320 :     =cut
4321 :    
4322 :     sub IntGen {
4323 : parrello 1.10 # Get the parameters.
4324 :     my ($min, $max) = @_;
4325 :     # Determine the range of possible values. Note we put some space well above the
4326 :     # maximum value to give it a fighting chance of apppearing in the list.
4327 :     my $span = $max + 0.99 - $min;
4328 :     # Create an integer in the range.
4329 :     my $retVal = $min + int(rand($span));
4330 :     # Return the result.
4331 :     return $retVal;
4332 : parrello 1.1 }
4333 :    
4334 :     =head3 RandChar
4335 :    
4336 :     C<< my $char = RandChar($sourceString); >>
4337 :    
4338 :     Select a random character from a string.
4339 :    
4340 :     =over 4
4341 :    
4342 :     =item sourceString
4343 :    
4344 :     String from which the random character should be selected.
4345 :    
4346 :     =item RETURN
4347 :    
4348 :     Returns a single character from the incoming string.
4349 :    
4350 :     =back
4351 :    
4352 :     =cut
4353 :    
4354 :     sub RandChar {
4355 : parrello 1.10 # Get the parameter.
4356 :     my ($sourceString) = @_;
4357 :     # Select a random character.
4358 :     my $retVal = IntGen(0, (length $sourceString) - 1);
4359 :     # Return it.
4360 :     return substr($sourceString, $retVal, 1);
4361 : parrello 1.1 }
4362 :    
4363 :     =head3 RandChars
4364 :    
4365 :     C<< my $string = RandChars($sourceString, $length); >>
4366 :    
4367 :     Create a string from characters taken from a source string.
4368 :    
4369 :     =over 4
4370 :    
4371 :     =item sourceString
4372 :    
4373 :     String from which the random characters should be selected.
4374 :    
4375 :     =item length
4376 :    
4377 :     Number of characters to put in the output string.
4378 :    
4379 :     =item RETURN
4380 :    
4381 :     Returns a string of the specified length consisting of characters taken from the
4382 :     source string.
4383 :    
4384 :     =back
4385 :    
4386 :     =cut