[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.20, Sun Sep 11 17:02:33 2005 UTC revision 1.28, Thu Oct 20 09:31:02 2005 UTC
# Line 553  Line 553 
553  sub CreateTables {  sub CreateTables {
554      # Get the parameters.      # Get the parameters.
555      my ($self) = @_;      my ($self) = @_;
556      my $metadata = $self->{_metaData};      # Get the relation names.
557      my $dbh = $self->{_dbh};      my @relNames = $self->GetTableNames();
558      # Loop through the entities.      # Loop through the relations.
559      my $entityHash = $metadata->{Entities};      for my $relationName (@relNames) {
     for my $entityName (keys %{$entityHash}) {  
         my $entityData = $entityHash->{$entityName};  
         # Tell the user what we're doing.  
         Trace("Creating relations for entity $entityName.") if T(1);  
         # Loop through the entity's relations.  
         for my $relationName (keys %{$entityData->{Relations}}) {  
560              # Create a table for this relation.              # Create a table for this relation.
561              $self->CreateTable($relationName);              $self->CreateTable($relationName);
562              Trace("Relation $relationName created.") if T(1);          Trace("Relation $relationName created.") if T(2);
         }  
     }  
     # Loop through the relationships.  
     my $relationshipTable = $metadata->{Relationships};  
     for my $relationshipName (keys %{$metadata->{Relationships}}) {  
         # Create a table for this relationship.  
         Trace("Creating relationship $relationshipName.") if T(1);  
         $self->CreateTable($relationshipName);  
563      }      }
564  }  }
565    
# Line 674  Line 660 
660          # Get the index's uniqueness flag.          # Get the index's uniqueness flag.
661          my $unique = (exists $indexData->{Unique} ? $indexData->{Unique} : 'false');          my $unique = (exists $indexData->{Unique} ? $indexData->{Unique} : 'false');
662          # Create the index.          # Create the index.
663          $dbh->create_index(idx => $indexName, tbl => $relationName, flds => $flds, unique => $unique);          my $rv = $dbh->create_index(idx => $indexName, tbl => $relationName,
664                                        flds => $flds, unique => $unique);
665            if ($rv) {
666          Trace("Index created: $indexName for $relationName ($flds)") if T(1);          Trace("Index created: $indexName for $relationName ($flds)") if T(1);
667            } else {
668                Confess("Error creating index $indexName for $relationName using ($flds): " . $dbh->error_message());
669            }
670      }      }
671  }  }
672    
# Line 724  Line 715 
715      $directoryName =~ s!/\\$!!;      $directoryName =~ s!/\\$!!;
716      # Declare the return variable.      # Declare the return variable.
717      my $retVal = Stats->new();      my $retVal = Stats->new();
718      # Get the metadata structure.      # Get the relation names.
719      my $metaData = $self->{_metaData};      my @relNames = $self->GetTableNames();
720      # Loop through the entities.      for my $relationName (@relNames) {
     for my $entity (values %{$metaData->{Entities}}) {  
         # Loop through the entity's relations.  
         for my $relationName (keys %{$entity->{Relations}}) {  
721              # Try to load this relation.              # Try to load this relation.
722              my $result = $self->_LoadRelation($directoryName, $relationName, $rebuild);              my $result = $self->_LoadRelation($directoryName, $relationName, $rebuild);
723              # Accumulate the statistics.              # Accumulate the statistics.
724              $retVal->Accumulate($result);              $retVal->Accumulate($result);
725          }          }
     }  
     # Loop through the relationships.  
     for my $relationshipName (keys %{$metaData->{Relationships}}) {  
         # Try to load this relationship's relation.  
         my $result = $self->_LoadRelation($directoryName, $relationshipName, $rebuild);  
         # Accumulate the statistics.  
         $retVal->Accumulate($result);  
     }  
726      # Add the duration of the load to the statistical object.      # Add the duration of the load to the statistical object.
727      $retVal->Add('duration', gettimeofday - $startTime);      $retVal->Add('duration', gettimeofday - $startTime);
728      # Return the accumulated statistics.      # Return the accumulated statistics.
729      return $retVal;      return $retVal;
730  }  }
731    
732    
733  =head3 GetTableNames  =head3 GetTableNames
734    
735  C<< my @names = $erdb->GetTableNames; >>  C<< my @names = $erdb->GetTableNames; >>
# Line 1000  Line 981 
981              $command .= " ORDER BY $orderClause";              $command .= " ORDER BY $orderClause";
982          }          }
983      }      }
984      Trace("SQL query: $command") if T(3);      Trace("SQL query: $command") if T(SQL => 4);
985      Trace("PARMS: '" . (join "', '", @params) . "'") if (T(4) && (@params > 0));      Trace("PARMS: '" . (join "', '", @params) . "'") if (T(SQL => 4) && (@params > 0));
986      my $sth = $dbh->prepare_command($command);      my $sth = $dbh->prepare_command($command);
987      # Execute it with the parameters bound in.      # Execute it with the parameters bound in.
988      $sth->execute(@params) || Confess("SELECT error" . $sth->errstr());      $sth->execute(@params) || Confess("SELECT error" . $sth->errstr());
# Line 1312  Line 1293 
1293    
1294  =item RETURN  =item RETURN
1295    
1296  Returns a statistical object containing the number of records read and a list of  Returns a statistical object containing a list of the error messages.
 the error messages.  
1297    
1298  =back  =back
1299    
# Line 1327  Line 1307 
1307      Trace("Loading table $relationName from $fileName") if T(2);      Trace("Loading table $relationName from $fileName") if T(2);
1308      # Get the database handle.      # Get the database handle.
1309      my $dbh = $self->{_dbh};      my $dbh = $self->{_dbh};
1310        # Get the input file size.
1311        my $fileSize = -s $fileName;
1312      # Get the relation data.      # Get the relation data.
1313      my $relation = $self->_FindRelation($relationName);      my $relation = $self->_FindRelation($relationName);
1314      # Check the truncation flag.      # Check the truncation flag.
# Line 1337  Line 1319 
1319          # leave extra room. We postulate a minimum row count of 1000 to          # leave extra room. We postulate a minimum row count of 1000 to
1320          # prevent problems with incoming empty load files.          # prevent problems with incoming empty load files.
1321          my $rowSize = $self->EstimateRowSize($relationName);          my $rowSize = $self->EstimateRowSize($relationName);
         my $fileSize = -s $fileName;  
1322          my $estimate = FIG::max($fileSize * 1.5 / $rowSize, 1000);          my $estimate = FIG::max($fileSize * 1.5 / $rowSize, 1000);
1323          # Re-create the table without its index.          # Re-create the table without its index.
1324          $self->CreateTable($relationName, 0, $estimate);          $self->CreateTable($relationName, 0, $estimate);
# Line 1361  Line 1342 
1342          $retVal->AddMessage("Table load failed for $relationName using $fileName.");          $retVal->AddMessage("Table load failed for $relationName using $fileName.");
1343          Trace("Table load failed for $relationName.") if T(1);          Trace("Table load failed for $relationName.") if T(1);
1344      } else {      } else {
1345          # Here we successfully loaded the table. Trace the number of records loaded.          # Here we successfully loaded the table.
1346          Trace("$retVal->{records} records read for $relationName.") if T(2);          $retVal->Add("tables");
1347            my $size = -s $fileName;
1348            Trace("$size bytes loaded into $relationName.") if T(2);
1349          # If we're rebuilding, we need to create the table indexes.          # If we're rebuilding, we need to create the table indexes.
1350          if ($truncateFlag && ! $dbh->{_preIndex}) {          if ($truncateFlag && ! $dbh->{_preIndex}) {
1351              eval {              eval {
# Line 1373  Line 1356 
1356              }              }
1357          }          }
1358      }      }
     # Commit the database changes.  
     $dbh->commit_tran;  
1359      # Analyze the table to improve performance.      # Analyze the table to improve performance.
1360      $dbh->vacuum_it($relationName);      $dbh->vacuum_it($relationName);
1361      # Return the statistics.      # Return the statistics.
# Line 1654  Line 1635 
1635  =cut  =cut
1636    
1637  sub _GetLoadStats {  sub _GetLoadStats {
1638      return Stats->new('records');      return Stats->new();
1639  }  }
1640    
1641  =head3 GenerateFields  =head3 GenerateFields
# Line 2225  Line 2206 
2206          my @fromList = ();          my @fromList = ();
2207          my @toList = ();          my @toList = ();
2208          my @bothList = ();          my @bothList = ();
2209          Trace("Join table build for $entityName.") if T(4);          Trace("Join table build for $entityName.") if T(metadata => 4);
2210          for my $relationshipName (keys %{$relationshipList}) {          for my $relationshipName (keys %{$relationshipList}) {
2211              my $relationship = $relationshipList->{$relationshipName};              my $relationship = $relationshipList->{$relationshipName};
2212              # Determine if this relationship has our entity in one of its link fields.              # Determine if this relationship has our entity in one of its link fields.
# Line 2236  Line 2217 
2217                  if ($toEntity eq $entityName) {                  if ($toEntity eq $entityName) {
2218                      # Here the relationship is recursive.                      # Here the relationship is recursive.
2219                      push @bothList, $relationshipName;                      push @bothList, $relationshipName;
2220                      Trace("Relationship $relationshipName put in both-list.") if T(4);                      Trace("Relationship $relationshipName put in both-list.") if T(metadata => 4);
2221                  } else {                  } else {
2222                      # Here the relationship comes from the entity.                      # Here the relationship comes from the entity.
2223                      push @fromList, $relationshipName;                      push @fromList, $relationshipName;
2224                      Trace("Relationship $relationshipName put in from-list.") if T(4);                      Trace("Relationship $relationshipName put in from-list.") if T(metadata => 4);
2225                  }                  }
2226              } elsif ($toEntity eq $entityName) {              } elsif ($toEntity eq $entityName) {
2227                  # Here the relationship goes to the entity.                  # Here the relationship goes to the entity.
2228                  push @toList, $relationshipName;                  push @toList, $relationshipName;
2229                  Trace("Relationship $relationshipName put in to-list.") if T(4);                  Trace("Relationship $relationshipName put in to-list.") if T(metadata => 4);
2230              }              }
2231          }          }
2232          # Create the nonrecursive joins. Note that we build two hashes for running          # Create the nonrecursive joins. Note that we build two hashes for running
# Line 2261  Line 2242 
2242                  # Create joins between the entity and this relationship.                  # Create joins between the entity and this relationship.
2243                  my $linkField = "$relationshipName.${linkType}_link";                  my $linkField = "$relationshipName.${linkType}_link";
2244                  my $joinClause = "$entityName.id = $linkField";                  my $joinClause = "$entityName.id = $linkField";
2245                  Trace("Entity join clause is $joinClause for $entityName and $relationshipName.") if T(4);                  Trace("Entity join clause is $joinClause for $entityName and $relationshipName.") if T(metadata => 4);
2246                  $joinTable{"$entityName/$relationshipName"} = $joinClause;                  $joinTable{"$entityName/$relationshipName"} = $joinClause;
2247                  $joinTable{"$relationshipName/$entityName"} = $joinClause;                  $joinTable{"$relationshipName/$entityName"} = $joinClause;
2248                  # Create joins between this relationship and the other relationships.                  # Create joins between this relationship and the other relationships.
# Line 2282  Line 2263 
2263                              # relationship and itself are prohibited.                              # relationship and itself are prohibited.
2264                              my $relJoinClause = "$otherName.${otherType}_link = $linkField";                              my $relJoinClause = "$otherName.${otherType}_link = $linkField";
2265                              $joinTable{$joinKey} = $relJoinClause;                              $joinTable{$joinKey} = $relJoinClause;
2266                              Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(4);                              Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(metadata => 4);
2267                          }                          }
2268                      }                      }
2269                  }                  }
# Line 2291  Line 2272 
2272                  # relationship can only be ambiguous with another recursive relationship,                  # relationship can only be ambiguous with another recursive relationship,
2273                  # and the incoming relationship from the outer loop is never recursive.                  # and the incoming relationship from the outer loop is never recursive.
2274                  for my $otherName (@bothList) {                  for my $otherName (@bothList) {
2275                      Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(4);                      Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(metadata => 4);
2276                      # Join from the left.                      # Join from the left.
2277                      $joinTable{"$relationshipName/$otherName"} =                      $joinTable{"$relationshipName/$otherName"} =
2278                          "$linkField = $otherName.from_link";                          "$linkField = $otherName.from_link";
# Line 2306  Line 2287 
2287          # rise to situations where we can't create the path we want; however, it is always          # rise to situations where we can't create the path we want; however, it is always
2288          # possible to get the same effect using multiple queries.          # possible to get the same effect using multiple queries.
2289          for my $relationshipName (@bothList) {          for my $relationshipName (@bothList) {
2290              Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(4);              Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(metadata => 4);
2291              # Join to the entity from each direction.              # Join to the entity from each direction.
2292              $joinTable{"$entityName/$relationshipName"} =              $joinTable{"$entityName/$relationshipName"} =
2293                  "$entityName.id = $relationshipName.from_link";                  "$entityName.id = $relationshipName.from_link";

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.28

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3