ETLp
view release on metacpan or search on metacpan
lib/ETLp/Loader/CSV.pm view on Meta::CPAN
package ETLp::Loader::CSV;
use MooseX::Declare;
=head1 NAME
ETLp::Loader::CSV - Load a CSV file's content into a table
=head1 DESCRIPTION
This class is used to manage the job's audit record
=head1 SYNOPSIS
use ETLp::Loader::CSV;
my $loader = ETLp::Loader::CSV->new(
table => 'table_name',
columns => [qw/col1 col2 col3/]
directory => '/data/incoming',
);
my $status = $loader->load('data.csv');
unless ($status) {
die $status->error;
}
print "Rows loaded: " $loader->rows_loaded;
=cut
class ETLp::Loader::CSV with ETLp::Role::Config {
use ETLp::File::Read::CSV;
use Data::Dumper;
use File::Basename;
use Convert::NLS_DATE_FORMAT qw(posix2oracle);
use DBI::Const::GetInfoType;
use ETLp::Exception;
use Try::Tiny;
has 'directory' => (is => 'ro', isa => 'Str');
has 'table' => (is => 'ro', isa => 'Str', required => 1);
has 'columns' => (is => 'ro', isa => 'ArrayRef', required => 1);
has 'rules' => (is => 'ro', isa => 'HashRef', required => 0);
has 'localize' => (is => 'ro', isa => 'Int', default => 0);
has 'error' => (is => 'rw', isa => 'Str');
has 'file_id' => (is => 'ro', isa => 'Int', required => 1);
has 'skip' => (is => 'ro', isa => 'Int', required => 0, default => 0);
has 'ignore_field_count' => (is => 'ro', isa => 'Bool', default => 0);
has 'csv_options' => (is => 'ro', isa => 'HashRef', required => 0,
default => sub{{allow_whitespace => 1}});
=head1 METHODS
=head2 new
Create a loader, specifying the characteristics
Parameters
* table: Required. The table the data is being loaded into
* columns: Required. The columns in the table that we are inserting into.
These should match the name of the file field names
* directory: Optional. The directory where the load files are located.
* localize: Optional. Whether to localize the input files (i.e. process
the input file setting the appropriate newline character for the
host OS)
Returns
* A ETLp::Loader::CSV object
=head2 load
Load a file into the specified table. If the directory attibute is set
then this should be a relative path.
Parameters
* The name of the file to be loaded
Returns
* Status. 1 - Success, 0 = failure
=cut
method load (Str $filename) {
my @columns = @{$self->columns};
$self->{_rows_loaded} = 0;
$self->error('');
my $error_flag = 1;
try {
my $directory = $self->directory;
unless ($directory) {
$directory = dirname($filename);
$filename = basename($filename);
};
my $csv = ETLp::File::Read::CSV->new(
directory => $directory,
filename => $filename,
localize => $self->localize,
fields => $self->columns,
skip => $self->skip,
ignore_field_count => $self->ignore_field_count,
( run in 1.736 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )