File Coverage

blib/lib/DBIx/TableLoader/CSV.pm
Criterion Covered Total %
statement 53 53 100.0
branch 20 20 100.0
condition 12 15 80.0
subroutine 14 14 100.0
pod 4 4 100.0
total 103 106 97.1


line stmt bran cond sub pod time code
1             # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2             #
3             # This file is part of DBIx-TableLoader-CSV
4             #
5             # This software is copyright (c) 2011 by Randy Stauner.
6             #
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             #
10 4     4   176591 use strict;
  4         12  
  4         458  
11 4     4   25 use warnings;
  4         9  
  4         267  
12              
13             package DBIx::TableLoader::CSV;
14             {
15             $DBIx::TableLoader::CSV::VERSION = '1.102';
16             }
17             # git description: v1.101-5-g35f5bf3
18              
19             BEGIN {
20 4     4   110 $DBIx::TableLoader::CSV::AUTHORITY = 'cpan:RWSTAUNER';
21             }
22             # ABSTRACT: Easily load a CSV into a database table
23              
24 4     4   4875 use DBIx::TableLoader 1.100; # rollback after error
  4         20317  
  4         216  
25             our @ISA = 'DBIx::TableLoader';
26              
27 4     4   43 use Carp qw(croak carp);
  4         11  
  4         249  
28 4     4   6700 use Module::Load ();
  4         13446  
  4         343  
29 4     4   7874 use Text::CSV 1.21 ();
  4         77166  
  4         2213  
30              
31              
32             # 'new' inherited
33              
34             sub defaults {
35 72     72 1 606687 my ($self) = @_;
36             return {
37 72         1436 csv => undef,
38             csv_class => 'Text::CSV',
39             csv_defaults => {
40             # Text::CSV encourages setting { binary => 1 }
41             binary => 1,
42             },
43             csv_opts => {},
44             file => undef,
45             file_encoding => '',
46             file_open_layers => '',
47             ignore_csv_errors => 0,
48             io => undef,
49             no_header => 0,
50             };
51             }
52              
53              
54             sub get_raw_row {
55 124     124 1 98943 my ($self) = @_;
56 4     4   4913 my $row = $self->{csv}->getline($self->{io});
  4         32650  
  4         165  
  124         2791  
57 123 100       127962 unless( $self->{ignore_csv_errors} ){
58 120 100 66     353 if( !$row && !$self->{csv}->eof ){
59 1         10 croak 'CSV parse error: ' . $self->{csv}->error_diag;
60             }
61             }
62 122         581 return $row;
63             }
64              
65              
66             sub default_name {
67 21     21 1 52841 my ($self) = @_;
68             # guess name if not provided
69             return $self->{name} ||=
70             $self->{file}
71 21 100 33     136 ? do {
72 18         146 require File::Basename; # core
73 18         905 File::Basename::fileparse($self->{file}, qr/\.[^.]*/);
74             }
75             : 'csv';
76             }
77              
78              
79             sub prepare_data {
80 72     72 1 6317 my ($self) = @_;
81              
82 72         301 Module::Load::load($self->{csv_class});
83              
84             # if an object is not passed in via 'csv', create one from 'csv_opts'
85 68         185 $self->{csv} ||= $self->{csv_class}->new({
86 68         452 %{ $self->{csv_defaults} },
87 72 100 100     4587 %{ $self->{csv_opts} }
88             })
89             or croak "Cannot use CSV: " . $self->{csv_class}->error_diag();
90              
91             # if 'io' not provided set it to the handle returned from opening 'file'
92 71 100 100     6018 $self->{io} ||= $self->_open_file
93             or croak("Cannot proceed without a 'file' or 'io' attribute");
94              
95             # discard first row if columns given (see POD for 'no_header' option)
96 65 100 100     305 $self->{first_row} = $self->get_raw_row()
97             if $self->{columns} && !$self->{no_header};
98             }
99              
100             sub _open_file {
101 26     26   50 my ($self) = @_;
102              
103             return
104 26 100       824 unless my $file = $self->{file};
105              
106 23         38 my $mode = '<';
107              
108 23 100       70 if( my $layers = $self->{file_open_layers} ){
109 6         10 $mode .= $layers;
110             }
111              
112             # convenience shortcut (layers would be sufficient but this is easier)
113 23 100       68 if( my $enc = $self->{file_encoding} ){
114 3         8 $mode .= ':encoding(' . $enc . ')';
115             }
116              
117 23 100   1   2263 open(my $fh, $mode, $file)
  1         11  
  1         2  
  1         9  
118             or croak("Failed to open '$file': $!");
119              
120 20         20442 return $fh;
121             }
122              
123             1;
124              
125             __END__