[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.97, Wed May 7 23:08:19 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;
15    
16  =head1 Entity-Relationship Database Package  =head1 Entity-Relationship Database Package
17    
# Line 408  Line 409 
409                    XMLDecl => 1,                    XMLDecl => 1,
410                   );                   );
411    
   
412  =head2 Public Methods  =head2 Public Methods
413    
414  =head3 new  =head3 new
# Line 433  Line 433 
433    
434  sub new {  sub new {
435      # Get the parameters.      # Get the parameters.
436      my ($class, $dbh, $metaFileName, $options) = @_;      my ($class, $dbh, $metaFileName, %options) = @_;
437      # Load the meta-data.      # Load the meta-data.
438      my $metaData = _LoadMetaData($metaFileName);      my $metaData = _LoadMetaData($metaFileName);
439      # Create the object.      # Create the object.
# Line 657  Line 657 
657      return Data::Dumper::Dumper($self->{_metaData});      return Data::Dumper::Dumper($self->{_metaData});
658  }  }
659    
660    =head3 GenerateWikiData
661    
662        my @wikiLines = $erdb->GenerateWikiData();
663    
664    Build a description of the database for the wiki. The database will be
665    organized into a single page, with sections for each entity and relationship.
666    The return value is a list of text lines.
667    
668    =cut
669    
670    sub GenerateWikiData {
671        # Get the parameters.
672        my ($self) = @_;
673        # We'll build the wiki text in here.
674        my @retVal = ();
675        # Get the metadata object.
676        my $metadata = $self->{_metaData};
677        # Get the title string. This will become the page name.
678        my $title = $metadata->{Title}->{content};
679        # Get the entity and relationship lists.
680        my $entityList = $metadata->{Entities};
681        my $relationshipList = $metadata->{Relationships};
682        # Start with the introductory text.
683        push @retVal, WikiTools::Heading(2, "Introduction");
684        if (my $notes = $metadata->{Notes}) {
685            push @retVal, WikiNote($notes->{content});
686        }
687        # Start the entity section.
688        push @retVal, WikiTools::Heading(2, "Entities");
689        # Loop through the entities. Note that unlike the situation with HTML, we
690        # don't need to generate the table of contents manually, just the data
691        # itself.
692        for my $key (sort keys %$entityList) {
693            # Create a header for this entity.
694            push @retVal, "", WikiTools::Heading(3, $key);
695            # Get the entity data.
696            my $entityData = $entityList->{$key};
697            # Plant the notes here, if there are any.
698            if (my $notes = $entityData->{Notes}) {
699                push @retVal, "", WikiNote($notes->{content});
700            }
701            # Get the entity's relations.
702            my $relationList = $entityData->{Relations};
703            # Loop through the relations, displaying them.
704            for my $relation (sort keys %{$relationList}) {
705                my $wikiString = _WikiRelationTable($relation, $relationList->{$relation});
706                push @retVal, $wikiString;
707            }
708            # Now we list the entity's relationships (if any). First, we build a list
709            # of the relationships relevant to this entity.
710            my @rels = ();
711            for my $rel (sort keys %$relationshipList) {
712                my $relStructure = $relationshipList->{$rel};
713                if ($relStructure->{from} eq $key || $relStructure->{to} eq $key) {
714                    # Get the relationship sentence.
715                    my $relSentence = _ComputeRelationshipSentence($rel, $relStructure);
716                    # Linkify it.
717                    my $linkedRel = WikiTools::LinkMarkup("#$rel", $rel);
718                    $relSentence =~ s/$rel/$linkedRel/;
719                    push @rels, $relSentence;
720                }
721            }
722            # Add the relationships as a Wiki list.
723            push @retVal, WikiTools::List(@rels);
724        }
725        # Now the entities are documented. Next we do the relationships.
726        push @retVal, WikiTools::Heading(2, "Relationships");
727        for my $key (sort keys %$relationshipList) {
728            my $relationshipData = $relationshipList->{$key};
729            # Create the relationship heading.
730            push @retVal, WikiTools::Heading(3, $key);
731            # Describe the relationship arity. Note there's a bit of trickiness involving recursive
732            # many-to-many relationships. In a normal many-to-many we use two sentences to describe
733            # the arity (one for each direction). This is a bad idea for a recursive relationship,
734            # since both sentences will say the same thing.
735            my $arity = $relationshipData->{arity};
736            my $fromEntity = $relationshipData->{from};
737            my $toEntity = $relationshipData->{to};
738            my @listElements = ();
739            my $boldCode = WikiTools::BoldCode();
740            if ($arity eq "11") {
741                push @listElements, "Each $boldCode$fromEntity$boldCode relates to at most one $boldCode$toEntity$boldCode.";
742            } else {
743                push @listElements, "Each $boldCode$fromEntity$boldCode relates to multiple $boldCode${toEntity}s$boldCode.\n";
744                if ($arity eq "MM" && $fromEntity ne $toEntity) {
745                    push @listElements, "Each $boldCode$toEntity$boldCode relates to multiple $boldCode${fromEntity}s$boldCode.\n";
746                }
747            }
748            push @retVal, WikiTools::List(@listElements);
749            # Plant the notes here, if there are any.
750            if (my $notes = $relationshipData->{Notes}) {
751                push @retVal, "", WikiNote($notes->{content});
752            }
753            # Finally, the relationship table.
754            my $wikiString = _WikiRelationTable($key, $relationshipData->{Relations}->{$key});
755            push @retVal, $wikiString;
756        }
757        # All done. Return the lines.
758        return @retVal;
759    }
760    
761    
762  =head3 CreatePPO  =head3 CreatePPO
763    
764      ERDB::CreatePPO($erdbXMLFile, $ppoXMLFile);      ERDB::CreatePPO($erdbXMLFile, $ppoXMLFile);
# Line 1999  Line 2101 
2101          # Loop through the ends of the relationship.          # Loop through the ends of the relationship.
2102          for my $dir ('from', 'to') {          for my $dir ('from', 'to') {
2103              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);  
2104                  $found = 1;                  $found = 1;
2105                    # Here we want to delete all relationship instances on this side of the
2106                    # entity instance.
2107                    Trace("Disconnecting in $dir direction with ID \"$originEntityID\".");
2108                    # We do this delete in batches to keep it from dragging down the
2109                    # server.
2110                    my $limitClause = ($FIG_Config::delete_limit ? "LIMIT $FIG_Config::delete_limit" : "");
2111                    my $done = 0;
2112                    while (! $done) {
2113                        # Do the delete.
2114                        my $rows = $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ? $limitClause", 0, $originEntityID);
2115                        # See if we're done. We're done if no rows were found or the delete is unlimited.
2116                        $done = ($rows == 0 || ! $limitClause);
2117                    }
2118              }              }
2119          }          }
2120          # Insure we found the entity on at least one end.          # Insure we found the entity on at least one end.
# Line 2723  Line 2835 
2835    
2836  =head3 LoadTable  =head3 LoadTable
2837    
2838      my $results = $erdb->LoadTable($fileName, $relationName, $truncateFlag);      my $results = $erdb->LoadTable($fileName, $relationName, %options);
2839    
2840  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
2841  first.  first.
# Line 2738  Line 2850 
2850    
2851  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.
2852    
2853  =item truncateFlag  =item options
2854    
2855  TRUE if the table should be dropped and re-created, else FALSE  A hash of load options.
2856    
2857  =item RETURN  =item RETURN
2858    
# Line 2748  Line 2860 
2860    
2861  =back  =back
2862    
2863    The permissible options are as follows.
2864    
2865    =over 4
2866    
2867    =item truncate
2868    
2869    If TRUE, then the table will be erased before loading.
2870    
2871    =item mode
2872    
2873    Mode in which the load should operate, either C<low_priority> or C<concurrent>.
2874    This option is only applicable to a MySQL database.
2875    
2876    =item partial
2877    
2878    If TRUE, then it is assumed that this is a partial load, and the table will not
2879    be analyzed and compacted at the end.
2880    
2881    =back
2882    
2883  =cut  =cut
2884  sub LoadTable {  sub LoadTable {
2885      # Get the parameters.      # Get the parameters.
2886      my ($self, $fileName, $relationName, $truncateFlag) = @_;      my ($self, $fileName, $relationName, %options) = @_;
2887      # Create the statistical return object.      # Create the statistical return object.
2888      my $retVal = _GetLoadStats();      my $retVal = _GetLoadStats();
2889      # Trace the fact of the load.      # Trace the fact of the load.
# Line 2763  Line 2895 
2895      # Get the relation data.      # Get the relation data.
2896      my $relation = $self->_FindRelation($relationName);      my $relation = $self->_FindRelation($relationName);
2897      # Check the truncation flag.      # Check the truncation flag.
2898      if ($truncateFlag) {      if ($options{truncate}) {
2899          Trace("Creating table $relationName") if T(2);          Trace("Creating table $relationName") if T(2);
2900          # 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,
2901          # 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 2921 
2921      # Load the table.      # Load the table.
2922      my $rv;      my $rv;
2923      eval {      eval {
2924          $rv = $dbh->load_table(file => $fileName, tbl => $relationName);          $rv = $dbh->load_table(file => $fileName, tbl => $relationName, style => $options{mode});
2925      };      };
2926      if (!defined $rv) {      if (!defined $rv) {
2927          $retVal->AddMessage($@) if ($@);          $retVal->AddMessage($@) if ($@);
# Line 2800  Line 2932 
2932          $retVal->Add("tables");          $retVal->Add("tables");
2933          my $size = -s $fileName;          my $size = -s $fileName;
2934          Trace("$size bytes loaded into $relationName.") if T(2);          Trace("$size bytes loaded into $relationName.") if T(2);
2935            $retVal->Add("bytes", $size);
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.97

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3