[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.32, Sat Jan 28 08:58:53 2006 UTC revision 1.33, Sat Jan 28 09:36:47 2006 UTC
# Line 1096  Line 1096 
1096      # the things we have to be careful about is preventing loops. We'll use a hash to      # the things we have to be careful about is preventing loops. We'll use a hash to
1097      # determine if we've hit a loop.      # determine if we've hit a loop.
1098      my %alreadyFound = ();      my %alreadyFound = ();
1099      # This next list will serve as our result stack. We start by pushing object lists onto      # These next lists will serve as our result stack. We start by pushing object lists onto
1100      # the stack, and then popping them off to do the deletes. This means the deletes will      # the stack, and then popping them off to do the deletes. This means the deletes will
1101      # start with the longer paths before getting to the shorter ones. That, in turn, makes      # start with the longer paths before getting to the shorter ones. That, in turn, makes
1102      # sure we don't delete records that might be needed to forge relationships back to the      # sure we don't delete records that might be needed to forge relationships back to the
1103      # original item.      # original item. We have two lists-- one for TO-relationships, and one for
1104      my @pathList = ();      # FROM-relationships and entities.
1105        my @fromPathList = ();
1106        my @toPathList = ();
1107      # This final hash is used to remember what work still needs to be done. We push paths      # This final hash is used to remember what work still needs to be done. We push paths
1108      # onto the list, then pop them off to extend the paths. We prime it with the starting      # onto the list, then pop them off to extend the paths. We prime it with the starting
1109      # point. Note that we will work hard to insure that the last item on a path in the      # point. Note that we will work hard to insure that the last item on a path in the
# Line 1123  Line 1125 
1125          my $relations = $entityData->{Relations};          my $relations = $entityData->{Relations};
1126          for my $relation (keys %{$relations}) {          for my $relation (keys %{$relations}) {
1127              my @augmentedList = (@stackedPath, $relation);              my @augmentedList = (@stackedPath, $relation);
1128              push @pathList, \@augmentedList;              push @fromPathList, \@augmentedList;
1129          }          }
1130          # Now we need to look for relationships connected to this entity.          # Now we need to look for relationships connected to this entity.
1131          my $relationshipList = $self->{_metaData}->{Relationships};          my $relationshipList = $self->{_metaData}->{Relationships};
# Line 1133  Line 1135 
1135              if ($relationship->{from} eq $entityName) {              if ($relationship->{from} eq $entityName) {
1136                  # Add the path to this relationship.                  # Add the path to this relationship.
1137                  my @augmentedList = (@stackedPath, $entityName, $relationshipName);                  my @augmentedList = (@stackedPath, $entityName, $relationshipName);
1138                  push @pathList, \@augmentedList;                  push @fromPathList, \@augmentedList;
1139                  # Check the arity. If it's MM we're done. If it's 1M                  # Check the arity. If it's MM we're done. If it's 1M
1140                  # and the target hasn't been seen yet, we want to                  # and the target hasn't been seen yet, we want to
1141                  # stack the entity for future processing.                  # stack the entity for future processing.
# Line 1143  Line 1145 
1145                          # Here we have a new entity that's dependent on                          # Here we have a new entity that's dependent on
1146                          # the current entity, so we need to stack it.                          # the current entity, so we need to stack it.
1147                          my @stackList = (@augmentedList, $toEntity);                          my @stackList = (@augmentedList, $toEntity);
1148                          push @pathList, \@stackList;                          push @fromPathList, \@stackList;
1149                      }                      }
1150                  }                  }
1151              }              }
# Line 1151  Line 1153 
1153              # deletion.              # deletion.
1154              if ($relationship->{to} eq $entityName) {              if ($relationship->{to} eq $entityName) {
1155                  my @augmentedList = (@stackedPath, $entityName, $relationshipName);                  my @augmentedList = (@stackedPath, $entityName, $relationshipName);
1156                  push @pathList, \@augmentedList;                  push @toPathList, \@augmentedList;
1157              }              }
1158          }          }
1159      }      }
# Line 1160  Line 1162 
1162      # from a dependent table, we construct a join page from the first qualifier      # from a dependent table, we construct a join page from the first qualifier
1163      # to the table containing the dependent records to delete.      # to the table containing the dependent records to delete.
1164      my $qualifier = ($objectID =~ /%/ ? "LIKE ?" : "= ?");      my $qualifier = ($objectID =~ /%/ ? "LIKE ?" : "= ?");
1165      # Now it's time to do the deletes. We simply pop the paths off the stack.      # We need to make two passes. The first is through the to-list, and
1166        # the second through the from-list. The from-list is second because
1167        # the to-list may need to pass through some of the entities the
1168        # from-list would delete.
1169        my %stackList = ( from_link => \@fromPathList, to_link => \@toPathList );
1170        # Now it's time to do the deletes. We do it in two passes.
1171        for my $keyName ('to_link', 'from_link') {
1172            # Get the list for this key.
1173            my @pathList = @{$stackList{$keyName}};
1174            # Loop through this list.
1175      while (my $path = pop @pathList) {      while (my $path = pop @pathList) {
1176          # Get the table whose rows are to be deleted.          # Get the table whose rows are to be deleted.
1177          my @pathTables = @{$path};          my @pathTables = @{$path};
# Line 1180  Line 1191 
1191              # Connect the current relationship to the preceding entity.              # Connect the current relationship to the preceding entity.
1192              my ($entity, $rel) = @pathTables[$i-1,$i];              my ($entity, $rel) = @pathTables[$i-1,$i];
1193              # The style of connection depends on the direction of the relationship.              # The style of connection depends on the direction of the relationship.
             # We compute the direction by checking whether the preceding entity is  
             # the FROM or TO entity.  
             my $relationship = $self->_GetStructure($rel);  
             if ($relationship->{to} eq $entity) {  
                 # Here we're the TO. A TO link is always the end of a chain, so  
                 # we just tack it on at the end.  
                 $stmt .= " AND $entity.id = $rel.to_link";  
             } else {  
                 # Here we're the FROM. In that case, we'll need to check for a  
                 # next entity.  
1194                  $stmt .= " AND $entity.id = $rel.from_link";                  $stmt .= " AND $entity.id = $rel.from_link";
1195                  if ($i + 1 <= $#pathTables) {                  if ($i + 1 <= $#pathTables) {
1196                      # Here there's a next entity, so connect that to the relationship's                      # Here there's a next entity, so connect that to the relationship's
1197                      # to-link.                      # to-link.
1198                      my $entity2 = $pathTables[$i+1];                      my $entity2 = $pathTables[$i+1];
1199                      $stmt .= " AND $rel.to_link = $entity2.id";                      $stmt .= " AND $rel.$keyName = $entity2.id";
                 }  
1200              }              }
1201          }          }
1202          # Now we have our desired DELETE statement.          # Now we have our desired DELETE statement.
# Line 1214  Line 1214 
1214              $retVal->Add($target, $rv);              $retVal->Add($target, $rv);
1215          }          }
1216      }      }
1217        }
1218      # Return the result.      # Return the result.
1219      return $retVal;      return $retVal;
1220  }  }

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.33

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3