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

Annotation of /Sprout/CustomAttributes.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     package CustomAttributes;
4 :    
5 :     use strict;
6 :     use Tracer;
7 : parrello 1.13 use Stats;
8 : parrello 1.28 use Time::HiRes qw(time);
9 : parrello 1.33 use FIGRules;
10 : parrello 1.40 use base qw(ERDB);
11 : parrello 1.1
12 :     =head1 Custom SEED Attribute Manager
13 :    
14 :     =head2 Introduction
15 :    
16 :     The Custom SEED Attributes Manager allows the user to upload and retrieve
17 :     custom data for SEED objects. It uses the B<ERDB> database system to
18 : parrello 1.10 store the attributes.
19 :    
20 :     Attributes are organized by I<attribute key>. Attribute values are
21 :     assigned to I<objects>. In the real world, objects have types and IDs;
22 :     however, to the attribute database only the ID matters. This will create
23 :     a problem if we have a single ID that applies to two objects of different
24 :     types, but it is more consistent with the original attribute implementation
25 : parrello 1.11 in the SEED (which this implementation replaces).
26 : parrello 1.10
27 : parrello 1.11 The actual attribute values are stored as a relationship between the attribute
28 :     keys and the objects. There can be multiple values for a single key/object pair.
29 : parrello 1.1
30 : parrello 1.19 =head3 Object IDs
31 :    
32 :     The object ID is normally represented as
33 :    
34 :     I<type>:I<id>
35 :    
36 :     where I<type> is the object type (C<Role>, C<Coupling>, etc.) and I<id> is
37 :     the actual object ID. Note that the object type must consist of only upper- and
38 :     lower-case letters! Thus, C<GenomeGroup> is a valid object type, but
39 :     C<genome_group> is not. Given that restriction, the object ID
40 :    
41 :     Family:aclame|cluster10
42 :    
43 :     would represent the FIG family C<aclame|cluster10>. For historical reasons,
44 :     there are three exceptions: subsystems, genomes, and features do not need
45 :     a type. So, for PEG 3361 of Streptomyces coelicolor A3(2), you simply code
46 :    
47 :     fig|100226.1.peg.3361
48 :    
49 :     The methods L</ParseID> and L</FormID> can be used to make this all seem
50 :     more consistent. Given any object ID string, L</ParseID> will convert it to an
51 :     object type and ID, and given any object type and ID, L</FormID> will
52 :     convert it to an object ID string. The attribute database is pretty
53 :     freewheeling about what it will allow for an ID; however, for best
54 :     results, the type should match an entity type from a Sprout genetics
55 :     database. If this rule is followed, then the database object
56 :     corresponding to an ID in the attribute database could be retrieved using
57 :     L</GetTargetObject> method.
58 :    
59 :     my $object = CustomAttributes::GetTargetObject($sprout, $idValue);
60 :    
61 :     =head3 Retrieval and Logging
62 :    
63 : parrello 1.1 The full suite of ERDB retrieval capabilities is provided. In addition,
64 :     custom methods are provided specific to this application. To get all
65 : parrello 1.6 the values of the attribute C<essential> in a specified B<Feature>, you
66 : parrello 1.1 would code
67 :    
68 : parrello 1.10 my @values = $attrDB->GetAttributes($fid, 'essential');
69 : parrello 1.1
70 : parrello 1.10 where I<$fid> contains the ID of the desired feature.
71 : parrello 1.1
72 : parrello 1.20 Keys can be split into two pieces using the splitter value defined in the
73 :     constructor (the default is C<::>). The first piece of the key is called
74 :     the I<real key>. This portion of the key must be defined using the
75 :     web interface (C<Attributes.cgi>). The second portion of the key is called
76 :     the I<sub key>, and can take any value.
77 : parrello 1.1
78 : parrello 1.18 Major attribute activity is recorded in a log (C<attributes.log>) in the
79 :     C<$FIG_Config::var> directory. The log reports the user name, time, and
80 :     the details of the operation. The user name will almost always be unknown,
81 : parrello 1.20 the exception being when it is specified in this object's constructor
82 :     (see L</new>).
83 : parrello 1.18
84 : parrello 1.1 =head2 FIG_Config Parameters
85 :    
86 :     The following configuration parameters are used to manage custom attributes.
87 :    
88 :     =over 4
89 :    
90 :     =item attrDbms
91 :    
92 :     Type of database manager used: C<mysql> for MySQL or C<pg> for PostGres.
93 :    
94 :     =item attrDbName
95 :    
96 :     Name of the attribute database.
97 :    
98 :     =item attrHost
99 :    
100 :     Name of the host server for the database. If omitted, the current host
101 :     is used.
102 :    
103 :     =item attrUser
104 :    
105 :     User name for logging in to the database.
106 :    
107 :     =item attrPass
108 :    
109 :     Password for logging in to the database.
110 :    
111 :     =item attrPort
112 :    
113 :     TCP/IP port for accessing the database.
114 :    
115 :     =item attrSock
116 :    
117 :     Socket name used to access the database. If omitted, the default socket
118 :     will be used.
119 :    
120 :     =item attrDBD
121 :    
122 :     Fully-qualified file name for the database definition XML file. This file
123 :     functions as data to the attribute management process, so if the data is
124 :     moved, this file must go with it.
125 :    
126 : parrello 1.33 =item attr_default_table
127 :    
128 :     Name of the default relationship for attribute values. If not present,
129 :     C<HasValueFor> is used.
130 :    
131 : parrello 1.1 =back
132 :    
133 :     =head2 Public Methods
134 :    
135 :     =head3 new
136 :    
137 : parrello 1.31 my $attrDB = CustomAttributes->new(%options);
138 : parrello 1.1
139 : parrello 1.18 Construct a new CustomAttributes object. The following options are
140 :     supported.
141 : parrello 1.3
142 :     =over 4
143 :    
144 :     =item splitter
145 :    
146 :     Value to be used to split attribute values into sections in the
147 : parrello 1.18 L</Fig Replacement Methods>. The default is a double colon C<::>,
148 :     and should only be overridden in extreme circumstances.
149 :    
150 :     =item user
151 :    
152 :     Name of the current user. This will appear in the attribute log.
153 : parrello 1.3
154 : parrello 1.43 =item DBD
155 : parrello 1.40
156 :     Filename for the DBD. If unspecified, the default DBD is used.
157 :    
158 : parrello 1.43 =item dbName
159 :    
160 :     SQL name of the database. If omitted, the value of
161 :     I<$FIG_Config::attrDBName> is used.
162 :    
163 : parrello 1.3 =back
164 : parrello 1.1
165 :     =cut
166 :    
167 :     sub new {
168 :     # Get the parameters.
169 : parrello 1.18 my ($class, %options) = @_;
170 : parrello 1.43 # Compute the database name.
171 :     my $dbName = $options{dbName} || $FIG_Config::attrDbName;
172 : parrello 1.1 # Connect to the database.
173 : parrello 1.43 my $dbh = DBKernel->new($FIG_Config::attrDbms, $dbName,
174 : parrello 1.1 $FIG_Config::attrUser, $FIG_Config::attrPass,
175 :     $FIG_Config::attrPort, $FIG_Config::attrHost,
176 :     $FIG_Config::attrSock);
177 :     # Create the ERDB object.
178 : parrello 1.43 my $xmlFileName = $options{DBD} || $FIG_Config::attrDBD;
179 : parrello 1.41 my $retVal = ERDB::new($class, $dbh, $xmlFileName, %options);
180 : parrello 1.3 # Store the splitter value.
181 : parrello 1.18 $retVal->{splitter} = $options{splitter} || '::';
182 :     # Store the user name.
183 :     $retVal->{user} = $options{user} || '<unknown>';
184 :     Trace("User $retVal->{user} selected for attribute object.") if T(3);
185 : parrello 1.33 # Compute the default value table name. If it's not overridden, the
186 :     # default is HasValueFor.
187 :     $retVal->{defaultRel} = $FIG_Config::attr_default_table || 'HasValueFor';
188 : parrello 1.1 # Return the result.
189 :     return $retVal;
190 :     }
191 :    
192 : parrello 1.10 =head3 StoreAttributeKey
193 :    
194 : parrello 1.33 $attrDB->StoreAttributeKey($attributeName, $notes, \@groups, $table);
195 : parrello 1.10
196 :     Create or update an attribute for the database.
197 :    
198 :     =over 4
199 : parrello 1.1
200 :     =item attributeName
201 :    
202 : parrello 1.20 Name of the attribute (the real key). If it does not exist already, it will be created.
203 : parrello 1.1
204 :     =item notes
205 :    
206 :     Descriptive notes about the attribute. It is presumed to be raw text, not HTML.
207 :    
208 : parrello 1.10 =item groups
209 : parrello 1.1
210 : parrello 1.10 Reference to a list of the groups to which the attribute should be associated.
211 :     This will replace any groups to which the attribute is currently attached.
212 : parrello 1.1
213 : parrello 1.33 =item table
214 :    
215 :     The name of the relationship in which the attribute's values are to be stored.
216 :     If empty or undefined, the default relationship (usually C<HasValueFor>) will be
217 :     assumed.
218 :    
219 : parrello 1.1 =back
220 :    
221 :     =cut
222 :    
223 : parrello 1.3 sub StoreAttributeKey {
224 : parrello 1.1 # Get the parameters.
225 : parrello 1.33 my ($self, $attributeName, $notes, $groups, $table) = @_;
226 : parrello 1.8 # Declare the return variable.
227 :     my $retVal;
228 : parrello 1.33 # Default the table name.
229 :     if (! $table) {
230 :     $table = $self->{defaultRel};
231 :     }
232 : parrello 1.1 # Validate the initial input values.
233 : parrello 1.20 if ($attributeName =~ /$self->{splitter}/) {
234 : parrello 1.1 Confess("Invalid attribute name \"$attributeName\" specified.");
235 : parrello 1.33 } elsif (! $notes) {
236 :     Confess("Missing description for $attributeName.");
237 :     } elsif (! grep { $_ eq $table } $self->GetConnectingRelationships('AttributeKey')) {
238 :     Confess("Invalid relationship name \"$table\" specified as a custom attribute table.");
239 : parrello 1.1 } else {
240 : parrello 1.18 # Create a variable to hold the action to be displayed for the log (Add or Update).
241 :     my $action;
242 : parrello 1.10 # Okay, we're ready to begin. See if this key exists.
243 :     my $attribute = $self->GetEntity('AttributeKey', $attributeName);
244 :     if (defined($attribute)) {
245 :     # It does, so we do an update.
246 : parrello 1.18 $action = "Update Key";
247 : parrello 1.10 $self->UpdateEntity('AttributeKey', $attributeName,
248 : parrello 1.33 { description => $notes,
249 :     'relationship-name' => $table});
250 : parrello 1.10 # Detach the key from its current groups.
251 :     $self->Disconnect('IsInGroup', 'AttributeKey', $attributeName);
252 :     } else {
253 :     # It doesn't, so we do an insert.
254 : parrello 1.18 $action = "Insert Key";
255 : parrello 1.10 $self->InsertObject('AttributeKey', { id => $attributeName,
256 : parrello 1.33 description => $notes,
257 :     'relationship-name' => $table});
258 : parrello 1.8 }
259 : parrello 1.10 # Attach the key to the specified groups. (We presume the groups already
260 :     # exist.)
261 :     for my $group (@{$groups}) {
262 :     $self->InsertObject('IsInGroup', { 'from-link' => $attributeName,
263 :     'to-link' => $group });
264 : parrello 1.1 }
265 : parrello 1.18 # Log the operation.
266 :     $self->LogOperation($action, $attributeName, "Group list is " . join(" ", @{$groups}));
267 : parrello 1.1 }
268 :     }
269 :    
270 :    
271 : parrello 1.3 =head3 DeleteAttributeKey
272 :    
273 : parrello 1.31 my $stats = $attrDB->DeleteAttributeKey($attributeName);
274 : parrello 1.1
275 :     Delete an attribute from the custom attributes database.
276 :    
277 :     =over 4
278 :    
279 : parrello 1.10 =item attributeName
280 : parrello 1.1
281 : parrello 1.10 Name of the attribute to delete.
282 : parrello 1.1
283 : parrello 1.10 =item RETURN
284 : parrello 1.1
285 : parrello 1.10 Returns a statistics object describing the effects of the deletion.
286 : parrello 1.1
287 :     =back
288 :    
289 :     =cut
290 :    
291 : parrello 1.3 sub DeleteAttributeKey {
292 : parrello 1.1 # Get the parameters.
293 : parrello 1.10 my ($self, $attributeName) = @_;
294 :     # Delete the attribute key.
295 :     my $retVal = $self->Delete('AttributeKey', $attributeName);
296 : parrello 1.18 # Log this operation.
297 :     $self->LogOperation("Delete Key", $attributeName, "Key will no longer be available for use by anyone.");
298 : parrello 1.10 # Return the result.
299 :     return $retVal;
300 : parrello 1.31
301 : parrello 1.10 }
302 :    
303 :     =head3 NewName
304 :    
305 : parrello 1.31 my $text = CustomAttributes::NewName();
306 : parrello 1.10
307 :     Return the string used to indicate the user wants to add a new attribute.
308 :    
309 :     =cut
310 :    
311 :     sub NewName {
312 :     return "(new)";
313 : parrello 1.1 }
314 :    
315 : parrello 1.11 =head3 LoadAttributesFrom
316 :    
317 : parrello 1.32 C<< my $stats = $attrDB->LoadAttributesFrom($fileName, %options); >>
318 :    
319 : parrello 1.11 Load attributes from the specified tab-delimited file. Each line of the file must
320 :     contain an object ID in the first column, an attribute key name in the second
321 : parrello 1.33 column, and attribute values in the remaining columns. The attribute values must
322 : parrello 1.20 be assembled into a single value using the splitter code. In addition, the key names may
323 :     contain a splitter. If this is the case, the portion of the key after the splitter is
324 :     treated as a subkey.
325 : parrello 1.11
326 :     =over 4
327 :    
328 :     =item fileName
329 :    
330 : parrello 1.20 Name of the file from which to load the attributes, or an open handle for the file.
331 :     (This last enables the method to be used in conjunction with the CGI form upload
332 :     control.)
333 : parrello 1.11
334 :     =item options
335 :    
336 :     Hash of options for modifying the load process.
337 :    
338 :     =item RETURN
339 :    
340 :     Returns a statistics object describing the load.
341 :    
342 :     =back
343 :    
344 :     Permissible option values are as follows.
345 :    
346 :     =over 4
347 :    
348 : parrello 1.42 =item noAnalyze
349 :    
350 :     Do not analyze the table after loading.
351 :    
352 : parrello 1.33 =item mode
353 :    
354 :     Loading mode. Legal values are C<low_priority> (which reduces the task priority
355 :     of the load) and C<concurrent> (which reduces the locking cost of the load). The
356 :     default is a normal load.
357 :    
358 : parrello 1.11 =item append
359 :    
360 :     If TRUE, then the attributes will be appended to existing data; otherwise, the
361 :     first time a key name is encountered, it will be erased.
362 :    
363 : parrello 1.20 =item archive
364 :    
365 : parrello 1.32 If specified, the name of a file into which the incoming data should be saved.
366 :     If I<resume> is also specified, only the lines actually loaded will be put
367 :     into this file.
368 : parrello 1.20
369 :     =item objectType
370 :    
371 :     If specified, the specified object type will be prefixed to each object ID.
372 :    
373 : parrello 1.28 =item resume
374 :    
375 :     If specified, key-value pairs already in the database will not be reinserted.
376 : parrello 1.32 Specify a number to start checking after the specified number of lines and
377 :     then admit everything after the first line not yet loaded. Specify C<careful>
378 :     to check every single line. Specify C<none> to ignore this option. The default
379 :     is C<none>. So, if you believe that a previous load failed somewhere after 50000
380 :     lines, a resume value of C<50000> would skip 50000 lines in the file, then
381 :     check each line after that until it finds one not already in the database. The
382 :     first such line found and all lines after that will be loaded. On the other
383 :     hand, if you have a file of 100000 records, and some have been loaded and some
384 :     not, you would use the word C<careful>, so that every line would be checked before
385 :     it is inserted. A resume of C<0> will start checking the first line of the
386 :     input file and then begin loading once it finds a line not in the database.
387 :    
388 :     =item chunkSize
389 :    
390 :     Number of lines to load in each burst. The default is 10,000.
391 : parrello 1.28
392 : parrello 1.11 =back
393 :    
394 :     =cut
395 :    
396 :     sub LoadAttributesFrom {
397 :     # Get the parameters.
398 :     my ($self, $fileName, %options) = @_;
399 :     # Declare the return variable.
400 : parrello 1.32 my $retVal = Stats->new('keys', 'values', 'linesOut');
401 : parrello 1.27 # Initialize the timers.
402 : parrello 1.33 my ($eraseTime, $archiveTime, $checkTime) = (0, 0, 0);
403 : parrello 1.11 # Check for append mode.
404 :     my $append = ($options{append} ? 1 : 0);
405 : parrello 1.28 # Check for resume mode.
406 : parrello 1.32 my $resume = (defined($options{resume}) ? $options{resume} : 'none');
407 : parrello 1.11 # Create a hash of key names found.
408 :     my %keyHash = ();
409 : parrello 1.33 # Create a hash of table names to files. Most attributes go into the HasValueFor
410 :     # table, but some are put into other tables. Each table name will be mapped
411 :     # to a sub-hash with keys "fileName" (output file for the table) and "count"
412 :     # (number of lines in the file).
413 :     my %tableHash = ();
414 : parrello 1.32 # Compute the chunk size.
415 :     my $chunkSize = ($options{chunkSize} ? $options{chunkSize} : 10000);
416 : parrello 1.20 # Open the file for input. Note we must anticipate the possibility of an
417 : parrello 1.33 # open filehandle being passed in. This occurs when the user is submitting
418 :     # the load file over the web.
419 : parrello 1.20 my $fh;
420 : parrello 1.21 if (ref $fileName) {
421 : parrello 1.20 Trace("Using file opened by caller.") if T(3);
422 :     $fh = $fileName;
423 :     } else {
424 :     Trace("Attributes will be loaded from $fileName.") if T(3);
425 :     $fh = Open(undef, "<$fileName");
426 :     }
427 : parrello 1.32 # Trace the mode.
428 : parrello 1.33 if (T(3)) {
429 :     if ($options{mode}) {
430 :     Trace("Mode is $options{mode}.")
431 :     } else {
432 :     Trace("No mode specified.")
433 :     }
434 :     }
435 : parrello 1.20 # Now check to see if we need to archive.
436 :     my $ah;
437 : parrello 1.32 if (exists $options{archive}) {
438 :     my $ah = Open(undef, ">$options{archive}");
439 : parrello 1.20 Trace("Load file will be archived to $options{archive}.") if T(3);
440 :     }
441 : parrello 1.28 # Insure we recover from errors.
442 : parrello 1.20 eval {
443 : parrello 1.32 # If we have a resume number, process it here.
444 :     if ($resume =~ /\d+/) {
445 :     Trace("Skipping $resume lines.") if T(2);
446 :     my $startTime = time();
447 :     # Skip the specified number of lines.
448 :     for (my $skipped = 0; ! eof($fh) && $skipped < $resume; $skipped++) {
449 :     my $line = <$fh>;
450 :     $retVal->Add(skipped => 1);
451 :     }
452 :     $checkTime += time() - $startTime;
453 :     }
454 : parrello 1.20 # Loop through the file.
455 : parrello 1.32 Trace("Starting load.") if T(2);
456 : parrello 1.20 while (! eof $fh) {
457 :     # Read the current line.
458 :     my ($id, $key, @values) = Tracer::GetLine($fh);
459 :     $retVal->Add(linesIn => 1);
460 :     # Do some validation.
461 :     if (! $id) {
462 :     # We ignore blank lines.
463 :     $retVal->Add(blankLines => 1);
464 :     } elsif (substr($id, 0, 1) eq '#') {
465 :     # A line beginning with a pound sign is a comment.
466 :     $retVal->Add(comments => 1);
467 :     } elsif (! defined($key)) {
468 :     # An ID without a key is a serious error.
469 :     my $lines = $retVal->Ask('linesIn');
470 :     Confess("Line $lines in $fileName has no attribute key.");
471 : parrello 1.23 } elsif (! @values) {
472 :     # A line with no values is not allowed.
473 :     my $lines = $retVal->Ask('linesIn');
474 :     Trace("Line $lines for key $key has no attribute values.") if T(1);
475 :     $retVal->Add(skipped => 1);
476 : parrello 1.20 } else {
477 : parrello 1.32 # Check to see if we need to fix up the object ID.
478 :     if ($options{objectType}) {
479 :     $id = "$options{objectType}:$id";
480 :     }
481 : parrello 1.20 # The key contains a real part and an optional sub-part. We need the real part.
482 :     my ($realKey, $subKey) = $self->SplitKey($key);
483 :     # Now we need to check for a new key.
484 :     if (! exists $keyHash{$realKey}) {
485 : parrello 1.32 my $keyObject = $self->GetEntity(AttributeKey => $realKey);
486 :     if (! defined($keyObject)) {
487 :     # Here the specified key does not exist, which is an error.
488 : parrello 1.20 my $line = $retVal->Ask('linesIn');
489 :     Confess("Attribute \"$realKey\" on line $line of $fileName not found in database.");
490 :     } else {
491 : parrello 1.33 # Make sure we know this is no longer a new key. We do this by putting
492 :     # its table name in the key hash.
493 :     $keyHash{$realKey} = $keyObject->PrimaryValue('AttributeKey(relationship-name)');
494 : parrello 1.20 $retVal->Add(keys => 1);
495 : parrello 1.32 # If this is NOT append mode, erase the key. This does not delete the key
496 :     # itself; it just clears out all the values.
497 : parrello 1.20 if (! $append) {
498 : parrello 1.27 my $startTime = time();
499 : parrello 1.20 $self->EraseAttribute($realKey);
500 : parrello 1.27 $eraseTime += time() - $startTime;
501 :     Trace("Attribute $realKey erased.") if T(3);
502 : parrello 1.20 }
503 : parrello 1.11 }
504 : parrello 1.20 Trace("Key $realKey found.") if T(3);
505 : parrello 1.11 }
506 : parrello 1.28 # If we're in resume mode, check to see if this insert is redundant.
507 :     my $ok = 1;
508 : parrello 1.32 if ($resume ne 'none') {
509 : parrello 1.28 my $startTime = time();
510 :     my $count = $self->GetAttributes($id, $key, @values);
511 : parrello 1.32 if ($count) {
512 :     # Here the record is found, so we skip it.
513 :     $ok = 0;
514 :     $retVal->Add(skipped => 1);
515 :     } else {
516 :     # Here the record is not found. If we're in non-careful mode, we
517 :     # stop resume checking at this point.
518 :     if ($resume ne 'careful') {
519 :     $resume = 'none';
520 :     }
521 :     }
522 : parrello 1.28 $checkTime += time() - $startTime;
523 :     }
524 :     if ($ok) {
525 : parrello 1.32 # We're in business. First, archive this row.
526 :     if (defined $ah) {
527 :     my $startTime = time();
528 :     Tracer::PutLine($ah, [$id, $key, @values]);
529 :     $archiveTime += time() - $startTime;
530 :     }
531 :     # We need to format the attribute data so it will work
532 :     # as if it were a load file. This means we join the
533 :     # values.
534 :     my $valueString = join('::', @values);
535 : parrello 1.33 # Now we need to get access to the key's load file. Check for it in the
536 :     # table hash.
537 :     my $keyTable = $keyHash{$realKey};
538 :     if (! exists $tableHash{$keyTable}) {
539 :     # This is a new table, so we need to set it up. First, we get
540 :     # a temporary file for it.
541 :     my $tempFileName = FIGRules::GetTempFileName(sessionID => $$ . $keyTable,
542 :     extension => 'dtx');
543 :     my $oh = Open(undef, ">$tempFileName");
544 :     # Now we create its descriptor in the table hash.
545 :     $tableHash{$keyTable} = {fileName => $tempFileName, handle => $oh, count => 0};
546 :     }
547 :     # Everything is all set up, so we put the value in the temporary file and
548 : parrello 1.32 # count it.
549 : parrello 1.33 my $tableData = $tableHash{$keyTable};
550 : parrello 1.28 my $startTime = time();
551 : parrello 1.33 Tracer::PutLine($tableData->{handle}, [$realKey, $id, $subKey, $valueString]);
552 : parrello 1.32 $archiveTime += time() - $startTime;
553 :     $retVal->Add(linesOut => 1);
554 : parrello 1.33 $tableData->{count}++;
555 :     # See if it's time to load a chunk.
556 :     if ($tableData->{count} >= $chunkSize) {
557 :     # We've filled a chunk, so it's time.
558 :     close $tableData->{handle};
559 :     $self->_LoadAttributeTable($keyTable, $tableData->{fileName}, $retVal);
560 :     # Reset for the next chunk.
561 :     $tableData->{count} = 0;
562 :     $tableData->{handle} = Open(undef, ">$tableData->{fileName}");
563 : parrello 1.32 }
564 : parrello 1.28 } else {
565 :     # Here we skipped because of resume mode.
566 :     $retVal->Add(resumeSkip => 1);
567 :     }
568 : parrello 1.33 Trace($retVal->Ask('values') . " values processed.") if $retVal->Check(values => 1000) && T(3);
569 : parrello 1.11 }
570 : parrello 1.20 }
571 : parrello 1.32 # Now we close the archive file. Note we undefine the handle so the error methods know
572 :     # not to worry.
573 :     if (defined $ah) {
574 :     close $ah;
575 :     undef $ah;
576 :     }
577 : parrello 1.33 # Now we load the residual from the temporary files (if any). This time we'll do an
578 : parrello 1.32 # analyze as well.
579 : parrello 1.33 for my $tableName (keys %tableHash) {
580 :     # Get the data for this table.
581 :     my $tableData = $tableHash{$tableName};
582 :     # Close the handle. ERDB will re-open it for input later.
583 :     close $tableData->{handle};
584 :     # Check to see if there's anything left to load.
585 :     if ($tableData->{count} > 0) {
586 :     # Yes, load the data.
587 :     $self->_LoadAttributeTable($tableName, $tableData->{fileName}, $retVal);
588 :     }
589 :     # Regardless of whether additional loading was required, we need to
590 :     # analyze the table for performance.
591 : parrello 1.42 if (! $options{noAnalyze}) {
592 :     my $startTime = time();
593 :     $self->Analyze($tableName);
594 :     $retVal->Add(analyzeTime => time() - $startTime);
595 :     }
596 : parrello 1.33 }
597 : parrello 1.32 Trace("Attribute load successful.") if T(2);
598 : parrello 1.20 };
599 :     # Check for an error.
600 :     if ($@) {
601 : parrello 1.28 # Here we have an error. Display the error message.
602 : parrello 1.20 my $message = $@;
603 : parrello 1.28 Trace("Error during attribute load: $message") if T(0);
604 :     $retVal->AddMessage($message);
605 : parrello 1.32 # Close the archive file if it's open. The archive file can sometimes provide
606 :     # clues as to what happened.
607 :     if (defined $ah) {
608 :     close $ah;
609 :     }
610 : parrello 1.28 }
611 : parrello 1.32 # Store the timers.
612 :     $retVal->Add(eraseTime => $eraseTime);
613 :     $retVal->Add(archiveTime => $archiveTime);
614 :     $retVal->Add(checkTime => $checkTime);
615 : parrello 1.11 # Return the result.
616 :     return $retVal;
617 :     }
618 :    
619 : parrello 1.13 =head3 BackupKeys
620 :    
621 : parrello 1.31 my $stats = $attrDB->BackupKeys($fileName, %options);
622 : parrello 1.13
623 :     Backup the attribute key information from the attribute database.
624 :    
625 :     =over 4
626 :    
627 :     =item fileName
628 :    
629 :     Name of the output file.
630 :    
631 :     =item options
632 :    
633 :     Options for modifying the backup process.
634 :    
635 :     =item RETURN
636 :    
637 :     Returns a statistics object for the backup.
638 :    
639 :     =back
640 :    
641 :     Currently there are no options. The backup is straight to a text file in
642 :     tab-delimited format. Each key is backup up to two lines. The first line
643 :     is all of the data from the B<AttributeKey> table. The second is a
644 :     tab-delimited list of all the groups.
645 :    
646 :     =cut
647 :    
648 :     sub BackupKeys {
649 :     # Get the parameters.
650 :     my ($self, $fileName, %options) = @_;
651 :     # Declare the return variable.
652 :     my $retVal = Stats->new();
653 :     # Open the output file.
654 :     my $fh = Open(undef, ">$fileName");
655 :     # Set up to read the keys.
656 :     my $keyQuery = $self->Get(['AttributeKey'], "", []);
657 :     # Loop through the keys.
658 :     while (my $keyData = $keyQuery->Fetch()) {
659 :     $retVal->Add(key => 1);
660 :     # Get the fields.
661 : parrello 1.33 my ($id, $type, $tableName, $description) =
662 :     $keyData->Values(['AttributeKey(id)', 'AttributeKey(relationship-name)',
663 :     'AttributeKey(description)']);
664 : parrello 1.13 # Escape any tabs or new-lines in the description.
665 :     my $escapedDescription = Tracer::Escape($description);
666 :     # Write the key data to the output.
667 : parrello 1.33 Tracer::PutLine($fh, [$id, $type, $tableName, $escapedDescription]);
668 : parrello 1.13 # Get the key's groups.
669 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(from-link) = ?", [$id],
670 :     'IsInGroup(to-link)');
671 :     $retVal->Add(memberships => scalar(@groups));
672 :     # Write them to the output. Note we put a marker at the beginning to insure the line
673 :     # is nonempty.
674 :     Tracer::PutLine($fh, ['#GROUPS', @groups]);
675 :     }
676 : parrello 1.18 # Log the operation.
677 :     $self->LogOperation("Backup Keys", $fileName, $retVal->Display());
678 : parrello 1.13 # Return the result.
679 :     return $retVal;
680 :     }
681 :    
682 :     =head3 RestoreKeys
683 :    
684 : parrello 1.31 my $stats = $attrDB->RestoreKeys($fileName, %options);
685 : parrello 1.13
686 :     Restore the attribute keys and groups from a backup file.
687 :    
688 :     =over 4
689 :    
690 :     =item fileName
691 :    
692 :     Name of the file containing the backed-up keys. Each key has a pair of lines,
693 :     one containing the key data and one listing its groups.
694 :    
695 :     =back
696 :    
697 :     =cut
698 :    
699 :     sub RestoreKeys {
700 :     # Get the parameters.
701 :     my ($self, $fileName, %options) = @_;
702 :     # Declare the return variable.
703 :     my $retVal = Stats->new();
704 :     # Set up a hash to hold the group IDs.
705 :     my %groups = ();
706 :     # Open the file.
707 :     my $fh = Open(undef, "<$fileName");
708 :     # Loop until we're done.
709 :     while (! eof $fh) {
710 :     # Get a key record.
711 : parrello 1.33 my ($id, $tableName, $description) = Tracer::GetLine($fh);
712 : parrello 1.13 if ($id eq '#GROUPS') {
713 :     Confess("Group record found when key record expected.");
714 :     } elsif (! defined($description)) {
715 :     Confess("Invalid format found for key record.");
716 :     } else {
717 :     $retVal->Add("keyIn" => 1);
718 :     # Add this key to the database.
719 : parrello 1.33 $self->InsertObject('AttributeKey', { id => $id,
720 :     description => Tracer::UnEscape($description),
721 :     'relationship-name' => $tableName});
722 : parrello 1.13 Trace("Attribute $id stored.") if T(3);
723 :     # Get the group line.
724 :     my ($marker, @groups) = Tracer::GetLine($fh);
725 :     if (! defined($marker)) {
726 :     Confess("End of file found where group record expected.");
727 :     } elsif ($marker ne '#GROUPS') {
728 :     Confess("Group record not found after key record.");
729 :     } else {
730 :     $retVal->Add(memberships => scalar(@groups));
731 :     # Connect the groups.
732 :     for my $group (@groups) {
733 :     # Find out if this is a new group.
734 :     if (! $groups{$group}) {
735 :     $retVal->Add(newGroup => 1);
736 :     # Add the group.
737 :     $self->InsertObject('AttributeGroup', { id => $group });
738 :     Trace("Group $group created.") if T(3);
739 :     # Make sure we know it's not new.
740 :     $groups{$group} = 1;
741 :     }
742 :     # Connect the group to our key.
743 :     $self->InsertObject('IsInGroup', { 'from-link' => $id, 'to-link' => $group });
744 :     }
745 :     Trace("$id added to " . scalar(@groups) . " groups.") if T(3);
746 :     }
747 :     }
748 :     }
749 : parrello 1.18 # Log the operation.
750 :     $self->LogOperation("Backup Keys", $fileName, $retVal->Display());
751 : parrello 1.13 # Return the result.
752 :     return $retVal;
753 :     }
754 :    
755 : parrello 1.20 =head3 ArchiveFileName
756 :    
757 : parrello 1.31 my $fileName = $ca->ArchiveFileName();
758 : parrello 1.20
759 :     Compute a file name for archiving attribute input data. The file will be in the attribute log directory
760 :    
761 :     =cut
762 :    
763 :     sub ArchiveFileName {
764 :     # Get the parameters.
765 :     my ($self) = @_;
766 :     # Declare the return variable.
767 :     my $retVal;
768 :     # We start by turning the timestamp into something usable as a file name.
769 :     my $now = Tracer::Now();
770 :     $now =~ tr/ :\//___/;
771 :     # Next we get the directory name.
772 :     my $dir = "$FIG_Config::var/attributes";
773 :     if (! -e $dir) {
774 :     Trace("Creating attribute file directory $dir.") if T(1);
775 :     mkdir $dir;
776 :     }
777 :     # Put it together with the field name and the time stamp.
778 :     $retVal = "$dir/upload.$now";
779 :     # Modify the file name to insure it's unique.
780 :     my $seq = 0;
781 :     while (-e "$retVal.$seq.tbl") { $seq++ }
782 :     # Use the computed sequence number to get the correct file name.
783 :     $retVal .= ".$seq.tbl";
784 :     # Return the result.
785 :     return $retVal;
786 :     }
787 : parrello 1.13
788 : parrello 1.11 =head3 BackupAllAttributes
789 :    
790 : parrello 1.31 my $stats = $attrDB->BackupAllAttributes($fileName, %options);
791 : parrello 1.11
792 :     Backup all of the attributes to a file. The attributes will be stored in a
793 :     tab-delimited file suitable for reloading via L</LoadAttributesFrom>.
794 :    
795 :     =over 4
796 :    
797 :     =item fileName
798 :    
799 :     Name of the file to which the attribute data should be backed up.
800 :    
801 :     =item options
802 :    
803 :     Hash of options for the backup.
804 :    
805 :     =item RETURN
806 :    
807 :     Returns a statistics object describing the backup.
808 :    
809 :     =back
810 :    
811 :     Currently there are no options defined.
812 :    
813 :     =cut
814 :    
815 :     sub BackupAllAttributes {
816 :     # Get the parameters.
817 :     my ($self, $fileName, %options) = @_;
818 :     # Declare the return variable.
819 :     my $retVal = Stats->new();
820 :     # Get a list of the keys.
821 : parrello 1.33 my %keys = map { $_->[0] => $_->[1] } $self->GetAll(['AttributeKey'],
822 :     "", [], ['AttributeKey(id)',
823 :     'AttributeKey(relationship-name)']);
824 :     Trace(scalar(keys %keys) . " keys found during backup.") if T(2);
825 : parrello 1.11 # Open the file for output.
826 : parrello 1.12 my $fh = Open(undef, ">$fileName");
827 : parrello 1.11 # Loop through the keys.
828 : parrello 1.33 for my $key (sort keys %keys) {
829 : parrello 1.11 Trace("Backing up attribute $key.") if T(3);
830 :     $retVal->Add(keys => 1);
831 : parrello 1.33 # Get the key's relevant relationship name.
832 :     my $relName = $keys{$key};
833 : parrello 1.11 # Loop through this key's values.
834 : parrello 1.33 my $query = $self->Get([$relName], "$relName(from-link) = ?", [$key]);
835 : parrello 1.11 my $valuesFound = 0;
836 :     while (my $line = $query->Fetch()) {
837 :     $valuesFound++;
838 :     # Get this row's data.
839 : parrello 1.33 my ($id, $key, $subKey, $value) = $line->Values(["$relName(to-link)",
840 :     "$relName(from-link)",
841 :     "$relName(subkey)",
842 :     "$relName(value)"]);
843 : parrello 1.20 # Check for a subkey.
844 :     if ($subKey ne '') {
845 :     $key = "$key$self->{splitter}$subKey";
846 : parrello 1.31 }
847 : parrello 1.11 # Write it to the file.
848 : parrello 1.33 Tracer::PutLine($fh, [$id, $key, Escape($value)]);
849 : parrello 1.11 }
850 :     Trace("$valuesFound values backed up for key $key.") if T(3);
851 :     $retVal->Add(values => $valuesFound);
852 :     }
853 : parrello 1.18 # Log the operation.
854 :     $self->LogOperation("Backup Data", $fileName, $retVal->Display());
855 : parrello 1.11 # Return the result.
856 :     return $retVal;
857 :     }
858 :    
859 : parrello 1.1
860 : parrello 1.10 =head3 GetGroups
861 : parrello 1.3
862 : parrello 1.31 my @groups = $attrDB->GetGroups();
863 : parrello 1.3
864 : parrello 1.10 Return a list of the available groups.
865 : parrello 1.3
866 :     =cut
867 :    
868 : parrello 1.10 sub GetGroups {
869 : parrello 1.3 # Get the parameters.
870 : parrello 1.10 my ($self) = @_;
871 :     # Get the groups.
872 :     my @retVal = $self->GetFlat(['AttributeGroup'], "", [], 'AttributeGroup(id)');
873 :     # Return them.
874 :     return @retVal;
875 : parrello 1.3 }
876 :    
877 : parrello 1.10 =head3 GetAttributeData
878 : parrello 1.3
879 : parrello 1.31 my %keys = $attrDB->GetAttributeData($type, @list);
880 : parrello 1.3
881 : parrello 1.10 Return attribute data for the selected attributes. The attribute
882 :     data is a hash mapping each attribute key name to a n-tuple containing the
883 : parrello 1.33 data type, the description, the table name, and the groups.
884 : parrello 1.3
885 :     =over 4
886 :    
887 : parrello 1.10 =item type
888 : parrello 1.4
889 : parrello 1.10 Type of attribute criterion: C<name> for attributes whose names begin with the
890 :     specified string, or C<group> for attributes in the specified group.
891 : parrello 1.4
892 : parrello 1.10 =item list
893 : parrello 1.4
894 : parrello 1.10 List containing the names of the groups or keys for the desired attributes.
895 : parrello 1.4
896 :     =item RETURN
897 :    
898 : parrello 1.33 Returns a hash mapping each attribute key name to its description,
899 :     table name, and parent groups.
900 : parrello 1.4
901 :     =back
902 :    
903 :     =cut
904 :    
905 : parrello 1.10 sub GetAttributeData {
906 : parrello 1.4 # Get the parameters.
907 : parrello 1.10 my ($self, $type, @list) = @_;
908 :     # Set up a hash to store the attribute data.
909 :     my %retVal = ();
910 :     # Loop through the list items.
911 :     for my $item (@list) {
912 :     # Set up a query for the desired attributes.
913 :     my $query;
914 :     if ($type eq 'name') {
915 :     # Here we're doing a generic name search. We need to escape it and then tack
916 :     # on a %.
917 :     my $parm = $item;
918 :     $parm =~ s/_/\\_/g;
919 :     $parm =~ s/%/\\%/g;
920 :     $parm .= "%";
921 :     # Ask for matching attributes. (Note that if the user passed in a null string
922 :     # he'll get everything.)
923 :     $query = $self->Get(['AttributeKey'], "AttributeKey(id) LIKE ?", [$parm]);
924 :     } elsif ($type eq 'group') {
925 :     $query = $self->Get(['IsInGroup', 'AttributeKey'], "IsInGroup(to-link) = ?", [$item]);
926 : parrello 1.4 } else {
927 : parrello 1.10 Confess("Unknown attribute query type \"$type\".");
928 :     }
929 :     while (my $row = $query->Fetch()) {
930 :     # Get this attribute's data.
931 : parrello 1.33 my ($key, $relName, $notes) = $row->Values(['AttributeKey(id)',
932 :     'AttributeKey(relationship-name)',
933 : parrello 1.10 'AttributeKey(description)']);
934 :     # If it's new, get its groups and add it to the return hash.
935 :     if (! exists $retVal{$key}) {
936 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(from-link) = ?",
937 :     [$key], 'IsInGroup(to-link)');
938 : parrello 1.33 $retVal{$key} = [$relName, $notes, @groups];
939 : parrello 1.4 }
940 :     }
941 :     }
942 :     # Return the result.
943 : parrello 1.10 return %retVal;
944 : parrello 1.4 }
945 :    
946 : parrello 1.18 =head3 LogOperation
947 :    
948 : parrello 1.31 $ca->LogOperation($action, $target, $description);
949 : parrello 1.18
950 :     Write an operation description to the attribute activity log (C<$FIG_Config::var/attributes.log>).
951 :    
952 :     =over 4
953 :    
954 :     =item action
955 :    
956 :     Action being logged (e.g. C<Delete Group> or C<Load Key>).
957 :    
958 :     =item target
959 :    
960 :     ID of the key or group affected.
961 :    
962 :     =item description
963 :    
964 :     Short description of the action.
965 :    
966 :     =back
967 :    
968 :     =cut
969 :    
970 :     sub LogOperation {
971 :     # Get the parameters.
972 :     my ($self, $action, $target, $description) = @_;
973 :     # Get the user ID.
974 :     my $user = $self->{user};
975 :     # Get a timestamp.
976 :     my $timeString = Tracer::Now();
977 :     # Open the log file for appending.
978 :     my $oh = Open(undef, ">>$FIG_Config::var/attributes.log");
979 :     # Write the data to it.
980 :     Tracer::PutLine($oh, [$timeString, $user, $action, $target, $description]);
981 :     # Close the log file.
982 :     close $oh;
983 :     }
984 :    
985 : parrello 1.3 =head2 FIG Method Replacements
986 :    
987 :     The following methods are used by B<FIG.pm> to replace the previous attribute functionality.
988 : parrello 1.10 Some of the old functionality is no longer present: controlled vocabulary is no longer
989 : parrello 1.3 supported and there is no longer any searching by URL. Fortunately, neither of these
990 :     capabilities were used in the old system.
991 :    
992 : parrello 1.4 The methods here are the only ones supported by the B<RemoteCustomAttributes> object.
993 :     The idea is that these methods represent attribute manipulation allowed by all users, while
994 :     the others are only for privileged users with access to the attribute server.
995 :    
996 : parrello 1.20 In the previous implementation, an attribute had a value and a URL. In this implementation,
997 :     each attribute has only a value. These methods will treat the value as a list with the individual
998 :     elements separated by the value of the splitter parameter on the constructor (L</new>). The default
999 :     is double colons C<::>.
1000 : parrello 1.3
1001 : parrello 1.10 So, for example, an old-style keyword with a value of C<essential> and a URL of
1002 : parrello 1.3 C<http://www.sciencemag.org/cgi/content/abstract/293/5538/2266> using the default
1003 :     splitter value would be stored as
1004 :    
1005 :     essential::http://www.sciencemag.org/cgi/content/abstract/293/5538/2266
1006 :    
1007 :     The best performance is achieved by searching for a particular key for a specified
1008 :     feature or genome.
1009 :    
1010 :     =head3 GetAttributes
1011 :    
1012 : parrello 1.31 my @attributeList = $attrDB->GetAttributes($objectID, $key, @values);
1013 : parrello 1.3
1014 :     In the database, attribute values are sectioned into pieces using a splitter
1015 :     value specified in the constructor (L</new>). This is not a requirement of
1016 :     the attribute system as a whole, merely a convenience for the purpose of
1017 : parrello 1.10 these methods. If a value has multiple sections, each section
1018 :     is matched against the corresponding criterion in the I<@valuePatterns> list.
1019 : parrello 1.3
1020 :     This method returns a series of tuples that match the specified criteria. Each tuple
1021 :     will contain an object ID, a key, and one or more values. The parameters to this
1022 : parrello 1.10 method therefore correspond structurally to the values expected in each tuple. In
1023 :     addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any
1024 :     of the parameters. So, for example,
1025 : parrello 1.3
1026 : parrello 1.10 my @attributeList = $attrDB->GetAttributes('fig|100226.1.peg.1004', 'structure%', 1, 2);
1027 : parrello 1.3
1028 :     would return something like
1029 :    
1030 :     ['fig}100226.1.peg.1004', 'structure', 1, 2]
1031 :     ['fig}100226.1.peg.1004', 'structure1', 1, 2]
1032 :     ['fig}100226.1.peg.1004', 'structure2', 1, 2]
1033 :     ['fig}100226.1.peg.1004', 'structureA', 1, 2]
1034 :    
1035 : parrello 1.10 Use of C<undef> in any position acts as a wild card (all values). You can also specify
1036 :     a list reference in the ID column. Thus,
1037 :    
1038 :     my @attributeList = $attrDB->GetAttributes(['100226.1', 'fig|100226.1.%'], 'PUBMED');
1039 :    
1040 :     would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its
1041 :     features.
1042 : parrello 1.3
1043 :     In addition to values in multiple sections, a single attribute key can have multiple
1044 :     values, so even
1045 :    
1046 : parrello 1.10 my @attributeList = $attrDB->GetAttributes($peg, 'virulent');
1047 : parrello 1.3
1048 :     which has no wildcard in the key or the object ID, may return multiple tuples.
1049 :    
1050 : parrello 1.10 Value matching in this system works very poorly, because of the way multiple values are
1051 : parrello 1.20 stored. For the object ID, key name, and first value, we create queries that filter for the
1052 :     desired results. On any filtering by value, we must do a comparison after the attributes are
1053 :     retrieved from the database, since the database has no notion of the multiple values, which
1054 :     are stored in a single string. As a result, queries in which filter only on value end up
1055 :     reading a lot more than they need to.
1056 : parrello 1.3
1057 :     =over 4
1058 :    
1059 :     =item objectID
1060 :    
1061 : parrello 1.10 ID of object whose attributes are desired. If the attributes are desired for multiple
1062 :     objects, this parameter can be specified as a list reference. If the attributes are
1063 :     desired for all objects, specify C<undef> or an empty string. Finally, you can specify
1064 :     attributes for a range of object IDs by putting a percent sign (C<%>) at the end.
1065 : parrello 1.3
1066 :     =item key
1067 :    
1068 : parrello 1.10 Attribute key name. A value of C<undef> or an empty string will match all
1069 :     attribute keys. If the values are desired for multiple keys, this parameter can be
1070 :     specified as a list reference. Finally, you can specify attributes for a range of
1071 :     keys by putting a percent sign (C<%>) at the end.
1072 : parrello 1.3
1073 : parrello 1.10 =item values
1074 : parrello 1.3
1075 :     List of the desired attribute values, section by section. If C<undef>
1076 : parrello 1.10 or an empty string is specified, all values in that section will match. A
1077 :     generic match can be requested by placing a percent sign (C<%>) at the end.
1078 :     In that case, all values that match up to and not including the percent sign
1079 : parrello 1.14 will match. You may also specify a regular expression enclosed
1080 :     in slashes. All values that match the regular expression will be returned. For
1081 :     performance reasons, only values have this extra capability.
1082 : parrello 1.3
1083 :     =item RETURN
1084 :    
1085 :     Returns a list of tuples. The first element in the tuple is an object ID, the
1086 :     second is an attribute key, and the remaining elements are the sections of
1087 :     the attribute value. All of the tuples will match the criteria set forth in
1088 :     the parameter list.
1089 :    
1090 :     =back
1091 :    
1092 :     =cut
1093 :    
1094 :     sub GetAttributes {
1095 : parrello 1.4 # Get the parameters.
1096 : parrello 1.10 my ($self, $objectID, $key, @values) = @_;
1097 : parrello 1.38 # Declare the return variable.
1098 :     my @retVal = ();
1099 :     # Insure we have at least some sort of filtering going on.
1100 :     if (! grep { defined $_ } $objectID, $key, @values) {
1101 :     Confess("No filters specified in GetAttributes call.");
1102 :     } else {
1103 :     # This hash will map value-table fields to patterns. We use it to build the
1104 :     # SQL statement.
1105 :     my %data;
1106 :     # Add the object ID to the key information.
1107 :     $data{'to-link'} = $objectID;
1108 :     # The first value represents a problem, because we can search it using SQL, but not
1109 :     # in the normal way. If the user specifies a generic search or exact match for
1110 :     # every alternative value (remember, the values may be specified as a list),
1111 :     # then we can create SQL filtering for it. If any of the values are specified
1112 :     # as a regular expression, however, that's more complicated, because
1113 :     # we need to read every value to verify a match.
1114 : parrello 1.39 if (@values > 0 && defined $values[0]) {
1115 : parrello 1.38 # Get the first value and put its alternatives in an array.
1116 :     my $valueParm = $values[0];
1117 :     my @valueList;
1118 :     if (ref $valueParm eq 'ARRAY') {
1119 :     @valueList = @{$valueParm};
1120 : parrello 1.20 } else {
1121 : parrello 1.38 @valueList = ($valueParm);
1122 : parrello 1.20 }
1123 : parrello 1.38 # Okay, now we have all the possible criteria for the first value in the list
1124 :     # @valueList. We'll copy the values to a new array in which they have been
1125 :     # converted to generic requests. If we find a regular-expression match
1126 :     # anywhere in the list, we toss the whole thing.
1127 :     my @valuePatterns = ();
1128 :     my $okValues = 1;
1129 :     for my $valuePattern (@valueList) {
1130 :     # Check the pattern type.
1131 :     if (substr($valuePattern, 0, 1) eq '/') {
1132 :     # Regular expressions invalidate the entire process.
1133 :     $okValues = 0;
1134 :     } elsif (substr($valuePattern, -1, 1) eq '%') {
1135 :     # A Generic pattern is passed in unmodified.
1136 :     push @valuePatterns, $valuePattern;
1137 :     } else {
1138 :     # An exact match is converted to generic.
1139 :     push @valuePatterns, "$valuePattern%";
1140 : parrello 1.33 }
1141 :     }
1142 : parrello 1.38 # If everything works, add the value data to the filtering hash.
1143 :     if ($okValues) {
1144 :     $data{value} = \@valuePatterns;
1145 : parrello 1.33 }
1146 : parrello 1.38 }
1147 :     # Now comes the really tricky part, which is key handling. The key is
1148 :     # actually split in two parts: the real key and a sub-key. The real key
1149 :     # determines which value table contains the relevant values. The information
1150 :     # we need is kept in here.
1151 :     my %tables = map { $_ => [] } $self->_GetAllTables();
1152 :     # See if we have any key filtering to worry about.
1153 :     if ($key) {
1154 :     # Here we have either a single key or a list. We convert both cases to a list.
1155 :     my $keyList = (ref $key ne 'ARRAY' ? [$key] : $key);
1156 : parrello 1.40 Trace("Reading key table.") if T(3);
1157 : parrello 1.38 # Get easy access to the key/table hash.
1158 :     my $keyTableHash = $self->_KeyTable();
1159 :     # Loop through the keys, discovering tables.
1160 :     for my $keyChoice (@$keyList) {
1161 :     # Now we have to start thinking about the real key and the subkeys.
1162 :     my ($realKey, $subKey) = $self->_SplitKeyPattern($keyChoice);
1163 : parrello 1.40 Trace("Checking $realKey against key table.") if T(3);
1164 : parrello 1.38 # Find the matches for the real key in the key hash. For each of
1165 :     # these, we memorize the table name in the hash below.
1166 :     my %tableNames = ();
1167 :     for my $keyInTable (keys %{$keyTableHash}) {
1168 : parrello 1.40 if (_CheckSQLPattern($realKey, $keyInTable)) {
1169 : parrello 1.38 $tableNames{$keyTableHash->{$key}} = 1;
1170 :     }
1171 :     }
1172 :     # If the key is generic, or didn't match anything, add
1173 :     # the default table to the mix.
1174 :     if (keys %tableNames == 0 || $keyChoice =~ /%/) {
1175 :     $tableNames{$self->{defaultRel}} = 1;
1176 :     }
1177 :     # Now we add this key combination to the key list for each relevant table.
1178 :     for my $tableName (keys %tableNames) {
1179 :     push @{$tables{$tableName}}, [$realKey, $subKey];
1180 :     }
1181 : parrello 1.33 }
1182 : parrello 1.20 }
1183 : parrello 1.38 # Now we loop through the tables of interest, performing queries.
1184 :     # Loop through the tables.
1185 :     for my $table (keys %tables) {
1186 :     # Get the key pairs for this table.
1187 :     my $pairs = $tables{$table};
1188 :     # Does this table have data? It does if there is no key specified or
1189 :     # it has at least one key pair.
1190 :     my $pairCount = scalar @{$pairs};
1191 :     Trace("Pair count for table $table is $pairCount.") if T(3);
1192 :     if ($pairCount || ! $key) {
1193 :     # Create some lists to contain the filter fragments and parameter values.
1194 :     my @filter = ();
1195 :     my @parms = ();
1196 :     # This next loop goes through the different fields that can be specified in the
1197 :     # parameter list and generates filters for each. The %data hash that we built above
1198 :     # contains most of the necessary information to do this. When we're done, we'll
1199 :     # paste on stuff for the key pairs.
1200 :     for my $field (keys %data) {
1201 :     # Accumulate filter information for this field. We will OR together all the
1202 :     # elements accumulated to create the final result.
1203 :     my @fieldFilter = ();
1204 :     # Get the specified filter for this field.
1205 :     my $fieldPattern = $data{$field};
1206 :     # Only proceed if the pattern is one that won't match everything.
1207 :     if (defined($fieldPattern) && $fieldPattern ne "" && $fieldPattern ne "%") {
1208 :     # Convert the pattern to an array.
1209 :     my @patterns = ();
1210 :     if (ref $fieldPattern eq 'ARRAY') {
1211 :     push @patterns, @{$fieldPattern};
1212 :     } else {
1213 :     push @patterns, $fieldPattern;
1214 :     }
1215 :     # Only proceed if the array is nonempty. The loop will work fine if the
1216 :     # array is empty, but when we build the filter string at the end we'll
1217 :     # get "()" in the filter list, which will result in an SQL syntax error.
1218 :     if (@patterns) {
1219 :     # Loop through the individual patterns.
1220 :     for my $pattern (@patterns) {
1221 :     my ($clause, $value) = _WherePart($table, $field, $pattern);
1222 :     push @fieldFilter, $clause;
1223 :     push @parms, $value;
1224 :     }
1225 :     # Form the filter for this field.
1226 :     my $fieldFilterString = join(" OR ", @fieldFilter);
1227 :     push @filter, "($fieldFilterString)";
1228 : parrello 1.33 }
1229 :     }
1230 :     }
1231 : parrello 1.38 # The final filter is for the key pairs. Only proceed if we have some.
1232 :     if ($pairCount) {
1233 :     # We'll accumulate pair filter clauses in here.
1234 :     my @pairFilters = ();
1235 :     # Loop through the key pairs.
1236 :     for my $pair (@$pairs) {
1237 :     my ($realKey, $subKey) = @{$pair};
1238 :     my ($realClause, $realValue) = _WherePart($table, 'from-link', $realKey);
1239 :     if (! $subKey) {
1240 :     # Here the subkey is wild, so only the real key matters.
1241 :     push @pairFilters, $realClause;
1242 :     push @parms, $realValue;
1243 :     } else {
1244 :     # Here we have to select on both keys.
1245 :     my ($subClause, $subValue) = _WherePart($table, 'subkey', $subKey);
1246 :     push @pairFilters, "($realClause AND $subClause)";
1247 :     push @parms, $realValue, $subValue;
1248 :     }
1249 : parrello 1.10 }
1250 : parrello 1.38 # Join the pair filters together to make a giant key filter.
1251 :     my $pairFilter = "(" . join(" OR ", @pairFilters) . ")";
1252 :     push @filter, $pairFilter;
1253 : parrello 1.10 }
1254 : parrello 1.38 # At this point, @filter contains one or more filter strings and @parms
1255 :     # contains the parameter values to bind to them.
1256 :     my $actualFilter = join(" AND ", @filter);
1257 :     # Now we're ready to make our query.
1258 :     my $query = $self->Get([$table], $actualFilter, \@parms);
1259 :     # Format the results.
1260 :     push @retVal, $self->_QueryResults($query, $table, @values);
1261 : parrello 1.10 }
1262 :     }
1263 :     }
1264 : parrello 1.33 # The above loop ran the query for each necessary value table and merged the
1265 :     # results into @retVal. Now we return the rows found.
1266 : parrello 1.3 return @retVal;
1267 :     }
1268 :    
1269 :     =head3 AddAttribute
1270 :    
1271 : parrello 1.31 $attrDB->AddAttribute($objectID, $key, @values);
1272 : parrello 1.3
1273 :     Add an attribute key/value pair to an object. This method cannot add a new key, merely
1274 :     add a value to an existing key. Use L</StoreAttributeKey> to create a new key.
1275 :    
1276 :     =over 4
1277 :    
1278 :     =item objectID
1279 :    
1280 : parrello 1.10 ID of the object to which the attribute is to be added.
1281 : parrello 1.3
1282 :     =item key
1283 :    
1284 : parrello 1.10 Attribute key name.
1285 : parrello 1.3
1286 :     =item values
1287 :    
1288 :     One or more values to be associated with the key. The values are joined together with
1289 :     the splitter value before being stored as field values. This enables L</GetAttributes>
1290 :     to split them apart during retrieval. The splitter value defaults to double colons C<::>.
1291 :    
1292 :     =back
1293 :    
1294 :     =cut
1295 :    
1296 :     sub AddAttribute {
1297 :     # Get the parameters.
1298 : parrello 1.4 my ($self, $objectID, $key, @values) = @_;
1299 : parrello 1.3 # Don't allow undefs.
1300 :     if (! defined($objectID)) {
1301 :     Confess("No object ID specified for AddAttribute call.");
1302 :     } elsif (! defined($key)) {
1303 :     Confess("No attribute key specified for AddAttribute call.");
1304 :     } elsif (! @values) {
1305 :     Confess("No values specified in AddAttribute call for key $key.");
1306 :     } else {
1307 : parrello 1.11 # Okay, now we have some reason to believe we can do this. Form the values
1308 :     # into a scalar.
1309 : parrello 1.3 my $valueString = join($self->{splitter}, @values);
1310 : parrello 1.20 # Split up the key.
1311 :     my ($realKey, $subKey) = $self->SplitKey($key);
1312 : parrello 1.33 # Find the table containing the key.
1313 :     my $table = $self->_KeyTable($realKey);
1314 : parrello 1.11 # Connect the object to the key.
1315 : parrello 1.33 $self->InsertObject($table, { 'from-link' => $realKey,
1316 : parrello 1.11 'to-link' => $objectID,
1317 : parrello 1.20 'subkey' => $subKey,
1318 : parrello 1.11 'value' => $valueString,
1319 : parrello 1.42 });
1320 : parrello 1.3 }
1321 : parrello 1.10 # Return a one, indicating success. We do this for backward compatability.
1322 : parrello 1.3 return 1;
1323 :     }
1324 :    
1325 :     =head3 DeleteAttribute
1326 :    
1327 : parrello 1.31 $attrDB->DeleteAttribute($objectID, $key, @values);
1328 : parrello 1.3
1329 :     Delete the specified attribute key/value combination from the database.
1330 :    
1331 :     =over 4
1332 :    
1333 :     =item objectID
1334 :    
1335 : parrello 1.10 ID of the object whose attribute is to be deleted.
1336 : parrello 1.3
1337 :     =item key
1338 :    
1339 : parrello 1.10 Attribute key name.
1340 : parrello 1.3
1341 :     =item values
1342 :    
1343 : parrello 1.10 One or more values associated with the key. If no values are specified, then all values
1344 :     will be deleted. Otherwise, only a matching value will be deleted.
1345 : parrello 1.3
1346 :     =back
1347 :    
1348 :     =cut
1349 :    
1350 :     sub DeleteAttribute {
1351 :     # Get the parameters.
1352 : parrello 1.4 my ($self, $objectID, $key, @values) = @_;
1353 : parrello 1.3 # Don't allow undefs.
1354 :     if (! defined($objectID)) {
1355 :     Confess("No object ID specified for DeleteAttribute call.");
1356 :     } elsif (! defined($key)) {
1357 :     Confess("No attribute key specified for DeleteAttribute call.");
1358 :     } else {
1359 : parrello 1.20 # Split the key into the real key and the subkey.
1360 :     my ($realKey, $subKey) = $self->SplitKey($key);
1361 : parrello 1.33 # Find the table containing the key's values.
1362 :     my $table = $self->_KeyTable($realKey);
1363 : parrello 1.20 if ($subKey eq '' && scalar(@values) == 0) {
1364 :     # Here we erase the entire key for this object.
1365 :     $self->DeleteRow('HasValueFor', $key, $objectID);
1366 :     } else {
1367 :     # Here we erase the matching values.
1368 :     my $valueString = join($self->{splitter}, @values);
1369 :     $self->DeleteRow('HasValueFor', $realKey, $objectID,
1370 :     { subkey => $subKey, value => $valueString });
1371 :     }
1372 : parrello 1.3 }
1373 :     # Return a one. This is for backward compatability.
1374 :     return 1;
1375 :     }
1376 :    
1377 : parrello 1.16 =head3 DeleteMatchingAttributes
1378 :    
1379 : parrello 1.31 my @deleted = $attrDB->DeleteMatchingAttributes($objectID, $key, @values);
1380 : parrello 1.16
1381 :     Delete all attributes that match the specified criteria. This is equivalent to
1382 :     calling L</GetAttributes> and then invoking L</DeleteAttribute> for each
1383 :     row found.
1384 :    
1385 :     =over 4
1386 :    
1387 :     =item objectID
1388 :    
1389 :     ID of object whose attributes are to be deleted. If the attributes for multiple
1390 :     objects are to be deleted, this parameter can be specified as a list reference. If
1391 :     attributes are to be deleted for all objects, specify C<undef> or an empty string.
1392 :     Finally, you can delete attributes for a range of object IDs by putting a percent
1393 :     sign (C<%>) at the end.
1394 :    
1395 :     =item key
1396 :    
1397 :     Attribute key name. A value of C<undef> or an empty string will match all
1398 :     attribute keys. If the values are to be deletedfor multiple keys, this parameter can be
1399 :     specified as a list reference. Finally, you can delete attributes for a range of
1400 :     keys by putting a percent sign (C<%>) at the end.
1401 :    
1402 :     =item values
1403 :    
1404 :     List of the desired attribute values, section by section. If C<undef>
1405 :     or an empty string is specified, all values in that section will match. A
1406 :     generic match can be requested by placing a percent sign (C<%>) at the end.
1407 :     In that case, all values that match up to and not including the percent sign
1408 :     will match. You may also specify a regular expression enclosed
1409 :     in slashes. All values that match the regular expression will be deleted. For
1410 :     performance reasons, only values have this extra capability.
1411 :    
1412 :     =item RETURN
1413 :    
1414 :     Returns a list of tuples for the attributes that were deleted, in the
1415 :     same form as L</GetAttributes>.
1416 :    
1417 :     =back
1418 :    
1419 :     =cut
1420 :    
1421 :     sub DeleteMatchingAttributes {
1422 :     # Get the parameters.
1423 :     my ($self, $objectID, $key, @values) = @_;
1424 :     # Get the matching attributes.
1425 :     my @retVal = $self->GetAttributes($objectID, $key, @values);
1426 :     # Loop through the attributes, deleting them.
1427 :     for my $tuple (@retVal) {
1428 :     $self->DeleteAttribute(@{$tuple});
1429 :     }
1430 : parrello 1.18 # Log this operation.
1431 :     my $count = @retVal;
1432 :     $self->LogOperation("Mass Delete", $key, "$count matching attributes deleted.");
1433 : parrello 1.16 # Return the deleted attributes.
1434 :     return @retVal;
1435 :     }
1436 :    
1437 : parrello 1.3 =head3 ChangeAttribute
1438 :    
1439 : parrello 1.31 $attrDB->ChangeAttribute($objectID, $key, \@oldValues, \@newValues);
1440 : parrello 1.3
1441 :     Change the value of an attribute key/value pair for an object.
1442 :    
1443 :     =over 4
1444 :    
1445 :     =item objectID
1446 :    
1447 :     ID of the genome or feature to which the attribute is to be changed. In general, an ID that
1448 :     starts with C<fig|> is treated as a feature ID, and an ID that is all digits and periods
1449 :     is treated as a genome ID. For IDs of other types, this parameter should be a reference
1450 :     to a 2-tuple consisting of the entity type name followed by the object ID.
1451 :    
1452 :     =item key
1453 :    
1454 :     Attribute key name. This corresponds to the name of a field in the database.
1455 :    
1456 :     =item oldValues
1457 :    
1458 :     One or more values identifying the key/value pair to change.
1459 :    
1460 :     =item newValues
1461 :    
1462 :     One or more values to be put in place of the old values.
1463 :    
1464 :     =back
1465 :    
1466 :     =cut
1467 :    
1468 :     sub ChangeAttribute {
1469 :     # Get the parameters.
1470 : parrello 1.4 my ($self, $objectID, $key, $oldValues, $newValues) = @_;
1471 : parrello 1.3 # Don't allow undefs.
1472 :     if (! defined($objectID)) {
1473 :     Confess("No object ID specified for ChangeAttribute call.");
1474 :     } elsif (! defined($key)) {
1475 :     Confess("No attribute key specified for ChangeAttribute call.");
1476 :     } elsif (! defined($oldValues) || ref $oldValues ne 'ARRAY') {
1477 :     Confess("No old values specified in ChangeAttribute call for key $key.");
1478 :     } elsif (! defined($newValues) || ref $newValues ne 'ARRAY') {
1479 :     Confess("No new values specified in ChangeAttribute call for key $key.");
1480 :     } else {
1481 : parrello 1.10 # We do the change as a delete/add.
1482 : parrello 1.3 $self->DeleteAttribute($objectID, $key, @{$oldValues});
1483 :     $self->AddAttribute($objectID, $key, @{$newValues});
1484 :     }
1485 :     # Return a one. We do this for backward compatability.
1486 :     return 1;
1487 :     }
1488 :    
1489 : parrello 1.7 =head3 EraseAttribute
1490 :    
1491 : parrello 1.31 $attrDB->EraseAttribute($key);
1492 : parrello 1.7
1493 :     Erase all values for the specified attribute key. This does not remove the
1494 :     key from the database; it merely removes all the values.
1495 :    
1496 :     =over 4
1497 :    
1498 :     =item key
1499 :    
1500 : parrello 1.20 Key to erase. This must be a real key; that is, it cannot have a subkey
1501 :     component.
1502 : parrello 1.7
1503 :     =back
1504 :    
1505 :     =cut
1506 :    
1507 :     sub EraseAttribute {
1508 :     # Get the parameters.
1509 : parrello 1.10 my ($self, $key) = @_;
1510 : parrello 1.33 # Find the table containing the key.
1511 :     my $table = $self->_KeyTable($key);
1512 :     # Is it the default table?
1513 :     if ($table eq $self->{defaultRel}) {
1514 :     # Yes, so the key is mixed in with other keys.
1515 :     # Delete everything connected to it.
1516 :     $self->Disconnect('HasValueFor', 'AttributeKey', $key);
1517 :     } else {
1518 :     # No. Drop and re-create the table.
1519 :     $self->TruncateTable($table);
1520 :     }
1521 : parrello 1.18 # Log the operation.
1522 :     $self->LogOperation("Erase Data", $key);
1523 : parrello 1.7 # Return a 1, for backward compatability.
1524 :     return 1;
1525 :     }
1526 :    
1527 : parrello 1.9 =head3 GetAttributeKeys
1528 :    
1529 : parrello 1.31 my @keyList = $attrDB->GetAttributeKeys($groupName);
1530 : parrello 1.9
1531 : parrello 1.10 Return a list of the attribute keys for a particular group.
1532 : parrello 1.9
1533 :     =over 4
1534 :    
1535 : parrello 1.10 =item groupName
1536 : parrello 1.9
1537 : parrello 1.10 Name of the group whose keys are desired.
1538 : parrello 1.9
1539 :     =item RETURN
1540 :    
1541 : parrello 1.10 Returns a list of the attribute keys for the specified group.
1542 : parrello 1.9
1543 :     =back
1544 :    
1545 :     =cut
1546 :    
1547 :     sub GetAttributeKeys {
1548 :     # Get the parameters.
1549 : parrello 1.10 my ($self, $groupName) = @_;
1550 :     # Get the attributes for the specified group.
1551 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(to-link) = ?", [$groupName],
1552 :     'IsInGroup(from-link)');
1553 : parrello 1.9 # Return the keys.
1554 : parrello 1.10 return sort @groups;
1555 : parrello 1.9 }
1556 :    
1557 : parrello 1.24 =head3 QueryAttributes
1558 :    
1559 : parrello 1.31 my @attributeData = $ca->QueryAttributes($filter, $filterParms);
1560 : parrello 1.24
1561 :     Return the attribute data based on an SQL filter clause. In the filter clause,
1562 :     the name C<$object> should be used for the object ID, C<$key> should be used for
1563 :     the key name, C<$subkey> for the subkey value, and C<$value> for the value field.
1564 :    
1565 :     =over 4
1566 :    
1567 :     =item filter
1568 :    
1569 :     Filter clause in the standard ERDB format, except that the field names are C<$object> for
1570 :     the object ID field, C<$key> for the key name field, C<$subkey> for the subkey field,
1571 :     and C<$value> for the value field. This abstraction enables us to hide the details of
1572 :     the database construction from the user.
1573 :    
1574 :     =item filterParms
1575 :    
1576 :     Parameters for the filter clause.
1577 :    
1578 :     =item RETURN
1579 :    
1580 :     Returns a list of tuples. Each tuple consists of an object ID, a key (with optional subkey), and
1581 :     one or more attribute values.
1582 :    
1583 :     =back
1584 :    
1585 :     =cut
1586 :    
1587 :     # This hash is used to drive the substitution process.
1588 : parrello 1.33 my %AttributeParms = (object => 'to-link',
1589 :     key => 'from-link',
1590 :     subkey => 'subkey',
1591 :     value => 'value');
1592 : parrello 1.24
1593 :     sub QueryAttributes {
1594 :     # Get the parameters.
1595 :     my ($self, $filter, $filterParms) = @_;
1596 :     # Declare the return variable.
1597 :     my @retVal = ();
1598 :     # Make sue we have filter parameters.
1599 :     my $realParms = (defined($filterParms) ? $filterParms : []);
1600 : parrello 1.33 # Loop through all the value tables.
1601 :     for my $table ($self->_GetAllTables()) {
1602 :     # Create the query for this table by converting the filter.
1603 :     my $realFilter = $filter;
1604 :     for my $name (keys %AttributeParms) {
1605 :     $realFilter =~ s/\$$name/$table($AttributeParms{$name})/g;
1606 :     }
1607 :     my $query = $self->Get([$table], $realFilter, $realParms);
1608 :     # Loop through the results, forming the output attribute tuples.
1609 :     while (my $result = $query->Fetch()) {
1610 :     # Get the four values from this query result row.
1611 :     my ($objectID, $key, $subkey, $value) = $result->Values(["$table($AttributeParms{object})",
1612 :     "$table($AttributeParms{key})",
1613 :     "$table($AttributeParms{subkey})",
1614 :     "$table($AttributeParms{value})"]);
1615 :     # Combine the key and the subkey.
1616 :     my $realKey = ($subkey ? $key . $self->{splitter} . $subkey : $key);
1617 :     # Split the value.
1618 :     my @values = split $self->{splitter}, $value;
1619 :     # Output the result.
1620 :     push @retVal, [$objectID, $realKey, @values];
1621 :     }
1622 : parrello 1.24 }
1623 :     # Return the result.
1624 :     return @retVal;
1625 :     }
1626 :    
1627 : parrello 1.20 =head2 Key and ID Manipulation Methods
1628 :    
1629 : parrello 1.19 =head3 ParseID
1630 :    
1631 : parrello 1.31 my ($type, $id) = CustomAttributes::ParseID($idValue);
1632 : parrello 1.19
1633 :     Determine the type and object ID corresponding to an ID value from the attribute database.
1634 :     Most ID values consist of a type name and an ID, separated by a colon (e.g. C<Family:aclame|cluster10>);
1635 :     however, Genomes, Features, and Subsystems are not stored with a type name, so we need to
1636 :     deduce the type from the ID value structure.
1637 :    
1638 :     The theory here is that you can plug the ID and type directly into a Sprout database method, as
1639 :     follows
1640 :    
1641 :     my ($type, $id) = CustomAttributes::ParseID($attrList[$num]->[0]);
1642 :     my $target = $sprout->GetEntity($type, $id);
1643 :    
1644 :     =over 4
1645 :    
1646 :     =item idValue
1647 :    
1648 :     ID value taken from the attribute database.
1649 :    
1650 :     =item RETURN
1651 :    
1652 :     Returns a two-element list. The first element is the type of object indicated by the ID value,
1653 :     and the second element is the actual object ID.
1654 :    
1655 :     =back
1656 :    
1657 :     =cut
1658 :    
1659 :     sub ParseID {
1660 :     # Get the parameters.
1661 :     my ($idValue) = @_;
1662 :     # Declare the return variables.
1663 :     my ($type, $id);
1664 :     # Parse the incoming ID. We first check for the presence of an entity name. Entity names
1665 :     # can only contain letters, which helps to insure typed object IDs don't collide with
1666 :     # subsystem names (which are untyped).
1667 :     if ($idValue =~ /^([A-Za-z]+):(.+)/) {
1668 :     # Here we have a typed ID.
1669 :     ($type, $id) = ($1, $2);
1670 : parrello 1.26 # Fix the case sensitivity on PDB IDs.
1671 :     if ($type eq 'PDB') { $id = lc $id; }
1672 : parrello 1.19 } elsif ($idValue =~ /fig\|/) {
1673 :     # Here we have a feature ID.
1674 :     ($type, $id) = (Feature => $idValue);
1675 :     } elsif ($idValue =~ /\d+\.\d+/) {
1676 :     # Here we have a genome ID.
1677 :     ($type, $id) = (Genome => $idValue);
1678 :     } else {
1679 :     # The default is a subsystem ID.
1680 :     ($type, $id) = (Subsystem => $idValue);
1681 :     }
1682 :     # Return the results.
1683 :     return ($type, $id);
1684 :     }
1685 :    
1686 :     =head3 FormID
1687 :    
1688 : parrello 1.31 my $idValue = CustomAttributes::FormID($type, $id);
1689 : parrello 1.19
1690 :     Convert an object type and ID pair into an object ID string for the attribute system. Subsystems,
1691 :     genomes, and features are stored in the database without type information, but all other object IDs
1692 :     must be prefixed with the object type.
1693 :    
1694 :     =over 4
1695 :    
1696 :     =item type
1697 :    
1698 :     Relevant object type.
1699 :    
1700 :     =item id
1701 :    
1702 :     ID of the object in question.
1703 :    
1704 :     =item RETURN
1705 :    
1706 :     Returns a string that will be recognized as an object ID in the attribute database.
1707 :    
1708 :     =back
1709 :    
1710 :     =cut
1711 :    
1712 :     sub FormID {
1713 :     # Get the parameters.
1714 :     my ($type, $id) = @_;
1715 :     # Declare the return variable.
1716 :     my $retVal;
1717 :     # Compute the ID string from the type.
1718 :     if (grep { $type eq $_ } qw(Feature Genome Subsystem)) {
1719 :     $retVal = $id;
1720 :     } else {
1721 :     $retVal = "$type:$id";
1722 :     }
1723 :     # Return the result.
1724 :     return $retVal;
1725 :     }
1726 :    
1727 :     =head3 GetTargetObject
1728 :    
1729 : parrello 1.31 my $object = CustomAttributes::GetTargetObject($erdb, $idValue);
1730 : parrello 1.19
1731 :     Return the database object corresponding to the specified attribute object ID. The
1732 :     object type associated with the ID value must correspond to an entity name in the
1733 :     specified database.
1734 :    
1735 :     =over 4
1736 :    
1737 :     =item erdb
1738 :    
1739 :     B<ERDB> object for accessing the target database.
1740 :    
1741 :     =item idValue
1742 :    
1743 :     ID value retrieved from the attribute database.
1744 :    
1745 :     =item RETURN
1746 :    
1747 : parrello 1.22 Returns a B<ERDBObject> for the attribute value's target object.
1748 : parrello 1.19
1749 :     =back
1750 :    
1751 :     =cut
1752 :    
1753 :     sub GetTargetObject {
1754 :     # Get the parameters.
1755 :     my ($erdb, $idValue) = @_;
1756 :     # Declare the return variable.
1757 :     my $retVal;
1758 :     # Get the type and ID for the target object.
1759 :     my ($type, $id) = ParseID($idValue);
1760 :     # Plug them into the GetEntity method.
1761 :     $retVal = $erdb->GetEntity($type, $id);
1762 :     # Return the resulting object.
1763 :     return $retVal;
1764 :     }
1765 :    
1766 : parrello 1.20 =head3 SplitKey
1767 :    
1768 : parrello 1.31 my ($realKey, $subKey) = $ca->SplitKey($key);
1769 : parrello 1.20
1770 :     Split an external key (that is, one passed in by a caller) into the real key and the sub key.
1771 :     The real and sub keys are separated by a splitter value (usually C<::>). If there is no splitter,
1772 :     then the sub key is presumed to be an empty string.
1773 :    
1774 :     =over 4
1775 :    
1776 :     =item key
1777 :    
1778 :     Incoming key to be split.
1779 :    
1780 :     =item RETURN
1781 :    
1782 :     Returns a two-element list, the first element of which is the real key and the second element of
1783 :     which is the sub key.
1784 :    
1785 :     =back
1786 :    
1787 :     =cut
1788 :    
1789 :     sub SplitKey {
1790 :     # Get the parameters.
1791 :     my ($self, $key) = @_;
1792 :     # Do the split.
1793 :     my ($realKey, $subKey) = split($self->{splitter}, $key, 2);
1794 :     # Insure the subkey has a value.
1795 :     if (! defined $subKey) {
1796 :     $subKey = '';
1797 :     }
1798 :     # Return the results.
1799 :     return ($realKey, $subKey);
1800 :     }
1801 :    
1802 : parrello 1.33
1803 : parrello 1.20 =head3 JoinKey
1804 :    
1805 : parrello 1.31 my $key = $ca->JoinKey($realKey, $subKey);
1806 : parrello 1.20
1807 :     Join a real key and a subkey together to make an external key. The external key is the attribute key
1808 :     used by the caller. The real key and the subkey are how the keys are represented in the database. The
1809 :     real key is the key to the B<AttributeKey> entity. The subkey is an attribute of the B<HasValueFor>
1810 :     relationship.
1811 :    
1812 :     =over 4
1813 :    
1814 :     =item realKey
1815 :    
1816 :     The real attribute key.
1817 :    
1818 :     =item subKey
1819 :    
1820 :     The subordinate portion of the attribute key.
1821 :    
1822 :     =item RETURN
1823 :    
1824 :     Returns a single string representing both keys.
1825 :    
1826 :     =back
1827 :    
1828 :     =cut
1829 :    
1830 :     sub JoinKey {
1831 :     # Get the parameters.
1832 :     my ($self, $realKey, $subKey) = @_;
1833 :     # Declare the return variable.
1834 :     my $retVal;
1835 :     # Check for a subkey.
1836 :     if ($subKey eq '') {
1837 :     # No subkey, so the real key is the key.
1838 :     $retVal = $realKey;
1839 :     } else {
1840 :     # Subkey found, so the two pieces must be joined by a splitter.
1841 :     $retVal = "$realKey$self->{splitter}$subKey";
1842 :     }
1843 :     # Return the result.
1844 :     return $retVal;
1845 :     }
1846 :    
1847 : parrello 1.26
1848 :     =head3 AttributeTable
1849 :    
1850 : parrello 1.31 my $tableHtml = CustomAttributes::AttributeTable($cgi, @attrList);
1851 : parrello 1.26
1852 :     Format the attribute data into an HTML table.
1853 :    
1854 :     =over 4
1855 :    
1856 :     =item cgi
1857 :    
1858 :     CGI query object used to generate the HTML
1859 :    
1860 :     =item attrList
1861 :    
1862 :     List of attribute results, in the format returned by the L</GetAttributes> or
1863 :     L</QueryAttributes> methods.
1864 :    
1865 :     =item RETURN
1866 :    
1867 :     Returns an HTML table displaying the attribute keys and values.
1868 :    
1869 :     =back
1870 :    
1871 :     =cut
1872 :    
1873 :     sub AttributeTable {
1874 :     # Get the parameters.
1875 :     my ($cgi, @attrList) = @_;
1876 :     # Accumulate the table rows.
1877 :     my @html = ();
1878 :     for my $attrData (@attrList) {
1879 :     # Format the object ID and key.
1880 :     my @columns = map { CGI::escapeHTML($_) } @{$attrData}[0,1];
1881 :     # Now we format the values. These remain unchanged unless one of them is a URL.
1882 :     my $lastValue = scalar(@{$attrData}) - 1;
1883 : parrello 1.40 push @columns, map { $_ =~ /^http:/ ? CGI::a({ href => $_ }, $_) : $_ } @{$attrData}[2 .. $lastValue];
1884 : parrello 1.26 # Assemble the values into a table row.
1885 : parrello 1.40 push @html, CGI::Tr(CGI::td(\@columns));
1886 : parrello 1.26 }
1887 :     # Format the table in the return variable.
1888 : parrello 1.40 my $retVal = CGI::table({ border => 2 }, CGI::Tr(CGI::th(['Object', 'Key', 'Values'])), @html);
1889 : parrello 1.26 # Return it.
1890 :     return $retVal;
1891 :     }
1892 : parrello 1.33
1893 :    
1894 :     =head2 Internal Utility Methods
1895 :    
1896 :     =head3 _KeyTable
1897 :    
1898 :     my $tableName = $ca->_KeyTable($keyName);
1899 :    
1900 :     Return the name of the table that contains the attribute values for the
1901 :     specified key.
1902 :    
1903 :     Most attribute values are stored in the default table (usually C<HasValueFor>).
1904 :     Some, however, are placed in private tables by themselves for performance reasons.
1905 :    
1906 :     =over 4
1907 :    
1908 :     =item keyName (optional)
1909 :    
1910 :     Name of the attribute key whose table name is desired. If not specified, the
1911 :     entire key/table hash is returned.
1912 :    
1913 :     =item RETURN
1914 :    
1915 :     Returns the name of the table containing the specified attribute key's values,
1916 :     or a reference to a hash that maps key names to table names.
1917 :    
1918 :     =back
1919 :    
1920 :     =cut
1921 :    
1922 :     sub _KeyTable {
1923 :     # Get the parameters.
1924 :     my ($self, $keyName) = @_;
1925 :     # Declare the return variable.
1926 :     my $retVal;
1927 :     # Insure the key table hash is present.
1928 :     if (! exists $self->{keyTables}) {
1929 : parrello 1.40 Trace("Creating key table.") if T(3);
1930 : parrello 1.33 $self->{keyTables} = { map { $_->[0] => $_->[1] } $self->GetAll(['AttributeKey'],
1931 :     "AttributeKey(relationship-name) <> ?",
1932 :     [$self->{defaultRel}],
1933 :     ['AttributeKey(id)', 'AttributeKey(relationship-name)']) };
1934 :     }
1935 :     # Get the key hash.
1936 :     my $keyHash = $self->{keyTables};
1937 :     # Does the user want a specific table or the whole thing?
1938 :     if ($keyName) {
1939 :     # Here we want a specific table. Is this key in the hash?
1940 :     if (exists $keyHash->{$keyName}) {
1941 :     # It's there, so return the specified table.
1942 :     $retVal = $keyHash->{$keyName};
1943 :     } else {
1944 :     # No, return the default table name.
1945 :     $retVal = $self->{defaultRel};
1946 :     }
1947 :     } else {
1948 :     # Here we want the whole hash.
1949 :     $retVal = $keyHash;
1950 :     }
1951 :     # Return the result.
1952 :     return $retVal;
1953 :     }
1954 :    
1955 :    
1956 :     =head3 _QueryResults
1957 :    
1958 :     my @attributeList = $attrDB->_QueryResults($query, $table, @values);
1959 :    
1960 :     Match the results of a query against value criteria and return
1961 :     the results. This is an internal method that splits the values coming back
1962 :     and matches the sections against the specified section patterns. It serves
1963 :     as the back end to L</GetAttributes> and L</FindAttributes>.
1964 :    
1965 :     =over 4
1966 :    
1967 :     =item query
1968 :    
1969 :     A query object that will return the desired records.
1970 :    
1971 :     =item table
1972 :    
1973 :     Name of the value table for the query.
1974 :    
1975 :     =item values
1976 :    
1977 :     List of the desired attribute values, section by section. If C<undef>
1978 :     or an empty string is specified, all values in that section will match. A
1979 :     generic match can be requested by placing a percent sign (C<%>) at the end.
1980 :     In that case, all values that match up to and not including the percent sign
1981 :     will match. You may also specify a regular expression enclosed
1982 :     in slashes. All values that match the regular expression will be returned. For
1983 :     performance reasons, only values have this extra capability.
1984 :    
1985 :     =item RETURN
1986 :    
1987 :     Returns a list of tuples. The first element in the tuple is an object ID, the
1988 :     second is an attribute key, and the remaining elements are the sections of
1989 :     the attribute value. All of the tuples will match the criteria set forth in
1990 :     the parameter list.
1991 :    
1992 :     =back
1993 :    
1994 :     =cut
1995 :    
1996 :     sub _QueryResults {
1997 :     # Get the parameters.
1998 :     my ($self, $query, $table, @values) = @_;
1999 :     # Declare the return value.
2000 :     my @retVal = ();
2001 : parrello 1.35 # We use this hash to check for duplicates.
2002 :     my %dupHash = ();
2003 : parrello 1.33 # Get the number of value sections we have to match.
2004 :     my $sectionCount = scalar(@values);
2005 :     # Loop through the assignments found.
2006 :     while (my $row = $query->Fetch()) {
2007 :     # Get the current row's data.
2008 :     my ($id, $realKey, $subKey, $valueString) = $row->Values(["$table(to-link)",
2009 :     "$table(from-link)",
2010 :     "$table(subkey)",
2011 :     "$table(value)"
2012 :     ]);
2013 :     # Form the key from the real key and the sub key.
2014 :     my $key = $self->JoinKey($realKey, $subKey);
2015 : parrello 1.38 # Break the value into sections.
2016 :     my @sections = split($self->{splitter}, $valueString);
2017 :     # Match each section against the incoming values. We'll assume we're
2018 :     # okay unless we learn otherwise.
2019 :     my $matching = 1;
2020 :     for (my $i = 0; $i < $sectionCount && $matching; $i++) {
2021 : parrello 1.44 # Get the pattern for this section.
2022 : parrello 1.38 my $value = $values[$i];
2023 : parrello 1.44 # Only check this value if it's defined. Undefined is a wild card.
2024 :     if (defined $value) {
2025 :     # The value pattern is a scalar or a reference to a list of possible
2026 :     # values. We convert it to a list and then record a match if any
2027 :     # list member matches.
2028 :     my $valueMatch = 0;
2029 :     my @valueList;
2030 :     if (ref $value eq 'ARRAY') {
2031 :     @valueList = @$value;
2032 :     } else {
2033 :     @valueList = ($value);
2034 :     }
2035 :     # Get this section of the value list.
2036 : parrello 1.38 my $section = $sections[$i];
2037 : parrello 1.44 # Loop through the pattern values WHILE ! $valueMatch.
2038 :     for my $thisValue (@valueList) { last unless ! $valueMatch;
2039 :     Trace("Current value pattern is \"$value\".") if T(4);
2040 :     if ($thisValue =~ m#^/(.+)/[a-z]*$#) {
2041 :     Trace("Regular expression detected.") if T(4);
2042 :     # Here we have a regular expression match.
2043 :     $valueMatch = eval("\$section =~ $thisValue");
2044 :     } else {
2045 :     # Here we have a normal match.
2046 :     Trace("SQL match used.") if T(4);
2047 :     $valueMatch = _CheckSQLPattern($thisValue, $section);
2048 :     }
2049 :     }
2050 :     # Record the match result.
2051 :     $matching = $valueMatch;
2052 : parrello 1.35 }
2053 : parrello 1.38 }
2054 :     # If we match, consider writing this row to the return list.
2055 :     if ($matching) {
2056 :     # Check for a duplicate.
2057 :     my $wholeThing = join($self->{splitter}, $id, $key, $valueString);
2058 :     if (! $dupHash{$wholeThing}) {
2059 :     # It's okay, we're not a duplicate. Insure we don't duplicate this result.
2060 :     $dupHash{$wholeThing} = 1;
2061 : parrello 1.35 push @retVal, [$id, $key, @sections];
2062 : parrello 1.33 }
2063 :     }
2064 :     }
2065 :     # Return the rows found.
2066 :     return @retVal;
2067 :     }
2068 :    
2069 :    
2070 :     =head3 _LoadAttributeTable
2071 :    
2072 :     $attr->_LoadAttributeTable($tableName, $fileName, $stats, $mode);
2073 :    
2074 :     Load a file's data into an attribute table. This is an internal method
2075 :     provided for the convenience of L</LoadAttributesFrom>. It loads the
2076 :     specified file into the specified table and updates the statistics
2077 :     object.
2078 :    
2079 :     =over 4
2080 :    
2081 :     =item tableName
2082 :    
2083 :     Name of the table being loaded. This is usually C<HasValueFor>, but may
2084 :     be a different table for some specific attribute keys.
2085 :    
2086 :     =item fileName
2087 :    
2088 :     Name of the file containing a chunk of attribute data to load.
2089 :    
2090 :     =item stats
2091 :    
2092 :     Statistics object into which counts and times should be placed.
2093 :    
2094 :     =item mode
2095 :    
2096 :     Load mode for the file, usually C<low_priority>, C<concurrent>, or
2097 :     an empty string. The mode is used by some applications to control access
2098 :     to the table while it's being loaded. The default (empty string) is to lock the
2099 :     table until all the data's in place.
2100 :    
2101 :     =back
2102 :    
2103 :     =cut
2104 :    
2105 :     sub _LoadAttributeTable {
2106 :     # Get the parameters.
2107 :     my ($self, $tableName, $fileName, $stats, $mode) = @_;
2108 :     # Load the table from the file. Note that we don't do an analyze.
2109 :     # The analyze is done only after everything is complete.
2110 :     my $startTime = time();
2111 :     Trace("Loading attributes from $fileName: " . (-s $fileName) .
2112 :     " characters.") if T(3);
2113 :     my $loadStats = $self->LoadTable($fileName, $tableName,
2114 :     mode => $mode, partial => 1);
2115 :     # Record the load time.
2116 :     $stats->Add(insertTime => time() - $startTime);
2117 :     # Roll up the other statistics.
2118 :     $stats->Accumulate($loadStats);
2119 :     }
2120 :    
2121 :    
2122 :     =head3 _GetAllTables
2123 :    
2124 :     my @tables = $ca->_GetAllTables();
2125 :    
2126 :     Return a list of the names of all the tables used to store attribute
2127 :     values.
2128 :    
2129 :     =cut
2130 :    
2131 :     sub _GetAllTables {
2132 :     # Get the parameters.
2133 :     my ($self) = @_;
2134 :     # Start with the default table.
2135 :     my @retVal = $self->{defaultRel};
2136 :     # Add the tables named in the key hash. These tables are automatically
2137 :     # NOT the default, and each can only occur once, because alternate tables
2138 :     # are allocated on a per-key basis.
2139 :     my $keyHash = $self->_KeyTable();
2140 :     push @retVal, values %$keyHash;
2141 :     # Return the result.
2142 :     return @retVal;
2143 :     }
2144 :    
2145 :    
2146 :     =head3 _SplitKeyPattern
2147 :    
2148 :     my ($realKey, $subKey) = $ca->_SplitKeyPattern($keyChoice);
2149 :    
2150 :     Split a key pattern into the main part (the I<real key>) and a sub-part
2151 :     (the I<sub key>). This method differs from L</SplitKey> in that it treats
2152 :     the key as an SQL pattern instead of a raw string. Also, if there is no
2153 :     incoming sub-part, the sub-key will be undefined instead of an empty
2154 :     string.
2155 :    
2156 :     =over 4
2157 :    
2158 :     =item keyChoice
2159 :    
2160 :     SQL key pattern to be examined. This can either be a literal, an SQL pattern,
2161 :     a literal with an internal splitter code (usually C<::>) or an SQL pattern with
2162 :     an internal splitter. Note that the only SQL pattern we support is a percent
2163 :     sign (C<%>) at the end. This is the way we've declared things in the documentation,
2164 :     so users who try anything else will have problems.
2165 :    
2166 :     =item RETURN
2167 :    
2168 :     Returns a two-element list. The first element is the SQL pattern for the
2169 :     real key and the second is the SQL pattern for the sub-key. If the value
2170 :     for either one does not matter (e.g., the user wants a real key value of
2171 :     C<iedb> and doesn't care about the sub-key value), it will be undefined.
2172 :    
2173 :     =back
2174 :    
2175 :     =cut
2176 :    
2177 :     sub _SplitKeyPattern {
2178 :     # Get the parameters.
2179 :     my ($self, $keyChoice) = @_;
2180 :     # Declare the return variables.
2181 :     my ($realKey, $subKey);
2182 :     # Look for a splitter in the input.
2183 :     if ($keyChoice =~ /^(.*?)$self->{splitter}(.*)/) {
2184 :     # We found one. This means we can treat both sides of the
2185 :     # splitter as known patterns.
2186 :     ($realKey, $subKey) = ($1, $2);
2187 :     } elsif ($keyChoice =~ /%$/) {
2188 :     # Here we have a generic pattern for the whole key. The pattern
2189 :     # is treated as the correct pattern for the real key, but the
2190 :     # sub-key is considered to be wild.
2191 :     $realKey = $keyChoice;
2192 :     } else {
2193 :     # Here we have a literal pattern for the whole key. The pattern
2194 :     # is treated as the correct pattern for the real key, and the
2195 :     # sub-key is required to be blank.
2196 :     $realKey = $keyChoice;
2197 :     $subKey = '';
2198 :     }
2199 :     # Return the results.
2200 :     return ($realKey, $subKey);
2201 :     }
2202 :    
2203 :    
2204 :     =head3 _WherePart
2205 :    
2206 :     my ($sqlClause, $escapedValue) = _WherePart($tableName, $fieldName, $sqlPattern);
2207 :    
2208 :     Return the SQL clause and value for checking a field against the
2209 :     specified SQL pattern value. If the pattern is generic (ends in a C<%>),
2210 :     then a C<LIKE> expression is returned. Otherwise, an equality expression
2211 :     is returned. We take in information describing the field being checked,
2212 :     and the pattern we're checking against it. The output is a WHERE clause
2213 :     fragment for the comparison and a value to be used as a bound parameter
2214 :     value for the clause.
2215 :    
2216 :     =over 4
2217 :    
2218 :     =item tableName
2219 :    
2220 :     Name of the table containing the field we want checked by the clause.
2221 :    
2222 :     =item fieldName
2223 :    
2224 :     Name of the field to check in that table.
2225 :    
2226 :     =item sqlPattern
2227 :    
2228 :     Pattern to be compared against the field. If the last character is a percent sign
2229 :     (C<%>), it will be treated as a generic SQL pattern; otherwise, it will be treated
2230 :     as a literal.
2231 :    
2232 :     =item RETURN
2233 :    
2234 :     Returns a two-element list. The first element will be an SQL comparison expression
2235 :     and the second will be the value to be used as a bound parameter for the expression
2236 :     in order to
2237 :    
2238 :     =back
2239 :    
2240 :     =cut
2241 :    
2242 :     sub _WherePart {
2243 :     # Get the parameters.
2244 :     my ($tableName, $fieldName, $sqlPattern) = @_;
2245 :     # Declare the return variables.
2246 :     my ($sqlClause, $escapedValue);
2247 :     # Copy the pattern into the return area.
2248 :     $escapedValue = $sqlPattern;
2249 :     # Check the pattern. Is it generic or exact?
2250 : parrello 1.34 if ($sqlPattern =~ /(.+)%$/) {
2251 : parrello 1.33 # Yes, it is. We need a LIKE clause and we must escape the underscores
2252 : parrello 1.34 # and percents in the pattern (except for the last one, of course).
2253 :     $escapedValue = $1;
2254 : parrello 1.33 $escapedValue =~ s/(%|_)/\\$1/g;
2255 : parrello 1.34 $escapedValue .= "%";
2256 : parrello 1.33 $sqlClause = "$tableName($fieldName) LIKE ?";
2257 :     } else {
2258 :     # No, it isn't. We use an equality clause.
2259 :     $sqlClause = "$tableName($fieldName) = ?";
2260 :     }
2261 :     # Return the results.
2262 :     return ($sqlClause, $escapedValue);
2263 :     }
2264 :    
2265 :    
2266 :     =head3 _CheckSQLPattern
2267 :    
2268 :     my $flag = _CheckSQLPattern($pattern, $value);
2269 :    
2270 :     Return TRUE if the specified SQL pattern matches the specified value,
2271 :     else FALSE. The pattern is not a true full-blown SQL LIKE pattern: the
2272 :     only wild-carding allowed is a percent sign (C<%>) at the end.
2273 :    
2274 :     =over 4
2275 :    
2276 :     =item pattern
2277 :    
2278 :     SQL pattern to match against a value.
2279 :    
2280 :     =item value
2281 :    
2282 :     Value to match against an SQL pattern.
2283 :    
2284 :     =item RETURN
2285 :    
2286 :     Returns TRUE if the pattern matches the value, else FALSE.
2287 :    
2288 :     =back
2289 :    
2290 :     =cut
2291 :    
2292 :     sub _CheckSQLPattern {
2293 :     # Get the parameters.
2294 :     my ($pattern, $value) = @_;
2295 :     # Declare the return variable.
2296 :     my $retVal;
2297 :     # Check for a generic pattern.
2298 :     if ($pattern =~ /(.*)%$/) {
2299 :     # Here we have one. Do a substring match.
2300 :     $retVal = (substr($value, 0, length $1) eq $1);
2301 :     } else {
2302 :     # Here it's an exact match.
2303 :     $retVal = ($pattern eq $value);
2304 :     }
2305 : parrello 1.40 Trace("SQL pattern check: \"$value\" vs \"$pattern\" = $retVal.") if T(3);
2306 : parrello 1.33 # Return the result.
2307 :     return $retVal;
2308 :     }
2309 :    
2310 : parrello 1.36 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3