| 1 | package ETLp::Plugin::Iterative::OracleSQLLoader; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ETLp::Plugin::Iterative::OracleSQLLoader - Plugin for loading data | ||
| 8 | using ORACLE SQL*Loader | ||
| 9 | |||
| 10 | =cut | ||
| 11 | |||
| 12 | class ETLp::Plugin::Iterative::OracleSQLLoader extends ETLp::Plugin { | ||
| 13 | use ETLp::Loader::OracleSQLLoader; | ||
| 14 | use Try::Tiny; | ||
| 15 | use File::Basename; | ||
| 16 | use DBI::Const::GetInfoType; | ||
| 17 | use File::Copy; | ||
| 18 | |||
| 19 | =head1 METHODS | ||
| 20 | |||
| 21 | =head2 type | ||
| 22 | |||
| 23 | Registers the plugin type. | ||
| 24 | |||
| 25 | =head2 run | ||
| 26 | |||
| 27 | Executes the pipeline | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | sub type { | ||
| 32 | return 'sql_loader'; | ||
| 33 | } | ||
| 34 | |||
| 35 | method run (Str $filename) { | ||
| 36 | my $base_filename = basename($filename); | ||
| 37 | my $userid; | ||
| 38 | my $item = $self->item; | ||
| 39 | my $app_config = $self->config->{config}; | ||
| 40 | |||
| 41 | my $on_error = $item->{on_error} || $app_config->{on_error} || 'die'; | ||
| 42 | |||
| 43 | my %sqlldr_retcodes = ( | ||
| 44 | 0 => 'EX_SUCC', | ||
| 45 | 1 => 'EX_FAIL', | ||
| 46 | 2 => 'EX_WARN', | ||
| 47 | 3 => 'EX_FTL' | ||
| 48 | ); | ||
| 49 | |||
| 50 | my $aud_file_process = $self->audit->item->file_process; | ||
| 51 | my $file_id = $aud_file_process->get_canonical_id; | ||
| 52 | $self->logger->debug("file proc id: " . $aud_file_process->id); | ||
| 53 | $self->logger->debug("file audit rec: $file_id"); | ||
| 54 | |||
| 55 | # The SQL*Loader userid will either be one that the | ||
| 56 | # user has explicitly stated, or it will use the | ||
| 57 | # default auth details specified in the environment | ||
| 58 | # config file | ||
| 59 | if ($item->{user_id}) { | ||
| 60 | $userid = $item->{user_id}; | ||
| 61 | } else { | ||
| 62 | my $db = $self->dbh->get_info($GetInfoType{SQL_SERVER_NAME}); | ||
| 63 | $userid = $self->env_conf->{user} . '/' . | ||
| 64 | $self->env_conf->{password}; | ||
| 65 | $userid .= '@' . $db if $db; | ||
| 66 | } | ||
| 67 | |||
| 68 | my ($retcode, $sqlldr); | ||
| 69 | |||
| 70 | $item->{specification} =~ s/%file_id%/$file_id/; | ||
| 71 | |||
| 72 | try { | ||
| 73 | $sqlldr = ETLp::Loader::OracleSQLLoader->new( | ||
| 74 | userid => $userid, | ||
| 75 | filename => $filename, | ||
| 76 | controlfile => $item->{controlfile}, | ||
| 77 | table => $item->{table}, | ||
| 78 | mode => $item->{mode}, | ||
| 79 | specification => $item->{specification}, | ||
| 80 | controlfile => $item->{controlfile}, | ||
| 81 | logfile => $item->{logfile}, | ||
| 82 | badfile => $item->{badfile}, | ||
| 83 | discardfile => $item->{discardfile}, | ||
| 84 | keep_controlfile => $item->{keep_controlfile}, | ||
| 85 | keep_badfile => $item->{keep_badfile}, | ||
| 86 | keep_logfile => $item->{keep_logfile}, | ||
| 87 | keep_discardfile => $item->{keep_discardfile}, | ||
| 88 | parameters => $item->{parameters}, | ||
| 89 | localize => $item->{localize}, | ||
| 90 | ); | ||
| 91 | |||
| 92 | $retcode = $sqlldr->run; | ||
| 93 | } catch { | ||
| 94 | $aud_file_process->update_status('failed'); | ||
| 95 | |||
| 96 | if ($on_error eq 'ignore') { | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | ETLpException->throw(error => $_); | ||
| 101 | }; | ||
| 102 | |||
| 103 | $self->logger->debug("Controlfile: " . $sqlldr->controlfile_content); | ||
| 104 | $self->logger->debug("Command: " . join(' ', @{$sqlldr->command})); | ||
| 105 | |||
| 106 | $aud_file_process->update_message(join(' ', @{$sqlldr->command})); | ||
| 107 | |||
| 108 | # Compre the SQL*loader return code with our | ||
| 109 | if ($sqlldr_retcodes{$retcode} eq 'EX_SUCC') { | ||
| 110 | $aud_file_process->update_status('succeeded'); | ||
| 111 | } elsif ($sqlldr_retcodes{$retcode} =~ /^(?:EX_FAIL|EX_FTL)$/) { | ||
| 112 | $aud_file_process->update_status('failed'); | ||
| 113 | $aud_file_process->update_message("Command: " | ||
| 114 | . $sqlldr->command | ||
| 115 | . "\nError: " | ||
| 116 | . $sqlldr->error); | ||
| 117 | ETLpException->throw(error => $sqlldr->error); | ||
| 118 | } elsif ($sqlldr_retcodes{$retcode} eq 'EX_WARN') { | ||
| 119 | my $log_message = 'SQL*Loader returned a warning'; | ||
| 120 | $log_message .= "\n\tFile: $filename"; | ||
| 121 | $log_message .= "\n\tError: " . $sqlldr->error | ||
| 122 | if $sqlldr->error; | ||
| 123 | $log_message .= | ||
| 124 | "\n\n" . join(" ", @{$sqlldr->command}); | ||
| 125 | |||
| 126 | if ($sqlldr->error) { | ||
| 127 | $aud_file_process->update_message("Command: " | ||
| 128 | . join(" ", @{$sqlldr->command}) | ||
| 129 | . "\nError: " | ||
| 130 | . $sqlldr->error); | ||
| 131 | } | ||
| 132 | |||
| 133 | if ($item->{is_warning_error}) { | ||
| 134 | $aud_file_process->update_status('failed'); | ||
| 135 | ETLpException->throw(error => $log_message); | ||
| 136 | } else { | ||
| 137 | $aud_file_process->update_status('warning'); | ||
| 138 | $self->logger->warn($log_message); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | move($filename, $app_config->{archive_dir}) || | ||
| 143 | ETLpException->throw(error => "Unable to move $filename to " | ||
| 144 | . $app_config->{archive_dir} . ": $|"); | ||
| 145 | |||
| 146 | return $app_config->{archive_dir} . '/' . basename($filename); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | =head1 LICENSE AND COPYRIGHT | ||
| 151 | |||
| 152 | Copyright 2010 Redbone Systems Ltd | ||
| 153 | |||
| 154 | This program is free software; you can redistribute it and/or modify it | ||
| 155 | under the terms of either: the GNU General Public License as published | ||
| 156 | by the Free Software Foundation; or the Artistic License. | ||
| 157 | |||
| 158 | The terms are in the LICENSE file that accompanies this application |