DBD/CSV.pm

filter: Statementstmt 95.4% 231 / 242 Branchbran 71.4% 80 / 112 Conditioncond 63.6% 28 / 44 MC/DCmcdc 32.0% 8 / 25 Subroutinesub 96.6% 29 / 30 Podpod n/a totaltotal 83.0% 376 / 453 CC 84 SCAR 25.9
CC
84
Cov
85%
open_data
33.8
init_table_meta
15.8
_register_compat_map
9.0
CRAP 13.3
↑ Summary DBD/CSV/GetInfo.pm »
1 #!/usr/bin/perl
2 #
3 # DBD::CSV - A DBI driver for CSV and similar structured files
4 #
5 # This module is currently maintained by
6 #
7 # H.Merijn Brand <h.m.brand@xs4all.nl>
8 #
9 # See for full acknowledgements the last two pod sections in this file
10  
11 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 24 require DynaLoader;
15 24 require DBD::File;
16 24 require IO::File;
17  
18 24 our @f_SHORT = qw( class file dir dir_search ext lock lockfile schema encoding );
19 24 our @c_SHORT = qw( eof
20 eol sep_char quote_char escape_char binary decode_utf8 auto_diag
21 diag_verbose blank_is_undef empty_is_undef allow_whitespace
22 allow_loose_quotes allow_loose_escapes allow_unquoted_escape
23 always_quote quote_empty quote_space escape_null quote_binary
24 keep_meta_info callbacks );
25  
26 package DBD::CSV;
27  
28 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 24 our @ISA = qw( DBD::File );
31  
32 24 our $VERSION = "0.64";
33 24 our $ATTRIBUTION = "DBD::CSV $DBD::CSV::VERSION by H.Merijn Brand";
34  
35 24 our $err = 0; # holds error code for DBI::err
36 24 our $errstr = ""; # holds error string for DBI::errstr
37 24 our $sqlstate = ""; # holds error state for DBI::state
38 24 our $drh = undef; # holds driver handle once initialized
39  
40 0 ▶sub CLONE { # empty method: prevent warnings when threads are cloned
Subroutine CLONE: not called CC=1 SCAR=0.0
41 } # CLONE
42  
43 # --- DRIVER -------------------------------------------------------------------
44  
45 package DBD::CSV::dr;
46  
47 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 24 ▶use Text::CSV_XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 24 our @CSV_TYPES = (
52 Text::CSV_XS::IV (), # SQL_TINYINT
53 Text::CSV_XS::IV (), # SQL_BIGINT
54 Text::CSV_XS::PV (), # SQL_LONGVARBINARY
55 Text::CSV_XS::PV (), # SQL_VARBINARY
56 Text::CSV_XS::PV (), # SQL_BINARY
57 Text::CSV_XS::PV (), # SQL_LONGVARCHAR
58 Text::CSV_XS::PV (), # SQL_ALL_TYPES
59 Text::CSV_XS::PV (), # SQL_CHAR
60 Text::CSV_XS::NV (), # SQL_NUMERIC
61 Text::CSV_XS::NV (), # SQL_DECIMAL
62 Text::CSV_XS::IV (), # SQL_INTEGER
63 Text::CSV_XS::IV (), # SQL_SMALLINT
64 Text::CSV_XS::NV (), # SQL_FLOAT
65 Text::CSV_XS::NV (), # SQL_REAL
66 Text::CSV_XS::NV (), # SQL_DOUBLE
67 );
68  
69 24 our @ISA = qw( DBD::File::dr );
70  
71 24 our $imp_data_size = 0;
72 24 our $data_sources_attr = undef;
73  
74 262 ▶sub connect {
Subroutine connect: called CC=8 SCAR=21.3
75 262 my ($drh, $dbname, $user, $auth, $attr) = @_;
76 262 ▶ if ($attr && ref $attr eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr and ref $attr eq "HASH") 262 0 262
Condition33%1/3
$attr and ref $attr eq "HASH"
!ll&&!rl&&r
00262
MC/DC0%0/2
$attr and ref $attr eq "HASH"
$attr ref $attr eq "HASH"
Truth table33%1/3
A and B
A= $attrB= ref $attr eq "HASH"
ABresult
0X0
100
111
77 # Top-level aliasses
78 262 foreach my $key (grep { exists $attr->{$_} } @f_SHORT) {
79 3 my $f_key = "f_$key";
80 3 ▶ exists $attr->{$f_key} and next;
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{$f_key} and next 0 3 3
81 3 $attr->{$f_key} = delete $attr->{$key};
82 }
83 262 foreach my $key (grep { exists $attr->{$_} } @c_SHORT) {
84 3 my $c_key = "csv_$key";
85 3 ▶ exists $attr->{$c_key} and next;
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{$c_key} and next 0 3 3
86 3 $attr->{$c_key} = delete $attr->{$key};
87 }
88 }
89  
90 262 my $dbh = $drh->DBD::File::dr::connect ($dbname, $user, $auth, $attr);
91 262 ▶ $dbh and $dbh->{Active} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$dbh and $dbh->{"Active"} = 1 261 1 262
92 262 $dbh;
93 } # connect
94  
95 # --- DATABASE -----------------------------------------------------------------
96  
97 package DBD::CSV::db;
98  
99 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 24 our $imp_data_size = 0;
102 24 our @ISA = qw( DBD::File::db );
103  
104 261 ▶sub set_versions {
Subroutine set_versions: called CC=1 SCAR=0.0
105 261 my $this = shift;
106 261 $this->{csv_version} = $DBD::CSV::VERSION;
107 261 return $this->SUPER::set_versions ();
108 } # set_versions
109  
110 24 my %csv_xs_attr;
111  
112 261 ▶sub init_valid_attributes {
Subroutine init_valid_attributes: called CC=4 SCAR=13.9
113 261 my $dbh = shift;
114  
115 # Straight from Text::CSV_XS.pm
116 261 my @xs_attr = @c_SHORT;
117 261 @csv_xs_attr{@xs_attr} = ();
118 # Dynamically add "new" attributes - available in Text::CSV_XS-1.20
119 261 ▶ if (my @ka = eval { Text::CSV_XS->known_attributes }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ka) = eval { do { "Text::CSV_XS"->known_attributes } }) 261 0 261
120 261 ▶ for (grep { m/^[a-su-z]/ && !exists $csv_xs_attr{$_} } @ka) {
Condition100%3/3
/^[a-su-z]/ && !exists $csv_xs_attr{$_}
!ll&&!rl&&r
5227614216
MC/DC100%2/2
/^[a-su-z]/ && !exists $csv_xs_attr{$_}
/^[a-su-z]/ !exists $csv_xs_attr{$_}
Truth table100%3/3
A && not(B)
A= /^[a-su-z]/B= !exists $csv_xs_attr{$_}
ABresult
0X0
100
111
121 216 push @xs_attr => $_;
122 216 $csv_xs_attr{$_} = undef;
123 }
124 };
125  
126 261 $dbh->{csv_xs_valid_attrs} = [ @xs_attr ];
127  
128 261 $dbh->{csv_valid_attrs} = { map {("csv_$_" => 1 )} @xs_attr, qw(
129  
130 class tables in csv_in out csv_out skip_first_row
131  
132 null sep quote escape bom
133 )};
134  
135 261 $dbh->{csv_readonly_attrs} = { };
136  
137 261 $dbh->{csv_meta} = "csv_tables";
138  
139 261 return $dbh->SUPER::init_valid_attributes ();
140 } # init_valid_attributes
141  
142 1 ▶sub get_csv_versions {
Subroutine get_csv_versions: called CC=5 SCAR=16.4
143 1 my ($dbh, $table) = @_;
144 1 ▶ $table ||= "";
Condition50%1/2
$table ||= ""
l!l
01
MC/DC0%0/1
$table ||= ""
$table
Truth table50%1/2
A
A= $table
Aresult
11
00
145 1 my $class = $dbh->{ImplementorClass};
146 1 $class =~ s/::db$/::Table/;
147 1 my $meta;
148 1 ▶ $table and (undef, $meta) = $class->get_table_meta ($dbh, $table, 1);
Branch: 1 branch
BranchTrueFalseTotal
$table and (undef, $meta) = $class->get_table_meta($dbh, $table, 1) 0 1 1
149 1 ▶ unless ($meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta) 1 0 1
150 1 $meta = {};
151 1 $class->bootstrap_table_meta ($dbh, $meta, $table);
152 }
153 1 my $dvsn = eval { $meta->{csv_class}->VERSION (); };
154 1 my $dtype = $meta->{csv_class};
155 1 ▶ $dvsn and $dtype .= " ($dvsn)";
Branch: 1 branch
BranchTrueFalseTotal
$dvsn and $dtype .= " ($dvsn)" 1 0 1
156 1 return sprintf "%s using %s", $dbh->{csv_version}, $dtype;
157 } # get_csv_versions
158  
159 209 ▶sub get_info {
Subroutine get_info: called CC=2 SCAR=7.0
160 209 my ($dbh, $info_type) = @_;
161 209 require DBD::CSV::GetInfo;
162 209 my $v = $DBD::CSV::GetInfo::info{int ($info_type)};
163 209 ▶ ref $v eq "CODE" and $v = $v->($dbh);
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq "CODE" and $v = &$v($dbh) 0 209 209
164 209 return $v;
165 } # get_info
166  
167 1 ▶sub type_info_all {
Subroutine type_info_all: called CC=1 SCAR=0.0
168 # my $dbh = shift;
169 1 require DBD::CSV::TypeInfo;
170 1 return [@$DBD::CSV::TypeInfo::type_info_all];
171 } # type_info_all
172  
173 # --- STATEMENT ----------------------------------------------------------------
174  
175 package DBD::CSV::st;
176  
177 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
178  
179 24 our $imp_data_size = 0;
180 24 our @ISA = qw( DBD::File::st );
181  
182 package DBD::CSV::Statement;
183  
184 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
186  
187 24 our @ISA = qw( DBD::File::Statement );
188  
189 package DBD::CSV::Table;
190  
191 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
193  
194 24 our @ISA = qw( DBD::File::Table );
195  
196 24 my %compat_map;
197  
198 24 { my %class_mapped;
199  
200 6370 ▶ sub _register_compat_map {
Subroutine _register_compat_map: called CC=9 SCAR=22.0
201 6370 my $class = shift;
202  
203 6370 my $x = 0;
204 6370 ▶ if (!%compat_map) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%compat_map) 23 6347 6370
205 23 $compat_map{$_} = "f_$_" for @f_SHORT;
206 23 $compat_map{$_} = "csv_$_" for @c_SHORT;
207 23 $x++;
208 }
209 6370 ▶ if ($class and !$class_mapped{$class}++ and
Branch: 1 branch
BranchTrueFalseTotal
if ($class and not $class_mapped{$class}++ and my(@ka) = eval { do { $class->known_attributes } }) 23 6347 6370
Condition83%5/6
$class and not $class_mapped{$class}++ and my(@ka) = eval { do { $class->known_attributes } }
!ll&&!rl&&r
6345223
$class and not $class_mapped{$class}++
!ll&&!rl&&r
0634525
MC/DC66%2/3
$class and not $class_mapped{$class}++ and my(@ka) = eval { do { $class->known_attributes } }
$class not $class_mapped{$class}++ my(@ka) = eval { do { $class->known_attributes } }
Truth table75%3/4
A and not(B) and C
A= $classB= not $class_mapped{$class}++C= my(@ka) = eval { do { $class->known_attributes } }
ABCresult
0XX0
10X0
1100
1111
210 25 my @ka = eval { $class->known_attributes }) {
211 # exclude types
212 23 $compat_map{$_} = "csv_$_" for grep m/^[a-su-z]/ => @ka;
213 23 $x++;
214 }
215 6370 ▶ if ($x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 23 6347 6370
216 23 __PACKAGE__->register_compat_map (\%compat_map);
217 }
218 } # _register_compat_map
219 }
220  
221 #sub DESTROY {
222 # my $self = shift or return;
223 #
224 # $self->{meta} and delete $self->{meta}{csv_in};
225 # } # DESTROY
226  
227 24 ▶sub bootstrap_table_meta {
Subroutine bootstrap_table_meta: called CC=8 SCAR=20.9
228 6025 my ($self, $dbh, $meta, $table) = @_;
229 6025 ▶ $meta->{csv_class} ||= $dbh->{csv_class} || "Text::CSV_XS";
Condition80%4/5
$meta->{"csv_class"} ||= $dbh->{"csv_class"} || "Text::CSV_XS"
l!l&&r!l&&!r
1960060
$dbh->{'csv_class'} || 'Text::CSV_XS'
l!l
36003
MC/DC0%0/2
$meta->{"csv_class"} ||= $dbh->{"csv_class"} || "Text::CSV_XS"
$meta->{"csv_class"} $dbh->{'csv_class'}
Truth table66%2/3
A ||= B
A= $meta->{"csv_class"}B= $dbh->{'csv_class'}
ABresult
1X1
011
000
230 6025 ▶ $meta->{csv_eol} ||= $dbh->{csv_eol} || "\r\n";
Condition80%4/5
$meta->{"csv_eol"} ||= $dbh->{"csv_eol"} || "\r\n"
l!l&&r!l&&!r
1960060
$dbh->{'csv_eol'} || "\r\n"
l!l
115995
MC/DC0%0/2
$meta->{"csv_eol"} ||= $dbh->{"csv_eol"} || "\r\n"
$meta->{"csv_eol"} $dbh->{'csv_eol'}
Truth table66%2/3
A ||= B
A= $meta->{"csv_eol"}B= $dbh->{'csv_eol'}
ABresult
1X1
011
000
231  
232 6025 _register_compat_map ($meta->{csv_class});
233  
234 exists $meta->{csv_skip_first_row} or
235 6025 ▶ $meta->{csv_skip_first_row} = $dbh->{csv_skip_first_row};
Branch: 1 branch
BranchTrueFalseTotal
exists $meta->{"csv_skip_first_row"} or $meta->{"csv_skip_first_row"} = $dbh->{"csv_skip_first_row"} 19 6006 6025
236 exists $meta->{csv_bom} or
237 6025 ▶ $meta->{csv_bom} = exists $dbh->{bom} ? $dbh->{bom} : $dbh->{csv_bom};
Branch: 2 branches
BranchTrueFalseTotal
exists $meta->{"csv_bom"} or $meta->{"csv_bom"} = exists $dbh->{"bom"} ? $dbh->{"bom"} : $dbh->{"csv_bom"} 19 6006 6025
exists $dbh->{'bom'} ? : 0 6006 6006
238 6025 $self->SUPER::bootstrap_table_meta ($dbh, $meta, $table);
239 } # bootstrap_table_meta
240  
241 319 ▶sub init_table_meta {
Subroutine init_table_meta: called CC=15 SCAR=27.6
242 319 my ($self, $dbh, $meta, $table) = @_;
243  
244 319 _register_compat_map ($meta->{csv_class});
245  
246 319 $self->SUPER::init_table_meta ($dbh, $table, $meta);
247  
248 319 ▶ my $csv_in = $meta->{csv_in} || $dbh->{csv_csv_in};
Condition66%2/3
$meta->{'csv_in'} || $dbh->{'csv_csv_in'}
l!l&&r!l&&!r
200299
MC/DC50%1/2
$meta->{'csv_in'} || $dbh->{'csv_csv_in'}
$meta->{'csv_in'} $dbh->{'csv_csv_in'}
Truth table66%2/3
A || B
A= $meta->{'csv_in'}B= $dbh->{'csv_csv_in'}
ABresult
1X1
011
000
249 319 ▶ unless ($csv_in) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csv_in) 299 20 319
250 299 my %opts = ( binary => 1, auto_diag => 1 );
251  
252 # Allow specific Text::CSV_XS options
253 299 foreach my $attr (@{$dbh->{csv_xs_valid_attrs}}) {
254 6821 ▶ $attr eq "eol" and next; # Handles below
Branch: 1 branch
BranchTrueFalseTotal
$attr eq "eol" and next 299 6522 6821
255 6522 ▶ exists $dbh->{"csv_$attr"} and $opts{$attr} = $dbh->{"csv_$attr"};
Branch: 1 branch
BranchTrueFalseTotal
exists $$dbh{"csv_$attr"} and $opts{$attr} = $$dbh{"csv_$attr"} 421 6101 6522
256 }
257 $dbh->{csv_null} || $meta->{csv_null} and
258 $opts{Text::CSV_XS->version < 1.18 ? "always_quote" : "quote_empty"} =
259 299 ▶ $opts{blank_is_undef} = 1;
Branch: 2 branches
BranchTrueFalseTotal
$dbh->{"csv_null"} || $meta->{"csv_null"} and $opts{"Text::CSV_XS"->version < "1.18" ? "always_quote" : "quote_empty"} = $opts{"blank_is_undef"} = 1 6 293 299
'Text::CSV_XS'->version < 1.18 ? : 0 6 6
Condition66%2/3
$dbh->{"csv_null"} or $meta->{"csv_null"}
l!l&&r!l&&!r
60293
MC/DC50%1/2
$dbh->{"csv_null"} or $meta->{"csv_null"}
$dbh->{"csv_null"} $meta->{"csv_null"}
Truth table66%2/3
A or B
A= $dbh->{"csv_null"}B= $meta->{"csv_null"}
ABresult
1X1
011
000
260  
261 299 my $class = $meta->{csv_class};
262 299 my $eol = $meta->{csv_eol};
263 299 ▶ $eol =~ m/^\A(?:[\r\n]|\r\n)\Z/ or $opts{eol} = $eol;
Branch: 1 branch
BranchTrueFalseTotal
$eol =~ /^\A(?:[\r\n]|\r\n)\Z/ or $opts{"eol"} = $eol 299 0 299
264 299 for ([ "sep", ',' ],
265 [ "quote", '"' ],
266 [ "escape", '"' ],
267 ) {
268 897 my ($attr, $def) = ($_->[0]."_char", $_->[1]);
269 $opts{$attr} =
270 exists $meta->{$attr} ? $meta->{$attr} :
271 897 ▶ exists $dbh->{"csv_$attr"} ? $dbh->{"csv_$attr"} : $def;
Branch: 2 branches
BranchTrueFalseTotal
exists $meta->{$attr} ? : 0 897 897
exists $$dbh{"csv_$attr"} ? : 11 886 897
272 }
273 299 ▶ $meta->{csv_in} = $class->new (\%opts) or
Branch: 1 branch
BranchTrueFalseTotal
$meta->{"csv_in"} = $class->new(\%opts) or $class->error_diag 297 0 297
274 $class->error_diag;
275 297 $opts{eol} = $eol;
276 297 ▶ $meta->{csv_out} = $class->new (\%opts) or
Branch: 1 branch
BranchTrueFalseTotal
$meta->{"csv_out"} = $class->new(\%opts) or $class->error_diag 297 0 297
277 $class->error_diag;
278 }
279 } # init_table_meta
280  
281 26 ▶sub table_meta_attr_changed {
Subroutine table_meta_attr_changed: called CC=5 SCAR=16.6
282 26 my ($class, $meta, $attr, $value) = @_;
283  
284 26 _register_compat_map ($meta->{csv_class});
285  
286 26 (my $csv_attr = $attr) =~ s/^csv_//;
287 26 ▶ if (exists $csv_xs_attr{$csv_attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $csv_xs_attr{$csv_attr}) 3 23 26
288 3 for ("csv_in", "csv_out") {
289 exists $meta->{$_} && exists $meta->{$_}{$csv_attr} and
290 6 ▶ $meta->{$_}{$csv_attr} = $value;
Branch: 1 branch
BranchTrueFalseTotal
exists $meta->{$_} and exists $meta->{$_}{$csv_attr} and $meta->{$_}{$csv_attr} = $value 6 0 6
Condition33%1/3
exists $meta->{$_} and exists $meta->{$_}{$csv_attr}
!ll&&!rl&&r
006
MC/DC0%0/2
exists $meta->{$_} and exists $meta->{$_}{$csv_attr}
exists $meta->{$_} exists $meta->{$_}{$csv_attr}
Truth table33%1/3
A and B
A= exists $meta->{$_}B= exists $meta->{$_}{$csv_attr}
ABresult
0X0
100
111
291 }
292 }
293  
294 26 $class->SUPER::table_meta_attr_changed ($meta, $attr, $value);
295 } # table_meta_attr_changed
296  
297 439 ▶sub open_data {
Subroutine open_data: called CC=26 SCAR=35.2
298 439 my ($self, $meta, $attrs, $flags) = @_;
299 439 $self->SUPER::open_file ($meta, $attrs, $flags);
300  
301 427 ▶ if ($meta && $meta->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta and $meta->{"fh"}) 427 0 427
Condition33%1/3
$meta and $meta->{"fh"}
!ll&&!rl&&r
00427
MC/DC0%0/2
$meta and $meta->{"fh"}
$meta $meta->{"fh"}
Truth table33%1/3
A and B
A= $metaB= $meta->{"fh"}
ABresult
0X0
100
111
302 427 $attrs->{csv_csv_in} = $meta->{csv_in};
303 427 $attrs->{csv_csv_out} = $meta->{csv_out};
304 427 ▶ if (my $types = $meta->{types}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $types = $meta->{"types"}) 2 425 427
305 # XXX $meta->{types} is nowhere assigned and should better $meta->{csv_types}
306 # The 'types' array contains DBI types, but we need types
307 # suitable for Text::CSV_XS.
308 2 my $t = [];
309 2 for (@{$types}) {
310 6 ▶ $_ = $_
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 5 1 6
Condition66%2/3
$DBD::CSV::dr::CSV_TYPES[$_ + 6] or Text::CSV_XS::PV()
l!l&&r!l&&!r
302
MC/DC50%1/2
$DBD::CSV::dr::CSV_TYPES[$_ + 6] or Text::CSV_XS::PV()
$DBD::CSV::dr::CSV_TYPES[$_ + 6] Text::CSV_XS::PV()
Truth table66%2/3
A or B
A= $DBD::CSV::dr::CSV_TYPES[$_ + 6]B= Text::CSV_XS::PV()
ABresult
1X1
011
000
311 ? $DBD::CSV::dr::CSV_TYPES[$_ + 6] || Text::CSV_XS::PV ()
312 : Text::CSV_XS::PV ();
313 6 push @$t, $_;
314 }
315 2 $meta->{types} = $t;
316 }
317 427 ▶ if (!$flags->{createMode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $flags->{"createMode"}) 397 30 427
318 397 my $array;
319 my $skipRows = defined $meta->{skip_rows}
320 ? $meta->{skip_rows}
321 : defined $meta->{csv_skip_first_row}
322 ? 1
323 397 ▶ : exists $meta->{col_names} ? 0 : 1;
Branch: 3 branches
BranchTrueFalseTotal
defined $meta->{'skip_rows'} ? : 147 250 397
defined $meta->{'csv_skip_first_row'} ? : 0 250 250
exists $meta->{'col_names'} ? : 3 247 250
324 defined $meta->{skip_rows} or
325 397 ▶ $meta->{skip_rows} = $skipRows;
Branch: 1 branch
BranchTrueFalseTotal
defined $meta->{"skip_rows"} or $meta->{"skip_rows"} = $skipRows 147 250 397
326 397 ▶ if ($meta->{csv_bom}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"csv_bom"}) 0 397 397
327 my @hdr = $attrs->{csv_csv_in}->header ($meta->{fh}) or
328 0 ▶ croak "Failed using the header row: ".$attrs->{csv_csv_in}->error_diag;
Branch: 1 branch
BranchTrueFalseTotal
my(@hdr) = $attrs->{"csv_csv_in"}->header($meta->{"fh"}) or croak('Failed using the header row: ' . $attrs->{"csv_csv_in"}->error_diag) 0 0 0
329 0 ▶ $meta->{col_names} ||= \@hdr;
Condition0%0/2
$meta->{"col_names"} ||= \@hdr
l!l
00
MC/DC0%0/1
$meta->{"col_names"} ||= \@hdr
$meta->{"col_names"}
Truth table0%0/2
A
A= $meta->{"col_names"}
Aresult
11
00
330 0 ▶ $skipRows and $skipRows = 0;
Branch: 1 branch
BranchTrueFalseTotal
$skipRows and $skipRows = 0 0 0 0
331 }
332 397 ▶ if ($skipRows--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skipRows--) 391 6 397
333 $array = $attrs->{csv_csv_in}->getline ($meta->{fh}) or
334 391 ▶ croak "Missing first row due to ".$attrs->{csv_csv_in}->error_diag;
Branch: 1 branch
BranchTrueFalseTotal
$array = $attrs->{"csv_csv_in"}->getline($meta->{"fh"}) or croak('Missing first row due to ' . $attrs->{"csv_csv_in"}->error_diag) 391 0 391
335 391 ▶ unless ($meta->{raw_header}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->{"raw_header"}) 391 0 391
336 391 s/\W/_/g for @$array;
337 }
338 defined $meta->{col_names} or
339 391 ▶ $meta->{col_names} = $array;
Branch: 1 branch
BranchTrueFalseTotal
defined $meta->{"col_names"} or $meta->{"col_names"} = $array 142 249 391
340 391 ▶ while ($skipRows--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skipRows--) 12 391 403
341 12 $attrs->{csv_csv_in}->getline ($meta->{fh});
342 }
343 }
344 # lockMode is set 1 for DELETE, INSERT or UPDATE
345 # no other case need seeking
346 $flags->{lockMode} and # $meta->{fh}->can ("tell") and
347 397 ▶ $meta->{first_row_pos} = $meta->{fh}->tell ();
Branch: 1 branch
BranchTrueFalseTotal
$flags->{"lockMode"} and $meta->{"first_row_pos"} = $meta->{"fh"}->tell 135 262 397
348 exists $meta->{col_names} and
349 397 ▶ $array = $meta->{col_names};
Branch: 1 branch
BranchTrueFalseTotal
exists $meta->{"col_names"} and $array = $meta->{"col_names"} 397 0 397
350 397 ▶ if (!$meta->{col_names} || !@{$meta->{col_names}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $meta->{"col_names"} && @{$meta->{"col_names"};}) 1 396 397
Condition66%2/3
$meta->{'col_names'} && @{$meta->{'col_names'};}
!ll&&!rl&&r
01396
MC/DC50%1/2
$meta->{'col_names'} && @{$meta->{'col_names'};}
$meta->{'col_names'} @{$meta->{'col_names'};}
Truth table66%2/3
A && B
A= $meta->{'col_names'}B= @{$meta->{'col_names'};}
ABresult
0X0
100
111
351 # No column names given; fetch first row and create default
352 # names.
353 my $ar = $meta->{cached_row} =
354 1 $attrs->{csv_csv_in}->getline ($meta->{fh});
355 1 $array = $meta->{col_names};
356 1 push @$array, map { "col$_" } 0 .. $#$ar;
357 }
358 }
359 }
360 } # open_file
361  
362 24 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
363 24 ▶$DBI::VERSION < 1.623 and
Branch: 1 branch
BranchTrueFalseTotal
$DBI::VERSION < "1.623" and *open_file = \&DBD::CSV::Table::open_data 0 24 24
364 *open_file = \&open_data;
365 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
366  
367 2 ▶sub _csv_diag {
Subroutine _csv_diag: called CC=3 SCAR=11.1
368 2 my @diag = $_[0]->error_diag;
369 2 for (2, 3) {
370 4 ▶ defined $diag[$_] or $diag[$_] = "?";
Branch: 1 branch
BranchTrueFalseTotal
defined $diag[$_] or $diag[$_] = "?" 4 0 4
371 }
372 2 return @diag;
373 } # _csv_diag
374  
375 1177 ▶sub fetch_row {
Subroutine fetch_row: called CC=8 SCAR=21.6
376 1177 my ($self, $data) = @_;
377  
378 1177 my $tbl = $self->{meta};
379  
380 exists $tbl->{cached_row} and
381 1177 ▶ return $self->{row} = delete $tbl->{cached_row};
Branch: 1 branch
BranchTrueFalseTotal
exists $tbl->{"cached_row"} and return $self->{"row"} = delete $tbl->{"cached_row"} 1 1176 1177
382  
383 my $csv = $self->{csv_csv_in} or
384 0 ▶ return do { $data->set_err ($DBI::stderr, "Fetch from undefined handle"); undef };
Branch: 1 branch
BranchTrueFalseTotal
my $csv = $self->{"csv_csv_in"} or return do { $data->set_err($DBI::stderr, "Fetch from undefined handle"); undef } 1176 0 1176
385  
386 1176 my $fields = eval { $csv->getline ($tbl->{fh}) };
387 1176 ▶ unless ($fields) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields) 285 891 1176
388 285 ▶ $csv->eof and return;
Branch: 1 branch
BranchTrueFalseTotal
$csv->eof and return 283 2 285
389  
390 2 my @diag = _csv_diag ($csv);
391 2 ▶ $diag[0] == 2012 and return; # Also EOF (broken in Text::CSV_XS-1.10)
Branch: 1 branch
BranchTrueFalseTotal
$diag[0] == 2012 and return 0 2 2
392  
393 2 my $file = $tbl->{f_fqfn};
394 2 croak "Error $diag[0] while reading file $file: $diag[1] \@ line $diag[3] pos $diag[2]";
395 }
396 891 @$fields < @{$tbl->{col_names}} and
397 0 ▶ push @$fields, (undef) x (@{$tbl->{col_names}} - @$fields);
Branch: 1 branch
BranchTrueFalseTotal
@$fields < @{$tbl->{"col_names"};} and push @$fields, (undef) x (@{$tbl->{"col_names"};} - @$fields) 0 891 891
398 891 ▶ $self->{row} = (@$fields ? $fields : undef);
Branch: 1 branch
BranchTrueFalseTotal
@$fields ? : 891 0 891
399 } # fetch_row
400  
401 189 ▶sub push_row {
Subroutine push_row: called CC=2 SCAR=8.7
402 189 my ($self, $data, $fields) = @_;
403 189 my $tbl = $self->{meta};
404 189 my $csv = $self->{csv_csv_out};
405 189 my $fh = $tbl->{fh};
406  
407 189 ▶ unless ($csv->print ($fh, $fields)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csv->print($fh, $fields)) 0 189 189
408 0 my @diag = _csv_diag ($csv);
409 0 my $file = $tbl->{f_fqfn};
410 0 return do { $data->set_err ($DBI::stderr,
411 0 "Error $diag[0] while writing file $file: $diag[1] \@ line $diag[3] pos $diag[2]"); undef };
412 }
413 189 1;
414 } # push_row
415  
416 24 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
417 24 *push_names = \&push_row;
418 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
419  
420 24 1;
421  
422 __END__
↑ Summary DBD/CSV/GetInfo.pm »