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

Diff of /Sprout/ERDB.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.94, Thu Dec 6 14:58:03 2007 UTC revision 1.95, Wed Jan 23 00:56:31 2008 UTC
# Line 11  Line 11 
11      use Time::HiRes qw(gettimeofday);      use Time::HiRes qw(gettimeofday);
12      use Digest::MD5 qw(md5_base64);      use Digest::MD5 qw(md5_base64);
13      use CGI;      use CGI;
14    #    use WikiTools;  ## HACK
15    
16  =head1 Entity-Relationship Database Package  =head1 Entity-Relationship Database Package
17    
# Line 433  Line 434 
434    
435  sub new {  sub new {
436      # Get the parameters.      # Get the parameters.
437      my ($class, $dbh, $metaFileName, $options) = @_;      my ($class, $dbh, $metaFileName, %options) = @_;
438      # Load the meta-data.      # Load the meta-data.
439      my $metaData = _LoadMetaData($metaFileName);      my $metaData = _LoadMetaData($metaFileName);
440      # Create the object.      # Create the object.
# Line 657  Line 658 
658      return Data::Dumper::Dumper($self->{_metaData});      return Data::Dumper::Dumper($self->{_metaData});
659  }  }
660    
661    =head3 GenerateWikiData
662    
663        my @wikiLines = $erdb->GenerateWikiData();
664    
665    Build a description of the database for the wiki. The database will be
666    organized into a single page, with sections for each entity and relationship.
667    The return value is a list of text lines.
668    
669    =cut
670    
671    sub GenerateWikiData {
672        # Get the parameters.
673        my ($self) = @_;
674        # We'll build the wiki text in here.
675        my @retVal = ();
676        # Get the metadata object.
677        my $metadata = $self->{_metaData};
678        # Get the title string. This will become the page name.
679        my $title = $metadata->{Title}->{content};
680        # Get the entity and relationship lists.
681        my $entityList = $metadata->{Entities};
682        my $relationshipList = $metadata->{Relationships};
683        # Start with the introductory text.
684        push @retVal, WikiTools::Heading(2, "Introduction");
685        if (my $notes = $metadata->{Notes}) {
686            push @retVal, WikiNote($notes->{content});
687        }
688        # Start the entity section.
689        push @retVal, WikiTools::Heading(2, "Entities");
690        # Loop through the entities. Note that unlike the situation with HTML, we
691        # don't need to generate the table of contents manually, just the data
692        # itself.
693        for my $key (sort keys %$entityList) {
694            # Create a header for this entity.
695            push @retVal, "", WikiTools::Heading(3, $key);
696            # Get the entity data.
697            my $entityData = $entityList->{$key};
698            # Plant the notes here, if there are any.
699            if (my $notes = $entityData->{Notes}) {
700                push @retVal, "", WikiNote($notes->{content});
701            }
702            # Get the entity's relations.
703            my $relationList = $entityData->{Relations};
704            # Loop through the relations, displaying them.
705            for my $relation (sort keys %{$relationList}) {
706                my $wikiString = _WikiRelationTable($relation, $relationList->{$relation});
707                push @retVal, $wikiString;
708            }
709            # Now we list the entity's relationships (if any). First, we build a list
710            # of the relationships relevant to this entity.
711            my @rels = ();
712            for my $rel (sort keys %$relationshipList) {
713                my $relStructure = $relationshipList->{$rel};
714                if ($relStructure->{from} eq $key || $relStructure->{to} eq $key) {
715                    # Get the relationship sentence.
716                    my $relSentence = _ComputeRelationshipSentence($rel, $relStructure);
717                    # Linkify it.
718                    my $linkedRel = WikiTools::LinkMarkup("#$rel", $rel);
719                    $relSentence =~ s/$rel/$linkedRel/;
720                    push @rels, $relSentence;
721                }
722            }
723            # Add the relationships as a Wiki list.
724            push @retVal, WikiTools::List(@rels);
725        }
726        # Now the entities are documented. Next we do the relationships.
727        push @retVal, WikiTools::Heading(2, "Relationships");
728        for my $key (sort keys %$relationshipList) {
729            my $relationshipData = $relationshipList->{$key};
730            # Create the relationship heading.
731            push @retVal, WikiTools::Heading(3, $key);
732            # Describe the relationship arity. Note there's a bit of trickiness involving recursive
733            # many-to-many relationships. In a normal many-to-many we use two sentences to describe
734            # the arity (one for each direction). This is a bad idea for a recursive relationship,
735            # since both sentences will say the same thing.
736            my $arity = $relationshipData->{arity};
737            my $fromEntity = $relationshipData->{from};
738            my $toEntity = $relationshipData->{to};
739            my @listElements = ();
740            my $boldCode = WikiTools::BoldCode();
741            if ($arity eq "11") {
742                push @listElements, "Each $boldCode$fromEntity$boldCode relates to at most one $boldCode$toEntity$boldCode.";
743            } else {
744                push @listElements, "Each $boldCode$fromEntity$boldCode relates to multiple $boldCode${toEntity}s$boldCode.\n";
745                if ($arity eq "MM" && $fromEntity ne $toEntity) {
746                    push @listElements, "Each $boldCode$toEntity$boldCode relates to multiple $boldCode${fromEntity}s$boldCode.\n";
747                }
748            }
749            push @retVal, WikiTools::List(@listElements);
750            # Plant the notes here, if there are any.
751            if (my $notes = $relationshipData->{Notes}) {
752                push @retVal, "", WikiNote($notes->{content});
753            }
754            # Finally, the relationship table.
755            my $wikiString = _WikiRelationTable($key, $relationshipData->{Relations}->{$key});
756            push @retVal, $wikiString;
757        }
758        # All done. Return the lines.
759        return @retVal;
760    }
761    
762    
763  =head3 CreatePPO  =head3 CreatePPO
764    
765      ERDB::CreatePPO($erdbXMLFile, $ppoXMLFile);      ERDB::CreatePPO($erdbXMLFile, $ppoXMLFile);
# Line 1999  Line 2102 
2102          # Loop through the ends of the relationship.          # Loop through the ends of the relationship.
2103          for my $dir ('from', 'to') {          for my $dir ('from', 'to') {
2104              if ($structure->{$dir} eq $originEntityName) {              if ($structure->{$dir} eq $originEntityName) {
                 # Delete all relationship instances on this side of the entity instance.  
                 Trace("Disconnecting in $dir direction with ID \"$originEntityID\".");  
                 $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ?", 0, $originEntityID);  
2105                  $found = 1;                  $found = 1;
2106                    # Here we want to delete all relationship instances on this side of the
2107                    # entity instance.
2108                    Trace("Disconnecting in $dir direction with ID \"$originEntityID\".");
2109                    # We do this delete in batches to keep it from dragging down the
2110                    # server.
2111                    my $limitClause = ($FIG_Config::delete_limit ? "LIMIT $FIG_Config::delete_limit" : "");
2112                    my $done = 0;
2113                    while (! $done) {
2114                        # Do the delete.
2115                        my $rows = $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ? $limitClause", 0, $originEntityID);
2116                        # See if we're done. We're done if no rows were found or the delete is unlimited.
2117                        $done = ($rows == 0 || ! $limitClause);
2118                    }
2119              }              }
2120          }          }
2121          # Insure we found the entity on at least one end.          # Insure we found the entity on at least one end.
# Line 2723  Line 2836 
2836    
2837  =head3 LoadTable  =head3 LoadTable
2838    
2839      my $results = $erdb->LoadTable($fileName, $relationName, $truncateFlag);      my $results = $erdb->LoadTable($fileName, $relationName, %options);
2840    
2841  Load data from a tab-delimited file into a specified table, optionally re-creating the table  Load data from a tab-delimited file into a specified table, optionally re-creating the table
2842  first.  first.
# Line 2738  Line 2851 
2851    
2852  Name of the relation to be loaded. This is the same as the table name.  Name of the relation to be loaded. This is the same as the table name.
2853    
2854  =item truncateFlag  =item options
2855    
2856  TRUE if the table should be dropped and re-created, else FALSE  A hash of load options.
2857    
2858  =item RETURN  =item RETURN
2859    
# Line 2748  Line 2861 
2861    
2862  =back  =back
2863    
2864    The permissible options are as follows.
2865    
2866    =over 4
2867    
2868    =item truncate
2869    
2870    If TRUE, then the table will be erased before loading.
2871    
2872    =item mode
2873    
2874    Mode in which the load should operate, either C<low_priority> or C<concurrent>.
2875    This option is only applicable to a MySQL database.
2876    
2877    =item partial
2878    
2879    If TRUE, then it is assumed that this is a partial load, and the table will not
2880    be analyzed and compacted at the end.
2881    
2882    =back
2883    
2884  =cut  =cut
2885  sub LoadTable {  sub LoadTable {
2886      # Get the parameters.      # Get the parameters.
2887      my ($self, $fileName, $relationName, $truncateFlag) = @_;      my ($self, $fileName, $relationName, %options) = @_;
2888      # Create the statistical return object.      # Create the statistical return object.
2889      my $retVal = _GetLoadStats();      my $retVal = _GetLoadStats();
2890      # Trace the fact of the load.      # Trace the fact of the load.
# Line 2763  Line 2896 
2896      # Get the relation data.      # Get the relation data.
2897      my $relation = $self->_FindRelation($relationName);      my $relation = $self->_FindRelation($relationName);
2898      # Check the truncation flag.      # Check the truncation flag.
2899      if ($truncateFlag) {      if ($options{truncate}) {
2900          Trace("Creating table $relationName") if T(2);          Trace("Creating table $relationName") if T(2);
2901          # Compute the row count estimate. We take the size of the load file,          # Compute the row count estimate. We take the size of the load file,
2902          # divide it by the estimated row size, and then multiply by 2 to          # divide it by the estimated row size, and then multiply by 2 to
# Line 2789  Line 2922 
2922      # Load the table.      # Load the table.
2923      my $rv;      my $rv;
2924      eval {      eval {
2925          $rv = $dbh->load_table(file => $fileName, tbl => $relationName);          $rv = $dbh->load_table(file => $fileName, tbl => $relationName, style => $options{mode});
2926      };      };
2927      if (!defined $rv) {      if (!defined $rv) {
2928          $retVal->AddMessage($@) if ($@);          $retVal->AddMessage($@) if ($@);
# Line 2801  Line 2934 
2934          my $size = -s $fileName;          my $size = -s $fileName;
2935          Trace("$size bytes loaded into $relationName.") if T(2);          Trace("$size bytes loaded into $relationName.") if T(2);
2936          # If we're rebuilding, we need to create the table indexes.          # If we're rebuilding, we need to create the table indexes.
2937          if ($truncateFlag) {          if ($options{truncate}) {
2938              # Indexes are created here for PostGres. For PostGres, indexes are              # Indexes are created here for PostGres. For PostGres, indexes are
2939              # best built at the end. For MySQL, the reverse is true.              # best built at the end. For MySQL, the reverse is true.
2940              if (! $dbh->{_preIndex}) {              if (! $dbh->{_preIndex}) {
# Line 2822  Line 2955 
2955          }          }
2956      }      }
2957      # Analyze the table to improve performance.      # Analyze the table to improve performance.
2958        if (! $options{partial}) {
2959      Trace("Analyzing and compacting $relationName.") if T(3);      Trace("Analyzing and compacting $relationName.") if T(3);
2960      $dbh->vacuum_it($relationName);      $dbh->vacuum_it($relationName);
2961        }
2962      Trace("$relationName load completed.") if T(3);      Trace("$relationName load completed.") if T(3);
2963      # Return the statistics.      # Return the statistics.
2964      return $retVal;      return $retVal;
# Line 3558  Line 3693 
3693      return $retVal;      return $retVal;
3694  }  }
3695    
3696    =head3 WikiNote
3697    
3698    Convert a note or comment to Wiki text by replacing some bulletin-board codes with HTML. The codes
3699    supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph.
3700    Except for C<[p]>, all the codes are closed by slash-codes. So, for
3701    example, C<[b]Feature[/b]> displays the string C<Feature> in boldface.
3702    
3703        my $wikiText = ERDB::WikiNote($dataString);
3704    
3705    =over 4
3706    
3707    =item dataString
3708    
3709    String to convert to Wiki text.
3710    
3711    =item RETURN
3712    
3713    An Wiki text string derived from the input string.
3714    
3715    =back
3716    
3717    =cut
3718    
3719    sub WikiNote {
3720        # Get the parameter.
3721        my ($dataString) = @_;
3722        # HTML-escape the text.
3723        my $retVal = CGI::escapeHTML($dataString);
3724        # Substitute the bulletin board codes.
3725        my $italic = WikiTools::ItalicCode();
3726        $retVal =~ s/\[\/?i\]/$italic/g;
3727        my $bold = WikiTools::BoldCode();
3728        $retVal =~ s/\[\/?b\]/$bold/g;
3729        # Paragraph breaks are the same no matter which Wiki you're using.
3730        $retVal =~ s!\[p\]!\n\n!g;
3731        # Now we do the links, which are complicated by the need to know two
3732        # things: the target URL and the text.
3733        while ($retVal =~ /\[link\s+([^\]]+)\]([^\[]+)\[\/link\]/g) {
3734            # Replace the matched string with the Wiki markup for links. Note that
3735            # $-[0] is the starting position of the match for the entire expression,
3736            # and $+[0] is past the ending position.
3737            substr $retVal, $-[0], $+[0] - $-[0], WikiTools::LinkMarkup($1, $2);
3738        }
3739        # Return the result.
3740        return $retVal;
3741    }
3742    
3743  =head3 BeginTran  =head3 BeginTran
3744    
3745      $erdb->BeginTran();      $erdb->BeginTran();
# Line 3840  Line 4022 
4022    
4023  =over 4  =over 4
4024    
4025    =item indexObject
4026    
4027  ERDB XML structure for an index.  ERDB XML structure for an index.
4028    
4029  =item RETURN  =item RETURN
# Line 4499  Line 4683 
4683      # be a null string.      # be a null string.
4684      if ($fileName ne "") {      if ($fileName ne "") {
4685          # Load the relation from the file.          # Load the relation from the file.
4686          $retVal = $self->LoadTable($fileName, $relationName, $rebuild);          $retVal = $self->LoadTable($fileName, $relationName, truncate => $rebuild);
4687      } elsif ($rebuild) {      } elsif ($rebuild) {
4688          # Here we are rebuilding, but no file exists, so we just re-create the table.          # Here we are rebuilding, but no file exists, so we just re-create the table.
4689          $self->CreateTable($relationName, 1);          $self->CreateTable($relationName, 1);
# Line 5278  Line 5462 
5462      # Get the parameters.      # Get the parameters.
5463      my ($relationshipName, $relationshipStructure) = @_;      my ($relationshipName, $relationshipStructure) = @_;
5464      # Format the relationship sentence.      # Format the relationship sentence.
5465      my $result = "$relationshipStructure->{from} <b>$relationshipName</b> $relationshipStructure->{to}";      my $result = "$relationshipStructure->{from} $relationshipName $relationshipStructure->{to}";
5466      # Compute the arity.      # Compute the arity.
5467      my $arityCode = $relationshipStructure->{arity};      my $arityCode = $relationshipStructure->{arity};
5468      my $arity = $ArityTable{$arityCode};      my $arity = $ArityTable{$arityCode};
# Line 5323  Line 5507 
5507      return $result;      return $result;
5508  }  }
5509    
5510    =head3 _WikiRelationTable
5511    
5512    Generate the Wiki text for a particular relation. The relation's data will be formatted as a
5513    table with three columns-- the field name, the field type, and the field description.
5514    
5515    This is a static method.
5516    
5517    =over 4
5518    
5519    =item relationName
5520    
5521    Name of the relation being formatted.
5522    
5523    =item relationData
5524    
5525    Hash containing the relation's fields and indexes.
5526    
5527    =item RETURN
5528    
5529    Returns a Wiki string that can be used to display the relation name and all of its fields.
5530    
5531    =back
5532    
5533    =cut
5534    
5535    sub _WikiRelationTable {
5536        # Get the parameters.
5537        my ($relationName, $relationData) = @_;
5538        # We'll create a list of lists in here, then call WikiTools::Table to
5539        # convert it into a table.
5540        my @rows = ();
5541        # Push in the header row.
5542        push @rows, [qw(Field Type Description)];
5543        # Loop through the fields.
5544        for my $field (@{$relationData->{Fields}}) {
5545            # Create this field's row. We always have a name and type.
5546            my @row = ($field->{name}, $field->{type});
5547            # If we have a description, add it as the third column.
5548            if (exists $field->{Notes}) {
5549                push @row, WikiNote($field->{Notes}->{content});
5550            }
5551            # Push this row onto the table list.
5552            push @rows, \@row;
5553        }
5554        # Store the rows as a Wiki table.
5555        my $retVal = WikiTools::Table(@rows);
5556        # Now we show the relation's indexes. These are formatted as another
5557        # table.
5558        @rows = ();
5559        # Push in the header row.
5560        push @rows, [qw(Index Unique Fields Notes)];
5561        # Get the index hash.
5562        my $indexTable = $relationData->{Indexes};
5563        # Loop through the indexes. For an entity, there is always at least one index.
5564        # For a relationship, there are at least two. The upshot is we don't need to
5565        # worry about accidentally generating a frivolous table here.
5566        for my $indexName (sort keys %$indexTable) {
5567            my $indexData = $indexTable->{$indexName};
5568            # Determine whether or not the index is unique.
5569            my $unique = ((exists $indexData->{Unique} && $indexData->{Unique} eq "true") ?
5570                          "yes" : "");
5571            # Get the field list.
5572            my $fields = join(', ', @{$indexData->{IndexFields}});
5573            # Get the note text.
5574            my $description = "";
5575            if (my $note = $indexData->{Notes}) {
5576                $description = WikiNote($note->{content});
5577            }
5578            # Format this row.
5579            my @row = ($indexName, $unique, $fields, $description);
5580            push @rows, \@row;
5581        }
5582        # Add the index list to the result.
5583        $retVal .= "\n\n" . WikiTools::Table(@rows);
5584    }
5585    
5586    
5587  =head3 _ShowRelationTable  =head3 _ShowRelationTable
5588    
5589  Generate the HTML string for a particular relation. The relation's data will be formatted as an HTML  Generate the HTML string for a particular relation. The relation's data will be formatted as an HTML

Legend:
Removed from v.1.94  
changed lines
  Added in v.1.95

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3