[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.68, Tue Sep 19 00:12:21 2006 UTC revision 1.71, Sat Oct 14 18:08:12 2006 UTC
# Line 190  Line 190 
190    
191  Name of the field. The field name should contain only letters, digits, and hyphens (C<->),  Name of the field. The field name should contain only letters, digits, and hyphens (C<->),
192  and the first character should be a letter. Most underlying databases are case-insensitive  and the first character should be a letter. Most underlying databases are case-insensitive
193  with the respect to field names, so a best practice is to use lower-case letters only.  with the respect to field names, so a best practice is to use lower-case letters only. Finally,
194    the name C<search-relevance> has special meaning for full-text searches and should not be
195    used as a field name.
196    
197  =item type  =item type
198    
# Line 209  Line 211 
211  entity, the fields without a relation attribute are said to belong to the  entity, the fields without a relation attribute are said to belong to the
212  I<primary relation>. This relation has the same name as the entity itself.  I<primary relation>. This relation has the same name as the entity itself.
213    
214    =item searchable
215    
216    If specified, then the field is a candidate for full-text searching. A single full-text
217    index will be created for each relation with at least one searchable field in it.
218    For best results, this option should only be used for string or text fields.
219    
220  =back  =back
221    
222  =head3 Indexes  =head3 Indexes
# Line 689  Line 697 
697      Trace("Creating table $relationName: $fieldThing") if T(2);      Trace("Creating table $relationName: $fieldThing") if T(2);
698      $dbh->create_table(tbl => $relationName, flds => $fieldThing, estimates => $estimation);      $dbh->create_table(tbl => $relationName, flds => $fieldThing, estimates => $estimation);
699      Trace("Relation $relationName created in database.") if T(2);      Trace("Relation $relationName created in database.") if T(2);
700      # If we want to build the indexes, we do it here.      # If we want to build the indexes, we do it here. Note that the full-text search
701        # index will not be built until the table has been loaded.
702      if ($indexFlag) {      if ($indexFlag) {
703          $self->CreateIndex($relationName);          $self->CreateIndex($relationName);
704      }      }
# Line 849  Line 858 
858          my @fieldList = _FixNames(@{$indexData->{IndexFields}});          my @fieldList = _FixNames(@{$indexData->{IndexFields}});
859          my $flds = join(', ', @fieldList);          my $flds = join(', ', @fieldList);
860          # Get the index's uniqueness flag.          # Get the index's uniqueness flag.
861          my $unique = (exists $indexData->{Unique} ? $indexData->{Unique} : 'false');          my $unique = (exists $indexData->{Unique} ? 'unique' : undef);
862          # Create the index.          # Create the index.
863          my $rv = $dbh->create_index(idx => $indexName, tbl => $relationName,          my $rv = $dbh->create_index(idx => $indexName, tbl => $relationName,
864                                      flds => $flds, unique => $unique);                                      flds => $flds, kind => $unique);
865          if ($rv) {          if ($rv) {
866              Trace("Index created: $indexName for $relationName ($flds)") if T(1);              Trace("Index created: $indexName for $relationName ($flds)") if T(1);
867          } else {          } else {
# Line 1099  Line 1108 
1108      return $retVal;      return $retVal;
1109  }  }
1110    
1111    =head3 Search
1112    
1113    C<< my $query = $erdb->Search($searchExpression, $idx, \@objectNames, $filterClause, \@params); >>
1114    
1115    Perform a full text search with filtering. The search will be against a specified object
1116    in the object name list. That object will get an extra field containing the search
1117    relevance. Note that except for the search expression, the parameters of this method are
1118    the same as those for L</Get> and follow the same rules.
1119    
1120    =over 4
1121    
1122    =item searchExpression
1123    
1124    Boolean search expression for the text fields of the target object.
1125    
1126    =item idx
1127    
1128    Index in the I<$objectNames> list of the table to be searched in full-text mode.
1129    
1130    =item objectNames
1131    
1132    List containing the names of the entity and relationship objects to be retrieved.
1133    
1134    =item filterClause
1135    
1136    WHERE clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
1137    be parameterized with parameter markers (C<?>). Each field used in the WHERE clause must be
1138    specified in the standard form B<I<objectName>(I<fieldName>)>. Any parameters specified
1139    in the filter clause should be added to the parameter list as additional parameters. The
1140    fields in a filter clause can come from primary entity relations, relationship relations,
1141    or secondary entity relations; however, all of the entities and relationships involved must
1142    be included in the list of object names.
1143    
1144    =item params
1145    
1146    Reference to a list of parameter values to be substituted into the filter clause.
1147    
1148    =item RETURN
1149    
1150    Returns a query object for the specified search.
1151    
1152    =back
1153    
1154    =cut
1155    
1156    sub Search {
1157        # Get the parameters.
1158        my ($self, $searchExpression, $idx, $objectNames, $filterClause, $params) = @_;
1159        # Declare the return variable.
1160        my $retVal;
1161        # Create a safety copy of the parameter list.
1162        my @myParams = @{$params};
1163        # Get the first object's structure so we have access to the searchable fields.
1164        my $object1Name = $objectNames->[$idx];
1165        my $object1Structure = $self->_GetStructure($object1Name);
1166        # Get the field list.
1167        if (! exists $object1Structure->{searchFields}) {
1168            Confess("No searchable index for $object1Name.");
1169        } else {
1170            # Get the field list.
1171            my @fields = @{$object1Structure->{searchFields}};
1172            # Clean the search expression.
1173            my $actualKeywords = $self->CleanKeywords($searchExpression);
1174            # We need two match expressions, one for the filter clause and one in the
1175            # query itself. Both will use a parameter mark, so we need to push the
1176            # search expression onto the front of the parameter list twice.
1177            unshift @myParams, $actualKeywords, $actualKeywords;
1178            # Build the match expression.
1179            my @matchFilterFields = map { "$object1Name." . _FixName($_) } @fields;
1180            my $matchClause = "MATCH (" . join(", ", @matchFilterFields) . ") AGAINST (? IN BOOLEAN MODE)";
1181            # Process the SQL stuff.
1182            my ($suffix, $mappedNameListRef, $mappedNameHashRef) =
1183                $self->_SetupSQL($objectNames, $filterClause, $matchClause);
1184            # Create the query. Note that the match clause is inserted at the front of
1185            # the select fields.
1186            my $command = "SELECT DISTINCT $matchClause, " . join(".*, ", @{$mappedNameListRef}) .
1187                ".* $suffix";
1188            my $sth = $self->_GetStatementHandle($command, \@myParams);
1189            # Now we create the relation map, which enables DBQuery to determine the order, name
1190            # and mapped name for each object in the query.
1191            my @relationMap = _RelationMap($mappedNameHashRef, $mappedNameListRef);
1192            # Return the statement object.
1193            $retVal = DBQuery::_new($self, $sth, \@relationMap, $object1Name);
1194        }
1195        return $retVal;
1196    }
1197    
1198  =head3 GetFlat  =head3 GetFlat
1199    
1200  C<< my @list = $erdb->GetFlat(\@objectNames, $filterClause, \@parameterList, $field); >>  C<< my @list = $erdb->GetFlat(\@objectNames, $filterClause, \@parameterList, $field); >>
# Line 1320  Line 1416 
1416      return $retVal;      return $retVal;
1417  }  }
1418    
1419    =head3 SortNeeded
1420    
1421    C<< my $parms = $erdb->SortNeeded($relationName); >>
1422    
1423    Return the pipe command for the sort that should be applied to the specified
1424    relation when creating the load file.
1425    
1426    For example, if the load file should be sorted ascending by the first
1427    field, this method would return
1428    
1429        sort -k1 -t"\t"
1430    
1431    If the first field is numeric, the method would return
1432    
1433        sort -k1n -t"\t"
1434    
1435    Unfortunately, due to a bug in the C<sort> command, we cannot eliminate duplicate
1436    keys using a sort.
1437    
1438    =over 4
1439    
1440    =item relationName
1441    
1442    Name of the relation to be examined.
1443    
1444    =item
1445    
1446    Returns the sort command to use for sorting the relation, suitable for piping.
1447    
1448    =back
1449    
1450    =cut
1451    #: Return Type $;
1452    sub SortNeeded {
1453        # Get the parameters.
1454        my ($self, $relationName) = @_;
1455        # Declare a descriptor to hold the names of the key fields.
1456        my @keyNames = ();
1457        # Get the relation structure.
1458        my $relationData = $self->_FindRelation($relationName);
1459        # Find out if the relation is a primary entity relation,
1460        # a relationship relation, or a secondary entity relation.
1461        my $entityTable = $self->{_metaData}->{Entities};
1462        my $relationshipTable = $self->{_metaData}->{Relationships};
1463        if (exists $entityTable->{$relationName}) {
1464            # Here we have a primary entity relation.
1465            push @keyNames, "id";
1466        } elsif (exists $relationshipTable->{$relationName}) {
1467            # Here we have a relationship. We sort using the FROM index.
1468            my $relationshipData = $relationshipTable->{$relationName};
1469            my $index = $relationData->{Indexes}->{"idx${relationName}From"};
1470            push @keyNames, @{$index->{IndexFields}};
1471        } else {
1472            # Here we have a secondary entity relation, so we have a sort on the ID field.
1473            push @keyNames, "id";
1474        }
1475        # Now we parse the key names into sort parameters. First, we prime the return
1476        # string.
1477        my $retVal = "sort -t\"\t\" ";
1478        # Get the relation's field list.
1479        my @fields = @{$relationData->{Fields}};
1480        # Loop through the keys.
1481        for my $keyData (@keyNames) {
1482            # Get the key and the ordering.
1483            my ($keyName, $ordering);
1484            if ($keyData =~ /^([^ ]+) DESC/) {
1485                ($keyName, $ordering) = ($1, "descending");
1486            } else {
1487                ($keyName, $ordering) = ($keyData, "ascending");
1488            }
1489            # Find the key's position and type.
1490            my $fieldSpec;
1491            for (my $i = 0; $i <= $#fields && ! $fieldSpec; $i++) {
1492                my $thisField = $fields[$i];
1493                if ($thisField->{name} eq $keyName) {
1494                    # Get the sort modifier for this field type. The modifier
1495                    # decides whether we're using a character, numeric, or
1496                    # floating-point sort.
1497                    my $modifier = $TypeTable{$thisField->{type}}->{sort};
1498                    # If the index is descending for this field, denote we want
1499                    # to reverse the sort order on this field.
1500                    if ($ordering eq 'descending') {
1501                        $modifier .= "r";
1502                    }
1503                    # Store the position and modifier into the field spec, which
1504                    # will stop the inner loop. Note that the field number is
1505                    # 1-based in the sort command, so we have to increment the
1506                    # index.
1507                    $fieldSpec = ($i + 1) . $modifier;
1508                }
1509            }
1510            # Add this field to the sort command.
1511            $retVal .= " -k$fieldSpec";
1512        }
1513        # Return the result.
1514        return $retVal;
1515    }
1516    
1517  =head3 GetList  =head3 GetList
1518    
1519  C<< my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, \@params); >>  C<< my @dbObjects = $erdb->GetList(\@objectNames, $filterClause, \@params); >>
# Line 1832  Line 2026 
2026          my $size = -s $fileName;          my $size = -s $fileName;
2027          Trace("$size bytes loaded into $relationName.") if T(2);          Trace("$size bytes loaded into $relationName.") if T(2);
2028          # If we're rebuilding, we need to create the table indexes.          # If we're rebuilding, we need to create the table indexes.
2029          if ($truncateFlag && ! $dbh->{_preIndex}) {          if ($truncateFlag) {
2030                # Indexes are created here for PostGres. For PostGres, indexes are
2031                # best built at the end. For MySQL, the reverse is true.
2032                if (! $dbh->{_preIndex}) {
2033              eval {              eval {
2034                  $self->CreateIndex($relationName);                  $self->CreateIndex($relationName);
2035              };              };
# Line 1840  Line 2037 
2037                  $retVal->AddMessage($@);                  $retVal->AddMessage($@);
2038              }              }
2039          }          }
2040                # The full-text index (if any) is always built last, even for MySQL.
2041                # First we need to see if this table has a full-text index. Only
2042                # primary relations are allowed that privilege.
2043                if ($self->_IsPrimary($relationName)) {
2044                    # Get the relation's entity/relationship structure.
2045                    my $structure = $self->_GetStructure($relationName);
2046                    # Check for a searchable fields list.
2047                    if (exists $structure->{searchFields}) {
2048                        # Here we know that we need to create a full-text search index.
2049                        # Get an SQL-formatted field name list.
2050                        my $fields = join(", ", $self->_FixNames(@{$structure->{searchFields}}));
2051                        # Create the index.
2052                        $dbh->create_index(tbl => $relationName, idx => "search_idx_$relationName",
2053                                           flds => $fields, kind => 'fulltext');
2054                    }
2055                }
2056            }
2057      }      }
2058      # Analyze the table to improve performance.      # Analyze the table to improve performance.
2059      Trace("Analyzing and compacting $relationName.") if T(3);      Trace("Analyzing and compacting $relationName.") if T(3);
# Line 1943  Line 2157 
2157      return $retVal;      return $retVal;
2158  }  }
2159    
2160    =head3 GetChoices
2161    
2162    C<< my @values = $erdb->GetChoices($entityName, $fieldName); >>
2163    
2164    Return a list of all the values for the specified field that are represented in the
2165    specified entity.
2166    
2167    Note that if the field is not indexed, then this will be a very slow operation.
2168    
2169    =over 4
2170    
2171    =item entityName
2172    
2173    Name of an entity in the database.
2174    
2175    =item fieldName
2176    
2177    Name of a field belonging to the entity. This is a raw field name without
2178    the standard parenthesized notation used in most calls.
2179    
2180    =item RETURN
2181    
2182    Returns a list of the distinct values for the specified field in the database.
2183    
2184    =back
2185    
2186    =cut
2187    
2188    sub GetChoices {
2189        # Get the parameters.
2190        my ($self, $entityName, $fieldName) = @_;
2191        # Declare the return variable.
2192        my @retVal;
2193        # Get the entity data structure.
2194        my $entityData = $self->_GetStructure($entityName);
2195        # Get the field.
2196        my $fieldHash = $entityData->{Fields};
2197        if (! exists $fieldHash->{$fieldName}) {
2198            Confess("$fieldName not found in $entityName.");
2199        } else {
2200            # Get the name of the relation containing the field.
2201            my $relation = $fieldHash->{$fieldName}->{relation};
2202            # Fix up the field name.
2203            my $realName = _FixName($fieldName);
2204            # Get the database handle.
2205            my $dbh = $self->{_dbh};
2206            # Query the database.
2207            my $results = $dbh->SQL("SELECT DISTINCT $realName FROM $relation");
2208            # Clean the results. They are stored as a list of lists, and we just want the one list.
2209            @retVal = sort map { $_->[0] } @{$results};
2210        }
2211        # Return the result.
2212        return @retVal;
2213    }
2214    
2215  =head3 GetEntityValues  =head3 GetEntityValues
2216    
2217  C<< my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); >>  C<< my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); >>
# Line 2336  Line 2605 
2605      return @retVal;      return @retVal;
2606  }  }
2607    
2608    =head2 Virtual Methods
2609    
2610    =head3 CleanKeywords
2611    
2612    C<< my $cleanedString = $erdb->CleanKeywords($searchExpression); >>
2613    
2614    Clean up a search expression or keyword list. This is a virtual method that may
2615    be overridden by the subclass. The base-class method removes extra spaces
2616    and converts everything to lower case.
2617    
2618    =over 4
2619    
2620    =item searchExpression
2621    
2622    Search expression or keyword list to clean. Note that a search expression may
2623    contain boolean operators which need to be preserved. This includes leading
2624    minus signs.
2625    
2626    =item RETURN
2627    
2628    Cleaned expression or keyword list.
2629    
2630    =back
2631    
2632    =cut
2633    
2634    sub CleanKeywords {
2635        # Get the parameters.
2636        my ($self, $searchExpression) = @_;
2637        # Lower-case the expression and copy it into the return variable. Note that we insure we
2638        # don't accidentally end up with an undefined value.
2639        my $retVal = lc($searchExpression || "");
2640        # Remove extra spaces.
2641        $retVal =~ s/\s+/ /g;
2642        $retVal =~ s/(^\s+)|(\s+$)//g;
2643        # Return the result.
2644        return $retVal;
2645    }
2646    
2647  =head2 Internal Utility Methods  =head2 Internal Utility Methods
2648    
2649  =head3 SetupSQL  =head3 _RelationMap
2650    
2651    C<< my @relationMap = _RelationMap($mappedNameHashRef, $mappedNameListRef); >>
2652    
2653    Create the relation map for an SQL query. The relation map is used by B<DBObject>
2654    to determine how to interpret the results of the query.
2655    
2656    =over 4
2657    
2658    =item mappedNameHashRef
2659    
2660    Reference to a hash that maps modified object names to real object names.
2661    
2662    =item mappedNameListRef
2663    
2664    Reference to a list of modified object names in the order they appear in the
2665    SELECT list.
2666    
2667    =item RETURN
2668    
2669    Returns a list of 2-tuples. Each tuple consists of an object name as used in the
2670    query followed by the actual name of that object. This enables the B<DBObject> to
2671    determine the order of the tables in the query and which object name belongs to each
2672    mapped object name. Most of the time these two values are the same; however, if a
2673    relation occurs twice in the query, the relation name in the field list and WHERE
2674    clause will use a mapped name (generally the actual relation name with a numeric
2675    suffix) that does not match the actual relation name.
2676    
2677    =back
2678    
2679    =cut
2680    
2681    sub _RelationMap {
2682        # Get the parameters.
2683        my ($mappedNameHashRef, $mappedNameListRef) = @_;
2684        # Declare the return variable.
2685        my @retVal = ();
2686        # Build the map.
2687        for my $mappedName (@{$mappedNameListRef}) {
2688            push @retVal, [$mappedName, $mappedNameHashRef->{$mappedName}];
2689        }
2690        # Return it.
2691        return @retVal;
2692    }
2693    
2694    
2695    =head3 _SetupSQL
2696    
2697  Process a list of object names and a filter clause so that they can be used to  Process a list of object names and a filter clause so that they can be used to
2698  build an SQL statement. This method takes in a reference to a list of object names  build an SQL statement. This method takes in a reference to a list of object names
# Line 2358  Line 2712 
2712  A string containing the WHERE clause for the query (without the C<WHERE>) and also  A string containing the WHERE clause for the query (without the C<WHERE>) and also
2713  optionally the C<ORDER BY> and C<LIMIT> clauses.  optionally the C<ORDER BY> and C<LIMIT> clauses.
2714    
2715    =item matchClause
2716    
2717    An optional full-text search clause. If specified, it will be inserted at the
2718    front of the WHERE clause. It should already be SQL-formatted; that is, the
2719    field names should be in the form I<table>C<.>I<fieldName>.
2720    
2721  =item RETURN  =item RETURN
2722    
2723  Returns a three-element list. The first element is the SQL statement suffix, beginning  Returns a three-element list. The first element is the SQL statement suffix, beginning
# Line 2370  Line 2730 
2730  =cut  =cut
2731    
2732  sub _SetupSQL {  sub _SetupSQL {
2733      my ($self, $objectNames, $filterClause) = @_;      my ($self, $objectNames, $filterClause, $matchClause) = @_;
2734      # Adjust the list of object names to account for multiple occurrences of the      # Adjust the list of object names to account for multiple occurrences of the
2735      # same object. We start with a hash table keyed on object name that will      # same object. We start with a hash table keyed on object name that will
2736      # return the object suffix. The first time an object is encountered it will      # return the object suffix. The first time an object is encountered it will
# Line 2419  Line 2779 
2779      # FROM name1, name2, ... nameN      # FROM name1, name2, ... nameN
2780      #      #
2781      my $suffix = "FROM " . join(', ', @fromList);      my $suffix = "FROM " . join(', ', @fromList);
2782        # Now for the WHERE. First, we need a place for the filter string.
2783        my $filterString = "";
2784        # We will also keep a list of conditions to add to the WHERE clause in order to link
2785        # entities and relationships as well as primary relations to secondary ones.
2786        my @joinWhere = ();
2787      # Check for a filter clause.      # Check for a filter clause.
2788      if ($filterClause) {      if ($filterClause) {
2789          # Here we have one, so we convert its field names and add it to the query. First,          # Here we have one, so we convert its field names and add it to the query. First,
2790          # We create a copy of the filter string we can work with.          # We create a copy of the filter string we can work with.
2791          my $filterString = $filterClause;          $filterString = $filterClause;
2792          # Next, we sort the object names by length. This helps protect us from finding          # Next, we sort the object names by length. This helps protect us from finding
2793          # object names inside other object names when we're doing our search and replace.          # object names inside other object names when we're doing our search and replace.
2794          my @sortedNames = sort { length($b) - length($a) } @mappedNameList;          my @sortedNames = sort { length($b) - length($a) } @mappedNameList;
         # We will also keep a list of conditions to add to the WHERE clause in order to link  
         # entities and relationships as well as primary relations to secondary ones.  
         my @joinWhere = ();  
2795          # The final preparatory step is to create a hash table of relation names. The          # The final preparatory step is to create a hash table of relation names. The
2796          # table begins with the relation names already in the SELECT command. We may          # table begins with the relation names already in the SELECT command. We may
2797          # need to add relations later if there is filtering on a field in a secondary          # need to add relations later if there is filtering on a field in a secondary
# Line 2497  Line 2859 
2859                  }                  }
2860              }              }
2861          }          }
2862        }
2863          # The next step is to join the objects together. We only need to do this if there          # The next step is to join the objects together. We only need to do this if there
2864          # is more than one object in the object list. We start with the first object and          # is more than one object in the object list. We start with the first object and
2865          # run through the objects after it. Note also that we make a safety copy of the          # run through the objects after it. Note also that we make a safety copy of the
2866          # list before running through it.      # list before running through it, because we shift off the first object before
2867        # processing the rest.
2868          my @mappedObjectList = @mappedNameList;          my @mappedObjectList = @mappedNameList;
2869          my $lastMappedObject = shift @mappedObjectList;          my $lastMappedObject = shift @mappedObjectList;
2870          # Get the join table.          # Get the join table.
# Line 2529  Line 2893 
2893          # here is we want the filter clause to be empty if there's no WHERE filter.          # here is we want the filter clause to be empty if there's no WHERE filter.
2894          # We'll put the ORDER BY / LIMIT clauses in the following variable.          # We'll put the ORDER BY / LIMIT clauses in the following variable.
2895          my $orderClause = "";          my $orderClause = "";
2896        # This is only necessary if we have a filter string in which the ORDER BY
2897        # and LIMIT clauses can live.
2898        if ($filterString) {
2899          # Locate the ORDER BY or LIMIT verbs (if any). We use a non-greedy          # Locate the ORDER BY or LIMIT verbs (if any). We use a non-greedy
2900          # operator so that we find the first occurrence of either verb.          # operator so that we find the first occurrence of either verb.
2901          if ($filterString =~ m/^(.*?)\s*(ORDER BY|LIMIT)/g) {          if ($filterString =~ m/^(.*?)\s*(ORDER BY|LIMIT)/g) {
# Line 2537  Line 2904 
2904              $orderClause = $2 . substr($filterString, $pos);              $orderClause = $2 . substr($filterString, $pos);
2905              $filterString = $1;              $filterString = $1;
2906          }          }
2907          # Add the filter and the join clauses (if any) to the SELECT command.      }
2908        # All the things that are supposed to be in the WHERE clause of the
2909        # SELECT command need to be put into @joinWhere so we can string them
2910        # together. We begin with the match clause. This is important,
2911        # because the match clause's parameter mark must precede any parameter
2912        # marks in the filter string.
2913        if ($matchClause) {
2914            push @joinWhere, $matchClause;
2915        }
2916        # Add the filter string. We put it in parentheses to avoid operator
2917        # precedence problems with the match clause or the joins.
2918          if ($filterString) {          if ($filterString) {
2919              Trace("Filter string is \"$filterString\".") if T(4);              Trace("Filter string is \"$filterString\".") if T(4);
2920              push @joinWhere, "($filterString)";              push @joinWhere, "($filterString)";
2921          }          }
2922        # String it all together into a big filter clause.
2923          if (@joinWhere) {          if (@joinWhere) {
2924              $suffix .= " WHERE " . join(' AND ', @joinWhere);              $suffix .= " WHERE " . join(' AND ', @joinWhere);
2925          }          }
2926          # Add the sort or limit clause (if any) to the SELECT command.      # Add the sort or limit clause (if any).
2927          if ($orderClause) {          if ($orderClause) {
2928              $suffix .= " $orderClause";              $suffix .= " $orderClause";
2929          }          }
     }  
2930      # Return the suffix, the mapped name list, and the mapped name hash.      # Return the suffix, the mapped name list, and the mapped name hash.
2931      return ($suffix, \@mappedNameList, \%mappedNameHash);      return ($suffix, \@mappedNameList, \%mappedNameHash);
2932  }  }
2933    
2934  =head3 GetStatementHandle  =head3 _GetStatementHandle
2935    
2936  This method will prepare and execute an SQL query, returning the statement handle.  This method will prepare and execute an SQL query, returning the statement handle.
2937  The main reason for doing this here is so that everybody who does SQL queries gets  The main reason for doing this here is so that everybody who does SQL queries gets
# Line 2597  Line 2974 
2974      return $sth;      return $sth;
2975  }  }
2976    
2977  =head3 GetLoadStats  =head3 _GetLoadStats
2978    
2979  Return a blank statistics object for use by the load methods.  Return a blank statistics object for use by the load methods.
2980    
# Line 2609  Line 2986 
2986      return Stats->new();      return Stats->new();
2987  }  }
2988    
2989  =head3 GenerateFields  =head3 _GenerateFields
2990    
2991  Generate field values from a field structure and store in a specified table. The field names  Generate field values from a field structure and store in a specified table. The field names
2992  are first sorted by pass count, certain pre-defined fields are removed from the list, and  are first sorted by pass count, certain pre-defined fields are removed from the list, and
# Line 2683  Line 3060 
3060      }      }
3061  }  }
3062    
3063  =head3 DumpRelation  =head3 _DumpRelation
3064    
3065  Dump the specified relation's to the specified output file in tab-delimited format.  Dump the specified relation's to the specified output file in tab-delimited format.
3066    
# Line 2733  Line 3110 
3110      close DTXOUT;      close DTXOUT;
3111  }  }
3112    
3113  =head3 GetStructure  =head3 _GetStructure
3114    
3115  Get the data structure for a specified entity or relationship.  Get the data structure for a specified entity or relationship.
3116    
# Line 2772  Line 3149 
3149      return $retVal;      return $retVal;
3150  }  }
3151    
3152  =head3 GetRelationTable  
3153    
3154    =head3 _GetRelationTable
3155    
3156  Get the list of relations for a specified entity or relationship.  Get the list of relations for a specified entity or relationship.
3157    
# Line 2801  Line 3180 
3180      return $objectData->{Relations};      return $objectData->{Relations};
3181  }  }
3182    
3183  =head3 ValidateFieldNames  =head3 _ValidateFieldNames
3184    
3185  Determine whether or not the field names are valid. A description of the problems with the names  Determine whether or not the field names are valid. A description of the problems with the names
3186  will be written to the standard error output. If there is an error, this method will abort. This is  will be written to the standard error output. If there is an error, this method will abort. This is
# Line 2856  Line 3235 
3235      }      }
3236  }  }
3237    
3238  =head3 LoadRelation  =head3 _LoadRelation
3239    
3240  Load a relation from the data in a tab-delimited disk file. The load will only take place if a disk  Load a relation from the data in a tab-delimited disk file. The load will only take place if a disk
3241  file with the same name as the relation exists in the specified directory.  file with the same name as the relation exists in the specified directory.
# Line 2916  Line 3295 
3295      return $retVal;      return $retVal;
3296  }  }
3297    
3298  =head3 LoadMetaData  =head3 _LoadMetaData
3299    
3300  This method loads the data describing this database from an XML file into a metadata structure.  This method loads the data describing this database from an XML file into a metadata structure.
3301  The resulting structure is a set of nested hash tables containing all the information needed to  The resulting structure is a set of nested hash tables containing all the information needed to
# Line 3243  Line 3622 
3622      return $metadata;      return $metadata;
3623  }  }
3624    
3625  =head3 SortNeeded  =head3 _CreateRelationshipIndex
   
 C<< my $parms = $erdb->SortNeeded($relationName); >>  
   
 Return the pipe command for the sort that should be applied to the specified  
 relation when creating the load file.  
   
 For example, if the load file should be sorted ascending by the first  
 field, this method would return  
   
     sort -k1 -t"\t"  
   
 If the first field is numeric, the method would return  
   
     sort -k1n -t"\t"  
   
 Unfortunately, due to a bug in the C<sort> command, we cannot eliminate duplicate  
 keys using a sort.  
   
 =over 4  
   
 =item relationName  
   
 Name of the relation to be examined.  
   
 =item  
   
 Returns the sort command to use for sorting the relation, suitable for piping.  
   
 =back  
   
 =cut  
 #: Return Type $;  
 sub SortNeeded {  
     # Get the parameters.  
     my ($self, $relationName) = @_;  
     # Declare a descriptor to hold the names of the key fields.  
     my @keyNames = ();  
     # Get the relation structure.  
     my $relationData = $self->_FindRelation($relationName);  
     # Find out if the relation is a primary entity relation,  
     # a relationship relation, or a secondary entity relation.  
     my $entityTable = $self->{_metaData}->{Entities};  
     my $relationshipTable = $self->{_metaData}->{Relationships};  
     if (exists $entityTable->{$relationName}) {  
         # Here we have a primary entity relation.  
         push @keyNames, "id";  
     } elsif (exists $relationshipTable->{$relationName}) {  
         # Here we have a relationship. We sort using the FROM index.  
         my $relationshipData = $relationshipTable->{$relationName};  
         my $index = $relationData->{Indexes}->{"idx${relationName}From"};  
         push @keyNames, @{$index->{IndexFields}};  
     } else {  
         # Here we have a secondary entity relation, so we have a sort on the ID field.  
         push @keyNames, "id";  
     }  
     # Now we parse the key names into sort parameters. First, we prime the return  
     # string.  
     my $retVal = "sort -t\"\t\" ";  
     # Get the relation's field list.  
     my @fields = @{$relationData->{Fields}};  
     # Loop through the keys.  
     for my $keyData (@keyNames) {  
         # Get the key and the ordering.  
         my ($keyName, $ordering);  
         if ($keyData =~ /^([^ ]+) DESC/) {  
             ($keyName, $ordering) = ($1, "descending");  
         } else {  
             ($keyName, $ordering) = ($keyData, "ascending");  
         }  
         # Find the key's position and type.  
         my $fieldSpec;  
         for (my $i = 0; $i <= $#fields && ! $fieldSpec; $i++) {  
             my $thisField = $fields[$i];  
             if ($thisField->{name} eq $keyName) {  
                 # Get the sort modifier for this field type. The modifier  
                 # decides whether we're using a character, numeric, or  
                 # floating-point sort.  
                 my $modifier = $TypeTable{$thisField->{type}}->{sort};  
                 # If the index is descending for this field, denote we want  
                 # to reverse the sort order on this field.  
                 if ($ordering eq 'descending') {  
                     $modifier .= "r";  
                 }  
                 # Store the position and modifier into the field spec, which  
                 # will stop the inner loop. Note that the field number is  
                 # 1-based in the sort command, so we have to increment the  
                 # index.  
                 $fieldSpec = ($i + 1) . $modifier;  
             }  
         }  
         # Add this field to the sort command.  
         $retVal .= " -k$fieldSpec";  
     }  
     # Return the result.  
     return $retVal;  
 }  
   
 =head3 CreateRelationshipIndex  
3626    
3627  Create an index for a relationship's relation.  Create an index for a relationship's relation.
3628    
# Line 3386  Line 3667 
3667      _AddIndex("idx$relationshipName$indexKey", $relationStructure, $newIndex);      _AddIndex("idx$relationshipName$indexKey", $relationStructure, $newIndex);
3668  }  }
3669    
3670  =head3 AddIndex  =head3 _AddIndex
3671    
3672  Add an index to a relation structure.  Add an index to a relation structure.
3673    
# Line 3432  Line 3713 
3713      $relationStructure->{Indexes}->{$indexName} = $newIndex;      $relationStructure->{Indexes}->{$indexName} = $newIndex;
3714  }  }
3715    
3716  =head3 FixupFields  =head3 _FixupFields
3717    
3718  This method fixes the field list for an entity or relationship. It will add the caller-specified  This method fixes the field list for an entity or relationship. It will add the caller-specified
3719  relation name to fields that do not have a name and set the C<PrettySort> value as specified.  relation name to fields that do not have a name and set the C<PrettySort> value as specified.
# Line 3470  Line 3751 
3751          # Here it doesn't, so we create a new one.          # Here it doesn't, so we create a new one.
3752          $structure->{Fields} = { };          $structure->{Fields} = { };
3753      } else {      } else {
3754          # Here we have a field list. Loop through its fields.          # Here we have a field list. We need to track the searchable fields, so we
3755            # create a list for stashing them.
3756            my @textFields = ();
3757            # Loop through the fields.
3758          my $fieldStructures = $structure->{Fields};          my $fieldStructures = $structure->{Fields};
3759          for my $fieldName (keys %{$fieldStructures}) {          for my $fieldName (keys %{$fieldStructures}) {
3760              Trace("Processing field $fieldName of $defaultRelationName.") if T(4);              Trace("Processing field $fieldName of $defaultRelationName.") if T(4);
# Line 3484  Line 3768 
3768                  # The data generator will use the default for the field's type.                  # The data generator will use the default for the field's type.
3769                  $fieldData->{DataGen} = { content => $TypeTable{$type}->{dataGen} };                  $fieldData->{DataGen} = { content => $TypeTable{$type}->{dataGen} };
3770              }              }
3771                # Check for searchability.
3772                if ($fieldData->{searchable}) {
3773                    # Only allow this for a primary relation.
3774                    if ($fieldData->{relation} ne $defaultRelationName) {
3775                        Confess("Field $fieldName of $defaultRelationName is in secondary relations and cannot be searchable.");
3776                    } else {
3777                        push @textFields, $fieldName;
3778                    }
3779                }
3780              # Plug in the defaults for the optional data generation parameters.              # Plug in the defaults for the optional data generation parameters.
3781              Tracer::MergeOptions($fieldData->{DataGen}, { testCount => 1, pass => 0 });              Tracer::MergeOptions($fieldData->{DataGen}, { testCount => 1, pass => 0 });
3782              # Add the PrettySortValue.              # Add the PrettySortValue.
3783              $fieldData->{PrettySort} = (($type eq "text") ? $textPrettySortValue : $prettySortValue);              $fieldData->{PrettySort} = (($type eq "text") ? $textPrettySortValue : $prettySortValue);
3784          }          }
3785            # If there are searchable fields, remember the fact.
3786            if (@textFields) {
3787                $structure->{searchFields} = \@textFields;
3788            }
3789      }      }
3790  }  }
3791    
3792  =head3 FixName  =head3 _FixName
3793    
3794  Fix the incoming field name so that it is a legal SQL column name.  Fix the incoming field name so that it is a legal SQL column name.
3795    
# Line 3521  Line 3818 
3818      return $fieldName;      return $fieldName;
3819  }  }
3820    
3821  =head3 FixNames  =head3 _FixNames
3822    
3823  Fix all the field names in a list.  Fix all the field names in a list.
3824    
# Line 3552  Line 3849 
3849      return @result;      return @result;
3850  }  }
3851    
3852  =head3 AddField  =head3 _AddField
3853    
3854  Add a field to a field list.  Add a field to a field list.
3855    
# Line 3587  Line 3884 
3884      $fieldList->{$fieldName} = $fieldStructure;      $fieldList->{$fieldName} = $fieldStructure;
3885  }  }
3886    
3887  =head3 ReOrderRelationTable  =head3 _ReOrderRelationTable
3888    
3889  This method will take a relation table and re-sort it according to the implicit ordering of the  This method will take a relation table and re-sort it according to the implicit ordering of the
3890  C<PrettySort> property. Instead of a hash based on field names, it will return a list of fields.  C<PrettySort> property. Instead of a hash based on field names, it will return a list of fields.
# Line 3648  Line 3945 
3945    
3946  }  }
3947    
3948  =head3 IsPrimary  =head3 _IsPrimary
3949    
3950  Return TRUE if a specified relation is a primary relation, else FALSE. A relation is primary  Return TRUE if a specified relation is a primary relation, else FALSE. A relation is primary
3951  if it has the same name as an entity or relationship.  if it has the same name as an entity or relationship.
# Line 3684  Line 3981 
3981      return $retVal;      return $retVal;
3982  }  }
3983    
3984  =head3 FindRelation  =head3 _FindRelation
3985    
3986  Return the descriptor for the specified relation.  Return the descriptor for the specified relation.
3987    
# Line 3715  Line 4012 
4012    
4013  =head2 HTML Documentation Utility Methods  =head2 HTML Documentation Utility Methods
4014    
4015  =head3 ComputeRelationshipSentence  =head3 _ComputeRelationshipSentence
4016    
4017  The relationship sentence consists of the relationship name between the names of the  The relationship sentence consists of the relationship name between the names of the
4018  two related entities and an arity indicator.  two related entities and an arity indicator.
# Line 3753  Line 4050 
4050      return $result;      return $result;
4051  }  }
4052    
4053  =head3 ComputeRelationshipHeading  =head3 _ComputeRelationshipHeading
4054    
4055  The relationship heading is the L<relationship sentence|/ComputeRelationshipSentence> with the entity  The relationship heading is the L<relationship sentence|/ComputeRelationshipSentence> with the entity
4056  names hyperlinked to the appropriate entity sections of the document.  names hyperlinked to the appropriate entity sections of the document.
# Line 3790  Line 4087 
4087      return $result;      return $result;
4088  }  }
4089    
4090  =head3 ShowRelationTable  =head3 _ShowRelationTable
4091    
4092  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
4093  table with three columns-- the field name, the field type, and the field description.  table with three columns-- the field name, the field type, and the field description.
# Line 3851  Line 4148 
4148      $htmlString .= "</ul>\n";      $htmlString .= "</ul>\n";
4149  }  }
4150    
4151  =head3 OpenFieldTable  =head3 _OpenFieldTable
4152    
4153  This method creates the header string for the field table generated by L</ShowMetaData>.  This method creates the header string for the field table generated by L</ShowMetaData>.
4154    
# Line 3876  Line 4173 
4173      return _OpenTable($tablename, 'Field', 'Type', 'Description');      return _OpenTable($tablename, 'Field', 'Type', 'Description');
4174  }  }
4175    
4176  =head3 OpenTable  =head3 _OpenTable
4177    
4178  This method creates the header string for an HTML table.  This method creates the header string for an HTML table.
4179    
# Line 3916  Line 4213 
4213      return $htmlString;      return $htmlString;
4214  }  }
4215    
4216  =head3 CloseTable  =head3 _CloseTable
4217    
4218  This method returns the HTML for closing a table.  This method returns the HTML for closing a table.
4219    
# Line 3928  Line 4225 
4225      return "</table></p>\n";      return "</table></p>\n";
4226  }  }
4227    
4228  =head3 ShowField  =head3 _ShowField
4229    
4230  This method returns the HTML for displaying a row of field information in a field table.  This method returns the HTML for displaying a row of field information in a field table.
4231    
# Line 3963  Line 4260 
4260      return $htmlString;      return $htmlString;
4261  }  }
4262    
4263  =head3 HTMLNote  =head3 _HTMLNote
4264    
4265  Convert a note or comment to HTML by replacing some bulletin-board codes with HTML. The codes  Convert a note or comment to HTML by replacing some bulletin-board codes with HTML. The codes
4266  supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph.  supported are C<[b]> for B<bold>, C<[i]> for I<italics>, and C<[p]> for a new paragraph.

Legend:
Removed from v.1.68  
changed lines
  Added in v.1.71

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3