[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.38, Fri Mar 17 22:02:03 2006 UTC revision 1.39, Sun Mar 26 17:24:55 2006 UTC
# Line 877  Line 877 
877    
878  If multiple names are specified, then the query processor will automatically determine a  If multiple names are specified, then the query processor will automatically determine a
879  join path between the entities and relationships. The algorithm used is very simplistic.  join path between the entities and relationships. The algorithm used is very simplistic.
880  In particular, you can't specify any entity or relationship more than once, and if a  In particular, if a relationship is recursive, the path is determined by the order in which
881  relationship is recursive, the path is determined by the order in which the entity  the entity and the relationship appear. For example, consider a recursive relationship
882  and the relationship appear. For example, consider a recursive relationship B<IsParentOf>  B<IsParentOf> which relates B<People> objects to other B<People> objects. If the join path is
 which relates B<People> objects to other B<People> objects. If the join path is  
883  coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however,  coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however,
884  the join path is C<['IsParentOf', 'People']>, then the people returned will be children.  the join path is C<['IsParentOf', 'People']>, then the people returned will be children.
885    
886    If an entity or relationship is mentioned twice, the name for the second occurrence will
887    be suffixed with C<2>, the third occurrence will be suffixed with C<3>, and so forth. So,
888    for example, if we have C<['Feature', 'HasContig', 'Contig', 'HasContig']>, then the
889    B<to-link> field of the first B<HasContig> is specified as C<HasContig(to-link)>, while
890    the B<to-link> field of the second B<HasContig> is specified as C<HasContig2(to-link)>.
891    
892  =over 4  =over 4
893    
894  =item objectNames  =item objectNames
# Line 913  Line 918 
918  filter clause in general; however, odd things may happen if a sort field is from a secondary  filter clause in general; however, odd things may happen if a sort field is from a secondary
919  relation.  relation.
920    
921    Finally, you can limit the number of rows returned by adding a LIMIT clause. The LIMIT must
922    be the last thing in the filter clause, and it contains only the word "LIMIT" followed by
923    a positive number. So, for example
924    
925    C<< "Genome(genus) = ? ORDER BY Genome(species) LIMIT 10" >>
926    
927    will only return the first ten genomes for the specified genus. The ORDER BY clause is not
928    required. For example, to just get the first 10 genomes in the B<Genome> table, you could
929    use
930    
931    C<< "LIMIT 10" >>
932    
933  =item param1, param2, ..., paramN  =item param1, param2, ..., paramN
934    
935  Parameter values to be substituted into the filter clause.  Parameter values to be substituted into the filter clause.
# Line 928  Line 945 
945  sub Get {  sub Get {
946      # Get the parameters.      # Get the parameters.
947      my ($self, $objectNames, $filterClause, @params) = @_;      my ($self, $objectNames, $filterClause, @params) = @_;
948        # Adjust the list of object names to account for multiple occurrences of the
949        # same object. We start with a hash table keyed on object name that will
950        # return the object suffix. The first time an object is encountered it will
951        # not be found in the hash. The next time the hash will map the object name
952        # to 2, then 3, and so forth.
953        my %objectHash = ();
954        # This list will contain the object names as they are to appear in the
955        # FROM list.
956        my @fromList = ();
957        # This list contains the suffixed object name for each object. It is exactly
958        # parallel to the list in the $objectNames parameter.
959        my @mappedNameList = ();
960        # Finally, this hash translates from a mapped name to its original object name.
961        my %mappedNameHash = ();
962        # Now we create the lists. Note that for every single name we push something into
963        # @fromList and @mappedNameList. This insures that those two arrays are exactly
964        # parallel to $objectNames.
965        for my $objectName (@{$objectNames}) {
966            # Get the next suffix for this object.
967            my $suffix = $objectHash{$objectName};
968            if (! $suffix) {
969                # Here we are seeing the object for the first time. The object name
970                # is used as is.
971                push @mappedNameList, $objectName;
972                push @fromList, $objectName;
973                $mappedNameHash{$objectName} = $objectName;
974                # Denote the next suffix will be 2.
975                $objectHash{$objectName} = 2;
976            } else {
977                # Here we've seen the object before. We construct a new name using
978                # the suffix from the hash and update the hash.
979                my $mappedName = "$objectName$suffix";
980                $objectHash{$objectName} = $suffix + 1;
981                # The FROM list has the object name followed by the mapped name. This
982                # tells SQL it's still the same table, but we're using a different name
983                # for it to avoid confusion.
984                push @fromList, "$objectName $mappedName";
985                # The mapped-name list contains the real mapped name.
986                push @mappedNameList, $mappedName;
987                # Finally, enable us to get back from the mapped name to the object name.
988                $mappedNameHash{$mappedName} = $objectName;
989            }
990        }
991      # Construct the SELECT statement. The general pattern is      # Construct the SELECT statement. The general pattern is
992      #      #
993      # SELECT name1.*, name2.*, ... nameN.* FROM name1, name2, ... nameN      # SELECT name1.*, name2.*, ... nameN.* FROM name1, name2, ... nameN
994      #      #
995      my $dbh = $self->{_dbh};      my $dbh = $self->{_dbh};
996      my $command = "SELECT DISTINCT " . join('.*, ', @{$objectNames}) . ".* FROM " .      my $command = "SELECT DISTINCT " . join('.*, ', @mappedNameList) . ".* FROM " .
997                  join(', ', @{$objectNames});                  join(', ', @fromList);
998      # Check for a filter clause.      # Check for a filter clause.
999      if ($filterClause) {      if ($filterClause) {
1000          # 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,
# Line 942  Line 1002 
1002          my $filterString = $filterClause;          my $filterString = $filterClause;
1003          # 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
1004          # 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.
1005          my @sortedNames = sort { length($b) - length($a) } @{$objectNames};          my @sortedNames = sort { length($b) - length($a) } @mappedNameList;
1006          # We will also keep a list of conditions to add to the WHERE clause in order to link          # We will also keep a list of conditions to add to the WHERE clause in order to link
1007          # entities and relationships as well as primary relations to secondary ones.          # entities and relationships as well as primary relations to secondary ones.
1008          my @joinWhere = ();          my @joinWhere = ();
1009          # 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
1010          # table begins with the relation names already in the SELECT command.          # table begins with the relation names already in the SELECT command. We may
1011          my %fromNames = ();          # need to add relations later if there is filtering on a field in a secondary
1012          for my $objectName (@sortedNames) {          # relation. The secondary relations are the ones that contain multiply-
1013              $fromNames{$objectName} = 1;          # occurring or optional fields.
1014          }          my %fromNames = map { $_ => 1 } @sortedNames;
1015          # We are ready to begin. We loop through the object names, replacing each          # We are ready to begin. We loop through the object names, replacing each
1016          # object name's field references by the corresponding SQL field reference.          # object name's field references by the corresponding SQL field reference.
1017          # Along the way, if we find a secondary relation, we will need to add it          # Along the way, if we find a secondary relation, we will need to add it
1018          # to the FROM clause.          # to the FROM clause.
1019          for my $objectName (@sortedNames) {          for my $mappedName (@sortedNames) {
1020              # Get the length of the object name plus 2. This is the value we add to the              # Get the length of the object name plus 2. This is the value we add to the
1021              # size of the field name to determine the size of the field reference as a              # size of the field name to determine the size of the field reference as a
1022              # whole.              # whole.
1023              my $nameLength = 2 + length $objectName;              my $nameLength = 2 + length $mappedName;
1024                # Get the real object name for this mapped name.
1025                my $objectName = $mappedNameHash{$mappedName};
1026              # Get the object's field list.              # Get the object's field list.
1027              my $fieldList = $self->GetFieldTable($objectName);              my $fieldList = $self->GetFieldTable($objectName);
1028              # Find the field references for this object.              # Find the field references for this object.
1029              while ($filterString =~ m/$objectName\(([^)]*)\)/g) {              while ($filterString =~ m/$mappedName\(([^)]*)\)/g) {
1030                  # At this point, $1 contains the field name, and the current position                  # At this point, $1 contains the field name, and the current position
1031                  # is set immediately after the final parenthesis. We pull out the name of                  # is set immediately after the final parenthesis. We pull out the name of
1032                  # the field and the position and length of the field reference as a whole.                  # the field and the position and length of the field reference as a whole.
# Line 977  Line 1039 
1039                  } else {                  } else {
1040                      # Get the field's relation.                      # Get the field's relation.
1041                      my $relationName = $fieldList->{$fieldName}->{relation};                      my $relationName = $fieldList->{$fieldName}->{relation};
1042                        # Now we have a secondary relation. We need to insure it matches the
1043                        # mapped name of the primary relation. First we peel off the suffix
1044                        # from the mapped name.
1045                        my $mappingSuffix = substr $mappedName, length($objectName);
1046                        # Put the mapping suffix onto the relation name to get the
1047                        # mapped relation name.
1048                        my $mappedRelationName = "$relationName$mappingSuffix";
1049                      # Insure the relation is in the FROM clause.                      # Insure the relation is in the FROM clause.
1050                      if (!exists $fromNames{$relationName}) {                      if (!exists $fromNames{$mappedRelationName}) {
1051                          # Add the relation to the FROM clause.                          # Add the relation to the FROM clause.
1052                            if ($mappedRelationName eq $relationName) {
1053                                # The name is un-mapped, so we add it without
1054                                # any frills.
1055                          $command .= ", $relationName";                          $command .= ", $relationName";
                         # Create its join sub-clause.  
1056                          push @joinWhere, "$objectName.id = $relationName.id";                          push @joinWhere, "$objectName.id = $relationName.id";
1057                          # Denote we have it available for future fields.                          } else {
1058                          $fromNames{$relationName} = 1;                              # Here we have a mapping situation.
1059                                $command .= ", $relationName $mappedRelationName";
1060                                push @joinWhere, "$mappedRelationName.id = $mappedName.id";
1061                            }
1062                            # Denote we have this relation available for future fields.
1063                            $fromNames{$mappedRelationName} = 1;
1064                      }                      }
1065                      # Form an SQL field reference from the relation name and the field name.                      # Form an SQL field reference from the relation name and the field name.
1066                      my $sqlReference = "$relationName." . _FixName($fieldName);                      my $sqlReference = "$mappedRelationName." . _FixName($fieldName);
1067                      # Put it into the filter string in place of the old value.                      # Put it into the filter string in place of the old value.
1068                      substr($filterString, $pos, $len) = $sqlReference;                      substr($filterString, $pos, $len) = $sqlReference;
1069                      # Reposition the search.                      # Reposition the search.
# Line 999  Line 1075 
1075          # 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
1076          # 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
1077          # list before running through it.          # list before running through it.
1078          my @objectList = @{$objectNames};          my @mappedObjectList = @mappedNameList;
1079          my $lastObject = shift @objectList;          my $lastMappedObject = shift @mappedObjectList;
1080          # Get the join table.          # Get the join table.
1081          my $joinTable = $self->{_metaData}->{Joins};          my $joinTable = $self->{_metaData}->{Joins};
1082          # Loop through the object list.          # Loop through the object list.
1083          for my $thisObject (@objectList) {          for my $thisMappedObject (@mappedObjectList) {
1084              # Look for a join.              # Look for a join using the real object names.
1085                my $lastObject = $mappedNameHash{$lastMappedObject};
1086                my $thisObject = $mappedNameHash{$thisMappedObject};
1087              my $joinKey = "$lastObject/$thisObject";              my $joinKey = "$lastObject/$thisObject";
1088              if (!exists $joinTable->{$joinKey}) {              if (!exists $joinTable->{$joinKey}) {
1089                  # Here there's no join, so we throw an error.                  # Here there's no join, so we throw an error.
1090                  Confess("No join exists to connect from $lastObject to $thisObject.");                  Confess("No join exists to connect from $lastMappedObject to $thisMappedObject.");
1091              } else {              } else {
1092                  # Get the join clause and add it to the WHERE list.                  # Get the join clause.
1093                  push @joinWhere, $joinTable->{$joinKey};                  my $unMappedJoin = $joinTable->{$joinKey};
1094                    # Fix the names.
1095                    $unMappedJoin =~ s/$lastObject/$lastMappedObject/;
1096                    $unMappedJoin =~ s/$thisObject/$thisMappedObject/;
1097                    push @joinWhere, $unMappedJoin;
1098                  # Save this object as the last object for the next iteration.                  # Save this object as the last object for the next iteration.
1099                  $lastObject = $thisObject;                  $lastMappedObject = $thisMappedObject;
1100              }              }
1101          }          }
1102          # Now we need to handle the whole ORDER BY / LIMIT thing. The important part          # Now we need to handle the whole ORDER BY / LIMIT thing. The important part
# Line 1046  Line 1128 
1128      my $sth = $dbh->prepare_command($command);      my $sth = $dbh->prepare_command($command);
1129      # Execute it with the parameters bound in.      # Execute it with the parameters bound in.
1130      $sth->execute(@params) || Confess("SELECT error" . $sth->errstr());      $sth->execute(@params) || Confess("SELECT error" . $sth->errstr());
1131        # Now we create the relation map, which enables DBQuery to determine the order, name
1132        # and mapped name for each object in the query.
1133        my @relationMap = ();
1134        for my $mappedName (@mappedNameList) {
1135            push @relationMap, [$mappedName, $mappedNameHash{$mappedName}];
1136        }
1137      # Return the statement object.      # Return the statement object.
1138      my $retVal = DBQuery::_new($self, $sth, @{$objectNames});      my $retVal = DBQuery::_new($self, $sth, \@relationMap);
1139      return $retVal;      return $retVal;
1140  }  }
1141    

Legend:
Removed from v.1.38  
changed lines
  Added in v.1.39

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3