| 1 | use MooseX::Declare; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | ETLp::File::Read::CSV - audit the execution of an ETLp job | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | This class is used to manage the job's audit record | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use ETLp::File::Read::CSV; | ||
| 14 | |||
| 15 | my $csv = ETLp::File::Read::CSV->new( | ||
| 16 | filename => "/data/comit/final.csv", | ||
| 17 | fields => [qw/id name score/], | ||
| 18 | csv_options => {allow_whitespace => 1}, | ||
| 19 | localize => 1, | ||
| 20 | ); | ||
| 21 | |||
| 22 | while (my $fields = $csv->get_fields) { | ||
| 23 | print $csv->line_counter() .": " . $csv->{name}, "\n"; | ||
| 24 | }; | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =head2 new | ||
| 31 | |||
| 32 | Create a File object. | ||
| 33 | |||
| 34 | Parameters | ||
| 35 | |||
| 36 | Hash or hashref consisting of | ||
| 37 | |||
| 38 | * filename: Required. Name of the file to be opened | ||
| 39 | * fields: Required. The fields in the file | ||
| 40 | * csv_options. Optional. Settings for parsing the CSV file. See | ||
| 41 | Text::CSV documentation | ||
| 42 | * localize. Optional. Whether the end of line characters should be | ||
| 43 | converted to the native ones | ||
| 44 | |||
| 45 | |||
| 46 | Returns | ||
| 47 | |||
| 48 | * a ETLp::File::Read::CSV object | ||
| 49 | |||
| 50 | =head2 get_fields | ||
| 51 | |||
| 52 | Extracts the fields from a file | ||
| 53 | |||
| 54 | Parameters | ||
| 55 | |||
| 56 | * None | ||
| 57 | |||
| 58 | Returns | ||
| 59 | |||
| 60 | * A hashref where each key is populated with the filed value from the | ||
| 61 | file record | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | class ETLp::File::Read::CSV extends ETLp::File::Read { | ||
| 66 | use Text::CSV; | ||
| 67 | use Data::Dumper; | ||
| 68 | use ETLp::Exception; | ||
| 69 | |||
| 70 | has 'csv_options' => (is => 'ro', isa => 'HashRef', default => sub { {} }); | ||
| 71 | has 'fields' => (is => 'ro', isa => 'ArrayRef'); | ||
| 72 | has 'ignore_field_count' => (is => 'ro', isa => 'Bool', default => 0); | ||
| 73 | |||
| 74 | method get_fields { | ||
| 75 | my $line = $self->get_line; | ||
| 76 | return unless $line; | ||
| 77 | my $csv = $self->{_csv}; | ||
| 78 | my $status = $csv->parse($line); | ||
| 79 | |||
| 80 | if ($status) { | ||
| 81 | my @columns = $csv->fields; | ||
| 82 | my @fields = @{$self->fields}; | ||
| 83 | if ((scalar(@columns) != scalar(@fields)) && !($self->ignore_field_count)) { | ||
| 84 | my $error = | ||
| 85 | "The number of data file fields does not match the number of control file fields\n" | ||
| 86 | . "line number: %s\nfields: %s\nline: %s"; | ||
| 87 | ETLpException->throw(error => sprintf($error, | ||
| 88 | $self->line_counter, join(', ', @fields), $line)); | ||
| 89 | } | ||
| 90 | |||
| 91 | # Create a hash where the keys are the field names and | ||
| 92 | # the values are the parsed columns | ||
| 93 | my %rec; | ||
| 94 | @rec{@fields} = @columns; | ||
| 95 | return \%rec; | ||
| 96 | } else { | ||
| 97 | my $error = "Error: %s\nline number: %s\nfields: %s\nline: %s"; | ||
| 98 | ETLpException->throw(error => sprintf($error, | ||
| 99 | $csv->error_diag(), $self->line_counter, $self->fields, $line)); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | method BUILD { | ||
| 104 | $self->logger->debug("CSV options: " . Dumper($self->csv_options)); | ||
| 105 | $self->{_csv} = Text::CSV->new($self->csv_options) ||\ | ||
| 106 | die "Cannot use CSV: ".Text::CSV->error_diag (); | ||
| 107 | } | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | =head1 LICENSE AND COPYRIGHT | ||
| 112 | |||
| 113 | Copyright 2010 Redbone Systems Ltd | ||
| 114 | |||
| 115 | This program is free software; you can redistribute it and/or modify it | ||
| 116 | under the terms of either: the GNU General Public License as published | ||
| 117 | by the Free Software Foundation; or the Artistic License. | ||
| 118 | |||
| 119 | The terms are in the LICENSE file that accompanies this application | ||
| 120 | |||
| 121 | =cut |