ETLp/Loader/OracleSQLLoader.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 92 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ETLp::Loader::OracleSQLLoader;
2  
3 use MooseX::Declare;
4  
5 =head1 NAME
6  
7 ETLp::Loader::OracleSQLLoader - Load data using SQL*Loader
8  
9 =head1 SYNOPSIS
10  
11 This module will load data using Oracle's SQL*Loader utility. It is designed
12 to be used when:
13  
14 * The backend database is Oracle
15 * The functionality is not provided by ETLp::Loader::CSV
16 * Speed is of the essence, and features such as direct path or
17 parallel loading are required.
18  
19 For Example
20  
21 use ETLp::Loader::OracleSQLLoader;
22  
23 my $sqlldr = ETLp::Loader::OracleSQLLoader->new(
24 userid => 'scott/tiger',
25 table => 'scores',
26 filename => "$data_directory/$filename",
27 specification => $specification,
28 mode => 'truncate',
29 controlfile => "$outfile_directory/$filename.ctl",
30 logfile => "$outfile_directory/$filename.log",
31 discardfile => "$outfile_directory/$filename.dsc",
32 badfile => "$outfile_directory/$filename.bad",
33 );
34
35 my $recode = $sqlldr->load;
36
37 if ($retcode) {
38 print $sqlldr->error;
39 }
40
41 =cut
42  
43 class ETLp::Loader::OracleSQLLoader with ETLp::Role::Config {
44 use ETLp::Exception;
45 use ETLp::Types;
46
47 has 'userid' => (is => 'ro', isa => 'Str', required => 1);
48 has 'controlfile' => (is => 'ro', isa => 'Str', required => 1);
49 has 'table' => (is => 'ro', isa => 'Str', required => 1);
50 has 'filename' => (is => 'ro', isa => 'Str', required => 1);
51 has 'specification' => (is => 'ro', isa => 'Str', required => 1);
52 has 'logfile' => (is => 'ro', isa => 'Maybe[Str]');
53 has 'badfile' => (is => 'ro', isa => 'Maybe[Str]');
54 has 'discardfile' => (is => 'ro', isa => 'Maybe[Str]');
55 has 'keep_controlfile' => (is => 'ro', isa => 'Bool', default => 0);
56 has 'keep_logfile' => (is => 'ro', isa => 'Bool', default => 1);
57 has 'keep_badfile' => (is => 'ro', isa => 'Bool', default => 1);
58 has 'keep_discardfile' => (is => 'ro', isa => 'Bool', default => 1);
59 has 'parameters' => (is => 'ro', isa => 'Maybe[HashRef]');
60 has 'localize' => (is => 'ro', isa => 'Bool', default => 0);
61 has 'mode' => (is => 'ro', isa => 'SQLLoaderMode', default => 'append');
62
63 our @keywords = qw/
64 discardmax
65 skip
66 load
67 errors
68 rows
69 bindsize
70 silent
71 direct
72 parfile
73 parallel
74 file
75 skip_unusable_indexes
76 skip_index_maintenance
77 readsize
78 external_table
79 columnarrayrows
80 streamsize
81 multithreading
82 resumable
83 resumable_name
84 resumable_timeout
85 /;
86
87 =head1 METHODS
88  
89 =head2 new
90  
91 Creates a new ETLp::Loader::OracleSQLLoader object. Accespts the
92 following attributes:
93  
94 * userid. Required. The username and password of the user connecting
95 to the database, in the form:
96 o <user>/<password> or
97 o <user>/<password>@<tns alias>
98 * controlfile. Required. The name of the control file. The content
99 will be generated by this module.
100 * table. Required. The name of the table that the data will be loaded
101 into
102 * filename. Required. The file to be loaded.
103 * mode. Optional. The type of load - append, truncate, insert or
104 replace. Defaults to append
105 * specification. Required. The specification of the incoming file and
106 the processing. E.g.
107
108 fields terminated by ","
109 (id, name, score, file_id constant 4)
110
111 * logfile. Optional. The name of the log file
112 * badfile. Optional. The name of the bad file.
113 * discardfile . Optional. The name of the discardfile.
114 * keep_controlfile. Optional. Whether to keep the control file
115 following processing. Defaults to no (0).
116 * keep_logfile. Optional. Whether to keep the log file
117 following processing. Defaults to yes (1).
118 * keep_badfile. Optional. Whether to keep the bad file
119 following processing. Defaults to yes (1).
120 * keep_discardfile. Optional. Whether to keep the discard file
121 following processing. Defaults to yes (1).
122 * localize. Optional. Whether to localize (fix for current OS), the
123 data file's end-of-line delimiters. Defaults to no (0).
124 * parameters. Optional. A hashref of SQL*Loader command-line
125 parameters (excluding control, log, bad and discard)
126  
127 =head2 run
128  
129 Invoke the SQL*Loader run. Will return a code, that is an integer
130 between 0 and 3:
131  
132 * 0 - successful
133 * 1 - failed
134 * 2 - warn
135 * 3 - fatal
136
137 Examples
138  
139 * All rows loader successfully - 0
140 * All/some rows discarded - 2
141 * All/some row rejected - 2
142 * Discontinued load - 2
143 * Command line/syntax errors - 1
144 * Errors Fatal to Sql*Loader - 1
145 * OS related errors - 3
146  
147 =cut
148
149 method run {
150 # First, generate the controlfile
151 open my $cf, '>', $self->controlfile;
152 print $cf $self->controlfile_content;
153 close $cf;
154
155 my $command = "sqlldr userid=" . $self->userid;
156 $command .= join(" ", @{$self->command});
157
158 $self->{exe} = $command;
159
160 my $sqlldr = ETLp::Utility::Command->new();
161 my ($stdout, $retcode) = $sqlldr->run($command);
162
163 $retcode = 0 unless defined $retcode;
164
165 $self->{_output} = $stdout;
166 $self->{_error} = $sqlldr->errstr;
167
168 $self->_cleanup;
169
170 $self->logger->debug("Retcode: " . $retcode);
171 #$retcode = $retcode >> 8;
172 return $retcode;
173 }
174
175 =head2 command
176  
177 Return the constructed SQL*Loader command - the sqlldr invocation and the
178 user authentication information if not included
179  
180 =cut
181  
182 method command {
183 return $self->{_command};
184 }
185  
186 =head2 controlfile_content
187  
188 Return the content of the controlfile
189  
190 =cut
191  
192 method controlfile_content {
193 return $self->{_controlfile_content};
194 }
195  
196 =head2 error
197  
198 Return any error generated following the SQL*Loader run
199  
200 =cut
201  
202 method error {
203 return $self->{_error};
204 }
205  
206 =head2 output
207  
208 Return the output from a SQL*Loader run
209  
210 =cut
211  
212 method output {
213 return $self->{_output};
214 }
215
216 # Remove files required for or generated by the SQL*Loader process unless
217 # they should be kept
218 method _cleanup {
219 foreach my $filetype (qw/controlfile logfile badfile discardfile/) {
220 my $keep = 'keep_' . $filetype;
221 unless ($self->$keep) {
222 $self->logger->debug("removing ". $self->$filetype);
223 unlink $self->$filetype
224 || ETLpException->throw(error => "Cannot remove " .
225 $self->filetype . ": $!");
226 }
227 }
228 }
229  
230 # create the SQL*Loader controlfile
231 method _build_controlfile {
232 $self->{_controlfile_content} =
233 "LOAD DATA\n".
234 "INFILE '".$self->filename."'\n".
235 "INTO TABLE ".$self->table."\n".
236 $self->mode."\n".
237 $self->specification;
238 }
239  
240 # Construct the SQL*Loader command
241 method _build_command {
242 my @command = " control=" . $self->controlfile;
243
244 push @command, "log=" . $self->logfile if $self->logfile;
245 push @command, "bad=" . $self->badfile if $self->badfile;
246 push @command, "discard=" . $self->discardfile if $self->discardfile;
247
248 if ($self->parameters) {
249 foreach my $sqlldr_keyword (sort keys %{$self->parameters}) {
250 unless (grep /^$sqlldr_keyword$/i, @keywords) {
251 ETLpException->throw("Invalid keyword: $sqlldr_keyword");
252 }
253 push @command,
254 "$sqlldr_keyword = " . $self->parameters->{$sqlldr_keyword};
255 }
256 }
257
258 $self->{_command} = \@command;
259 }
260
261 method BUILD {
262 if ($self->localize) {
263 my $l = File::LocalizeNewlines->new;
264 $l->localize($self->filename);
265 }
266
267 $self->_build_command;
268 $self->_build_controlfile;
269 }
270  
271 }
272  
273 =head1 LICENSE AND COPYRIGHT
274  
275 Copyright 2010 Redbone Systems Ltd
276  
277 This program is free software; you can redistribute it and/or modify it
278 under the terms of either: the GNU General Public License as published
279 by the Free Software Foundation; or the Artistic License.
280  
281 The terms are in the LICENSE file that accompanies this application
282
283 =cut