28 |
|
|
29 |
=head3 new |
=head3 new |
30 |
|
|
31 |
C<< my $erload = ERDBLoad->new($erdb, $relationName, $directory); >> |
C<< my $erload = ERDBLoad->new($erdb, $relationName, $directory, $loadOnly, $ignore); >> |
32 |
|
|
33 |
Begin loading an ERDB relation. |
Begin loading an ERDB relation. |
34 |
|
|
46 |
|
|
47 |
Name of the directory to use for the load files, WITHOUT a trailing slash. |
Name of the directory to use for the load files, WITHOUT a trailing slash. |
48 |
|
|
49 |
=item estimatedRows (optional) |
=item loadOnly |
50 |
|
|
51 |
Estimated maximum number of table rows. If omitted, the table will be created in |
TRUE if the data is to be loaded from an existing file, FALSE if a file is |
52 |
a format that permits an essentially unlimited number of rows. |
to be created. |
53 |
|
|
54 |
|
=item ignore |
55 |
|
|
56 |
|
TRUE if the data is to be discarded. This is used to save time when only |
57 |
|
a subset of the tables need to be loaded: the data for the ignored tables |
58 |
|
is simply discarded. |
59 |
|
|
60 |
=back |
=back |
61 |
|
|
63 |
|
|
64 |
sub new { |
sub new { |
65 |
# Get the parameters. |
# Get the parameters. |
66 |
my ($class, $erdb, $relationName, $directory, $estimatedRows) = @_; |
my ($class, $erdb, $relationName, $directory, $loadOnly, $ignore) = @_; |
67 |
# Validate the directory name. |
# Validate the directory name. |
68 |
if (! -d $directory) { |
if (! -d $directory) { |
69 |
Confess("Load directory \"$directory\" not found."); |
Confess("Load directory \"$directory\" not found."); |
70 |
} |
} |
71 |
# Determine the name for this relation's load file. |
# Determine the name for this relation's load file. |
72 |
my $fileName = "$directory/$relationName.dtx"; |
my $fileName = "$directory/$relationName.dtx"; |
73 |
|
# Declare the file handle variable. |
74 |
|
my $fileHandle; |
75 |
|
# Determine whether or not this is a primary relation. |
76 |
|
my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0); |
77 |
|
# Check to see if this is a load-only, ignore, or a generate-and-load. |
78 |
|
if ($ignore) { |
79 |
|
Trace("Relation $relationName will be ignored.") if T(2); |
80 |
|
$fileHandle = ""; |
81 |
|
} elsif ($loadOnly) { |
82 |
|
Trace("Relation $relationName will be loaded from $fileName.") if T(2); |
83 |
|
$fileHandle = ""; |
84 |
|
} else { |
85 |
|
# Determine the sort for this relation. |
86 |
|
my $fileString = "| " . $erdb->SortNeeded($relationName) . " >$fileName"; |
87 |
|
Trace("Load file creation string is \"$fileString\".") if T(3); |
88 |
# Open the output file and remember its handle. |
# Open the output file and remember its handle. |
89 |
my $fileHandle = Open(undef, ">$fileName"); |
$fileHandle = Open(undef, $fileString); |
90 |
|
Trace("Relation $relationName load file created with primary flag $primary.") if T(2); |
91 |
|
} |
92 |
# Create the $erload object. |
# Create the $erload object. |
93 |
my $retVal = { |
my $retVal = { |
94 |
dbh => $erdb, |
dbh => $erdb, |
98 |
fileSize => 0, |
fileSize => 0, |
99 |
lineCount => 0, |
lineCount => 0, |
100 |
stats => Stats->new(), |
stats => Stats->new(), |
101 |
primary => $erdb->_IsPrimary($relationName) |
primary => $primary, |
102 |
|
ignore => ($ignore ? 1 : 0) |
103 |
}; |
}; |
104 |
# Bless and return it. |
# Bless and return it. |
105 |
bless $retVal, $class; |
bless $retVal, $class; |
106 |
return $retVal; |
return $retVal; |
107 |
} |
} |
108 |
|
|
109 |
|
=head3 Ignore |
110 |
|
|
111 |
|
C<< my $flag = $erload->Ignore; >> |
112 |
|
|
113 |
|
Return TRUE if we are ignoring this table, else FALSE. |
114 |
|
|
115 |
|
=cut |
116 |
|
#: Return Type $; |
117 |
|
sub Ignore { |
118 |
|
# Get the parameters. |
119 |
|
my ($self) = @_; |
120 |
|
# Return the result. |
121 |
|
return $self->{ignore}; |
122 |
|
} |
123 |
|
|
124 |
=head3 Put |
=head3 Put |
125 |
|
|
126 |
C<< my = $erload->Put($field1, $field2, ..., $fieldN); >> |
C<< my = $erload->Put($field1, $field2, ..., $fieldN); >> |
141 |
=cut |
=cut |
142 |
#: Return Type ; |
#: Return Type ; |
143 |
sub Put { |
sub Put { |
144 |
# Get the ERDBLoad instance. |
# Get the ERDBLoad instance and the field list. |
145 |
my $self = shift @_; |
my ($self, @rawFields) = @_; |
146 |
|
# Only proceed if we're not ignoring. |
147 |
|
if (! $self->{ignore}) { |
148 |
|
# Convert the hash-string fields to their digested value. |
149 |
|
$self->{dbh}->DigestFields($self->{relName}, \@rawFields); |
150 |
|
# Insure the field values are okay. |
151 |
|
my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields); |
152 |
# Run through the list of field values, escaping them. |
# Run through the list of field values, escaping them. |
153 |
my @fields = map { Tracer::Escape($_) } @_; |
my @fields = map { Tracer::Escape($_) } @rawFields; |
154 |
# If this is a primary relation, append the new-record field. |
# If this is a primary relation, append the new-record field. |
155 |
if ($self->{primary}) { |
if ($self->{primary}) { |
156 |
push @fields, '0'; |
push @fields, '0'; |
165 |
# Update the statistics. |
# Update the statistics. |
166 |
$self->{fileSize} += $lineLength; |
$self->{fileSize} += $lineLength; |
167 |
$self->{lineCount} ++; |
$self->{lineCount} ++; |
168 |
|
$self->Add("lineOut"); |
169 |
|
if ($truncates > 0) { |
170 |
|
$self->Add("truncated", $truncates); |
171 |
|
} |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
=head3 Add |
176 |
|
|
177 |
|
C<< my = $stats->Add($statName, $value); >> |
178 |
|
|
179 |
|
Increment the specified statistic. |
180 |
|
|
181 |
|
=over 4 |
182 |
|
|
183 |
|
=item statName |
184 |
|
|
185 |
|
Name of the statistic to increment. |
186 |
|
|
187 |
|
=item value (optional) |
188 |
|
|
189 |
|
Value by which to increment it. If omitted, C<1> is assumed. |
190 |
|
|
191 |
|
=back |
192 |
|
|
193 |
|
=cut |
194 |
|
#: Return Type ; |
195 |
|
sub Add { |
196 |
|
# Get the parameters. |
197 |
|
my ($self, $statName, $value) = @_; |
198 |
|
# Fix the value. |
199 |
|
if (! defined $value) { |
200 |
|
$value = 1; |
201 |
|
} |
202 |
|
# Increment the statistic. |
203 |
|
$self->{stats}->Add($statName, $value); |
204 |
} |
} |
205 |
|
|
206 |
=head3 Finish |
=head3 Finish |
207 |
|
|
208 |
C<< my $stats = $erload->Finish(); >> |
C<< my $stats = $erload->Finish(); >> |
209 |
|
|
210 |
Finish loading the table. This closes the load file and loads its contents into the database. |
Finish loading the table. This closes the load file. |
|
It also creates the indexes if the DBMS uses post-indexing. |
|
211 |
|
|
212 |
=over 4 |
=over 4 |
213 |
|
|
223 |
sub Finish { |
sub Finish { |
224 |
# Get this object instance. |
# Get this object instance. |
225 |
my ($self) = @_; |
my ($self) = @_; |
226 |
|
if ($self->{fh}) { |
227 |
# Close the load file. |
# Close the load file. |
228 |
close $self->{fh}; |
close $self->{fh}; |
229 |
|
} |
230 |
# Return the statistics object. |
# Return the statistics object. |
231 |
return $self->{stats}; |
return $self->{stats}; |
232 |
} |
} |