[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.98, Mon Jul 7 19:05:55 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 389  Line 390 
390                                    Entities => 'Entity',                                    Entities => 'Entity',
391                                    Fields => 'Field',                                    Fields => 'Field',
392                                    Indexes => 'Index',                                    Indexes => 'Index',
393                                    IndexFields => 'IndexField'                                    IndexFields => 'IndexField',
394                                      Issues => 'Issue',
395                                      Shapes => 'Shape'
396                                  },                                  },
397                    KeyAttr =>    { Relationship => 'name',                    KeyAttr =>    { Relationship => 'name',
398                                    Entity => 'name',                                    Entity => 'name',
399                                    Field => 'name'                                    Field => 'name',
400                                      Shape => 'name'
401                                  },                                  },
402                    SuppressEmpty => 1,                    SuppressEmpty => 1,
403                   );                   );
404    
405  my %XmlInOpts  = (  my %XmlInOpts  = (
406                    ForceArray => ['Field', 'Index', 'IndexField', 'Relationship', 'Entity'],                    ForceArray => [qw(Field Index IndexField Relationship Entity Shape)],
407                    ForceContent => 1,                    ForceContent => 1,
408                    NormalizeSpace => 2,                    NormalizeSpace => 2,
409                   );                   );
# Line 408  Line 412 
412                    XMLDecl => 1,                    XMLDecl => 1,
413                   );                   );
414    
   
415  =head2 Public Methods  =head2 Public Methods
416    
417  =head3 new  =head3 new
# Line 433  Line 436 
436    
437  sub new {  sub new {
438      # Get the parameters.      # Get the parameters.
439      my ($class, $dbh, $metaFileName, $options) = @_;      my ($class, $dbh, $metaFileName, %options) = @_;
440      # Load the meta-data.      # Load the meta-data.
441      my $metaData = _LoadMetaData($metaFileName);      my $metaData = _LoadMetaData($metaFileName);
442      # Create the object.      # Create the object.
# Line 657  Line 660 
660      return Data::Dumper::Dumper($self->{_metaData});      return Data::Dumper::Dumper($self->{_metaData});
661  }  }
662    
663    =head3 GenerateWikiData
664    
665        my @wikiLines = $erdb->GenerateWikiData();
666    
667    Build a description of the database for the wiki. The database will be
668    organized into a single page, with sections for each entity and relationship.
669    The return value is a list of text lines.
670    
671    =cut
672    
673    sub GenerateWikiData {
674        # Get the parameters.
675        my ($self) = @_;
676        # We'll build the wiki text in here.
677        my @retVal = ();
678        # Get the metadata object.
679        my $metadata = $self->{_metaData};
680        # Get the title string. This will become the page name.
681        my $title = $metadata->{Title}->{content};
682        # Get the entity and relationship lists.
683        my $entityList = $metadata->{Entities};
684        my $relationshipList = $metadata->{Relationships};
685        my $shapeList = $metadata->{Shapes};
686        # Start with the introductory text.
687        push @retVal, WikiTools::Heading(2, "Introduction");
688        if (my $notes = $metadata->{Notes}) {
689            push @retVal, WikiNote($notes->{content});
690        }
691        # Generate issue list.
692        if (my $issues = $metadata->{Issues}) {
693            push @retVal, WikiTools::Heading(3, 'Issues');
694            push @retVal, WikiTools::List(map { $_->{content} } @{$issues});
695        }
696        # Start the entity section.
697        push @retVal, WikiTools::Heading(2, "Entities");
698        # Loop through the entities. Note that unlike the situation with HTML, we
699        # don't need to generate the table of contents manually, just the data
700        # itself.
701        for my $key (sort keys %$entityList) {
702            # Create a header for this entity.
703            push @retVal, "", WikiTools::Heading(3, $key);
704            # Get the entity data.
705            my $entityData = $entityList->{$key};
706            # Plant the notes here, if there are any.
707            push @retVal, _ObjectNotes($entityData);
708            # Get the entity's relations.
709            my $relationList = $entityData->{Relations};
710            # Loop through the relations, displaying them.
711            for my $relation (sort keys %{$relationList}) {
712                my $wikiString = _WikiRelationTable($relation, $relationList->{$relation});
713                push @retVal, $wikiString;
714            }
715            # Now we list the entity's relationships (if any). First, we build a list
716            # of the relationships relevant to this entity.
717            my @rels = ();
718            for my $rel (sort keys %$relationshipList) {
719                my $relStructure = $relationshipList->{$rel};
720                if ($relStructure->{from} eq $key || $relStructure->{to} eq $key) {
721                    # Get the relationship sentence.
722                    my $relSentence = _ComputeRelationshipSentence($rel, $relStructure);
723                    # Linkify it.
724                    my $linkedRel = WikiTools::LinkMarkup("#$rel", $rel);
725                    $relSentence =~ s/$rel/$linkedRel/;
726                    push @rels, $relSentence;
727                }
728            }
729            # Add the relationships as a Wiki list.
730            push @retVal, WikiTools::List(@rels);
731        }
732        # Now the entities are documented. Next we do the relationships.
733        push @retVal, WikiTools::Heading(2, "Relationships");
734        for my $key (sort keys %$relationshipList) {
735            my $relationshipData = $relationshipList->{$key};
736            # Create the relationship heading.
737            push @retVal, WikiTools::Heading(3, $key);
738            # Describe the relationship arity. Note there's a bit of trickiness involving recursive
739            # many-to-many relationships. In a normal many-to-many we use two sentences to describe
740            # the arity (one for each direction). This is a bad idea for a recursive relationship,
741            # since both sentences will say the same thing.
742            my $arity = $relationshipData->{arity};
743            my $fromEntity = $relationshipData->{from};
744            my $toEntity = $relationshipData->{to};
745            my @listElements = ();
746            my $boldCode = WikiTools::BoldCode();
747            if ($arity eq "11") {
748                push @listElements, "Each $boldCode$fromEntity$boldCode relates to at most one $boldCode$toEntity$boldCode.";
749            } else {
750                push @listElements, "Each $boldCode$fromEntity$boldCode relates to multiple $boldCode${toEntity}s$boldCode.";
751                if ($arity eq "MM" && $fromEntity ne $toEntity) {
752                    push @listElements, "Each $boldCode$toEntity$boldCode relates to multiple $boldCode${fromEntity}s$boldCode.";
753                }
754            }
755            push @retVal, WikiTools::List(@listElements);
756            # Plant the notes here, if there are any.
757            push @retVal, _ObjectNotes($relationshipData);
758            # Finally, the relationship table.
759            my $wikiString = _WikiRelationTable($key, $relationshipData->{Relations}->{$key});
760            push @retVal, $wikiString;
761        }
762        # Now loop through the miscellaneous shapes.
763        if ($shapeList) {
764            push @retVal, WikiTools::Heading(2, "Miscellaneous");
765            for my $shape (sort keys %$shapeList) {
766                push @retVal, WikiTools::Heading(3, $shape);
767                my $shapeData = $shapeList->{$shape};
768                push @retVal, _ObjectNotes($shapeData);
769            }
770        }
771        # All done. Return the lines.
772        return @retVal;
773    }
774    
775    
776  =head3 CreatePPO  =head3 CreatePPO
777    
778      ERDB::CreatePPO($erdbXMLFile, $ppoXMLFile);      ERDB::CreatePPO($erdbXMLFile, $ppoXMLFile);
# Line 1999  Line 2115 
2115          # Loop through the ends of the relationship.          # Loop through the ends of the relationship.
2116          for my $dir ('from', 'to') {          for my $dir ('from', 'to') {
2117              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);  
2118                  $found = 1;                  $found = 1;
2119                    # Here we want to delete all relationship instances on this side of the
2120                    # entity instance.
2121                    Trace("Disconnecting in $dir direction with ID \"$originEntityID\".");
2122                    # We do this delete in batches to keep it from dragging down the
2123                    # server.
2124                    my $limitClause = ($FIG_Config::delete_limit ? "LIMIT $FIG_Config::delete_limit" : "");
2125                    my $done = 0;
2126                    while (! $done) {
2127                        # Do the delete.
2128                        my $rows = $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ? $limitClause", 0, $originEntityID);
2129                        # See if we're done. We're done if no rows were found or the delete is unlimited.
2130                        $done = ($rows == 0 || ! $limitClause);
2131                    }
2132              }              }
2133          }          }
2134          # Insure we found the entity on at least one end.          # Insure we found the entity on at least one end.
# Line 2723  Line 2849 
2849    
2850  =head3 LoadTable  =head3 LoadTable
2851    
2852      my $results = $erdb->LoadTable($fileName, $relationName, $truncateFlag);      my $results = $erdb->LoadTable($fileName, $relationName, %options);
2853    
2854  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
2855  first.  first.
# Line 2738  Line 2864 
2864    
2865  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.
2866    
2867  =item truncateFlag  =item options
2868    
2869  TRUE if the table should be dropped and re-created, else FALSE  A hash of load options.
2870    
2871  =item RETURN  =item RETURN
2872    
# Line 2748  Line 2874 
2874    
2875  =back  =back
2876    
2877    The permissible options are as follows.
2878    
2879    =over 4
2880    
2881    =item truncate
2882    
2883    If TRUE, then the table will be erased before loading.
2884    
2885    =item mode
2886    
2887    Mode in which the load should operate, either C<low_priority> or C<concurrent>.
2888    This option is only applicable to a MySQL database.
2889    
2890    =item partial
2891    
2892    If TRUE, then it is assumed that this is a partial load, and the table will not
2893    be analyzed and compacted at the end.
2894    
2895    =back
2896    
2897  =cut  =cut
2898  sub LoadTable {  sub LoadTable {
2899      # Get the parameters.      # Get the parameters.
2900      my ($self, $fileName, $relationName, $truncateFlag) = @_;      my ($self, $fileName, $relationName, %options) = @_;
2901      # Create the statistical return object.      # Create the statistical return object.
2902      my $retVal = _GetLoadStats();      my $retVal = _GetLoadStats();
2903      # Trace the fact of the load.      # Trace the fact of the load.
# Line 2763  Line 2909 
2909      # Get the relation data.      # Get the relation data.
2910      my $relation = $self->_FindRelation($relationName);      my $relation = $self->_FindRelation($relationName);
2911      # Check the truncation flag.      # Check the truncation flag.
2912      if ($truncateFlag) {      if ($options{truncate}) {
2913          Trace("Creating table $relationName") if T(2);          Trace("Creating table $relationName") if T(2);
2914          # 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,
2915          # 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 2935 
2935      # Load the table.      # Load the table.
2936      my $rv;      my $rv;
2937      eval {      eval {
2938          $rv = $dbh->load_table(file => $fileName, tbl => $relationName);          $rv = $dbh->load_table(file => $fileName, tbl => $relationName, style => $options{mode});
2939      };      };
2940      if (!defined $rv) {      if (!defined $rv) {
2941          $retVal->AddMessage($@) if ($@);          $retVal->AddMessage($@) if ($@);
# Line 2800  Line 2946 
2946          $retVal->Add("tables");          $retVal->Add("tables");
2947          my $size = -s $fileName;          my $size = -s $fileName;
2948          Trace("$size bytes loaded into $relationName.") if T(2);          Trace("$size bytes loaded into $relationName.") if T(2);
2949            $retVal->Add("bytes", $size);
2950          # If we're rebuilding, we need to create the table indexes.          # If we're rebuilding, we need to create the table indexes.
2951          if ($truncateFlag) {          if ($options{truncate}) {
2952              # Indexes are created here for PostGres. For PostGres, indexes are              # Indexes are created here for PostGres. For PostGres, indexes are
2953              # best built at the end. For MySQL, the reverse is true.              # best built at the end. For MySQL, the reverse is true.
2954              if (! $dbh->{_preIndex}) {              if (! $dbh->{_preIndex}) {
# Line 2822  Line 2969 
2969          }          }
2970      }      }
2971      # Analyze the table to improve performance.      # Analyze the table to improve performance.
2972        if (! $options{partial}) {
2973      Trace("Analyzing and compacting $relationName.") if T(3);      Trace("Analyzing and compacting $relationName.") if T(3);
2974      $dbh->vacuum_it($relationName);      $dbh->vacuum_it($relationName);
2975        }
2976      Trace("$relationName load completed.") if T(3);      Trace("$relationName load completed.") if T(3);
2977      # Return the statistics.      # Return the statistics.
2978      return $retVal;      return $retVal;
# Line 3558  Line 3707 
3707      return $retVal;      return $retVal;
3708  }  }
3709    
3710    =head3 WikiNote
3711    
3712    Convert a note or comment to Wiki text by replacing some bulletin-board codes with HTML. The codes
3713    supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph.
3714    Except for C<[p]>, all the codes are closed by slash-codes. So, for
3715    example, C<[b]Feature[/b]> displays the string C<Feature> in boldface.
3716    
3717        my $wikiText = ERDB::WikiNote($dataString);
3718    
3719    =over 4
3720    
3721    =item dataString
3722    
3723    String to convert to Wiki text.
3724    
3725    =item RETURN
3726    
3727    An Wiki text string derived from the input string.
3728    
3729    =back
3730    
3731    =cut
3732    
3733    sub WikiNote {
3734        # Get the parameter.
3735        my ($dataString) = @_;
3736        # HTML-escape the text.
3737        my $retVal = CGI::escapeHTML($dataString);
3738        # Substitute the bulletin board codes.
3739        my $italic = WikiTools::ItalicCode();
3740        $retVal =~ s/\[\/?i\]/$italic/g;
3741        my $bold = WikiTools::BoldCode();
3742        $retVal =~ s/\[\/?b\]/$bold/g;
3743        # Paragraph breaks are the same no matter which Wiki you're using.
3744        $retVal =~ s!\[p\]!\n\n!g;
3745        # Now we do the links, which are complicated by the need to know two
3746        # things: the target URL and the text.
3747        while ($retVal =~ /\[link\s+([^\]]+)\]([^\[]+)\[\/link\]/g) {
3748            # Replace the matched string with the Wiki markup for links. Note that
3749            # $-[0] is the starting position of the match for the entire expression,
3750            # and $+[0] is past the ending position.
3751            substr $retVal, $-[0], $+[0] - $-[0], WikiTools::LinkMarkup($1, $2);
3752        }
3753        # Return the result.
3754        return $retVal;
3755    }
3756    
3757  =head3 BeginTran  =head3 BeginTran
3758    
3759      $erdb->BeginTran();      $erdb->BeginTran();
# Line 3840  Line 4036 
4036    
4037  =over 4  =over 4
4038    
4039    =item indexObject
4040    
4041  ERDB XML structure for an index.  ERDB XML structure for an index.
4042    
4043  =item RETURN  =item RETURN
# Line 4499  Line 4697 
4697      # be a null string.      # be a null string.
4698      if ($fileName ne "") {      if ($fileName ne "") {
4699          # Load the relation from the file.          # Load the relation from the file.
4700          $retVal = $self->LoadTable($fileName, $relationName, $rebuild);          $retVal = $self->LoadTable($fileName, $relationName, truncate => $rebuild);
4701      } elsif ($rebuild) {      } elsif ($rebuild) {
4702          # 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.
4703          $self->CreateTable($relationName, 1);          $self->CreateTable($relationName, 1);
# Line 5246  Line 5444 
5444      return $retVal;      return $retVal;
5445  }  }
5446    
5447  =head2 HTML Documentation Utility Methods  =head2 Documentation Utility Methods
5448    
5449  =head3 _ComputeRelationshipSentence  =head3 _ComputeRelationshipSentence
5450    
# Line 5278  Line 5476 
5476      # Get the parameters.      # Get the parameters.
5477      my ($relationshipName, $relationshipStructure) = @_;      my ($relationshipName, $relationshipStructure) = @_;
5478      # Format the relationship sentence.      # Format the relationship sentence.
5479      my $result = "$relationshipStructure->{from} <b>$relationshipName</b> $relationshipStructure->{to}";      my $result = "$relationshipStructure->{from} $relationshipName $relationshipStructure->{to}";
5480      # Compute the arity.      # Compute the arity.
5481      my $arityCode = $relationshipStructure->{arity};      my $arityCode = $relationshipStructure->{arity};
5482      my $arity = $ArityTable{$arityCode};      my $arity = $ArityTable{$arityCode};
# Line 5323  Line 5521 
5521      return $result;      return $result;
5522  }  }
5523    
5524    =head3 _WikiRelationTable
5525    
5526    Generate the Wiki text for a particular relation. The relation's data will be formatted as a
5527    table with three columns-- the field name, the field type, and the field description.
5528    
5529    This is a static method.
5530    
5531    =over 4
5532    
5533    =item relationName
5534    
5535    Name of the relation being formatted.
5536    
5537    =item relationData
5538    
5539    Hash containing the relation's fields and indexes.
5540    
5541    =item RETURN
5542    
5543    Returns a Wiki string that can be used to display the relation name and all of its fields.
5544    
5545    =back
5546    
5547    =cut
5548    
5549    sub _WikiRelationTable {
5550        # Get the parameters.
5551        my ($relationName, $relationData) = @_;
5552        # We'll create a list of lists in here, then call WikiTools::Table to
5553        # convert it into a table.
5554        my @rows = ();
5555        # Push in the header row.
5556        push @rows, [qw(Field Type Description)];
5557        # Loop through the fields.
5558        for my $field (@{$relationData->{Fields}}) {
5559            # Create this field's row. We always have a name and type.
5560            my @row = ($field->{name}, $field->{type});
5561            # If we have a description, add it as the third column.
5562            if (exists $field->{Notes}) {
5563                push @row, WikiNote($field->{Notes}->{content});
5564            }
5565            # Push this row onto the table list.
5566            push @rows, \@row;
5567        }
5568        # Store the rows as a Wiki table with a level-4 heading.
5569        my $retVal = join("\n\n", WikiTools::Heading(4, "$relationName Table"),
5570                          WikiTools::Table(@rows));
5571        # Now we show the relation's indexes. These are formatted as another
5572        # table.
5573        @rows = ();
5574        # Push in the header row.
5575        push @rows, [qw(Index Unique Fields Notes)];
5576        # Get the index hash.
5577        my $indexTable = $relationData->{Indexes};
5578        # Loop through the indexes. For an entity, there is always at least one index.
5579        # For a relationship, there are at least two. The upshot is we don't need to
5580        # worry about accidentally generating a frivolous table here.
5581        for my $indexName (sort keys %$indexTable) {
5582            my $indexData = $indexTable->{$indexName};
5583            # Determine whether or not the index is unique.
5584            my $unique = ((exists $indexData->{Unique} && $indexData->{Unique} eq "true") ?
5585                          "yes" : "");
5586            # Get the field list.
5587            my $fields = join(', ', @{$indexData->{IndexFields}});
5588            # Get the note text.
5589            my $description = "";
5590            if (my $note = $indexData->{Notes}) {
5591                $description = WikiNote($note->{content});
5592            }
5593            # Format this row.
5594            my @row = ($indexName, $unique, $fields, $description);
5595            push @rows, \@row;
5596        }
5597        # Add the index list to the result.
5598        $retVal .= "\n\n" . WikiTools::Table(@rows);
5599    }
5600    
5601  =head3 _ShowRelationTable  =head3 _ShowRelationTable
5602    
5603  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
# Line 5496  Line 5771 
5771      return $htmlString;      return $htmlString;
5772  }  }
5773    
5774    =head3 _ObjectNotes
5775    
5776        my @noteParagraphs = _ObjectNotes($objectData);
5777    
5778    Return a list of the notes and asides for an entity or relationship in
5779    Wiki format.
5780    
5781    =over 4
5782    
5783    =item objectData
5784    
5785    The metadata for the desired entity or relationship.
5786    
5787    =item RETURN
5788    
5789    Returns a list of text paragraphs in Wiki markup form.
5790    
5791    =back
5792    
5793    =cut
5794    
5795    sub _ObjectNotes {
5796        # Get the parameters.
5797        my ($objectData) = @_;
5798        # Declare the return variable.
5799        my @retVal;
5800        # Loop through the types of notes.
5801        for my $noteType (qw(Notes Asides)) {
5802            my $text = $objectData->{$noteType};
5803            if ($text) {
5804                push @retVal, "", WikiNote($text->{content});
5805            }
5806        }
5807        # Return the result.
5808        return @retVal;
5809    }
5810    
5811  1;  1;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3