[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.8, Thu Jun 9 19:06:55 2005 UTC revision 1.11, Thu Jun 23 21:24:49 2005 UTC
# Line 1252  Line 1252 
1252    
1253  C<< my %results = $database->LoadTable($fileName, $relationName, $truncateFlag); >>  C<< my %results = $database->LoadTable($fileName, $relationName, $truncateFlag); >>
1254    
1255  Load data from a tab-delimited file into a specified table, optionally re-creating the table first.  Load data from a tab-delimited file into a specified table, optionally re-creating the table
1256    first.
1257    
1258  =over 4  =over 4
1259    
# Line 1270  Line 1271 
1271    
1272  =item RETURN  =item RETURN
1273    
1274  Returns a statistical object containing the number of records read and a list of the error messages.  Returns a statistical object containing the number of records read and a list of
1275    the error messages.
1276    
1277  =back  =back
1278    
# Line 1281  Line 1283 
1283          # Create the statistical return object.          # Create the statistical return object.
1284          my $retVal = _GetLoadStats();          my $retVal = _GetLoadStats();
1285          # Trace the fact of the load.          # Trace the fact of the load.
1286          Trace("Loading table $relationName from $fileName") if T(1);      Trace("Loading table $relationName from $fileName") if T(2);
1287          # Get the database handle.          # Get the database handle.
1288          my $dbh = $self->{_dbh};          my $dbh = $self->{_dbh};
1289          # Get the relation data.          # Get the relation data.
1290          my $relation = $self->_FindRelation($relationName);          my $relation = $self->_FindRelation($relationName);
1291          # Check the truncation flag.          # Check the truncation flag.
1292          if ($truncateFlag) {          if ($truncateFlag) {
1293                  Trace("Creating table $relationName") if T(1);          Trace("Creating table $relationName") if T(2);
1294                  # Re-create the table without its index.                  # Re-create the table without its index.
1295                  $self->CreateTable($relationName, 0);                  $self->CreateTable($relationName, 0);
1296            # If this is a pre-index DBMS, create the index here.
1297            if ($dbh->{_preIndex}) {
1298                eval {
1299                    $self->CreateIndex($relationName);
1300                };
1301                if ($@) {
1302                    $retVal->AddMessage($@);
1303                }
1304            }
1305          }          }
1306          # Determine whether or not this is a primary relation. Primary relations have an extra          # Determine whether or not this is a primary relation. Primary relations have an extra
1307          # field indicating whether or not a given object is new or was loaded from the flat files.          # field indicating whether or not a given object is new or was loaded from the flat files.
# Line 1298  Line 1309 
1309          # Get the number of fields in this relation.          # Get the number of fields in this relation.
1310          my @fieldList = @{$relation->{Fields}};          my @fieldList = @{$relation->{Fields}};
1311          my $fieldCount = @fieldList;          my $fieldCount = @fieldList;
         # Record the number of expected fields.  
         my $expectedFields = $fieldCount + ($primary ? 1 : 0);  
1312          # Start a database transaction.          # Start a database transaction.
1313          $dbh->begin_tran;          $dbh->begin_tran;
1314          # Open the relation file. We need to create a cleaned-up copy before loading.          # Open the relation file. We need to create a cleaned-up copy before loading.
1315          open TABLEIN, '<', $fileName;          open TABLEIN, '<', $fileName;
1316          my $tempName = "$fileName.tbl";          my $tempName = "$fileName.tbl";
1317          open TABLEOUT, '>', $tempName;          open TABLEOUT, '>', $tempName;
1318        my $inputCount = 0;
1319          # Loop through the file.          # Loop through the file.
1320          while (<TABLEIN>) {          while (<TABLEIN>) {
1321            $inputCount++;
1322                  # Chop off the new-line character.                  # Chop off the new-line character.
1323                  my $record = $_;          my $record = Tracer::Strip($_);
                 chomp $record;  
1324          # Only proceed if the record is non-blank.          # Only proceed if the record is non-blank.
1325          if ($record) {          if ($record) {
1326              # Escape all the backslashes found in the line.              # Escape all the backslashes found in the line.
1327              $record =~ s/\\/\\\\/g;              $record =~ s/\\/\\\\/g;
1328              # Eliminate any trailing tabs.              # Insure the number of fields is correct.
1329              chop $record while substr($record, -1) eq "\t";              my @fields = split /\t/, $record;
1330                while (@fields > $fieldCount) {
1331                    my $extraField = $fields[$#fields];
1332                    delete $fields[$#fields];
1333                    if ($extraField) {
1334                        Trace("Nonblank extra field value \"$extraField\" deleted from record $inputCount of $fileName.") if T(1);
1335                    }
1336                }
1337                while (@fields < $fieldCount) {
1338                    push @fields, "";
1339                }
1340              # If this is a primary relation, add a 0 for the new-record flag (indicating that              # If this is a primary relation, add a 0 for the new-record flag (indicating that
1341              # this record is not new, but part of the original load).              # this record is not new, but part of the original load).
1342              if ($primary) {              if ($primary) {
1343                  $record .= "\t0";                  push @fields, "0";
1344              }              }
1345              # Write the record.              # Write the record.
1346                $record = join "\t", @fields;
1347              print TABLEOUT "$record\n";              print TABLEOUT "$record\n";
1348              # Count the record read.              # Count the record written.
1349              my $count = $retVal->Add('records');              my $count = $retVal->Add('records');
1350              my $len = length $record;              my $len = length $record;
1351              Trace("Record $count written with $len characters.") if T(4);              Trace("Record $count written with $len characters.") if T(4);
1352            } else {
1353                # Here we have a blank record.
1354                $retVal->Add('skipped');
1355          }          }
1356          }          }
1357          # Close the files.          # Close the files.
1358          close TABLEIN;          close TABLEIN;
1359          close TABLEOUT;          close TABLEOUT;
1360      Trace("Temporary file $tempName created.") if T(4);      Trace("Temporary file $tempName created.") if T(2);
1361      # Load the table.      # Load the table.
1362          my $rv;          my $rv;
1363          eval {          eval {
# Line 1345  Line 1369 
1369                  Trace("Table load failed for $relationName.") if T(1);                  Trace("Table load failed for $relationName.") if T(1);
1370          } else {          } else {
1371                  # Here we successfully loaded the table. Trace the number of records loaded.                  # Here we successfully loaded the table. Trace the number of records loaded.
1372                  Trace("$retVal->{records} records read for $relationName.") if T(1);          Trace("$retVal->{records} records read for $relationName.") if T(2);
1373                  # If we're rebuilding, we need to create the table indexes.                  # If we're rebuilding, we need to create the table indexes.
1374                  if ($truncateFlag) {          if ($truncateFlag && ! $dbh->{_preIndex}) {
1375                          eval {                          eval {
1376                                  $self->CreateIndex($relationName);                                  $self->CreateIndex($relationName);
1377                          };                          };

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.11

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3