[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.9, Thu Jun 16 19:08:59 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 1298  Line 1300 
1300          # Get the number of fields in this relation.          # Get the number of fields in this relation.
1301          my @fieldList = @{$relation->{Fields}};          my @fieldList = @{$relation->{Fields}};
1302          my $fieldCount = @fieldList;          my $fieldCount = @fieldList;
         # Record the number of expected fields.  
         my $expectedFields = $fieldCount + ($primary ? 1 : 0);  
1303          # Start a database transaction.          # Start a database transaction.
1304          $dbh->begin_tran;          $dbh->begin_tran;
1305          # 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.
1306          open TABLEIN, '<', $fileName;          open TABLEIN, '<', $fileName;
1307          my $tempName = "$fileName.tbl";          my $tempName = "$fileName.tbl";
1308          open TABLEOUT, '>', $tempName;          open TABLEOUT, '>', $tempName;
1309            my $inputCount = 0;
1310          # Loop through the file.          # Loop through the file.
1311          while (<TABLEIN>) {          while (<TABLEIN>) {
1312                    $inputCount++;
1313                  # Chop off the new-line character.                  # Chop off the new-line character.
1314                  my $record = $_;                  my $record = Tracer::Strip($_);
                 chomp $record;  
1315          # Only proceed if the record is non-blank.          # Only proceed if the record is non-blank.
1316          if ($record) {          if ($record) {
1317              # Escape all the backslashes found in the line.              # Escape all the backslashes found in the line.
1318              $record =~ s/\\/\\\\/g;              $record =~ s/\\/\\\\/g;
1319              # Eliminate any trailing tabs.                          # Insure the number of fields is correct.
1320              chop $record while substr($record, -1) eq "\t";                          my @fields = split /\t/, $record;
1321                            while (@fields > $fieldCount) {
1322                                    my $extraField = $fields[$#fields];
1323                                    delete $fields[$#fields];
1324                                    if ($extraField) {
1325                                            Trace("Nonblank extra field value \"$extraField\" deleted from record $inputCount of $fileName.") if T(1);
1326                                    }
1327                            }
1328                            while (@fields < $fieldCount) {
1329                                    push @fields, "";
1330                            }
1331              # 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
1332              # this record is not new, but part of the original load).              # this record is not new, but part of the original load).
1333              if ($primary) {              if ($primary) {
1334                  $record .= "\t0";                  push @fields, "0";
1335              }              }
1336              # Write the record.              # Write the record.
1337                            $record = join "\t", @fields;
1338              print TABLEOUT "$record\n";              print TABLEOUT "$record\n";
1339              # Count the record read.              # Count the record written.
1340              my $count = $retVal->Add('records');              my $count = $retVal->Add('records');
1341              my $len = length $record;              my $len = length $record;
1342              Trace("Record $count written with $len characters.") if T(4);              Trace("Record $count written with $len characters.") if T(4);
1343            } else {
1344                            # Here we have a blank record.
1345                            $retVal->Add('skipped');
1346          }          }
1347          }          }
1348          # Close the files.          # Close the files.

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3