| 1 | package ETLp::Loader::CSV; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ETLp::Loader::CSV - Load a CSV file's content into a table | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This class is used to manage the job's audit record | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use ETLp::Loader::CSV; | ||
| 16 | |||
| 17 | my $loader = ETLp::Loader::CSV->new( | ||
| 18 | table => 'table_name', | ||
| 19 | columns => [qw/col1 col2 col3/] | ||
| 20 | directory => '/data/incoming', | ||
| 21 | ); | ||
| 22 | |||
| 23 | my $status = $loader->load('data.csv'); | ||
| 24 | |||
| 25 | unless ($status) { | ||
| 26 | die $status->error; | ||
| 27 | } | ||
| 28 | |||
| 29 | print "Rows loaded: " $loader->rows_loaded; | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | class ETLp::Loader::CSV with ETLp::Role::Config { | ||
| 34 | |||
| 35 | use ETLp::File::Read::CSV; | ||
| 36 | use Data::Dumper; | ||
| 37 | use File::Basename; | ||
| 38 | use Convert::NLS_DATE_FORMAT qw(posix2oracle); | ||
| 39 | use DBI::Const::GetInfoType; | ||
| 40 | use ETLp::Exception; | ||
| 41 | use Try::Tiny; | ||
| 42 | |||
| 43 | has 'directory' => (is => 'ro', isa => 'Str'); | ||
| 44 | has 'table' => (is => 'ro', isa => 'Str', required => 1); | ||
| 45 | has 'columns' => (is => 'ro', isa => 'ArrayRef', required => 1); | ||
| 46 | has 'rules' => (is => 'ro', isa => 'HashRef', required => 0); | ||
| 47 | has 'localize' => (is => 'ro', isa => 'Int', default => 0); | ||
| 48 | has 'error' => (is => 'rw', isa => 'Str'); | ||
| 49 | has 'file_id' => (is => 'ro', isa => 'Int', required => 1); | ||
| 50 | has 'skip' => (is => 'ro', isa => 'Int', required => 0, default => 0); | ||
| 51 | has 'ignore_field_count' => (is => 'ro', isa => 'Bool', default => 0); | ||
| 52 | has 'csv_options' => (is => 'ro', isa => 'HashRef', required => 0, | ||
| 53 | default => sub{{allow_whitespace => 1}}); | ||
| 54 | |||
| 55 | =head1 METHODS | ||
| 56 | |||
| 57 | =head2 new | ||
| 58 | |||
| 59 | Create a loader, specifying the characteristics | ||
| 60 | |||
| 61 | Parameters | ||
| 62 | |||
| 63 | * table: Required. The table the data is being loaded into | ||
| 64 | * columns: Required. The columns in the table that we are inserting into. | ||
| 65 | These should match the name of the file field names | ||
| 66 | * directory: Optional. The directory where the load files are located. | ||
| 67 | * localize: Optional. Whether to localize the input files (i.e. process | ||
| 68 | the input file setting the appropriate newline character for the | ||
| 69 | host OS) | ||
| 70 | |||
| 71 | Returns | ||
| 72 | |||
| 73 | * A ETLp::Loader::CSV object | ||
| 74 | |||
| 75 | =head2 load | ||
| 76 | |||
| 77 | Load a file into the specified table. If the directory attibute is set | ||
| 78 | then this should be a relative path. | ||
| 79 | |||
| 80 | Parameters | ||
| 81 | |||
| 82 | * The name of the file to be loaded | ||
| 83 | |||
| 84 | Returns | ||
| 85 | |||
| 86 | * Status. 1 - Success, 0 = failure | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | method load (Str $filename) { | ||
| 91 | my @columns = @{$self->columns}; | ||
| 92 | |||
| 93 | $self->{_rows_loaded} = 0; | ||
| 94 | $self->error(''); | ||
| 95 | |||
| 96 | my $error_flag = 1; | ||
| 97 | |||
| 98 | try { | ||
| 99 | my $directory = $self->directory; | ||
| 100 | |||
| 101 | unless ($directory) { | ||
| 102 | $directory = dirname($filename); | ||
| 103 | $filename = basename($filename); | ||
| 104 | }; | ||
| 105 | |||
| 106 | my $csv = ETLp::File::Read::CSV->new( | ||
| 107 | directory => $directory, | ||
| 108 | filename => $filename, | ||
| 109 | localize => $self->localize, | ||
| 110 | fields => $self->columns, | ||
| 111 | skip => $self->skip, | ||
| 112 | ignore_field_count => $self->ignore_field_count, | ||
| 113 | csv_options => $self->csv_options, | ||
| 114 | ); | ||
| 115 | |||
| 116 | $self->logger->debug("SQL: $self->{_sql}"); | ||
| 117 | $self->logger->debug("Columns: " . Dumper(\@columns)); | ||
| 118 | |||
| 119 | my $sth = $self->dbh->prepare($self->{_sql}); | ||
| 120 | |||
| 121 | my $row_counter = 0; | ||
| 122 | |||
| 123 | while (my $fields = $csv->get_fields) { | ||
| 124 | my @vals = map($fields->{$_}, @columns); | ||
| 125 | $sth->execute(@vals); | ||
| 126 | $self->{_rows_loaded}++; | ||
| 127 | } | ||
| 128 | |||
| 129 | $sth->finish; | ||
| 130 | |||
| 131 | $self->dbh->commit; | ||
| 132 | } catch { | ||
| 133 | my $error = $_; | ||
| 134 | $self->dbh->rollback; | ||
| 135 | |||
| 136 | $self->{_rows_loaded} = 0; | ||
| 137 | $self->error($error); | ||
| 138 | $error_flag = 0; | ||
| 139 | }; | ||
| 140 | |||
| 141 | return $error_flag; | ||
| 142 | } | ||
| 143 | |||
| 144 | =head2 rows_loaded | ||
| 145 | |||
| 146 | Returns the number of rows inserted by the last load | ||
| 147 | |||
| 148 | Parameters | ||
| 149 | |||
| 150 | * None | ||
| 151 | |||
| 152 | Returns | ||
| 153 | |||
| 154 | * An integer | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | method rows_loaded { | ||
| 159 | return $self->{_rows_loaded}; | ||
| 160 | } | ||
| 161 | |||
| 162 | # Construct the SQL statement that will perform the insert | ||
| 163 | method _construct_sql { | ||
| 164 | |||
| 165 | my @columns = @{$self->columns}; | ||
| 166 | my $column_rules = $self->rules; | ||
| 167 | my $placeholders; | ||
| 168 | |||
| 169 | # deal with the case where the column is a date and we need to cast the | ||
| 170 | # date string | ||
| 171 | |||
| 172 | if ($column_rules) { | ||
| 173 | |||
| 174 | my @placeholders; | ||
| 175 | $self->logger->debug("Columns: " . Dumper(\@columns)); | ||
| 176 | foreach my $column (@columns) { | ||
| 177 | $self->logger->debug("Column: $column"); | ||
| 178 | my $date_flag = 0; | ||
| 179 | my $rules = $column_rules->{$column}->{rule}; | ||
| 180 | $rules = [$rules] unless ref $rules eq 'ARRAY'; | ||
| 181 | foreach my $rule (@$rules) { | ||
| 182 | if ($rule =~ /^date\('?(.*)'?\)$/i) { | ||
| 183 | my $posix_pattern = $1; | ||
| 184 | $self->logger->debug("Date pattern: $posix_pattern"); | ||
| 185 | my $db_date_pattern = | ||
| 186 | $self->_get_db_date_formatter($posix_pattern); | ||
| 187 | $self->logger->debug("DB Conversion: " . | ||
| 188 | $db_date_pattern); | ||
| 189 | push @placeholders, $db_date_pattern; | ||
| 190 | $date_flag = 1; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | push @placeholders, '?' unless $date_flag; | ||
| 194 | } | ||
| 195 | |||
| 196 | $self->logger->debug("Placeholders: " . Dumper(\@placeholders)); | ||
| 197 | $placeholders = join(',', @placeholders); | ||
| 198 | } else { | ||
| 199 | $placeholders = join(', ', split(//, '?' x @columns)); | ||
| 200 | } | ||
| 201 | |||
| 202 | return | ||
| 203 | 'insert into ' | ||
| 204 | . $self->table . '(' | ||
| 205 | . join(', ', @columns, 'file_id') | ||
| 206 | . ') values (' | ||
| 207 | . $placeholders . ', '. $self->file_id.')'; | ||
| 208 | } | ||
| 209 | |||
| 210 | # Each vendor has a dfferent wasy of handling dates. This method provides | ||
| 211 | # the specific SQL to deal with date formatting | ||
| 212 | method _get_db_date_formatter(Str $posix_pattern) { | ||
| 213 | $self->logger->debug('POSIX Pattern: ' . $posix_pattern); | ||
| 214 | |||
| 215 | my $driver = lc $self->dbh->get_info($GetInfoType{SQL_DBMS_NAME}); | ||
| 216 | |||
| 217 | if (($driver eq 'oracle') || ($driver eq 'postgresql')){ | ||
| 218 | return "TO_DATE(?, '" . posix2oracle($posix_pattern) . "')"; | ||
| 219 | } elsif ($driver eq 'mysql') { | ||
| 220 | return "STR_TO_DATE(?,'$posix_pattern')"; | ||
| 221 | } elsif ($driver eq 'sqlite') { | ||
| 222 | return '?' | ||
| 223 | } | ||
| 224 | ETLpException->throw(error => "Unknown Database Driver: $driver"); | ||
| 225 | } | ||
| 226 | |||
| 227 | method BUILD { | ||
| 228 | $self->{_sql} = $self->_construct_sql; | ||
| 229 | $self->{_rows_loaded} = 0; | ||
| 230 | $self->logger->debug("Rules " . Dumper($self->rules)); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | =head1 LICENSE AND COPYRIGHT | ||
| 235 | |||
| 236 | Copyright 2010 Redbone Systems Ltd | ||
| 237 | |||
| 238 | This program is free software; you can redistribute it and/or modify it | ||
| 239 | under the terms of either: the GNU General Public License as published | ||
| 240 | by the Free Software Foundation; or the Artistic License. | ||
| 241 | |||
| 242 | The terms are in the LICENSE file that accompanies this application | ||
| 243 | |||
| 244 | =cut | ||
| 245 |