Parent Directory
|
Revision Log
Added the ability to load a table in a discard mode that throws away the data.
#!/usr/bin/perl -w package ERDBLoad; use strict; use Tracer; use PageBuilder; use ERDB; use Stats; =head1 ERDB Table Load Utility Object =head2 Introduction This object is designed to assist with creating the load file for an ERDB data relation. The user constructs the object by specifying an ERDB object and a relation name. This create the load file for the relevant relation. The client then passes in data lines which are written to a file, and calls L</Finish> to close the file and get the statistics. This module makes use of the internal ERDB method C<_IsPrimary>. =cut # =head2 Public Methods =head3 new C<< my $erload = ERDBLoad->new($erdb, $relationName, $directory); >> Begin loading an ERDB relation. =over 4 =item erdb ERDB object representing the target database. =item relationName Name of the relation being loaded. =item directory Name of the directory to use for the load files, WITHOUT a trailing slash. =item loadOnly TRUE if the data is to be loaded from an existing file, FALSE if a file is to be created. =item ignore TRUE if the data is to be discarded. This is used to save time when only a subset of the tables need to be loaded: the data for the ignored tables is simply discarded. =back =cut sub new { # Get the parameters. my ($class, $erdb, $relationName, $directory, $loadOnly, $ignore) = @_; # Validate the directory name. if (! -d $directory) { Confess("Load directory \"$directory\" not found."); } # Determine the name for this relation's load file. my $fileName = "$directory/$relationName.dtx"; # Declare the file handle variable. my $fileHandle; # Determine whether or not this is a primary relation. my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0); # Check to see if this is a load-only, ignore, or a generate-and-load. if ($ignore) { Trace("Relation $relationName will be ignored.") if T(2); $fileHandle = ""; } elsif ($loadOnly) { Trace("Relation $relationName will be loaded from $fileName.") if T(2); $fileHandle = ""; } else { # If this is a primary entity relation, sort the output to remove # duplicate keys. my $fileString = ($erdb->IsEntity($relationName) ? "| sort +0 -1 -u -t \"\t\" >$fileName" : ">$fileName"); # Open the output file and remember its handle. $fileHandle = Open(undef, $fileString); Trace("Relation $relationName load file created with primary flag $primary.") if T(2); } # Create the $erload object. my $retVal = { dbh => $erdb, fh => $fileHandle, fileName => $fileName, relName => $relationName, fileSize => 0, lineCount => 0, stats => Stats->new(), primary => $primary, ignore => ($ignore ? 1 : 0) }; # Bless and return it. bless $retVal, $class; return $retVal; } =head3 Ignore C<< my $flag = $erload->Ignore(); >> Return TRUE if we are ignoring this table, else FALSE. =cut #: Return Type $; sub Ignore { # Get the parameters. my ($self) = @_; # Return the result. return $self->{ignore}; } =head3 Put C<< my = $erload->Put($field1, $field2, ..., $fieldN); >> Write a line of data to the load file. This may also cause the load file to be closed and data read into the table. =over 4 =item field1, field2, ..., fieldN List of field values to be put into the data line. The field values must be in the order determined shown in the documentation for the table. Internal tabs and new-lines will automatically be escaped before the data line is formatted. =back =cut #: Return Type ; sub Put { # Get the ERDBLoad instance and the field list. my ($self, @rawFields) = @_; # Only proceed if we're not ignoring. if (! $self->{ignore}) { # Insure the field values are okay. my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields); # Run through the list of field values, escaping them. my @fields = map { Tracer::Escape($_) } @rawFields; # If this is a primary relation, append the new-record field. if ($self->{primary}) { push @fields, '0'; } # Form a data line from the fields. my $line = join("\t", @fields) . "\n"; # Write the new record to the load file. my $fh = $self->{fh}; print $fh $line; # Determine how long this will make the load file. my $lineLength = length $line; # Update the statistics. $self->{fileSize} += $lineLength; $self->{lineCount} ++; $self->Add("lineOut"); if ($truncates > 0) { $self->Add("truncated", $truncates); } } } =head3 Add C<< my = $stats->Add($statName, $value); >> Increment the specified statistic. =over 4 =item statName Name of the statistic to increment. =item value (optional) Value by which to increment it. If omitted, C<1> is assumed. =back =cut #: Return Type ; sub Add { # Get the parameters. my ($self, $statName, $value) = @_; # Fix the value. if (! defined $value) { $value = 1; } # Increment the statistic. $self->{stats}->Add($statName, $value); } =head3 Finish C<< my $stats = $erload->Finish(); >> Finish loading the table. This closes the load file and loads its contents into the database. It also creates the indexes if the DBMS uses post-indexing. =over 4 =item RETURN Returns a statistics object describing what happened during the load and containing any error messages. =back =cut sub Finish { # Get this object instance. my ($self) = @_; if ($self->{fh}) { # Close the load file. close $self->{fh}; } # Return the statistics object. return $self->{stats}; } =head3 RelName C<< my $name = $erload->RelName; >> Name of the relation being loaded by this object. =cut sub RelName { # Get the object instance. my ($self) = @_; # Return the relation name. return $self->{relName}; } 1;
MCS Webmaster | ViewVC Help |
Powered by ViewVC 1.0.3 |