[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.101, Wed Sep 3 20:56:20 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            # 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            # Get the entity's relations.
725            my $relationList = $entityData->{Relations};
726            # Loop through the relations, displaying them.
727            for my $relation (sort keys %{$relationList}) {
728                my $wikiString = _WikiRelationTable($relation, $relationList->{$relation});
729                push @retVal, $wikiString;
730            }
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 946  Line 1062 
1062          # Push the result into the field list.          # Push the result into the field list.
1063          push @fieldList, $fieldString;          push @fieldList, $fieldString;
1064      }      }
     # If this is a root table, add the "new_record" flag. It defaults to 0, so  
     if ($rootFlag) {  
         push @fieldList, "new_record $TypeTable{boolean}->{sqlType} NOT NULL DEFAULT 0";  
     }  
1065      # Convert the field list into a comma-delimited string.      # Convert the field list into a comma-delimited string.
1066      my $fieldThing = join(', ', @fieldList);      my $fieldThing = join(', ', @fieldList);
1067      # Insure the table is not already there.      # Insure the table is not already there.
# Line 1416  Line 1528 
1528      return sort keys %{$entityList};      return sort keys %{$entityList};
1529  }  }
1530    
1531    =head3 GetConnectingRelationships
1532    
1533        my @list = $erdb->GetConnectingRelationships($entityName);
1534    
1535    Return a list of the relationships connected to the specified entity.
1536    
1537    =over 4
1538    
1539    =item entityName
1540    
1541    Entity whose connected relationships are desired.
1542    
1543    =item RETURN
1544    
1545    Returns a list of the relationships that originate from the entity.
1546    If the entity is on the from end, it will return the relationship
1547    name. If the entity is on the to end it will return the converse of
1548    the relationship name.
1549    
1550    =back
1551    
1552    =cut
1553    
1554    sub GetConnectingRelationships {
1555        # Get the parameters.
1556        my ($self, $entityName) = @_;
1557        # Declare the return variable.
1558        my @retVal;
1559        # Get the relationship list.
1560        my $relationships = $self->{_metaData}->{Relationships};
1561        # Find the entity.
1562        my $entity = $self->{_metaData}->{Entities}->{$entityName};
1563        # Only proceed if the entity exists.
1564        if (! defined $entity) {
1565            Trace("Entity $entityName not found.") if T(3);
1566        } else {
1567            # Loop through the relationships.
1568            my @rels = keys %$relationships;
1569            Trace(scalar(@rels) . " relationships found in connection search.") if T(3);
1570            for my $relationshipName (@rels) {
1571                my $relationship = $relationships->{$relationshipName};
1572                if ($relationship->{from} eq $entityName) {
1573                    # Here we have a forward relationship.
1574                    push @retVal, $relationshipName;
1575                } elsif ($relationship->{to} eq $entityName) {
1576                    # Here we have a backward relationship. In this case, the
1577                    # converse relationship name is preferred if it exists.
1578                    my $converse = $relationship->{converse} || $relationshipName;
1579                    push @retVal, $converse;
1580                }
1581            }
1582        }
1583        # Return the result.
1584        return @retVal;
1585    }
1586    
1587    
1588    
1589    
1590  =head3 GetDataTypes  =head3 GetDataTypes
1591    
1592      my %types = ERDB::GetDataTypes();      my %types = ERDB::GetDataTypes();
# Line 1999  Line 2170 
2170          # Loop through the ends of the relationship.          # Loop through the ends of the relationship.
2171          for my $dir ('from', 'to') {          for my $dir ('from', 'to') {
2172              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);  
2173                  $found = 1;                  $found = 1;
2174                    # Here we want to delete all relationship instances on this side of the
2175                    # entity instance.
2176                    Trace("Disconnecting in $dir direction with ID \"$originEntityID\".");
2177                    # We do this delete in batches to keep it from dragging down the
2178                    # server.
2179                    my $limitClause = ($FIG_Config::delete_limit ? "LIMIT $FIG_Config::delete_limit" : "");
2180                    my $done = 0;
2181                    while (! $done) {
2182                        # Do the delete.
2183                        my $rows = $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ? $limitClause", 0, $originEntityID);
2184                        # See if we're done. We're done if no rows were found or the delete is unlimited.
2185                        $done = ($rows == 0 || ! $limitClause);
2186                    }
2187              }              }
2188          }          }
2189          # Insure we found the entity on at least one end.          # Insure we found the entity on at least one end.
# Line 2190  Line 2371 
2371      }      }
2372      # Now we parse the key names into sort parameters. First, we prime the return      # Now we parse the key names into sort parameters. First, we prime the return
2373      # string.      # string.
2374      my $retVal = "sort -t\"\t\" ";      my $retVal = "sort -S 1G -T\"$FIG_Config::temp\" -t\"\t\" ";
2375      # Get the relation's field list.      # Get the relation's field list.
2376      my @fields = @{$relationData->{Fields}};      my @fields = @{$relationData->{Fields}};
2377      # Loop through the keys.      # Loop through the keys.
# Line 2220  Line 2401 
2401                  # will stop the inner loop. Note that the field number is                  # will stop the inner loop. Note that the field number is
2402                  # 1-based in the sort command, so we have to increment the                  # 1-based in the sort command, so we have to increment the
2403                  # index.                  # index.
2404                  $fieldSpec = ($i + 1) . $modifier;                  my $realI = $i + 1;
2405                    $fieldSpec = "$realI,$realI$modifier";
2406              }              }
2407          }          }
2408          # Add this field to the sort command.          # Add this field to the sort command.
# Line 2611  Line 2793 
2793                  push @missing, $fieldName;                  push @missing, $fieldName;
2794              }              }
2795          }          }
         # If we are the primary relation, add the new-record flag.  
         if ($relationName eq $newObjectType) {  
             push @valueList, 1;  
             push @fieldNameList, "new_record";  
         }  
2796          # Only proceed if there are no missing fields.          # Only proceed if there are no missing fields.
2797          if (@missing > 0) {          if (@missing > 0) {
2798              Trace("Relation $relationName for $newObjectType skipped due to missing fields: " .              Trace("Relation $relationName for $newObjectType skipped due to missing fields: " .
# Line 2723  Line 2900 
2900    
2901  =head3 LoadTable  =head3 LoadTable
2902    
2903      my $results = $erdb->LoadTable($fileName, $relationName, $truncateFlag);      my $results = $erdb->LoadTable($fileName, $relationName, %options);
2904    
2905  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
2906  first.  first.
# Line 2738  Line 2915 
2915    
2916  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.
2917    
2918  =item truncateFlag  =item options
2919    
2920  TRUE if the table should be dropped and re-created, else FALSE  A hash of load options.
2921    
2922  =item RETURN  =item RETURN
2923    
# Line 2748  Line 2925 
2925    
2926  =back  =back
2927    
2928    The permissible options are as follows.
2929    
2930    =over 4
2931    
2932    =item truncate
2933    
2934    If TRUE, then the table will be erased before loading.
2935    
2936    =item mode
2937    
2938    Mode in which the load should operate, either C<low_priority> or C<concurrent>.
2939    This option is only applicable to a MySQL database.
2940    
2941    =item partial
2942    
2943    If TRUE, then it is assumed that this is a partial load, and the table will not
2944    be analyzed and compacted at the end.
2945    
2946    =back
2947    
2948  =cut  =cut
2949  sub LoadTable {  sub LoadTable {
2950      # Get the parameters.      # Get the parameters.
2951      my ($self, $fileName, $relationName, $truncateFlag) = @_;      my ($self, $fileName, $relationName, %options) = @_;
2952      # Create the statistical return object.      # Create the statistical return object.
2953      my $retVal = _GetLoadStats();      my $retVal = _GetLoadStats();
2954      # Trace the fact of the load.      # Trace the fact of the load.
# Line 2763  Line 2960 
2960      # Get the relation data.      # Get the relation data.
2961      my $relation = $self->_FindRelation($relationName);      my $relation = $self->_FindRelation($relationName);
2962      # Check the truncation flag.      # Check the truncation flag.
2963      if ($truncateFlag) {      if ($options{truncate}) {
2964          Trace("Creating table $relationName") if T(2);          Trace("Creating table $relationName") if T(2);
2965          # 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,
2966          # 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 2986 
2986      # Load the table.      # Load the table.
2987      my $rv;      my $rv;
2988      eval {      eval {
2989          $rv = $dbh->load_table(file => $fileName, tbl => $relationName);          $rv = $dbh->load_table(file => $fileName, tbl => $relationName, style => $options{mode});
2990      };      };
2991      if (!defined $rv) {      if (!defined $rv) {
2992          $retVal->AddMessage($@) if ($@);          $retVal->AddMessage($@) if ($@);
# Line 2800  Line 2997 
2997          $retVal->Add("tables");          $retVal->Add("tables");
2998          my $size = -s $fileName;          my $size = -s $fileName;
2999          Trace("$size bytes loaded into $relationName.") if T(2);          Trace("$size bytes loaded into $relationName.") if T(2);
3000            $retVal->Add("bytes", $size);
3001          # If we're rebuilding, we need to create the table indexes.          # If we're rebuilding, we need to create the table indexes.
3002          if ($truncateFlag) {          if ($options{truncate}) {
3003              # Indexes are created here for PostGres. For PostGres, indexes are              # Indexes are created here for PostGres. For PostGres, indexes are
3004              # best built at the end. For MySQL, the reverse is true.              # best built at the end. For MySQL, the reverse is true.
3005              if (! $dbh->{_preIndex}) {              if (! $dbh->{_preIndex}) {
# Line 2822  Line 3020 
3020          }          }
3021      }      }
3022      # Analyze the table to improve performance.      # Analyze the table to improve performance.
3023        if (! $options{partial}) {
3024      Trace("Analyzing and compacting $relationName.") if T(3);      Trace("Analyzing and compacting $relationName.") if T(3);
3025      $dbh->vacuum_it($relationName);          $self->Analyze($relationName);
3026        }
3027      Trace("$relationName load completed.") if T(3);      Trace("$relationName load completed.") if T(3);
3028      # Return the statistics.      # Return the statistics.
3029      return $retVal;      return $retVal;
3030  }  }
3031    
3032    =head3 Analyze
3033    
3034        $erdb->Analyze($tableName);
3035    
3036    Analyze and compact a table in the database. This is useful after a load
3037    to improve the performance of the indexes.
3038    
3039    =over 4
3040    
3041    =item tableName
3042    
3043    Name of the table to be analyzed and compacted.
3044    
3045    =back
3046    
3047    =cut
3048    
3049    sub Analyze {
3050        # Get the parameters.
3051        my ($self, $tableName) = @_;
3052        # Analyze the table.
3053        $self->{_dbh}->vacuum_it($tableName);
3054    }
3055    
3056    =head3 TruncateTable
3057    
3058        $erdb->TruncateTable($table);
3059    
3060    Delete all rows from a table quickly. This uses the built-in SQL
3061    C<TRUNCATE> statement, which effectively drops and re-creates a table
3062    with all its settings intact.
3063    
3064    =over 4
3065    
3066    =item table
3067    
3068    Name of the table to be cleared.
3069    
3070    =back
3071    
3072    =cut
3073    
3074    sub TruncateTable {
3075        # Get the parameters.
3076        my ($self, $table) = @_;
3077        # Get the database handle.
3078        my $dbh = $self->{_dbh};
3079        # Execute a truncation comment.
3080        $dbh->SQL("TRUNCATE TABLE $table");
3081    }
3082    
3083    
3084  =head3 CreateSearchIndex  =head3 CreateSearchIndex
3085    
3086      $erdb->CreateSearchIndex($objectName);      $erdb->CreateSearchIndex($objectName);
# Line 3009  Line 3261 
3261      my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);
3262      # Get the first (and only) object.      # Get the first (and only) object.
3263      my $retVal = $query->Fetch();      my $retVal = $query->Fetch();
3264        if (T(3)) {
3265            if ($retVal) {
3266                Trace("Entity $entityType \"$ID\" found.");
3267            } else {
3268                Trace("Entity $entityType \"$ID\" not found.");
3269            }
3270        }
3271      # Return the result.      # Return the result.
3272      return $retVal;      return $retVal;
3273  }  }
# Line 3558  Line 3817 
3817      return $retVal;      return $retVal;
3818  }  }
3819    
3820    =head3 WikiNote
3821    
3822    Convert a note or comment to Wiki text by replacing some bulletin-board codes with HTML. The codes
3823    supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph.
3824    Except for C<[p]>, all the codes are closed by slash-codes. So, for
3825    example, C<[b]Feature[/b]> displays the string C<Feature> in boldface.
3826    
3827        my $wikiText = ERDB::WikiNote($dataString);
3828    
3829    =over 4
3830    
3831    =item dataString
3832    
3833    String to convert to Wiki text.
3834    
3835    =item RETURN
3836    
3837    An Wiki text string derived from the input string.
3838    
3839    =back
3840    
3841    =cut
3842    
3843    sub WikiNote {
3844        # Get the parameter.
3845        my ($dataString) = @_;
3846        # HTML-escape the text.
3847        my $retVal = CGI::escapeHTML($dataString);
3848        # Substitute the bulletin board codes.
3849        my $italic = WikiTools::ItalicCode();
3850        $retVal =~ s/\[\/?i\]/$italic/g;
3851        my $bold = WikiTools::BoldCode();
3852        $retVal =~ s/\[\/?b\]/$bold/g;
3853        # Paragraph breaks are the same no matter which Wiki you're using.
3854        $retVal =~ s!\[p\]!\n\n!g;
3855        # Now we do the links, which are complicated by the need to know two
3856        # things: the target URL and the text.
3857        while ($retVal =~ /\[link\s+([^\]]+)\]([^\[]+)\[\/link\]/g) {
3858            # Replace the matched string with the Wiki markup for links. Note that
3859            # $-[0] is the starting position of the match for the entire expression,
3860            # and $+[0] is past the ending position.
3861            substr $retVal, $-[0], $+[0] - $-[0], WikiTools::LinkMarkup($1, $2);
3862        }
3863        # Return the result.
3864        return $retVal;
3865    }
3866    
3867  =head3 BeginTran  =head3 BeginTran
3868    
3869      $erdb->BeginTran();      $erdb->BeginTran();
# Line 3840  Line 4146 
4146    
4147  =over 4  =over 4
4148    
4149    =item indexObject
4150    
4151  ERDB XML structure for an index.  ERDB XML structure for an index.
4152    
4153  =item RETURN  =item RETURN
# Line 4499  Line 4807 
4807      # be a null string.      # be a null string.
4808      if ($fileName ne "") {      if ($fileName ne "") {
4809          # Load the relation from the file.          # Load the relation from the file.
4810          $retVal = $self->LoadTable($fileName, $relationName, $rebuild);          $retVal = $self->LoadTable($fileName, $relationName, truncate => $rebuild);
4811      } elsif ($rebuild) {      } elsif ($rebuild) {
4812          # 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.
4813          $self->CreateTable($relationName, 1);          $self->CreateTable($relationName, 1);
# Line 5246  Line 5554 
5554      return $retVal;      return $retVal;
5555  }  }
5556    
5557  =head2 HTML Documentation Utility Methods  =head2 Documentation Utility Methods
5558    
5559  =head3 _ComputeRelationshipSentence  =head3 _ComputeRelationshipSentence
5560    
# Line 5278  Line 5586 
5586      # Get the parameters.      # Get the parameters.
5587      my ($relationshipName, $relationshipStructure) = @_;      my ($relationshipName, $relationshipStructure) = @_;
5588      # Format the relationship sentence.      # Format the relationship sentence.
5589      my $result = "$relationshipStructure->{from} <b>$relationshipName</b> $relationshipStructure->{to}";      my $result = "$relationshipStructure->{from} $relationshipName $relationshipStructure->{to}";
5590      # Compute the arity.      # Compute the arity.
5591      my $arityCode = $relationshipStructure->{arity};      my $arityCode = $relationshipStructure->{arity};
5592      my $arity = $ArityTable{$arityCode};      my $arity = $ArityTable{$arityCode};
# Line 5323  Line 5631 
5631      return $result;      return $result;
5632  }  }
5633    
5634    =head3 _WikiRelationTable
5635    
5636    Generate the Wiki text for a particular relation. The relation's data will be formatted as a
5637    table with three columns-- the field name, the field type, and the field description.
5638    
5639    This is a static method.
5640    
5641    =over 4
5642    
5643    =item relationName
5644    
5645    Name of the relation being formatted.
5646    
5647    =item relationData
5648    
5649    Hash containing the relation's fields and indexes.
5650    
5651    =item RETURN
5652    
5653    Returns a Wiki string that can be used to display the relation name and all of its fields.
5654    
5655    =back
5656    
5657    =cut
5658    
5659    sub _WikiRelationTable {
5660        # Get the parameters.
5661        my ($relationName, $relationData) = @_;
5662        # We'll create a list of lists in here, then call WikiTools::Table to
5663        # convert it into a table.
5664        my @rows = ();
5665        # Push in the header row.
5666        push @rows, [qw(Field Type Description)];
5667        # Loop through the fields.
5668        for my $field (@{$relationData->{Fields}}) {
5669            # Create this field's row. We always have a name and type.
5670            my @row = ($field->{name}, $field->{type});
5671            # If we have a description, add it as the third column.
5672            if (exists $field->{Notes}) {
5673                push @row, WikiNote($field->{Notes}->{content});
5674            }
5675            # Push this row onto the table list.
5676            push @rows, \@row;
5677        }
5678        # Store the rows as a Wiki table with a level-4 heading.
5679        my $retVal = join("\n\n", WikiTools::Heading(4, "$relationName Table"),
5680                          WikiTools::Table(@rows));
5681        # Now we show the relation's indexes. These are formatted as another
5682        # table.
5683        @rows = ();
5684        # Push in the header row.
5685        push @rows, [qw(Index Unique Fields Notes)];
5686        # Get the index hash.
5687        my $indexTable = $relationData->{Indexes};
5688        # Loop through the indexes. For an entity, there is always at least one index.
5689        # For a relationship, there are at least two. The upshot is we don't need to
5690        # worry about accidentally generating a frivolous table here.
5691        for my $indexName (sort keys %$indexTable) {
5692            my $indexData = $indexTable->{$indexName};
5693            # Determine whether or not the index is unique.
5694            my $unique = ((exists $indexData->{Unique} && $indexData->{Unique} eq "true") ?
5695                          "yes" : "");
5696            # Get the field list.
5697            my $fields = join(', ', @{$indexData->{IndexFields}});
5698            # Get the note text.
5699            my $description = "";
5700            if (my $note = $indexData->{Notes}) {
5701                $description = WikiNote($note->{content});
5702            }
5703            # Format this row.
5704            my @row = ($indexName, $unique, $fields, $description);
5705            push @rows, \@row;
5706        }
5707        # Add the index list to the result.
5708        $retVal .= "\n\n" . WikiTools::Table(@rows);
5709    }
5710    
5711  =head3 _ShowRelationTable  =head3 _ShowRelationTable
5712    
5713  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 5881 
5881      return $htmlString;      return $htmlString;
5882  }  }
5883    
5884    =head3 _ObjectNotes
5885    
5886        my @noteParagraphs = _ObjectNotes($objectData);
5887    
5888    Return a list of the notes and asides for an entity or relationship in
5889    Wiki format.
5890    
5891    =over 4
5892    
5893    =item objectData
5894    
5895    The metadata for the desired entity or relationship.
5896    
5897    =item RETURN
5898    
5899    Returns a list of text paragraphs in Wiki markup form.
5900    
5901    =back
5902    
5903    =cut
5904    
5905    sub _ObjectNotes {
5906        # Get the parameters.
5907        my ($objectData) = @_;
5908        # Declare the return variable.
5909        my @retVal;
5910        # Loop through the types of notes.
5911        for my $noteType (qw(Notes Asides)) {
5912            my $text = $objectData->{$noteType};
5913            if ($text) {
5914                push @retVal, "", WikiNote($text->{content});
5915            }
5916        }
5917        # Return the result.
5918        return @retVal;
5919    }
5920    
5921  1;  1;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3