blib/lib/DBIx/TableLoader/CSV.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 20 / 20 Conditioncond 86.6% 13 / 15 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 96.4% 108 / 112 CC 16 SCAR 13.8
CC
16
Cov
98%
prepare_data
7.0
_open_file
5.0
get_raw_row
4.0
CRAP 4.0
↑ Summary
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 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package DBIx::TableLoader::CSV;
14 {
15 5 $DBIx::TableLoader::CSV::VERSION = '1.102';
16 }
17 # git description: v1.101-5-g35f5bf3
18  
19 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 $DBIx::TableLoader::CSV::AUTHORITY = 'cpan:RWSTAUNER';
21 }
22 # ABSTRACT: Easily load a CSV into a database table
23  
24 5 ▶use DBIx::TableLoader 1.100; # rollback after error
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 our @ISA = 'DBIx::TableLoader';
26  
27 5 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use Text::CSV 1.21 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 # 'new' inherited
33  
34 69 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
35 69 my ($self) = @_;
36 return {
37 69 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 109 ▶sub get_raw_row {
Subroutine get_raw_row: called CC=4 SCAR=13.9
Pod documented
55 109 my ($self) = @_;
56 109 my $row = $self->{csv}->getline($self->{io});
57 108 ▶ unless( $self->{ignore_csv_errors} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ignore_csv_errors"}) 105 3 108
58 105 ▶ if( !$row && !$self->{csv}->eof ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($row or $self->{"csv"}->eof) 1 104 105
Condition100%3/3
$row or $self->{"csv"}->eof
l!l&&r!l&&!r
9681
MC/DC100%2/2
$row or $self->{"csv"}->eof
$row $self->{"csv"}->eof
Truth table100%3/3
A or B
A= $rowB= $self->{"csv"}->eof
ABresult
1X1
011
000
59 1 croak 'CSV parse error: ' . $self->{csv}->error_diag;
60 }
61 }
62 107 return $row;
63 }
64  
65  
66 18 ▶sub default_name {
Subroutine default_name: called CC=3 SCAR=11.3
Pod documented
67 18 my ($self) = @_;
68 # guess name if not provided
69 return $self->{name} ||=
70 $self->{file}
71 18 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'file'} ? : 15 3 18
Condition33%1/3
$self->{"name"} ||= $self->{"file"} ? do { require File::Basename; File::Basename::fileparse($self->{"file"}, qr/\.[^.]*/) } : "csv"
l!l&&r!l&&!r
0180
MC/DC0%0/2
$self->{"name"} ||= $self->{"file"} ? do { require File::Basename; File::Basename::fileparse($self->{"file"}, qr/\.[^.]*/) } : "csv"
$self->{"name"} $self->{"file"} ? do { require File::Basename; File::Basename::fileparse($self->{"file"}, qr/\.[^.]*/) } : "csv"
Truth table33%1/3
A ||= B
A= $self->{"name"}B= $self->{"file"} ? do { require File::Basename; File::Basename::fileparse($self->{"file"}, qr/\.[^.]*/) } : "csv"
ABresult
1X1
011
000
72 15 require File::Basename; # core
73 15 File::Basename::fileparse($self->{file}, qr/\.[^.]*/);
74 }
75 : 'csv';
76 }
77  
78  
79 69 ▶sub prepare_data {
Subroutine prepare_data: called CC=7 SCAR=19.5
Pod documented
80 69 my ($self) = @_;
81  
82 69 Module::Load::load($self->{csv_class});
83  
84 # if an object is not passed in via 'csv', create one from 'csv_opts'
85 $self->{csv} ||= $self->{csv_class}->new({
86 65 %{ $self->{csv_defaults} },
87 65 %{ $self->{csv_opts} }
88 })
89 69 ▶ or croak "Cannot use CSV: " . $self->{csv_class}->error_diag();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"csv"} ||= $self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}}) or croak('Cannot use CSV: ' . $self->{"csv_class"}->error_diag) 68 1 69
Condition100%3/3
$self->{"csv"} ||= $self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}})
l!l&&r!l&&!r
4641
MC/DC100%2/2
$self->{"csv"} ||= $self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}})
$self->{"csv"} $self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}})
Truth table100%3/3
A ||= B
A= $self->{"csv"}B= $self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}})
ABresult
1X1
011
000
90  
91 # if 'io' not provided set it to the handle returned from opening 'file'
92 68 ▶ $self->{io} ||= $self->_open_file
Branch: 1 branch
BranchTrueFalseTotal
$self->{"io"} ||= $self->_open_file or croak("Cannot proceed without a 'file' or 'io' attribute") 62 3 65
Condition100%3/3
$self->{"io"} ||= $self->_open_file
l!l&&r!l&&!r
45203
MC/DC100%2/2
$self->{"io"} ||= $self->_open_file
$self->{"io"} $self->_open_file
Truth table100%3/3
A ||= B
A= $self->{"io"}B= $self->_open_file
ABresult
1X1
011
000
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 $self->{first_row} = $self->get_raw_row()
97 62 ▶ if $self->{columns} && !$self->{no_header};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"columns"} and not $self->{"no_header"} 3 59 62
Condition100%3/3
$self->{"columns"} and not $self->{"no_header"}
!ll&&!rl&&r
5633
MC/DC100%2/2
$self->{"columns"} and not $self->{"no_header"}
$self->{"columns"} not $self->{"no_header"}
Truth table100%3/3
A and not(B)
A= $self->{"columns"}B= not $self->{"no_header"}
ABresult
0X0
100
111
98 }
99  
100 23 ▶sub _open_file {
Subroutine _open_file: called CC=5 SCAR=16.1
101 23 my ($self) = @_;
102  
103 return
104 23 ▶ unless my $file = $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless my $file = $self->{"file"} 3 20 23
105  
106 20 my $mode = '<';
107  
108 20 ▶ if( my $layers = $self->{file_open_layers} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $layers = $self->{"file_open_layers"}) 6 14 20
109 6 $mode .= $layers;
110 }
111  
112 # convenience shortcut (layers would be sufficient but this is easier)
113 20 ▶ if( my $enc = $self->{file_encoding} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $enc = $self->{"file_encoding"}) 3 17 20
114 3 $mode .= ':encoding(' . $enc . ')';
115 }
116  
117 20 ▶ open(my $fh, $mode, $file)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $file or croak("Failed to open '${file}': $!") 17 3 20
118 or croak("Failed to open '$file': $!");
119  
120 17 return $fh;
121 }
122  
123 5 1;
124  
125 __END__
↑ Summary