blib/lib/Zuzu/Module/CSV.pm

filter: Statementstmt 7.3% 70 / 951 Branchbran 0.0% 0 / 490 Conditioncond 0.0% 0 / 218 MC/DCmcdc 0.0% 0 / 121 Subroutinesub 7.6% 9 / 117 Podpod 0.0% 0 / 1 totaltotal 4.1% 79 / 1898 CC 355 SCAR 49.4
CC
355
Cov
4%
_apply_row_rules
812.0
__ANON__
600.0
_coerce_scalar
420.0
CRAP 140.4
« blib/lib/Zuzu/Module/CLib.pm ↑ Summary blib/lib/Zuzu/Module/Math.pm »
1 package Zuzu::Module::CSV;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.007002';
7  
8 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Text::CSV_XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Zuzu::Value::BinaryString;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 native_class
15 native_function
16 native_object
17 perl_to_zuzu
18 zuzu_bool
19 zuzu_to_perl
20 2 );
21  
22 0 ▶sub _path_tiny_from_object {
Subroutine _path_tiny_from_object: not called CC=6 SCAR=37.4
23 0 my ( $path_obj, $method_name ) = @_;
24  
25 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($path_obj) and $path_obj->isa('Zuzu::Value::Object') and exists $path_obj->slots->{'_path_tiny'}) { } 0 0 0
elsif (ref $path_obj eq 'HASH' and exists $path_obj->{'_path_tiny'}) { } 0 0 0
Condition0%0/6
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object") and exists $path_obj->slots->{"_path_tiny"}
!ll&&!rl&&r
000
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object") and exists $path_obj->slots->{"_path_tiny"}
builtin::blessed($path_obj) $path_obj->isa("Zuzu::Value::Object") exists $path_obj->slots->{"_path_tiny"}
Truth table0%0/4
A and B and C
A= builtin::blessed($path_obj)B= $path_obj->isa("Zuzu::Value::Object")C= exists $path_obj->slots->{"_path_tiny"}
ABCresult
0XX0
10X0
1100
1111
26 blessed($path_obj)
27 and $path_obj->isa('Zuzu::Value::Object')
28 and exists $path_obj->slots->{_path_tiny}
29 ) {
30 0 return $path_obj->slots->{_path_tiny};
31 }
32 ▶ elsif ( ref($path_obj) eq 'HASH' and exists $path_obj->{_path_tiny} ) {
Condition0%0/3
ref $path_obj eq "HASH" and exists $path_obj->{"_path_tiny"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $path_obj eq "HASH" and exists $path_obj->{"_path_tiny"}
ref $path_obj eq "HASH" exists $path_obj->{"_path_tiny"}
Truth table0%0/3
A and B
A= ref $path_obj eq "HASH"B= exists $path_obj->{"_path_tiny"}
ABresult
0X0
100
111
33 0 return $path_obj->{_path_tiny};
34 }
35  
36 0 die Zuzu::Error->new_runtime(
37 message => "TypeException: $method_name expects Path as first argument",
38 file => '<std/data/csv>',
39 line => 0,
40 );
41 }
42  
43 0 ▶sub _dbh_from_object {
Subroutine _dbh_from_object: not called CC=6 SCAR=37.4
44 0 my ( $dbh_obj, $method_name ) = @_;
45  
46 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($dbh_obj) and $dbh_obj->isa('Zuzu::Value::Object') and exists $dbh_obj->slots->{'_dbh'}) { } 0 0 0
elsif (ref $dbh_obj eq 'HASH' and exists $dbh_obj->{'_dbh'}) { } 0 0 0
Condition0%0/6
builtin::blessed($dbh_obj) and $dbh_obj->isa("Zuzu::Value::Object") and exists $dbh_obj->slots->{"_dbh"}
!ll&&!rl&&r
000
builtin::blessed($dbh_obj) and $dbh_obj->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($dbh_obj) and $dbh_obj->isa("Zuzu::Value::Object") and exists $dbh_obj->slots->{"_dbh"}
builtin::blessed($dbh_obj) $dbh_obj->isa("Zuzu::Value::Object") exists $dbh_obj->slots->{"_dbh"}
Truth table0%0/4
A and B and C
A= builtin::blessed($dbh_obj)B= $dbh_obj->isa("Zuzu::Value::Object")C= exists $dbh_obj->slots->{"_dbh"}
ABCresult
0XX0
10X0
1100
1111
47 blessed($dbh_obj)
48 and $dbh_obj->isa('Zuzu::Value::Object')
49 and exists $dbh_obj->slots->{_dbh}
50 ) {
51 0 return $dbh_obj->slots->{_dbh};
52 }
53 ▶ elsif ( ref($dbh_obj) eq 'HASH' and exists $dbh_obj->{_dbh} ) {
Condition0%0/3
ref $dbh_obj eq "HASH" and exists $dbh_obj->{"_dbh"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $dbh_obj eq "HASH" and exists $dbh_obj->{"_dbh"}
ref $dbh_obj eq "HASH" exists $dbh_obj->{"_dbh"}
Truth table0%0/3
A and B
A= ref $dbh_obj eq "HASH"B= exists $dbh_obj->{"_dbh"}
ABresult
0X0
100
111
54 0 return $dbh_obj->{_dbh};
55 }
56  
57 0 die Zuzu::Error->new_runtime(
58 message => "TypeException: $method_name expects DatabaseHandle",
59 file => '<std/data/csv>',
60 line => 0,
61 );
62 }
63  
64 0 ▶sub _dbi_call {
Subroutine _dbi_call: not called CC=4 SCAR=30.0
65 0 my ( $code, $prefix ) = @_;
66  
67 0 my $result;
68 eval {
69 0 $result = $code->();
70 0 1;
71 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $result = &$code(); 1 } } or do { my $err = $@; $err = "$err"; chomp $err; my $message = $err; if (defined $prefix and $prefix ne "") { $message = "${prefix}: $err"; } ; die "Zuzu::Error"->new_runtime("message", $message, "file", "<std/data/csv>", "line", 0) } 0 0 0
72 0 my $err = $@;
73 0 $err = "$err";
74 0 chomp $err;
75 0 my $message = $err;
76 0 ▶ if ( defined $prefix and $prefix ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix and $prefix ne "") 0 0 0
Condition0%0/3
defined $prefix and $prefix ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prefix and $prefix ne ""
defined $prefix $prefix ne ""
Truth table0%0/3
A and B
A= defined $prefixB= $prefix ne ""
ABresult
0X0
100
111
77 0 $message = "$prefix: $err";
78 }
79 0 die Zuzu::Error->new_runtime(
80 message => $message,
81 file => '<std/data/csv>',
82 line => 0,
83 );
84 };
85  
86 0 return $result;
87 }
88  
89 0 ▶sub _normalize_config {
Subroutine _normalize_config: not called CC=7 SCAR=40.3
90 0 my ( $positional, $named ) = @_;
91  
92 0 ▶ my %config = %{ $named // {} };
Condition0%0/2
$named // {}
l!l
00
MC/DC0%0/1
$named // {}
$named
Truth table0%0/2
A
A= $named
Aresult
11
00
93 0 ▶ if ( @{ $positional // [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$positional // [];}) 0 0 0
Condition0%0/2
$positional // []
l!l
00
MC/DC0%0/1
$positional // []
$positional
Truth table0%0/2
A
A= $positional
Aresult
11
00
94 0 my $first = $positional->[0];
95 0 ▶ if ( ref($first) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first) 0 0 0
96 0 my $first_hash = zuzu_to_perl( $first );
97 0 ▶ if ( ref($first_hash) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first_hash eq "HASH") 0 0 0
98 0 for my $key ( CORE::keys %{ $first_hash } ) {
99 0 $config{$key} = $first_hash->{$key};
100 }
101 }
102 }
103 }
104  
105 0 return \%config;
106 }
107  
108 0 ▶sub _options_hash {
Subroutine _options_hash: not called CC=4 SCAR=30.0
109 0 my ( $value ) = @_;
110  
111 0 ▶ return {} if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
112 0 ▶ return $value if ref($value) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 0 0 0
113  
114 0 my $perl = zuzu_to_perl( $value );
115 0 ▶ return $perl if ref($perl) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $perl eq "HASH" 0 0 0
116 0 return {};
117 }
118  
119 0 ▶sub _hash_from_value {
Subroutine _hash_from_value: not called CC=2 SCAR=17.9
120 0 my ( $value ) = @_;
121  
122 0 my $hash = _options_hash( $value );
123 0 ▶ return $hash if ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash eq "HASH" 0 0 0
124 0 return {};
125 }
126  
127 0 ▶sub _array_from_value {
Subroutine _array_from_value: not called CC=4 SCAR=30.0
128 0 my ( $value ) = @_;
129  
130 0 ▶ return [] if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
131 0 ▶ return $value if ref($value) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 0 0
132  
133 0 my $perl = zuzu_to_perl( $value );
134 0 ▶ return $perl if ref($perl) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $perl eq "ARRAY" 0 0 0
135 0 return [];
136 }
137  
138 0 ▶sub _config_merge {
Subroutine _config_merge: not called CC=4 SCAR=30.0
139 0 my ( @parts ) = @_;
140  
141 0 my %out;
142 0 for my $part ( @parts ) {
143 0 ▶ next if ref($part) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $part ne "HASH" 0 0 0
144 0 for my $key ( CORE::keys %{$part} ) {
145 0 $out{$key} = $part->{$key};
146 }
147 }
148 0 return \%out;
149 }
150  
151 0 ▶sub _normalize_runtime_config {
Subroutine _normalize_runtime_config: not called CC=5 SCAR=34.0
152 0 my ( $config ) = @_;
153  
154 0 $config = _config_merge( $config );
155  
156 0 for my $key ( qw(
157 columns
158 required_columns
159 sniff_candidates
160 ) ) {
161 $config->{$key} = _array_from_value( $config->{$key} )
162 0 ▶ if exists $config->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{$key} 0 0 0
163 }
164  
165 0 for my $key ( qw(
166 defaults
167 rename_headers
168 column_map
169 column_types
170 types
171 ) ) {
172 $config->{$key} = _hash_from_value( $config->{$key} )
173 0 ▶ if exists $config->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{$key} 0 0 0
174 }
175  
176 0 return $config;
177 }
178  
179 0 ▶sub _text_csv_config {
Subroutine _text_csv_config: not called CC=12 SCAR=50.5
180 0 my ( $config ) = @_;
181  
182 0 my %allowed = map { $_ => 1 } qw(
183 allow_loose_quotes
184 allow_loose_escapes
185 allow_unquoted_escape
186 allow_whitespace
187 always_quote
188 binary
189 blank_is_undef
190 empty_is_undef
191 escape_char
192 eol
193 quote_char
194 quote_empty
195 quote_null
196 quote_space
197 sep_char
198 verbatim
199 );
200  
201 0 my %opts;
202 0 ▶ for my $key ( CORE::keys %{ $config // {} } ) {
Condition0%0/2
$config // {}
l!l
00
MC/DC0%0/1
$config // {}
$config
Truth table0%0/2
A
A= $config
Aresult
11
00
203 0 ▶ next if !$allowed{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed{$key} 0 0 0
204 0 my $value = $config->{$key};
205 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A(?:allow_|always_|binary|blank_|empty_|quote_|verbatim)/) { } 0 0 0
206 $key =~ /\A(?:allow_|always_|binary|blank_|empty_|quote_|verbatim)/
207 ) {
208 0 ▶ $opts{$key} = zuzu_bool( $value, 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($value, 0) ? : 0 0 0
209 }
210 else {
211 0 ▶ $opts{$key} = defined $value ? "$value" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
212 }
213 }
214  
215 0 ▶ $opts{binary} = 1 if !exists $opts{binary};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"binary"} 0 0 0
216 0 ▶ $opts{sep_char} = ',' if !exists $opts{sep_char};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"sep_char"} 0 0 0
217 0 ▶ $opts{quote_char} = '"' if !exists $opts{quote_char};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"quote_char"} 0 0 0
218 0 ▶ $opts{escape_char} = '"' if !exists $opts{escape_char};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"escape_char"} 0 0 0
219 0 ▶ $opts{eol} = "\n" if !exists $opts{eol};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"eol"} 0 0 0
220  
221 0 return \%opts;
222 }
223  
224 0 ▶sub _encoding_name {
Subroutine _encoding_name: not called CC=4 SCAR=30.0
225 0 my ( $config ) = @_;
226  
227 0 ▶ return 'UTF-8' if !defined $config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 0 0
228 0 ▶ return 'UTF-8' if !exists $config->{encoding};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"encoding"} 0 0 0
229 0 ▶ return undef if !defined $config->{encoding};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"encoding"} 0 0 0
230 0 return "$config->{encoding}";
231 }
232  
233 0 ▶sub _assert_binary_string {
Subroutine _assert_binary_string: not called CC=5 SCAR=34.0
234 0 my ( $value, $label ) = @_;
235  
236 0 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
237 if blessed($value) and $value->isa('Zuzu::Value::BinaryString');
238  
239 0 ▶ my $type = !defined($value) ? 'Null' : ref($value) ? ref($value) : 'String';
Branch: 2 branches
BranchTrueFalseTotal
!defined($value) ? : 0 0 0
ref $value ? : 0 0 0
240 0 die Zuzu::Error->new_runtime(
241 message => "TypeException: $label expects BinaryString, got $type",
242 file => '<std/data/csv>',
243 line => 0,
244 );
245 }
246  
247 0 ▶sub _text_from_bytes {
Subroutine _text_from_bytes: not called CC=2 SCAR=17.9
248 0 my ( $bytes, $config ) = @_;
249  
250 0 my $encoding = _encoding_name($config);
251 0 ▶ return $bytes if !defined $encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoding 0 0 0
252 0 return Encode::decode( $encoding, $bytes, Encode::FB_CROAK );
253 }
254  
255 0 ▶sub _bytes_from_text {
Subroutine _bytes_from_text: not called CC=3 SCAR=24.8
256 0 my ( $text, $config ) = @_;
257  
258 0 ▶ $text = '' if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
259 0 my $encoding = _encoding_name($config);
260 0 ▶ return "$text" if !defined $encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoding 0 0 0
261 0 return Encode::encode( $encoding, "$text", Encode::FB_CROAK );
262 }
263  
264 0 ▶sub _new_csv_backend {
Subroutine _new_csv_backend: not called CC=2 SCAR=17.9
265 0 my ( $config ) = @_;
266  
267 0 my $csv = Text::CSV_XS->new( _text_csv_config($config) );
268 0 ▶ if ( !$csv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csv) 0 0 0
269 0 die Zuzu::Error->new_runtime(
270 message => 'CSV backend initialization failed',
271 file => '<std/data/csv>',
272 line => 0,
273 );
274 }
275 0 return $csv;
276 }
277  
278 0 ▶sub _open_input_handle {
Subroutine _open_input_handle: not called CC=3 SCAR=24.8
279 0 my ( $path_tiny, $config ) = @_;
280  
281 0 my $encoding = _encoding_name( $config );
282 0 ▶ my $mode = defined $encoding
Branch: 1 branch
BranchTrueFalseTotal
defined $encoding ? : 0 0 0
283 ? "<:encoding($encoding)"
284 : '<';
285  
286 0 ▶ open my $fh, $mode, $path_tiny->stringify
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $path_tiny->stringify or die "Zuzu::Error"->new_runtime("message", "Could not open '${path_tiny}' for reading: $!", "file", "<std/data/csv>", "line", 0) 0 0 0
287 or die Zuzu::Error->new_runtime(
288 message => "Could not open '$path_tiny' for reading: $!",
289 file => '<std/data/csv>',
290 line => 0,
291 );
292  
293 0 return $fh;
294 }
295  
296 0 ▶sub _open_output_handle {
Subroutine _open_output_handle: not called CC=5 SCAR=34.0
297 0 my ( $path_tiny, $config ) = @_;
298  
299 0 my $encoding = _encoding_name( $config );
300 0 ▶ my $append = zuzu_bool( $config->{append}, 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($config->{'append'}, 0) ? : 0 0 0
301 0 ▶ my $mode = $append ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$append ? : 0 0 0
302 0 ▶ $mode .= ":encoding($encoding)" if defined $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if defined $encoding 0 0 0
303  
304 0 ▶ open my $fh, $mode, $path_tiny->stringify
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $path_tiny->stringify or die "Zuzu::Error"->new_runtime("message", "Could not open '${path_tiny}' for writing: $!", "file", "<std/data/csv>", "line", 0) 0 0 0
305 or die Zuzu::Error->new_runtime(
306 message => "Could not open '$path_tiny' for writing: $!",
307 file => '<std/data/csv>',
308 line => 0,
309 );
310  
311 0 return $fh;
312 }
313  
314 0 ▶sub _string_input_handle {
Subroutine _string_input_handle: not called CC=3 SCAR=24.8
315 0 my ( $text ) = @_;
316  
317 0 ▶ my $scalar = defined $text ? "$text" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 0 0 0
318 0 ▶ open my $fh, '<', \$scalar
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", \$scalar or die "Zuzu::Error"->new_runtime("message", "Could not open in-memory CSV source: $!", "file", "<std/data/csv>", "line", 0) 0 0 0
319 or die Zuzu::Error->new_runtime(
320 message => "Could not open in-memory CSV source: $!",
321 file => '<std/data/csv>',
322 line => 0,
323 );
324 0 return $fh;
325 }
326  
327 0 ▶sub _string_output_handle {
Subroutine _string_output_handle: not called CC=2 SCAR=17.9
328 0 my $out = '';
329 0 ▶ open my $fh, '>', \$out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", \$out or die "Zuzu::Error"->new_runtime("message", "Could not open in-memory CSV sink: $!", "file", "<std/data/csv>", "line", 0) 0 0 0
330 or die Zuzu::Error->new_runtime(
331 message => "Could not open in-memory CSV sink: $!",
332 file => '<std/data/csv>',
333 line => 0,
334 );
335 0 return ( $fh, \$out );
336 }
337  
338 0 ▶sub _columns_from_config {
Subroutine _columns_from_config: not called CC=4 SCAR=30.0
339 0 my ( $config ) = @_;
340  
341 0 ▶ if ( exists $config->{columns} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"columns"}) 0 0 0
342 0 my $cols = $config->{columns};
343 0 ▶ if ( ref($cols) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cols eq "ARRAY") 0 0 0
344 0 ▶ return [ map { defined $_ ? "$_" : '' } @{ $cols } ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
345 }
346 }
347  
348 0 return undef;
349 }
350  
351 0 ▶sub _hashify_row {
Subroutine _hashify_row: not called CC=2 SCAR=17.9
352 0 my ( $row, $headers ) = @_;
353  
354 0 my %out;
355 0 for my $i ( 0 .. $#{ $headers } ) {
356 0 $out{ $headers->[$i] } = $row->[$i];
357 }
358 0 return \%out;
359 }
360  
361 0 ▶sub _header_name {
Subroutine _header_name: not called CC=6 SCAR=37.4
362 0 my ( $raw, $config ) = @_;
363  
364 0 ▶ $raw = defined $raw ? "$raw" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 0 0 0
365 0 ▶ $raw =~ s/^\s+// if zuzu_bool( $config->{trim_headers}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($config->{"trim_headers"}, 0) 0 0 0
366 0 ▶ $raw =~ s/\s+\z// if zuzu_bool( $config->{trim_headers}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($config->{"trim_headers"}, 0) 0 0 0
367 0 ▶ $raw = lc($raw) if zuzu_bool( $config->{lowercase_headers}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($config->{"lowercase_headers"}, 0) 0 0 0
368  
369 0 my $rename = _hash_from_value( $config->{rename_headers} );
370 0 ▶ $raw = $rename->{$raw} if exists $rename->{$raw};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rename->{$raw} 0 0 0
371  
372 0 return $raw;
373 }
374  
375 0 ▶sub _normalize_headers {
Subroutine _normalize_headers: not called CC=8 SCAR=42.8
376 0 my ( $headers, $config ) = @_;
377  
378 0 ▶ return [] if ref($headers) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "ARRAY" 0 0 0
379 my $duplicate_policy = exists $config->{duplicate_headers}
380 0 ▶ ? lc( "$config->{duplicate_headers}" )
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'duplicate_headers'} ? : 0 0 0
381 : 'overwrite';
382 0 my %seen;
383 my @out;
384  
385 0 for my $name ( @{$headers} ) {
386 0 my $normalized = _header_name( $name, $config );
387 0 ▶ if ( exists $seen{$normalized} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$normalized}) { } 0 0 0
388 0 ▶ if ( $duplicate_policy eq 'error' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($duplicate_policy eq 'error') { } 0 0 0
elsif ($duplicate_policy eq 'suffix') { } 0 0 0
elsif ($duplicate_policy eq 'keep_first') { } 0 0 0
389 0 die Zuzu::Error->new_runtime(
390 message => "Duplicate CSV header '$normalized'",
391 file => '<std/data/csv>',
392 line => 0,
393 );
394 }
395 elsif ( $duplicate_policy eq 'suffix' ) {
396 0 $seen{$normalized}++;
397 0 $normalized = $normalized . '_' . $seen{$normalized};
398 }
399 elsif ( $duplicate_policy eq 'keep_first' ) {
400 0 $normalized = $normalized . '__ignored_' . ( $seen{$normalized} + 1 );
401 0 $seen{$normalized}++;
402 }
403 else {
404 0 $seen{$normalized}++;
405 }
406 }
407 else {
408 0 $seen{$normalized} = 1;
409 }
410 0 push @out, $normalized;
411 }
412  
413 0 return \@out;
414 }
415  
416 0 ▶sub _required_columns_check {
Subroutine _required_columns_check: not called CC=5 SCAR=34.0
417 0 my ( $headers, $config ) = @_;
418  
419 0 my $required = _array_from_value( $config->{required_columns} );
420 0 ▶ return if !@{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$required;} 0 0 0
421  
422 0 ▶ my %have = map { $_ => 1 } @{ $headers // [] };
Condition0%0/2
$headers // []
l!l
00
MC/DC0%0/1
$headers // []
$headers
Truth table0%0/2
A
A= $headers
Aresult
11
00
423 0 for my $name ( @{$required} ) {
424 0 ▶ next if $have{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $have{$name} 0 0 0
425 0 die Zuzu::Error->new_runtime(
426 message => "Required CSV column '$name' missing",
427 file => '<std/data/csv>',
428 line => 0,
429 );
430 }
431 }
432  
433 0 ▶sub _default_columns_for_row {
Subroutine _default_columns_for_row: not called CC=2 SCAR=17.9
434 0 my ( $row ) = @_;
435 0 my @cols;
436 0 for my $i ( 0 .. $#{ $row } ) {
437 0 push @cols, 'column' . ( $i + 1 );
438 }
439 0 return \@cols;
440 }
441  
442 0 ▶sub _csv_error_detail {
Subroutine _csv_error_detail: not called CC=8 SCAR=42.8
443 0 my ( $csv, $prefix, $state ) = @_;
444  
445 0 ▶ my $diag = $csv ? $csv->error_diag : undef;
Branch: 1 branch
BranchTrueFalseTotal
$csv ? : 0 0 0
446 0 ▶ $diag = defined $diag ? "$diag" : 'unknown CSV error';
Branch: 1 branch
BranchTrueFalseTotal
defined $diag ? : 0 0 0
447  
448 0 my $message = $prefix . ': ' . $diag;
449 0 ▶ if ( ref($state) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state eq "HASH") 0 0 0
450 0 my @parts;
451 push @parts, 'line ' . $state->{line_number}
452 0 ▶ if defined $state->{line_number};
Branch: 1 branch
BranchTrueFalseTotal
if defined $state->{"line_number"} 0 0 0
453 push @parts, 'column ' . $state->{column_number}
454 0 ▶ if defined $state->{column_number};
Branch: 1 branch
BranchTrueFalseTotal
if defined $state->{"column_number"} 0 0 0
455 push @parts, 'row ' . $state->{raw_line}
456 0 ▶ if defined $state->{raw_line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $state->{"raw_line"} 0 0 0
457 0 ▶ $message .= ' (' . join( ', ', @parts ) . ')' if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 0 0 0
458 }
459  
460 return {
461 message => $message,
462 line_number => $state->{line_number},
463 column_number => $state->{column_number},
464 raw_line => $state->{raw_line},
465 0 };
466 }
467  
468 0 ▶sub _die_csv_error {
Subroutine _die_csv_error: not called CC=1 SCAR=6.9
469 0 my ( $csv, $prefix, $state ) = @_;
470  
471 0 my $detail = _csv_error_detail( $csv, $prefix, $state );
472 die Zuzu::Error->new_runtime(
473 message => $detail->{message},
474 0 file => '<std/data/csv>',
475 line => 0,
476 );
477 }
478  
479 0 ▶sub _coerce_scalar {
Subroutine _coerce_scalar: not called CC=20 SCAR=60.4
480 0 my ( $value, $type ) = @_;
481  
482 0 ▶ return undef if !defined $value and ( defined $type and $type ne 'string' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and (defined $type and $type ne "string") 0 0 0
Condition0%0/6
not defined $value and (defined $type and $type ne "string")
!ll&&!rl&&r
000
defined $type and $type ne "string"
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $value and (defined $type and $type ne "string")
not defined $value defined $type $type ne "string"
Truth table0%0/4
not(A) and B and C
A= not defined $valueB= defined $typeC= $type ne "string"
ABCresult
0XX0
10X0
1100
1111
483 0 ▶ return $value if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
484  
485 0 $type = lc("$type");
486  
487 0 ▶ return undef if $type eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "null" 0 0 0
488 0 ▶ if ( $type eq 'integer' or $type eq 'int' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "integer" or $type eq "int") 0 0 0
Condition0%0/3
$type eq "integer" or $type eq "int"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "integer" or $type eq "int"
$type eq "integer" $type eq "int"
Truth table0%0/3
A or B
A= $type eq "integer"B= $type eq "int"
ABresult
1X1
011
000
489 0 ▶ return 0 + int( $value // 0 );
Condition0%0/2
$value // 0
l!l
00
MC/DC0%0/1
$value // 0
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
490 }
491 0 ▶ if ( $type eq 'number' or $type eq 'float' or $type eq 'real' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number" or $type eq "float" or $type eq "real") 0 0 0
Condition0%0/6
$type eq "number" or $type eq "float" or $type eq "real"
l!l&&r!l&&!r
000
$type eq "number" or $type eq "float"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "number" or $type eq "float" or $type eq "real"
$type eq "number" $type eq "float" $type eq "real"
Truth table0%0/4
A or B or C
A= $type eq "number"B= $type eq "float"C= $type eq "real"
ABCresult
1XX1
01X1
0011
0000
492 0 ▶ return 0.0 + ( $value // 0 );
Condition0%0/2
$value // 0
l!l
00
MC/DC0%0/1
$value // 0
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
493 }
494 0 ▶ if ( $type eq 'boolean' or $type eq 'bool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "boolean" or $type eq "bool") 0 0 0
Condition0%0/3
$type eq "boolean" or $type eq "bool"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "boolean" or $type eq "bool"
$type eq "boolean" $type eq "bool"
Truth table0%0/3
A or B
A= $type eq "boolean"B= $type eq "bool"
ABresult
1X1
011
000
495 0 ▶ my $text = defined $value ? lc("$value") : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
496 0 ▶ return 1 if $text =~ /\A(?:1|true|yes|on)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A(?:1|true|yes|on)\z/ 0 0 0
497 0 ▶ return 0 if $text =~ /\A(?:0|false|no|off|)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A(?:0|false|no|off|)\z/ 0 0 0
498 0 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
499 }
500  
501 0 ▶ return defined $value ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
502 }
503  
504 0 ▶sub _invoke_hook {
Subroutine _invoke_hook: not called CC=3 SCAR=24.8
505 0 my ( $runtime, $hook, @args ) = @_;
506  
507 return undef
508 0 ▶ if !blessed($hook)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($hook) and $hook->isa("Zuzu::Value::Function") 0 0 0
Condition0%0/3
builtin::blessed($hook) and $hook->isa("Zuzu::Value::Function")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($hook) and $hook->isa("Zuzu::Value::Function")
builtin::blessed($hook) $hook->isa("Zuzu::Value::Function")
Truth table0%0/3
A and B
A= builtin::blessed($hook)B= $hook->isa("Zuzu::Value::Function")
ABresult
0X0
100
111
509 or !$hook->isa('Zuzu::Value::Function');
510  
511 0 my @zuzu_args = map { perl_to_zuzu($_) } @args;
512 0 my $result = $runtime->_call_function(
513 $hook,
514 \@zuzu_args,
515 '<std/data/csv>',
516 0,
517 );
518 0 return zuzu_to_perl( $result );
519 }
520  
521 0 ▶sub _column_hook_for {
Subroutine _column_hook_for: not called CC=6 SCAR=37.4
522 0 my ( $config, $key ) = @_;
523  
524 0 ▶ return undef if ref($config) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $config ne "HASH" 0 0 0
525 0 my $map = $config->{parsers};
526 0 ▶ $map = $config->{formatters} if $config->{_hook_mode} && $config->{_hook_mode} eq 'format';
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"_hook_mode"} and $config->{"_hook_mode"} eq "format" 0 0 0
Condition0%0/3
$config->{"_hook_mode"} and $config->{"_hook_mode"} eq "format"
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"_hook_mode"} and $config->{"_hook_mode"} eq "format"
$config->{"_hook_mode"} $config->{"_hook_mode"} eq "format"
Truth table0%0/3
A and B
A= $config->{"_hook_mode"}B= $config->{"_hook_mode"} eq "format"
ABresult
0X0
100
111
527 0 ▶ return undef if ref($map) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $map ne "HASH" 0 0 0
528 0 ▶ return $map->{$key} if exists $map->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map->{$key} 0 0 0
529 0 return undef;
530 }
531  
532 0 ▶sub _value_type_for {
Subroutine _value_type_for: not called CC=7 SCAR=40.3
533 0 my ( $config, $headers, $index ) = @_;
534  
535 0 my $types = $config->{types};
536 0 ▶ return undef if !defined $types;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $types 0 0 0
537  
538 0 ▶ if ( ref($types) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $types eq "ARRAY") 0 0 0
539 0 return $types->[$index];
540 }
541 0 ▶ if ( ref($types) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $types eq "HASH") 0 0 0
542 0 ▶ my $name = ref($headers) eq 'ARRAY' ? $headers->[$index] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'ARRAY' ? : 0 0 0
543 0 ▶ return $types->{$name} if defined $name and exists $types->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and exists $types->{$name} 0 0 0
Condition0%0/3
defined $name and exists $types->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and exists $types->{$name}
defined $name exists $types->{$name}
Truth table0%0/3
A and B
A= defined $nameB= exists $types->{$name}
ABresult
0X0
100
111
544 }
545 0 return undef;
546 }
547  
548 0 ▶sub _apply_row_rules {
Subroutine _apply_row_rules: not called CC=28 SCAR=67.0
549 0 my ( $runtime, $row, $headers, $config ) = @_;
550  
551 0 ▶ my $state = $config->{_row_state} // {};
Condition0%0/2
$config->{"_row_state"} // {}
l!l
00
MC/DC0%0/1
$config->{"_row_state"} // {}
$config->{"_row_state"}
Truth table0%0/2
A
A= $config->{"_row_state"}
Aresult
11
00
552  
553 0 ▶ if ( ref($headers) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers eq "ARRAY") 0 0 0
554 0 my $expected = scalar @{$headers};
555 0 my $actual = scalar @{$row};
556 my $ragged = exists $config->{ragged}
557 0 ▶ ? lc( "$config->{ragged}" )
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'ragged'} ? : 0 0 0
558 : 'allow';
559 my $fill_value = exists $config->{fill_value}
560 ? $config->{fill_value}
561 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'fill_value'} ? : 0 0 0
562  
563 0 ▶ if ( $actual < $expected ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($actual < $expected) { } 0 0 0
elsif ($actual > $expected) { } 0 0 0
564 0 ▶ if ( $ragged eq 'fill' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ragged eq 'fill') { } 0 0 0
elsif ($ragged eq 'error') { } 0 0 0
565 0 push @{$row}, ($fill_value) x ($expected - $actual);
566 }
567 elsif ( $ragged eq 'error' ) {
568 die Zuzu::Error->new_runtime(
569 message => "CSV row has too few fields at line "
570 0 ▶ . ( $state->{line_number} // '?' ),
Condition0%0/2
$state->{"line_number"} // "?"
l!l
00
MC/DC0%0/1
$state->{"line_number"} // "?"
$state->{"line_number"}
Truth table0%0/2
A
A= $state->{"line_number"}
Aresult
11
00
571 file => '<std/data/csv>',
572 line => 0,
573 );
574 }
575 }
576 elsif ( $actual > $expected ) {
577 0 ▶ if ( $ragged eq 'truncate' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ragged eq 'truncate') { } 0 0 0
elsif ($ragged eq 'error') { } 0 0 0
578 0 splice @{$row}, $expected;
579 }
580 elsif ( $ragged eq 'error' ) {
581 die Zuzu::Error->new_runtime(
582 message => "CSV row has too many fields at line "
583 0 ▶ . ( $state->{line_number} // '?' ),
Condition0%0/2
$state->{"line_number"} // "?"
l!l
00
MC/DC0%0/1
$state->{"line_number"} // "?"
$state->{"line_number"}
Truth table0%0/2
A
A= $state->{"line_number"}
Aresult
11
00
584 file => '<std/data/csv>',
585 line => 0,
586 );
587 }
588 }
589 }
590  
591 0 for my $i ( 0 .. $#{ $row } ) {
592 0 my $type = _value_type_for( $config, $headers, $i );
593 my $hook = _column_hook_for(
594 {
595 0 ▶ %{$config},
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'ARRAY' ? : 0 0 0
596 0 parsers => _hash_from_value( $config->{parsers} ),
597 },
598 ( ref($headers) eq 'ARRAY' ? $headers->[$i] : $i ),
599 );
600  
601 0 ▶ $row->[$i] = _coerce_scalar( $row->[$i], $type ) if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 0 0 0
602 0 ▶ if ( defined $hook ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hook) 0 0 0
603 $row->[$i] = _invoke_hook(
604 $runtime,
605 $hook,
606 $row->[$i],
607 {
608 column => ref($headers) eq 'ARRAY' ? $headers->[$i] : $i,
609 row_number => $state->{row_number},
610 line_number => $state->{line_number},
611 },
612 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'ARRAY' ? : 0 0 0
613 }
614 }
615  
616 0 ▶ if ( ref($headers) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers eq "ARRAY") 0 0 0
617 0 my $dict = _hashify_row( $row, $headers );
618 0 my $defaults = _hash_from_value( $config->{defaults} );
619 0 for my $key ( CORE::keys %{$defaults} ) {
620 $dict->{$key} = $defaults->{$key}
621 0 ▶ if !exists $dict->{$key} or !defined $dict->{$key} or $dict->{$key} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not exists $dict->{$key} && defined $dict->{$key} or $dict->{$key} eq "" 0 0 0
Condition0%0/6
not exists $dict->{$key} && defined $dict->{$key} or $dict->{$key} eq ""
l!l&&r!l&&!r
000
exists $dict->{$key} && defined $dict->{$key}
!ll&&!rl&&r
000
MC/DC0%0/3
not exists $dict->{$key} && defined $dict->{$key} or $dict->{$key} eq ""
exists $dict->{$key} defined $dict->{$key} $dict->{$key} eq ""
Truth table0%0/4
not(A && B) or C
A= exists $dict->{$key}B= defined $dict->{$key}C= $dict->{$key} eq ""
ABCresult
0XX1
10X1
1111
1100
622 }
623  
624 my $unknown_policy = exists $config->{unknown_columns}
625 0 ▶ ? lc( "$config->{unknown_columns}" )
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'unknown_columns'} ? : 0 0 0
626 : 'keep';
627 0 ▶ if ( $unknown_policy eq 'ignore' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unknown_policy eq "ignore") 0 0 0
628 0 my %allowed = map { $_ => 1 } @{$headers};
629 0 $allowed{$_} = 1 for CORE::keys %{$defaults};
630 0 for my $key ( CORE::keys %{$dict} ) {
631 0 ▶ delete $dict->{$key} if !$allowed{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed{$key} 0 0 0
632 }
633 }
634  
635 0 return $dict;
636 }
637  
638 0 return $row;
639 }
640  
641 0 ▶sub _reader_state {
Subroutine _reader_state: not called CC=3 SCAR=24.8
642 0 my ( $self ) = @_;
643  
644 return {
645 line_number => $self->slots->{_line_number} // 0,
646 row_number => $self->slots->{_row_number} // 0,
647 column_number => undef,
648 raw_line => $self->slots->{_last_raw_line},
649 0 ▶ };
Condition0%0/4
$self->slots->{"_line_number"} // 0
l!l
00
$self->slots->{"_row_number"} // 0
l!l
00
MC/DC0%0/1
$self->slots->{"_line_number"} // 0
$self->slots->{"_line_number"}
MC/DC0%0/1
$self->slots->{"_row_number"} // 0
$self->slots->{"_row_number"}
Truth table0%0/2
A
A= $self->slots->{"_line_number"}
Aresult
11
00
Truth table0%0/2
A
A= $self->slots->{"_row_number"}
Aresult
11
00
650 }
651  
652 0 ▶sub _reader_next_raw {
Subroutine _reader_next_raw: not called CC=12 SCAR=50.5
653 0 my ( $self ) = @_;
654  
655 0 ▶ return undef if zuzu_bool( $self->slots->{_closed}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($self->slots->{"_closed"}, 0) 0 0 0
656  
657 0 my $csv = $self->slots->{_csv};
658 0 my $fh = $self->slots->{_fh};
659 0 while (1) {
660 0 my $pos = tell($fh);
661 0 my $raw_line = <$fh>;
662 0 ▶ return undef if !defined $raw_line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw_line 0 0 0
663  
664 0 ▶ $self->slots->{_line_number} = ( $self->slots->{_line_number} // 0 ) + 1;
Condition0%0/2
$self->slots->{"_line_number"} // 0
l!l
00
MC/DC0%0/1
$self->slots->{"_line_number"} // 0
$self->slots->{"_line_number"}
Truth table0%0/2
A
A= $self->slots->{"_line_number"}
Aresult
11
00
665 0 $self->slots->{_last_raw_line} = $raw_line;
666  
667 0 my $comment_char = $self->slots->{_config}{comment_char};
668 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment_char and $comment_char ne "" and substr($raw_line, 0, length $comment_char) eq $comment_char) 0 0 0
Condition0%0/6
defined $comment_char and $comment_char ne "" and substr($raw_line, 0, length $comment_char) eq $comment_char
!ll&&!rl&&r
000
defined $comment_char and $comment_char ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
defined $comment_char and $comment_char ne "" and substr($raw_line, 0, length $comment_char) eq $comment_char
defined $comment_char $comment_char ne "" substr($raw_line, 0, length $comment_char) eq $comment_char
Truth table0%0/4
A and B and C
A= defined $comment_charB= $comment_char ne ""C= substr($raw_line, 0, length $comment_char) eq $comment_char
ABCresult
0XX0
10X0
1100
1111
669 defined $comment_char
670 and $comment_char ne ''
671 and substr( $raw_line, 0, length($comment_char) ) eq $comment_char
672 ) {
673 0 next;
674 }
675  
676 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (zuzu_bool($self->slots->{"_config"}{"skip_empty_rows"}, 0) and $raw_line =~ /\A\s*\z/) 0 0 0
Condition0%0/3
zuzu_bool($self->slots->{"_config"}{"skip_empty_rows"}, 0) and $raw_line =~ /\A\s*\z/
!ll&&!rl&&r
000
MC/DC0%0/2
zuzu_bool($self->slots->{"_config"}{"skip_empty_rows"}, 0) and $raw_line =~ /\A\s*\z/
zuzu_bool($self->slots->{"_config"}{"skip_empty_rows"}, 0) $raw_line =~ /\A\s*\z/
Truth table0%0/3
A and B
A= zuzu_bool($self->slots->{"_config"}{"skip_empty_rows"}, 0)B= $raw_line =~ /\A\s*\z/
ABresult
0X0
100
111
677 zuzu_bool( $self->slots->{_config}{skip_empty_rows}, 0 )
678 and $raw_line =~ /\A\s*\z/
679 ) {
680 0 next;
681 }
682  
683 0 seek( $fh, $pos, 0 );
684 0 my $row = $csv->getline($fh);
685 0 ▶ if ( !$row ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 0 0 0
686 0 ▶ return undef if $csv->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $csv->eof 0 0 0
687 0 return { __error__ => _csv_error_detail( $csv, 'CSV read failed', _reader_state($self) ) };
688 }
689  
690 0 ▶ $self->slots->{_row_number} = ( $self->slots->{_row_number} // 0 ) + 1;
Condition0%0/2
$self->slots->{"_row_number"} // 0
l!l
00
MC/DC0%0/1
$self->slots->{"_row_number"} // 0
$self->slots->{"_row_number"}
Truth table0%0/2
A
A= $self->slots->{"_row_number"}
Aresult
11
00
691 0 return $row;
692 }
693 }
694  
695 0 ▶sub _reader_fetch_array {
Subroutine _reader_fetch_array: not called CC=8 SCAR=42.8
696 0 my ( $runtime, $self ) = @_;
697  
698 0 while (1) {
699 0 my $row = _reader_next_raw( $self );
700 0 ▶ return undef if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
701  
702 0 ▶ if ( ref($row) eq 'HASH' and exists $row->{__error__} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq "HASH" and exists $row->{"__error__"}) 0 0 0
Condition0%0/3
ref $row eq "HASH" and exists $row->{"__error__"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $row eq "HASH" and exists $row->{"__error__"}
ref $row eq "HASH" exists $row->{"__error__"}
Truth table0%0/3
A and B
A= ref $row eq "HASH"B= exists $row->{"__error__"}
ABresult
0X0
100
111
703 my $policy = exists $self->slots->{_config}{on_error}
704 0 ▶ ? lc( "$self->slots->{_config}{on_error}" )
Branch: 1 branch
BranchTrueFalseTotal
exists $self->slots->{'_config'}{'on_error'} ? : 0 0 0
705 : 'die';
706 0 push @{ $self->slots->{_errors} }, $row->{__error__};
707 0 ▶ if ( $policy eq 'collect' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy eq "collect") 0 0 0
708 0 next;
709 }
710 die Zuzu::Error->new_runtime(
711 message => $row->{__error__}{message},
712 0 file => '<std/data/csv>',
713 line => 0,
714 );
715 }
716  
717 0 $self->slots->{_config}{_row_state} = _reader_state($self);
718 my $result = _apply_row_rules(
719 $runtime,
720 $row,
721 $self->slots->{_headers},
722 $self->slots->{_config},
723 0 );
724  
725 return ref($result) eq 'HASH'
726 0 ▶ ? [ map { $result->{$_} } @{ $self->slots->{_headers} // [] } ]
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'HASH' ? : 0 0 0
Condition0%0/2
$self->slots->{"_headers"} // []
l!l
00
MC/DC0%0/1
$self->slots->{"_headers"} // []
$self->slots->{"_headers"}
Truth table0%0/2
A
A= $self->slots->{"_headers"}
Aresult
11
00
727 : $result;
728 }
729 }
730  
731 0 ▶sub _reader_fetch_dict {
Subroutine _reader_fetch_dict: not called CC=9 SCAR=45.0
732 0 my ( $runtime, $self ) = @_;
733  
734 0 my $headers = $self->slots->{_headers};
735 0 ▶ if ( ref($headers) ne 'ARRAY' or !@{$headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers ne "ARRAY" or not @{$headers;}) 0 0 0
Condition0%0/3
ref $headers ne "ARRAY" or not @{$headers;}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $headers ne "ARRAY" or not @{$headers;}
ref $headers ne "ARRAY" not @{$headers;}
Truth table0%0/3
A or not(B)
A= ref $headers ne "ARRAY"B= not @{$headers;}
ABresult
1X1
011
000
736 0 die Zuzu::Error->new_runtime(
737 message => 'CSVReader.next_dict requires headers or columns',
738 file => '<std/data/csv>',
739 line => 0,
740 );
741 }
742  
743 0 while (1) {
744 0 my $row = _reader_next_raw( $self );
745 0 ▶ return undef if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
746  
747 0 ▶ if ( ref($row) eq 'HASH' and exists $row->{__error__} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq "HASH" and exists $row->{"__error__"}) 0 0 0
Condition0%0/3
ref $row eq "HASH" and exists $row->{"__error__"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $row eq "HASH" and exists $row->{"__error__"}
ref $row eq "HASH" exists $row->{"__error__"}
Truth table0%0/3
A and B
A= ref $row eq "HASH"B= exists $row->{"__error__"}
ABresult
0X0
100
111
748 my $policy = exists $self->slots->{_config}{on_error}
749 0 ▶ ? lc( "$self->slots->{_config}{on_error}" )
Branch: 1 branch
BranchTrueFalseTotal
exists $self->slots->{'_config'}{'on_error'} ? : 0 0 0
750 : 'die';
751 0 push @{ $self->slots->{_errors} }, $row->{__error__};
752 0 ▶ if ( $policy eq 'collect' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy eq "collect") 0 0 0
753 0 next;
754 }
755 die Zuzu::Error->new_runtime(
756 message => $row->{__error__}{message},
757 0 file => '<std/data/csv>',
758 line => 0,
759 );
760 }
761  
762 0 $self->slots->{_config}{_row_state} = _reader_state($self);
763 my $result = _apply_row_rules(
764 $runtime,
765 $row,
766 $headers,
767 $self->slots->{_config},
768 0 );
769 0 ▶ return $result if ref($result) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "HASH" 0 0 0
770 0 return _hashify_row( $result, $headers );
771 }
772 }
773  
774 0 ▶sub _reader_default_mode {
Subroutine _reader_default_mode: not called CC=2 SCAR=17.9
775 0 my ( $self ) = @_;
776 0 ▶ return $self->slots->{_row_mode} // 'array';
Condition0%0/2
$self->slots->{"_row_mode"} // "array"
l!l
00
MC/DC0%0/1
$self->slots->{"_row_mode"} // "array"
$self->slots->{"_row_mode"}
Truth table0%0/2
A
A= $self->slots->{"_row_mode"}
Aresult
11
00
777 }
778  
779 0 ▶sub _reader_to_iterator {
Subroutine _reader_to_iterator: not called CC=1 SCAR=6.9
780 0 my ( $runtime, $self ) = @_;
781  
782 return native_function(
783 name => 'iterator',
784 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
785 0 ▶ my $row = _reader_default_mode($self) eq 'dict'
Branch: 1 branch
BranchTrueFalseTotal
_reader_default_mode($self) eq 'dict' ? : 0 0 0
786 ? _reader_fetch_dict( $runtime, $self )
787 : _reader_fetch_array( $runtime, $self );
788  
789 0 ▶ if ( !defined $row ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row) 0 0 0
790 my $exhausted = $runtime->_instantiate_builtin_object(
791 $runtime->{_builtin_classes}{ExhaustedException},
792 {
793 0 message => 'iterator exhausted',
794 file => '<std/data/csv>',
795 line => 0,
796 },
797 );
798 0 die {
799 _zuzu_throw => 1,
800 value => $exhausted,
801 };
802 }
803  
804 0 return perl_to_zuzu( $row );
805 },
806 0 );
807 }
808  
809 0 ▶sub _quote_identifier {
Subroutine _quote_identifier: not called CC=2 SCAR=17.9
810 0 my ( $name ) = @_;
811 0 ▶ $name = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
812 0 $name =~ s/"/""/g;
813 0 return qq("$name");
814 }
815  
816 0 ▶sub _table_column_names {
Subroutine _table_column_names: not called CC=3 SCAR=24.8
817 0 my ( $dbh, $table ) = @_;
818  
819 my $sth = _dbi_call(
820 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
821 0 return $dbh->prepare(
822 'select * from ' . _quote_identifier($table) . ' where 1 = 0'
823 );
824 },
825 0 'prepare table metadata query failed',
826 );
827 _dbi_call(
828 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
829 0 $sth->execute;
830 0 return 1;
831 },
832 0 'execute table metadata query failed',
833 );
834  
835 0 ▶ my $names = $sth->{NAME} // [];
Condition0%0/2
$sth->{"NAME"} // []
l!l
00
MC/DC0%0/1
$sth->{"NAME"} // []
$sth->{"NAME"}
Truth table0%0/2
A
A= $sth->{"NAME"}
Aresult
11
00
836 0 ▶ return [ map { defined $_ ? "$_" : '' } @{ $names } ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
837 }
838  
839 0 ▶sub _column_type_for_index {
Subroutine _column_type_for_index: not called CC=6 SCAR=37.4
840 0 my ( $column_types, $columns, $index ) = @_;
841  
842 0 ▶ return 'TEXT' if !defined $column_types;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $column_types 0 0 0
843 0 ▶ if ( ref($column_types) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $column_types eq "ARRAY") 0 0 0
844 0 ▶ return defined $column_types->[$index] ? "$column_types->[$index]" : 'TEXT';
Branch: 1 branch
BranchTrueFalseTotal
defined $column_types->[$index] ? : 0 0 0
845 }
846 0 ▶ if ( ref($column_types) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $column_types eq "HASH") 0 0 0
847 0 my $name = $columns->[$index];
848 0 ▶ return defined $column_types->{$name} ? "$column_types->{$name}" : 'TEXT';
Branch: 1 branch
BranchTrueFalseTotal
defined $column_types->{$name} ? : 0 0 0
849 }
850 0 return 'TEXT';
851 }
852  
853 0 ▶sub _create_table_if_needed {
Subroutine _create_table_if_needed: not called CC=6 SCAR=37.4
854 0 my ( $dbh, $table, $columns, $options ) = @_;
855  
856 0 ▶ return if !zuzu_bool( $options->{create_table}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless zuzu_bool($options->{"create_table"}, 0) 0 0 0
857  
858 my $if_exists = exists $options->{if_exists}
859 0 ▶ ? lc( "$options->{if_exists}" )
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'if_exists'} ? : 0 0 0
860 : 'append';
861  
862 0 ▶ if ( $if_exists eq 'replace' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($if_exists eq "replace") 0 0 0
863 _dbi_call(
864 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
865 0 $dbh->do( 'drop table if exists ' . _quote_identifier($table) );
866 0 return 1;
867 },
868 0 'drop table failed',
869 );
870 }
871  
872 0 my $column_types = $options->{column_types};
873 0 my @defs;
874 0 for my $i ( 0 .. $#{ $columns } ) {
875 0 push @defs, join ' ',
876 _quote_identifier( $columns->[$i] ),
877 _column_type_for_index( $column_types, $columns, $i );
878 }
879  
880 0 my $create_sql = 'create table ';
881 0 ▶ $create_sql .= 'if not exists ' if $if_exists eq 'append';
Branch: 1 branch
BranchTrueFalseTotal
if $if_exists eq "append" 0 0 0
882 0 $create_sql .= _quote_identifier($table) . ' (' . join( ', ', @defs ) . ')';
883  
884 _dbi_call(
885 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
886 0 $dbh->do( $create_sql );
887 0 return 1;
888 },
889 0 'create table failed',
890 );
891 }
892  
893 0 ▶sub _output_columns_from_rows {
Subroutine _output_columns_from_rows: not called CC=9 SCAR=45.0
894 0 my ( $rows, $config ) = @_;
895  
896 0 my $columns = _columns_from_config($config);
897 0 ▶ return $columns if ref($columns) eq 'ARRAY' && @{$columns};
Branch: 1 branch
BranchTrueFalseTotal
if ref $columns eq "ARRAY" and @{$columns;} 0 0 0
Condition0%0/3
ref $columns eq "ARRAY" and @{$columns;}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $columns eq "ARRAY" and @{$columns;}
ref $columns eq "ARRAY" @{$columns;}
Truth table0%0/3
A and B
A= ref $columns eq "ARRAY"B= @{$columns;}
ABresult
0X0
100
111
898  
899 0 ▶ for my $row ( @{ $rows // [] } ) {
Condition0%0/2
$rows // []
l!l
00
MC/DC0%0/1
$rows // []
$rows
Truth table0%0/2
A
A= $rows
Aresult
11
00
900 0 ▶ if ( ref($row) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq "HASH") 0 0 0
901 0 my @keys = CORE::keys %{$row};
902 0 ▶ @keys = sort @keys if zuzu_bool( $config->{sort_columns}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($config->{"sort_columns"}, 0) 0 0 0
903 0 return \@keys;
904 }
905 0 ▶ if ( ref($row) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq "ARRAY") 0 0 0
906 return _default_columns_for_row($row)
907 0 ▶ if zuzu_bool( $config->{headers}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($config->{"headers"}, 0) 0 0 0
908 0 return undef;
909 }
910 }
911  
912 0 return undef;
913 }
914  
915 0 ▶sub _map_output_row {
Subroutine _map_output_row: not called CC=10 SCAR=47.0
916 0 my ( $runtime, $row, $columns, $config ) = @_;
917  
918 0 my $formatters = _hash_from_value( $config->{formatters} );
919 0 my @out;
920 0 ▶ my $source = ref($row) eq 'HASH' ? $row : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'HASH' ? : 0 0 0
921 0 ▶ my $array = ref($row) eq 'ARRAY' ? $row : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'ARRAY' ? : 0 0 0
922  
923 0 ▶ if ( ref($source) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source eq "HASH") 0 0 0
924 0 ▶ for my $i ( 0 .. $#{ $columns // [] } ) {
Condition0%0/2
$columns // []
l!l
00
MC/DC0%0/1
$columns // []
$columns
Truth table0%0/2
A
A= $columns
Aresult
11
00
925 0 my $key = $columns->[$i];
926 0 my $value = $source->{$key};
927 0 ▶ if ( exists $formatters->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $formatters->{$key}) 0 0 0
928 $value = _invoke_hook(
929 $runtime,
930 0 $formatters->{$key},
931 $value,
932 { column => $key, index => $i },
933 );
934 }
935 0 push @out, $value;
936 }
937 0 return \@out;
938 }
939  
940 0 ▶ if ( ref($array) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $array eq "ARRAY") 0 0 0
941 0 for my $i ( 0 .. $#{ $array } ) {
942 0 my $value = $array->[$i];
943 0 ▶ if ( exists $formatters->{$i} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $formatters->{$i}) 0 0 0
944 $value = _invoke_hook(
945 $runtime,
946 0 $formatters->{$i},
947 $value,
948 { index => $i },
949 );
950 }
951 0 push @out, $value;
952 }
953 0 return \@out;
954 }
955  
956 0 return [ $row ];
957 }
958  
959 0 ▶sub _write_rows {
Subroutine _write_rows: not called CC=10 SCAR=47.0
960 0 my ( $runtime, $csv, $fh, $rows, $options ) = @_;
961  
962 0 ▶ my $headers = zuzu_bool( $options->{headers}, 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($options->{'headers'}, 0) ? : 0 0 0
963 0 my $columns = _output_columns_from_rows( $rows, $options );
964 0 ▶ my $wrote_header = zuzu_bool( $options->{append}, 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($options->{'append'}, 0) ? : 0 0 0
965  
966 0 ▶ if ( !$wrote_header and $headers and ref($columns) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $wrote_header and $headers and ref $columns eq "ARRAY") 0 0 0
Condition0%0/6
not $wrote_header and $headers and ref $columns eq "ARRAY"
!ll&&!rl&&r
000
not $wrote_header and $headers
!ll&&!rl&&r
000
MC/DC0%0/3
not $wrote_header and $headers and ref $columns eq "ARRAY"
not $wrote_header $headers ref $columns eq "ARRAY"
Truth table0%0/4
not(A) and B and C
A= not $wrote_headerB= $headersC= ref $columns eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
967 0 ▶ $csv->print( $fh, $columns )
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, $columns) or _die_csv_error($csv, "CSV write header failed", scalar {}) 0 0 0
968 or _die_csv_error( $csv, 'CSV write header failed', {} );
969 0 $wrote_header = 1;
970 }
971  
972 0 ▶ for my $row ( @{ $rows // [] } ) {
Condition0%0/2
$rows // []
l!l
00
MC/DC0%0/1
$rows // []
$rows
Truth table0%0/2
A
A= $rows
Aresult
11
00
973 0 my $array = _map_output_row( $runtime, $row, $columns, $options );
974 0 ▶ $csv->print( $fh, $array )
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, $array) or _die_csv_error($csv, "CSV write row failed", scalar {}) 0 0 0
975 or _die_csv_error( $csv, 'CSV write row failed', {} );
976 }
977 }
978  
979 0 ▶sub _new_reader_object {
Subroutine _new_reader_object: not called CC=1 SCAR=6.9
980 0 my ( $class_obj, $csv, $fh, $headers, $row_mode, $config ) = @_;
981  
982 0 return native_object(
983 class => $class_obj,
984 slots => {
985 _csv => $csv,
986 _fh => $fh,
987 _headers => $headers,
988 _row_mode => $row_mode,
989 _config => $config,
990 _closed => 0,
991 _line_number => 0,
992 _row_number => 0,
993 _last_raw_line => undef,
994 _errors => [],
995 },
996 const => {
997 _csv => 1,
998 _fh => 1,
999 _headers => 0,
1000 _row_mode => 0,
1001 _config => 0,
1002 _closed => 0,
1003 _line_number => 0,
1004 _row_number => 0,
1005 _last_raw_line => 0,
1006 _errors => 0,
1007 },
1008 );
1009 }
1010  
1011 0 ▶sub _new_writer_object {
Subroutine _new_writer_object: not called CC=2 SCAR=17.9
1012 0 my ( $class_obj, $csv, $fh, $config ) = @_;
1013  
1014 0 my $columns = _columns_from_config($config);
1015 return native_object(
1016 class => $class_obj,
1017 slots => {
1018 _csv => $csv,
1019 _fh => $fh,
1020 _config => $config,
1021 _columns => $columns,
1022 0 ▶ _wrote_header => zuzu_bool( $config->{append}, 0 ) ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($config->{'append'}, 0) ? : 0 0 0
1023 _closed => 0,
1024 _row_number => 0,
1025 },
1026 const => {
1027 _csv => 1,
1028 _fh => 1,
1029 _config => 0,
1030 _columns => 0,
1031 _wrote_header => 0,
1032 _closed => 0,
1033 _row_number => 0,
1034 },
1035 );
1036 }
1037  
1038 0 ▶sub _writer_columns {
Subroutine _writer_columns: not called CC=8 SCAR=42.8
1039 0 my ( $self, $row ) = @_;
1040  
1041 0 my $columns = $self->slots->{_columns};
1042 0 ▶ return $columns if ref($columns) eq 'ARRAY' && @{$columns};
Branch: 1 branch
BranchTrueFalseTotal
if ref $columns eq "ARRAY" and @{$columns;} 0 0 0
Condition0%0/3
ref $columns eq "ARRAY" and @{$columns;}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $columns eq "ARRAY" and @{$columns;}
ref $columns eq "ARRAY" @{$columns;}
Truth table0%0/3
A and B
A= ref $columns eq "ARRAY"B= @{$columns;}
ABresult
0X0
100
111
1043  
1044 0 ▶ if ( ref($row) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $row eq 'HASH') { } 0 0 0
elsif (ref $row eq 'ARRAY' and zuzu_bool($self->slots->{'_config'}{'headers'}, 0)) { } 0 0 0
1045 0 my @keys = CORE::keys %{$row};
1046 0 ▶ @keys = sort @keys if zuzu_bool( $self->slots->{_config}{sort_columns}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($self->slots->{"_config"}{"sort_columns"}, 0) 0 0 0
1047 0 $columns = \@keys;
1048 }
1049 ▶ elsif ( ref($row) eq 'ARRAY' and zuzu_bool( $self->slots->{_config}{headers}, 0 ) ) {
Condition0%0/3
ref $row eq "ARRAY" and zuzu_bool($self->slots->{"_config"}{"headers"}, 0)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $row eq "ARRAY" and zuzu_bool($self->slots->{"_config"}{"headers"}, 0)
ref $row eq "ARRAY" zuzu_bool($self->slots->{"_config"}{"headers"}, 0)
Truth table0%0/3
A and B
A= ref $row eq "ARRAY"B= zuzu_bool($self->slots->{"_config"}{"headers"}, 0)
ABresult
0X0
100
111
1050 0 $columns = _default_columns_for_row($row);
1051 }
1052  
1053 0 ▶ $self->slots->{_columns} = $columns if ref($columns) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $columns eq "ARRAY" 0 0 0
1054 0 return $columns;
1055 }
1056  
1057 0 ▶sub _writer_maybe_header {
Subroutine _writer_maybe_header: not called CC=5 SCAR=34.0
1058 0 my ( $runtime, $self, $columns ) = @_;
1059  
1060 0 ▶ return if $self->slots->{_wrote_header};
Branch: 1 branch
BranchTrueFalseTotal
if $self->slots->{"_wrote_header"} 0 0 0
1061 0 ▶ return if !zuzu_bool( $self->slots->{_config}{headers}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless zuzu_bool($self->slots->{"_config"}{"headers"}, 0) 0 0 0
1062 0 ▶ return if ref($columns) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $columns ne "ARRAY" 0 0 0
1063  
1064 $self->slots->{_csv}->print( $self->slots->{_fh}, $columns )
1065 0 ▶ or _die_csv_error( $self->slots->{_csv}, 'CSV write header failed', {} );
Branch: 1 branch
BranchTrueFalseTotal
$self->slots->{"_csv"}->print($self->slots->{"_fh"}, $columns) or _die_csv_error($self->slots->{"_csv"}, "CSV write header failed", scalar {}) 0 0 0
1066 0 $self->slots->{_wrote_header} = 1;
1067 }
1068  
1069 0 ▶sub _writer_write_row {
Subroutine _writer_write_row: not called CC=4 SCAR=30.0
1070 0 my ( $runtime, $self, $row ) = @_;
1071  
1072 die Zuzu::Error->new_runtime(
1073 message => 'CSVWriter is closed',
1074 file => '<std/data/csv>',
1075 line => 0,
1076 0 ▶ ) if zuzu_bool( $self->slots->{_closed}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($self->slots->{"_closed"}, 0) 0 0 0
1077  
1078 0 my $columns = _writer_columns( $self, $row );
1079 0 _writer_maybe_header( $runtime, $self, $columns );
1080  
1081 0 my $array = _map_output_row( $runtime, $row, $columns, $self->slots->{_config} );
1082 $self->slots->{_csv}->print( $self->slots->{_fh}, $array )
1083 0 ▶ or _die_csv_error( $self->slots->{_csv}, 'CSV write row failed', {} );
Branch: 1 branch
BranchTrueFalseTotal
$self->slots->{"_csv"}->print($self->slots->{"_fh"}, $array) or _die_csv_error($self->slots->{"_csv"}, "CSV write row failed", scalar {}) 0 0 0
1084  
1085 0 ▶ $self->slots->{_row_number} = ( $self->slots->{_row_number} // 0 ) + 1;
Condition0%0/2
$self->slots->{"_row_number"} // 0
l!l
00
MC/DC0%0/1
$self->slots->{"_row_number"} // 0
$self->slots->{"_row_number"}
Truth table0%0/2
A
A= $self->slots->{"_row_number"}
Aresult
11
00
1086 0 return perl_to_zuzu( $array );
1087 }
1088  
1089 0 ▶sub _sniff_counts {
Subroutine _sniff_counts: not called CC=3 SCAR=24.8
1090 0 my ( $text, $char ) = @_;
1091 0 my @lines = grep { $_ ne '' } split /\r?\n/, $text;
1092 0 my $count = 0;
1093 0 ▶ for my $line ( @lines[ 0 .. ( $#lines > 4 ? 4 : $#lines ) ] ) {
Branch: 1 branch
BranchTrueFalseTotal
$#lines > 4 ? : 0 0 0
1094 0 my $c = () = $line =~ /\Q$char\E/g;
1095 0 $count += $c;
1096 }
1097 0 return $count;
1098 }
1099  
1100 0 ▶sub _sniff_text {
Subroutine _sniff_text: not called CC=9 SCAR=45.0
1101 0 my ( $text, $config ) = @_;
1102  
1103 0 my @candidates = @{ _array_from_value( $config->{sniff_candidates} ) };
1104 0 ▶ @candidates = ( ',', "\t", ';', '|' ) if !@candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 0 0 0
1105 0 my $best = $candidates[0];
1106 0 my $best_score = -1;
1107 0 for my $char ( @candidates ) {
1108 0 my $score = _sniff_counts( $text, $char );
1109 0 ▶ if ( $score > $best_score ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score > $best_score) 0 0 0
1110 0 $best_score = $score;
1111 0 $best = $char;
1112 }
1113 }
1114  
1115 0 my @lines = grep { $_ !~ /\A\s*\z/ } split /\r?\n/, $text;
1116 0 ▶ my $first = $lines[0] // '';
Condition0%0/2
$lines[0] // ""
l!l
00
MC/DC0%0/1
$lines[0] // ""
$lines[0]
Truth table0%0/2
A
A= $lines[0]
Aresult
11
00
1117 0 my $headers = 0;
1118 0 ▶ if ( $first ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first ne "") 0 0 0
1119 0 my @parts = split /\Q$best\E/, $first;
1120 0 ▶ $headers = 1 if @parts && !grep { /\A-?(?:\d+(?:\.\d+)?)\z/ } @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts and not grep({/\A-?(?:\d+(?:\.\d+)?)\z/;} @parts) 0 0 0
Condition0%0/3
@parts and not grep({/\A-?(?:\d+(?:\.\d+)?)\z/;} @parts)
!ll&&!rl&&r
000
MC/DC0%0/2
@parts and not grep({/\A-?(?:\d+(?:\.\d+)?)\z/;} @parts)
@parts not grep({/\A-?(?:\d+(?:\.\d+)?)\z/;} @parts)
Truth table0%0/3
A and not(B)
A= @partsB= not grep({/\A-?(?:\d+(?:\.\d+)?)\z/;} @parts)
ABresult
0X0
100
111
1121 }
1122  
1123 return {
1124 0 ▶ sep_char => $best,
Branch: 1 branch
BranchTrueFalseTotal
$headers ? : 0 0 0
1125 headers => $headers ? 1 : 0,
1126 quote_char => '"',
1127 };
1128 }
1129  
1130 0 ▶sub _conflict_clause {
Subroutine _conflict_clause: not called CC=4 SCAR=30.0
1131 0 my ( $config ) = @_;
1132  
1133 my $policy = exists $config->{conflict}
1134 0 ▶ ? lc( "$config->{conflict}" )
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'conflict'} ? : 0 0 0
1135 : '';
1136 0 ▶ return '' if $policy eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $policy eq "" 0 0 0
1137 0 ▶ return '' if $policy eq 'default';
Branch: 1 branch
BranchTrueFalseTotal
if $policy eq "default" 0 0 0
1138 0 return "or " . uc($policy);
1139 }
1140  
1141 0 ▶sub _mapped_columns {
Subroutine _mapped_columns: not called CC=4 SCAR=30.0
1142 0 my ( $columns, $map ) = @_;
1143  
1144 0 ▶ return $columns if ref($map) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $map ne "HASH" 0 0 0
1145 my @out = map {
1146 0 ▶ exists $map->{$_} ? $map->{$_} : $_
Branch: 1 branch
BranchTrueFalseTotal
exists $map->{$_} ? : 0 0 0
1147 0 ▶ } @{ $columns // [] };
Condition0%0/2
$columns // []
l!l
00
MC/DC0%0/1
$columns // []
$columns
Truth table0%0/2
A
A= $columns
Aresult
11
00
1148 0 return \@out;
1149 }
1150  
1151 0 ▶sub _transaction_begin {
Subroutine _transaction_begin: not called CC=2 SCAR=17.9
1152 0 my ( $dbh, $enabled ) = @_;
1153 0 ▶ return if !$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 0 0 0
1154 _dbi_call(
1155 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1156 0 $dbh->begin_work;
1157 0 return 1;
1158 },
1159 0 'CSV transaction begin failed',
1160 );
1161 }
1162  
1163 0 ▶sub _transaction_commit {
Subroutine _transaction_commit: not called CC=2 SCAR=17.9
1164 0 my ( $dbh, $enabled ) = @_;
1165 0 ▶ return if !$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 0 0 0
1166 _dbi_call(
1167 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1168 0 $dbh->commit;
1169 0 return 1;
1170 },
1171 0 'CSV transaction commit failed',
1172 );
1173 }
1174  
1175 0 ▶sub _transaction_rollback {
Subroutine _transaction_rollback: not called CC=2 SCAR=17.9
1176 0 my ( $dbh, $enabled ) = @_;
1177 0 ▶ return if !$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 0 0 0
1178 0 eval { $dbh->rollback; 1 };
1179 }
1180  
1181 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=5.9
Pod undocumented
1182 2 my ( $class, $runtime ) = @_;
1183  
1184 2 my $csv_class = native_class( name => 'CSV' );
1185 2 my $reader_class = native_class( name => 'CSVReader' );
1186 2 my $writer_class = native_class( name => 'CSVWriter' );
1187  
1188 0 ▶ $csv_class->native_constructor( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1189 0 my ( $rt, $klass, $positional, $named ) = @_;
1190 0 my $config = _normalize_config( $positional, $named );
1191  
1192 0 return native_object(
1193 class => $klass,
1194 slots => {
1195 _config => $config,
1196 },
1197 const => {
1198 _config => 1,
1199 },
1200 );
1201 2 } );
1202  
1203 $csv_class->methods->{sniff} = native_function(
1204 name => 'sniff',
1205 0 ▶ native => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
1206 0 my ( $self, $source ) = @_;
1207 0 ▶ my $config = _normalize_runtime_config( $self->slots->{_config} // {} );
Condition0%0/2
$self->slots->{"_config"} // {}
l!l
00
MC/DC0%0/1
$self->slots->{"_config"} // {}
$self->slots->{"_config"}
Truth table0%0/2
A
A= $self->slots->{"_config"}
Aresult
11
00
1208 0 my $text;
1209 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($source) and $source->isa('Zuzu::Value::Object') and exists $source->slots->{'_path_tiny'}) { } 0 0 0
Condition0%0/6
builtin::blessed($source) and $source->isa("Zuzu::Value::Object") and exists $source->slots->{"_path_tiny"}
!ll&&!rl&&r
000
builtin::blessed($source) and $source->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($source) and $source->isa("Zuzu::Value::Object") and exists $source->slots->{"_path_tiny"}
builtin::blessed($source) $source->isa("Zuzu::Value::Object") exists $source->slots->{"_path_tiny"}
Truth table0%0/4
A and B and C
A= builtin::blessed($source)B= $source->isa("Zuzu::Value::Object")C= exists $source->slots->{"_path_tiny"}
ABCresult
0XX0
10X0
1100
1111
1210 blessed($source)
1211 and $source->isa('Zuzu::Value::Object')
1212 and exists $source->slots->{_path_tiny}
1213 ) {
1214 0 $runtime->assert_capability( 'fs', "CSV.sniff is denied by runtime policy" );
1215 $text = _text_from_bytes(
1216 $source->slots->{_path_tiny}->slurp_raw,
1217 0 $config,
1218 );
1219 }
1220 else {
1221 0 ▶ $text = defined $source ? "$source" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $source ? : 0 0 0
1222 }
1223 0 return perl_to_zuzu( _sniff_text( $text, $config ) );
1224 },
1225 2 );
1226  
1227 $csv_class->methods->{sniff_binarystring} = native_function(
1228 name => 'sniff_binarystring',
1229 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1230 0 my ( $self, $source ) = @_;
1231 0 ▶ my $config = _normalize_runtime_config( $self->slots->{_config} // {} );
Condition0%0/2
$self->slots->{"_config"} // {}
l!l
00
MC/DC0%0/1
$self->slots->{"_config"} // {}
$self->slots->{"_config"}
Truth table0%0/2
A
A= $self->slots->{"_config"}
Aresult
11
00
1232 0 $source = _assert_binary_string( $source, 'CSV.sniff_binarystring' );
1233 0 return perl_to_zuzu(
1234 _sniff_text( _text_from_bytes( $source->bytes, $config ), $config )
1235 );
1236 },
1237 2 );
1238  
1239 $csv_class->methods->{transpose} = native_function(
1240 name => 'transpose',
1241 0 ▶ native => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
1242 0 my ( $self, $rows ) = @_;
1243 0 my $perl_rows = zuzu_to_perl( $rows );
1244 0 ▶ $perl_rows = [] if ref($perl_rows) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $perl_rows ne "ARRAY" 0 0 0
1245 0 my $max = 0;
1246 0 for my $row ( @{$perl_rows} ) {
1247 0 ▶ my $len = ref($row) eq 'ARRAY' ? scalar @{$row} : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'ARRAY' ? : 0 0 0
1248 0 ▶ $max = $len if $len > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $max 0 0 0
1249 }
1250 0 my @out;
1251 0 ▶ for my $i ( 0 .. ( $max ? $max - 1 : -1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
$max ? : 0 0 0
1252 0 my @next;
1253 0 for my $row ( @{$perl_rows} ) {
1254 0 ▶ if ( ref($row) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq 'ARRAY') { } 0 0 0
1255 0 push @next, $row->[$i];
1256 }
1257 else {
1258 0 ▶ push @next, ( $i == 0 ? $row : undef );
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 0 0 0
1259 }
1260 }
1261 0 push @out, \@next;
1262 }
1263 0 return perl_to_zuzu( \@out );
1264 },
1265 2 );
1266  
1267 $csv_class->methods->{decode} = native_function(
1268 name => 'decode',
1269 0 ▶ native => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
1270 0 my ( $self, $text ) = @_;
1271 0 my $config = _normalize_runtime_config( _config_merge( $self->slots->{_config} ) );
1272 0 my $csv = _new_csv_backend( $config );
1273 0 my $fh = _string_input_handle( $text );
1274 0 my $headers = _columns_from_config($config);
1275  
1276 0 ▶ if ( !defined $headers and zuzu_bool( $config->{headers}, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $headers and zuzu_bool($config->{"headers"}, 0)) 0 0 0
Condition0%0/3
not defined $headers and zuzu_bool($config->{"headers"}, 0)
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $headers and zuzu_bool($config->{"headers"}, 0)
not defined $headers zuzu_bool($config->{"headers"}, 0)
Truth table0%0/3
not(A) and B
A= not defined $headersB= zuzu_bool($config->{"headers"}, 0)
ABresult
0X0
100
111
1277 0 my $header_row = $csv->getline($fh);
1278 0 ▶ _die_csv_error( $csv, 'CSV header read failed', { line_number => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
unless $header_row or $csv->eof 0 0 0
Condition0%0/3
$header_row or $csv->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
$header_row or $csv->eof
$header_row $csv->eof
Truth table0%0/3
A or B
A= $header_rowB= $csv->eof
ABresult
1X1
011
000
1279 if !$header_row and !$csv->eof;
1280 0 $headers = _normalize_headers( $header_row, $config );
1281 }
1282 0 ▶ _required_columns_check( $headers, $config ) if ref($headers) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers eq "ARRAY" 0 0 0
1283  
1284 0 ▶ my $reader = _new_reader_object(
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'ARRAY' ? : 0 0 0
1285 $reader_class,
1286 $csv,
1287 $fh,
1288 $headers,
1289 ref($headers) eq 'ARRAY' ? 'dict' : 'array',
1290 $config,
1291 );
1292  
1293 0 my @rows;
1294 0 while (1) {
1295 0 my $row;
1296 0 my $ok = eval {
1297 0 ▶ $row = _reader_default_mode($reader) eq 'dict'
Branch: 1 branch
BranchTrueFalseTotal
_reader_default_mode($reader) eq 'dict' ? : 0 0 0
1298 ? _reader_fetch_dict( $runtime, $reader )
1299 : _reader_fetch_array( $runtime, $reader );
1300 0 1;
1301 };
1302 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
1303 0 my $err = $@;
1304 0 $err = "$err";
1305 0 chomp $err;
1306 0 push @{ $reader->slots->{_errors} }, {
1307 message => $err,
1308 line_number => $reader->slots->{_line_number},
1309 row_number => $reader->slots->{_row_number},
1310 raw_line => $reader->slots->{_last_raw_line},
1311 0 };
1312 0 last;
1313 }
1314 0 ▶ last if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
1315 0 push @rows, $row;
1316 }
1317 0 close $fh;
1318 0 return perl_to_zuzu( \@rows );
1319 },
1320 2 );
1321  
1322 $csv_class->methods->{decode_binarystring} = native_function(
1323 name => 'decode_binarystring',
1324 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1325 0 my ( $self, $raw ) = @_;
1326 my $config = _normalize_runtime_config(
1327 _config_merge( $self->slots->{_config} )
1328 0 );
1329 0 $raw = _assert_binary_string( $raw, 'CSV.decode_binarystring' );
1330 0 my $text = _text_from_bytes( $raw->bytes, $config );
1331 0 return $csv_class->methods->{decode}->{_native}->( $self, $text );
1332 },
1333 2 );
1334  
1335 $csv_class->methods->{decode_report} = native_function(
1336 name => 'decode_report',
1337 0 ▶ native => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
1338 0 my ( $self, $text ) = @_;
1339 my $config = _normalize_runtime_config(
1340 0 _config_merge( $self->slots->{_config}, { on_error => 'collect' } )
1341 );
1342 0 my $csv = _new_csv_backend( $config );
1343 0 my $fh = _string_input_handle( $text );
1344 0 my $headers = _columns_from_config($config);
1345  
1346 0 ▶ if ( !defined $headers and zuzu_bool( $config->{headers}, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $headers and zuzu_bool($config->{"headers"}, 0)) 0 0 0
Condition0%0/3
not defined $headers and zuzu_bool($config->{"headers"}, 0)
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $headers and zuzu_bool($config->{"headers"}, 0)
not defined $headers zuzu_bool($config->{"headers"}, 0)
Truth table0%0/3
not(A) and B
A= not defined $headersB= zuzu_bool($config->{"headers"}, 0)
ABresult
0X0
100
111
1347 0 my $header_row = $csv->getline($fh);
1348 0 ▶ if ( !$header_row and !$csv->eof ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_row or $csv->eof) 0 0 0
Condition0%0/3
$header_row or $csv->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
$header_row or $csv->eof
$header_row $csv->eof
Truth table0%0/3
A or B
A= $header_rowB= $csv->eof
ABresult
1X1
011
000
1349 0 my $detail = _csv_error_detail( $csv, 'CSV header read failed', { line_number => 1 } );
1350 0 close $fh;
1351 0 return perl_to_zuzu( { rows => [], errors => [ $detail ] } );
1352 }
1353 0 $headers = _normalize_headers( $header_row, $config );
1354 }
1355  
1356 0 ▶ my $reader = _new_reader_object(
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'ARRAY' ? : 0 0 0
1357 $reader_class,
1358 $csv,
1359 $fh,
1360 $headers,
1361 ref($headers) eq 'ARRAY' ? 'dict' : 'array',
1362 $config,
1363 );
1364  
1365 0 my @rows;
1366 0 while (1) {
1367 0 my $row;
1368 0 my $ok = eval {
1369 0 ▶ $row = _reader_default_mode($reader) eq 'dict'
Branch: 1 branch
BranchTrueFalseTotal
_reader_default_mode($reader) eq 'dict' ? : 0 0 0
1370 ? _reader_fetch_dict( $runtime, $reader )
1371 : _reader_fetch_array( $runtime, $reader );
1372 0 1;
1373 };
1374 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
1375 0 my $err = $@;
1376 0 $err = "$err";
1377 0 chomp $err;
1378 0 push @{ $reader->slots->{_errors} }, {
1379 message => $err,
1380 line_number => $reader->slots->{_line_number},
1381 row_number => $reader->slots->{_row_number},
1382 raw_line => $reader->slots->{_last_raw_line},
1383 0 };
1384 0 last;
1385 }
1386 0 ▶ last if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
1387 0 push @rows, $row;
1388 }
1389 0 close $fh;
1390 return perl_to_zuzu(
1391 {
1392 rows => \@rows,
1393 errors => $reader->slots->{_errors},
1394 }
1395 0 );
1396 },
1397 2 );
1398  
1399 $csv_class->methods->{decode_report_binarystring} = native_function(
1400 name => 'decode_report_binarystring',
1401 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1402 0 my ( $self, $raw ) = @_;
1403 my $config = _normalize_runtime_config(
1404 0 _config_merge( $self->slots->{_config}, { on_error => 'collect' } )
1405 );
1406 0 $raw = _assert_binary_string(
1407 $raw,
1408 'CSV.decode_report_binarystring',
1409 );
1410 0 my $text = _text_from_bytes( $raw->bytes, $config );
1411 0 my $temp = native_object(
1412 class => $self->class,
1413 slots => { _config => $config },
1414 const => { _config => 1 },
1415 );
1416 0 return $csv_class->methods->{decode_report}->{_native}->(
1417 $temp,
1418 $text,
1419 );
1420 },
1421 2 );
1422  
1423 $csv_class->methods->{encode} = native_function(
1424 name => 'encode',
1425 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1426 0 my ( $self, $rows ) = @_;
1427 0 my $config = _normalize_runtime_config( _config_merge( $self->slots->{_config} ) );
1428 0 my $perl_rows = zuzu_to_perl( $rows );
1429 0 ▶ $perl_rows = [] if ref($perl_rows) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $perl_rows ne "ARRAY" 0 0 0
1430 0 my $csv = _new_csv_backend( $config );
1431 0 my ( $fh, $out_ref ) = _string_output_handle();
1432 0 _write_rows( $runtime, $csv, $fh, $perl_rows, $config );
1433 0 close $fh;
1434 0 return $$out_ref;
1435 },
1436 2 );
1437  
1438 $csv_class->methods->{encode_binarystring} = native_function(
1439 name => 'encode_binarystring',
1440 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1441 0 my ( $self, @args ) = @_;
1442 my $config = _normalize_runtime_config(
1443 _config_merge( $self->slots->{_config} )
1444 0 );
1445 0 my $text = $csv_class->methods->{encode}->{_native}->(
1446 $self,
1447 @args,
1448 );
1449 0 return Zuzu::Value::BinaryString->new(
1450 bytes => _bytes_from_text( $text, $config ),
1451 );
1452 },
1453 2 );
1454  
1455 $csv_class->methods->{encode_row} = native_function(
1456 name => 'encode_row',
1457 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1458 0 my ( $self, $row ) = @_;
1459 my $config = _normalize_runtime_config(
1460 0 _config_merge( $self->slots->{_config}, { headers => 0 } )
1461 );
1462 0 my $csv = _new_csv_backend( $config );
1463 0 my ( $fh, $out_ref ) = _string_output_handle();
1464 0 my $array = _map_output_row(
1465 $runtime,
1466 zuzu_to_perl($row),
1467 _columns_from_config($config),
1468 $config,
1469 );
1470 0 ▶ $csv->print( $fh, $array )
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, $array) or _die_csv_error($csv, "CSV encode_row failed", scalar {}) 0 0 0
1471 or _die_csv_error( $csv, 'CSV encode_row failed', {} );
1472 0 close $fh;
1473 0 return $$out_ref;
1474 },
1475 2 );
1476  
1477 $csv_class->methods->{encode_row_binarystring} = native_function(
1478 name => 'encode_row_binarystring',
1479 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1480 0 my ( $self, @args ) = @_;
1481 my $config = _normalize_runtime_config(
1482 0 _config_merge( $self->slots->{_config}, { headers => 0 } )
1483 );
1484 0 my $text = $csv_class->methods->{encode_row}->{_native}->(
1485 $self,
1486 @args,
1487 );
1488 0 return Zuzu::Value::BinaryString->new(
1489 bytes => _bytes_from_text( $text, $config ),
1490 );
1491 },
1492 2 );
1493  
1494 $csv_class->methods->{load} = native_function(
1495 name => 'load',
1496 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1497 0 my ( $self, $path_obj ) = @_;
1498 0 $runtime->assert_capability( 'fs', "CSV.load is denied by runtime policy" );
1499 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'CSV.load' );
1500 0 my $source = Zuzu::Value::BinaryString->new(
1501 bytes => $path_tiny->slurp_raw,
1502 );
1503 0 return $csv_class->methods->{decode_binarystring}->{_native}->(
1504 $self,
1505 $source,
1506 );
1507 },
1508 2 );
1509  
1510 $csv_class->methods->{load_report} = native_function(
1511 name => 'load_report',
1512 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1513 0 my ( $self, $path_obj ) = @_;
1514 0 $runtime->assert_capability( 'fs', "CSV.load_report is denied by runtime policy" );
1515 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'CSV.load_report' );
1516 my $config = _normalize_runtime_config(
1517 0 _config_merge( $self->slots->{_config}, { on_error => 'collect' } )
1518 );
1519 0 my $source = Zuzu::Value::BinaryString->new(
1520 bytes => $path_tiny->slurp_raw,
1521 );
1522 0 my $temp = native_object(
1523 class => $self->class,
1524 slots => { _config => $config },
1525 const => { _config => 1 },
1526 );
1527 0 return $csv_class->methods->{decode_report_binarystring}->{_native}->(
1528 $temp,
1529 $source,
1530 );
1531 },
1532 2 );
1533  
1534 $csv_class->methods->{dump} = native_function(
1535 name => 'dump',
1536 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1537 0 my ( $self, $path_obj, $rows ) = @_;
1538 0 $runtime->assert_capability( 'fs', "CSV.dump is denied by runtime policy" );
1539 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'CSV.dump' );
1540 0 my $config = _normalize_runtime_config( _config_merge( $self->slots->{_config} ) );
1541 0 my $perl_rows = zuzu_to_perl( $rows );
1542 0 ▶ $perl_rows = [] if ref($perl_rows) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $perl_rows ne "ARRAY" 0 0 0
1543 0 my $csv = _new_csv_backend( $config );
1544 0 my $fh = _open_output_handle( $path_tiny, $config );
1545 0 _write_rows( $runtime, $csv, $fh, $perl_rows, $config );
1546 0 close $fh;
1547 0 return $path_obj;
1548 },
1549 2 );
1550  
1551 $csv_class->methods->{open} = native_function(
1552 name => 'open',
1553 0 ▶ native => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
1554 0 my ( $self, $path_obj ) = @_;
1555 0 $runtime->assert_capability( 'fs', "CSV.open is denied by runtime policy" );
1556 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'CSV.open' );
1557 0 my $config = _normalize_runtime_config( _config_merge( $self->slots->{_config} ) );
1558 0 my $csv = _new_csv_backend( $config );
1559 0 my $fh = _open_input_handle( $path_tiny, $config );
1560  
1561 0 ▶ my $skip = int( $config->{skip_lines} // 0 );
Condition0%0/2
$config->{"skip_lines"} // 0
l!l
00
MC/DC0%0/1
$config->{"skip_lines"} // 0
$config->{"skip_lines"}
Truth table0%0/2
A
A= $config->{"skip_lines"}
Aresult
11
00
1562 0 ▶ while ( $skip > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip > 0) 0 0 0
1563 0 <$fh>;
1564 0 $skip--;
1565 }
1566  
1567 0 my $headers = _columns_from_config($config);
1568 0 ▶ if ( !defined $headers and zuzu_bool( $config->{headers}, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $headers and zuzu_bool($config->{"headers"}, 0)) 0 0 0
Condition0%0/3
not defined $headers and zuzu_bool($config->{"headers"}, 0)
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $headers and zuzu_bool($config->{"headers"}, 0)
not defined $headers zuzu_bool($config->{"headers"}, 0)
Truth table0%0/3
not(A) and B
A= not defined $headersB= zuzu_bool($config->{"headers"}, 0)
ABresult
0X0
100
111
1569 0 my $header_row = $csv->getline($fh);
1570 0 ▶ _die_csv_error( $csv, 'CSV header read failed', { line_number => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
unless $header_row or $csv->eof 0 0 0
Condition0%0/3
$header_row or $csv->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
$header_row or $csv->eof
$header_row $csv->eof
Truth table0%0/3
A or B
A= $header_rowB= $csv->eof
ABresult
1X1
011
000
1571 if !$header_row and !$csv->eof;
1572 0 $headers = _normalize_headers( $header_row, $config );
1573 }
1574 0 ▶ _required_columns_check( $headers, $config ) if ref($headers) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers eq "ARRAY" 0 0 0
1575  
1576 0 ▶ return _new_reader_object(
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'ARRAY' ? : 0 0 0
1577 $reader_class,
1578 $csv,
1579 $fh,
1580 $headers,
1581 ref($headers) eq 'ARRAY' ? 'dict' : 'array',
1582 $config,
1583 );
1584 },
1585 2 );
1586  
1587 $csv_class->methods->{open_writer} = native_function(
1588 name => 'open_writer',
1589 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1590 0 my ( $self, $path_obj, $options ) = @_;
1591 0 $runtime->assert_capability( 'fs', "CSV.open_writer is denied by runtime policy" );
1592 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'CSV.open_writer' );
1593 my $config = _normalize_runtime_config(
1594 0 _config_merge( $self->slots->{_config}, _options_hash($options) )
1595 );
1596 0 my $csv = _new_csv_backend( $config );
1597 0 my $fh = _open_output_handle( $path_tiny, $config );
1598 0 return _new_writer_object( $writer_class, $csv, $fh, $config );
1599 },
1600 2 );
1601  
1602 $csv_class->methods->{dump_table} = native_function(
1603 name => 'dump_table',
1604 0 ▶ native => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
1605 0 my ( $self, $path_obj, $dbh_obj, $table_name, $options ) = @_;
1606 0 $runtime->assert_capability( 'fs', "CSV.dump_table is denied by runtime policy" );
1607 0 $runtime->assert_capability( 'db', "CSV.dump_table is denied by runtime policy" );
1608 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'CSV.dump_table' );
1609 0 my $dbh = _dbh_from_object( $dbh_obj, 'CSV.dump_table' );
1610 my $config = _normalize_runtime_config(
1611 0 _config_merge( $self->slots->{_config}, { headers => 1 }, _options_hash($options) )
1612 );
1613 my $query = exists $config->{query}
1614 ? $config->{query}
1615 0 ▶ : 'select * from ' . _quote_identifier( $table_name );
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'query'} ? : 0 0 0
1616 0 my $bind = _array_from_value( $config->{bind} );
1617  
1618 0 my $csv = _new_csv_backend( $config );
1619 0 my $fh = _open_output_handle( $path_tiny, $config );
1620 my $sth = _dbi_call(
1621 0 ▶ sub { $dbh->prepare( $query ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1622 0 'dump_table prepare failed',
1623 );
1624 _dbi_call(
1625 0 ▶ sub { $sth->execute( @{$bind} ); return 1; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1626 0 'dump_table execute failed',
1627 );
1628  
1629 0 ▶ my $raw_columns = [ @{ $sth->{NAME} // [] } ];
Condition0%0/2
$sth->{"NAME"} // []
l!l
00
MC/DC0%0/1
$sth->{"NAME"} // []
$sth->{"NAME"}
Truth table0%0/2
A
A= $sth->{"NAME"}
Aresult
11
00
1630 0 my $map = _hash_from_value( $config->{column_map} );
1631 my @out_columns = map {
1632 0 my $db_name = $_;
1633 0 my @hits = grep { $map->{$_} eq $db_name } CORE::keys %{$map};
1634 0 ▶ @hits ? $hits[0] : $db_name;
Branch: 1 branch
BranchTrueFalseTotal
@hits ? : 0 0 0
1635 0 } @{$raw_columns};
1636 $csv->print( $fh, \@out_columns )
1637 or _die_csv_error( $csv, 'CSV table header write failed', {} )
1638 0 ▶ if zuzu_bool( $config->{headers}, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if zuzu_bool($config->{"headers"}, 1) 0 0 0
Condition0%0/6
zuzu_bool($config->{'headers'}, 1) && ($csv->print($fh, \@out_columns) || _die_csv_error($csv, 'CSV table header write failed', scalar {}))
!ll&&!rl&&r
000
$csv->print($fh, \@out_columns) or _die_csv_error($csv, "CSV table header write failed", scalar {})
l!l&&r!l&&!r
000
MC/DC0%0/3
zuzu_bool($config->{'headers'}, 1) && ($csv->print($fh, \@out_columns) || _die_csv_error($csv, 'CSV table header write failed', scalar {}))
zuzu_bool($config->{'headers'}, 1) $csv->print($fh, \@out_columns) _die_csv_error($csv, "CSV table header write failed", scalar {})
Truth table0%0/4
A && B or C
A= zuzu_bool($config->{'headers'}, 1)B= $csv->print($fh, \@out_columns)C= _die_csv_error($csv, "CSV table header write failed", scalar {})
ABCresult
0XX0
1000
11X1
1011
1639  
1640 0 ▶ while ( my $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
1641 0 my %mapped;
1642 0 for my $key ( CORE::keys %{$row} ) {
1643 0 my @hits = grep { $map->{$_} eq $key } CORE::keys %{$map};
1644 0 ▶ $mapped{ @hits ? $hits[0] : $key } = $row->{$key};
Branch: 1 branch
BranchTrueFalseTotal
@hits ? : 0 0 0
1645 }
1646 0 my $array = _map_output_row(
1647 $runtime,
1648 \%mapped,
1649 \@out_columns,
1650 $config,
1651 );
1652 0 ▶ $csv->print( $fh, $array )
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, $array) or _die_csv_error($csv, "CSV table row write failed", scalar {}) 0 0 0
1653 or _die_csv_error( $csv, 'CSV table row write failed', {} );
1654 }
1655  
1656 0 close $fh;
1657 0 return $path_obj;
1658 },
1659 2 );
1660  
1661 $csv_class->methods->{dump_query} = native_function(
1662 name => 'dump_query',
1663 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1664 0 my ( $self, $path_obj, $dbh_obj, $sql, $bind, $options ) = @_;
1665 0 my $opt = _options_hash($options);
1666 0 ▶ $opt->{query} = defined $sql ? "$sql" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $sql ? : 0 0 0
1667 0 $opt->{bind} = _array_from_value($bind);
1668 0 return $csv_class->methods->{dump_table}->{_native}->(
1669 $self,
1670 $path_obj,
1671 $dbh_obj,
1672 '',
1673 $opt,
1674 );
1675 },
1676 2 );
1677  
1678 $csv_class->methods->{load_table} = native_function(
1679 name => 'load_table',
1680 0 ▶ native => sub {
Subroutine __ANON__: not called CC=24 SCAR=64.0
1681 0 my ( $self, $path_obj, $dbh_obj, $table_name, $options ) = @_;
1682 0 $runtime->assert_capability( 'fs', "CSV.load_table is denied by runtime policy" );
1683 0 $runtime->assert_capability( 'db', "CSV.load_table is denied by runtime policy" );
1684 0 my $path_tiny = _path_tiny_from_object( $path_obj, 'CSV.load_table' );
1685 0 my $dbh = _dbh_from_object( $dbh_obj, 'CSV.load_table' );
1686 my $config = _normalize_runtime_config(
1687 0 _config_merge( $self->slots->{_config}, _options_hash($options) )
1688 );
1689  
1690 0 my $reader = $csv_class->methods->{open}->{_native}->(
1691 $self,
1692 $path_obj,
1693 );
1694 0 $reader->slots->{_config} = $config;
1695  
1696 0 my $headers = $reader->slots->{_headers};
1697 0 ▶ my $csv_columns = ref($headers) eq 'ARRAY' && @{$headers}
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'ARRAY' && @{$headers;} ? : 0 0 0
Condition0%0/3
ref $headers eq "ARRAY" and @{$headers;}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $headers eq "ARRAY" and @{$headers;}
ref $headers eq "ARRAY" @{$headers;}
Truth table0%0/3
A and B
A= ref $headers eq "ARRAY"B= @{$headers;}
ABresult
0X0
100
111
1698 ? $headers
1699 : _columns_from_config($config);
1700 $csv_columns //= _table_column_names( $dbh, $table_name )
1701 0 ▶ if !zuzu_bool( $config->{create_table}, 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless zuzu_bool($config->{"create_table"}, 0) 0 0 0
Condition0%0/3
$csv_columns //= _table_column_names($dbh, $table_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$csv_columns //= _table_column_names($dbh, $table_name)
$csv_columns _table_column_names($dbh, $table_name)
Truth table0%0/3
A //= B
A= $csv_columnsB= _table_column_names($dbh, $table_name)
ABresult
1X1
011
000
1702  
1703 0 my $first;
1704 0 ▶ if ( !defined $csv_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $csv_columns) 0 0 0
1705 0 $first = _reader_fetch_array( $runtime, $reader );
1706 0 ▶ $csv_columns = _default_columns_for_row($first) if defined $first;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first 0 0 0
1707 }
1708  
1709 die Zuzu::Error->new_runtime(
1710 message => 'CSV.load_table could not determine target columns',
1711 file => '<std/data/csv>',
1712 line => 0,
1713 0 ▶ ) if !defined $csv_columns or !@{$csv_columns};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $csv_columns and @{$csv_columns;} 0 0 0
Condition0%0/3
defined $csv_columns and @{$csv_columns;}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $csv_columns and @{$csv_columns;}
defined $csv_columns @{$csv_columns;}
Truth table0%0/3
A and B
A= defined $csv_columnsB= @{$csv_columns;}
ABresult
0X0
100
111
1714  
1715 0 my $column_map = _hash_from_value( $config->{column_map} );
1716 0 my $db_columns = _mapped_columns( $csv_columns, $column_map );
1717  
1718 0 _create_table_if_needed( $dbh, $table_name, $db_columns, $config );
1719  
1720 0 my $placeholders = join ', ', ('?') x scalar @{$db_columns};
1721 0 my $conflict = _conflict_clause($config);
1722 0 my $insert_sql = 'insert ';
1723 0 ▶ $insert_sql .= $conflict . ' ' if $conflict ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $conflict ne "" 0 0 0
1724 $insert_sql .= 'into '
1725 . _quote_identifier($table_name)
1726 . ' ('
1727 0 . join( ', ', map { _quote_identifier($_) } @{$db_columns} )
1728 . ') values ('
1729 . $placeholders
1730 . ')';
1731  
1732 my $sth = _dbi_call(
1733 0 ▶ sub { $dbh->prepare( $insert_sql ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1734 0 'load_table prepare failed',
1735 );
1736  
1737 my $transaction = exists $config->{transaction}
1738 0 ▶ ? zuzu_bool( $config->{transaction}, 1 )
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'transaction'} ? : 0 0 0
1739 : 1;
1740 0 ▶ my $batch_size = int( $config->{batch_size} // 0 );
Condition0%0/2
$config->{"batch_size"} // 0
l!l
00
MC/DC0%0/1
$config->{"batch_size"} // 0
$config->{"batch_size"}
Truth table0%0/2
A
A= $config->{"batch_size"}
Aresult
11
00
1741 0 ▶ my $commit_interval = int( $config->{commit_interval} // 0 );
Condition0%0/2
$config->{"commit_interval"} // 0
l!l
00
MC/DC0%0/1
$config->{"commit_interval"} // 0
$config->{"commit_interval"}
Truth table0%0/2
A
A= $config->{"commit_interval"}
Aresult
11
00
1742 0 my $inserted = 0;
1743 0 my $pending = 0;
1744  
1745 eval {
1746 0 _transaction_begin( $dbh, $transaction );
1747  
1748 0 ▶ if ( defined $first ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first) 0 0 0
1749 _dbi_call(
1750 0 ▶ sub { $sth->execute( @{$first} ); return 1; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1751 0 'load_table insert failed',
1752 );
1753 0 $inserted++;
1754 0 $pending++;
1755 }
1756  
1757 0 while (1) {
1758 0 ▶ my $row = ref($reader->slots->{_headers}) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $reader->slots->{'_headers'} eq 'ARRAY' ? : 0 0 0
1759 ? _reader_fetch_dict( $runtime, $reader )
1760 : _reader_fetch_array( $runtime, $reader );
1761 0 ▶ last if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
1762  
1763 my $array = ref($row) eq 'HASH'
1764 0 ▶ ? [ map { $row->{$_} } @{$csv_columns} ]
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'HASH' ? : 0 0 0
1765 : $row;
1766 _dbi_call(
1767 0 ▶ sub { $sth->execute( @{$array} ); return 1; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1768 0 'load_table insert failed',
1769 );
1770 0 $inserted++;
1771 0 $pending++;
1772  
1773 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($transaction and $batch_size > 0 && $pending >= $batch_size || $commit_interval > 0 && $pending >= $commit_interval) 0 0 0
Condition0%0/12
$transaction and $batch_size > 0 && $pending >= $batch_size || $commit_interval > 0 && $pending >= $commit_interval
!ll&&!rl&&r
000
$batch_size > 0 and $pending >= $batch_size or $commit_interval > 0 and $pending >= $commit_interval
l!l&&r!l&&!r
000
$batch_size > 0 and $pending >= $batch_size
!ll&&!rl&&r
000
$commit_interval > 0 and $pending >= $commit_interval
!ll&&!rl&&r
000
MC/DC0%0/5
$transaction and $batch_size > 0 && $pending >= $batch_size || $commit_interval > 0 && $pending >= $commit_interval
$transaction $batch_size > 0 $pending >= $batch_size $commit_interval > 0 $pending >= $commit_interval
Truth table0%0/8
A and B and C or D and E
A= $transactionB= $batch_size > 0C= $pending >= $batch_sizeD= $commit_interval > 0E= $pending >= $commit_interval
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
1774 $transaction
1775 and (
1776 ( $batch_size > 0 and $pending >= $batch_size ) or
1777 ( $commit_interval > 0 and $pending >= $commit_interval )
1778 )
1779 ) {
1780 0 _transaction_commit( $dbh, 1 );
1781 0 _transaction_begin( $dbh, 1 );
1782 0 $pending = 0;
1783 }
1784 }
1785  
1786 0 _transaction_commit( $dbh, $transaction );
1787 0 1;
1788 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _transaction_begin($dbh, $transaction); if (defined $first) { _dbi_call(sub { $sth->execute(@{$first;}); return 1; } , "load_table insert failed"); ++$inserted; ++$pending; } ; while (1) { my $row = ref $reader->slots->{"_headers"} eq "ARRAY" ? _reader_fetch_dict($runtime, $reader) : _reader_fetch_array($runtime, $reader); last unless defined $row; my $array = ref $row eq "HASH" ? [map({$row->{$_};} @{$csv_columns;})] : $row; _dbi_call(sub { $sth->execute(@{$array;}); return 1; } , "load_table insert failed"); ++$inserted; ++$pending; if ($transaction and $batch_size > 0 && $pending >= $batch_size || $commit_interval > 0 && $pending >= $commit_interval) { _transaction_commit($dbh, 1); _transaction_begin($dbh, 1); $pending = 0; } ; } ; _transaction_commit($dbh, $transaction); 1 } } or do { my $err = $@; _transaction_rollback($dbh, $transaction); die $err } 0 0 0
1789 0 my $err = $@;
1790 0 _transaction_rollback( $dbh, $transaction );
1791 0 die $err;
1792 };
1793  
1794 0 ▶ close $reader->slots->{_fh} if !$reader->slots->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->slots->{"_closed"} 0 0 0
1795 0 $reader->slots->{_closed} = 1;
1796 0 return $inserted;
1797 },
1798 2 );
1799  
1800 $reader_class->methods->{next} = native_function(
1801 name => 'next',
1802 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1803 0 my ( $self ) = @_;
1804 0 ▶ my $row = _reader_default_mode($self) eq 'dict'
Branch: 1 branch
BranchTrueFalseTotal
_reader_default_mode($self) eq 'dict' ? : 0 0 0
1805 ? _reader_fetch_dict( $runtime, $self )
1806 : _reader_fetch_array( $runtime, $self );
1807 0 ▶ return undef if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
1808 0 return perl_to_zuzu( $row );
1809 },
1810 2 );
1811  
1812 $reader_class->methods->{next_array} = native_function(
1813 name => 'next_array',
1814 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1815 0 my ( $self ) = @_;
1816 0 my $row = _reader_fetch_array( $runtime, $self );
1817 0 ▶ return undef if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
1818 0 return perl_to_zuzu( $row );
1819 },
1820 2 );
1821  
1822 $reader_class->methods->{next_dict} = native_function(
1823 name => 'next_dict',
1824 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1825 0 my ( $self ) = @_;
1826 0 my $row = _reader_fetch_dict( $runtime, $self );
1827 0 ▶ return undef if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
1828 0 return perl_to_zuzu( $row );
1829 },
1830 2 );
1831  
1832 $reader_class->methods->{all_array} = native_function(
1833 name => 'all_array',
1834 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1835 0 my ( $self ) = @_;
1836 0 my @rows;
1837 0 ▶ while ( my $row = _reader_fetch_array( $runtime, $self ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = _reader_fetch_array($runtime, $self)) 0 0 0
1838 0 push @rows, $row;
1839 }
1840 0 return perl_to_zuzu( \@rows );
1841 },
1842 2 );
1843  
1844 $reader_class->methods->{all_dict} = native_function(
1845 name => 'all_dict',
1846 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1847 0 my ( $self ) = @_;
1848 0 my @rows;
1849 0 ▶ while ( my $row = _reader_fetch_dict( $runtime, $self ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = _reader_fetch_dict($runtime, $self)) 0 0 0
1850 0 push @rows, $row;
1851 }
1852 0 return perl_to_zuzu( \@rows );
1853 },
1854 2 );
1855  
1856 $reader_class->methods->{headers} = native_function(
1857 name => 'headers',
1858 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1859 0 my ( $self ) = @_;
1860 0 ▶ return perl_to_zuzu( $self->slots->{_headers} // [] );
Condition0%0/2
$self->slots->{"_headers"} // []
l!l
00
MC/DC0%0/1
$self->slots->{"_headers"} // []
$self->slots->{"_headers"}
Truth table0%0/2
A
A= $self->slots->{"_headers"}
Aresult
11
00
1861 },
1862 2 );
1863  
1864 $reader_class->methods->{columns} = native_function(
1865 name => 'columns',
1866 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1867 0 my ( $self ) = @_;
1868 0 ▶ return perl_to_zuzu( $self->slots->{_headers} // [] );
Condition0%0/2
$self->slots->{"_headers"} // []
l!l
00
MC/DC0%0/1
$self->slots->{"_headers"} // []
$self->slots->{"_headers"}
Truth table0%0/2
A
A= $self->slots->{"_headers"}
Aresult
11
00
1869 },
1870 2 );
1871  
1872 $reader_class->methods->{set_columns} = native_function(
1873 name => 'set_columns',
1874 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1875 0 my ( $self, $columns ) = @_;
1876 0 my $cols = _array_from_value($columns);
1877 0 ▶ $self->slots->{_headers} = [ map { defined $_ ? "$_" : '' } @{$cols} ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
1878 0 ▶ $self->slots->{_row_mode} = @{$cols} ? 'dict' : 'array';
Branch: 1 branch
BranchTrueFalseTotal
@{$cols;} ? : 0 0 0
1879 0 return perl_to_zuzu( $self->slots->{_headers} );
1880 },
1881 2 );
1882  
1883 $reader_class->methods->{row_number} = native_function(
1884 name => 'row_number',
1885 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1886 0 my ( $self ) = @_;
1887 0 ▶ return 0 + ( $self->slots->{_row_number} // 0 );
Condition0%0/2
$self->slots->{"_row_number"} // 0
l!l
00
MC/DC0%0/1
$self->slots->{"_row_number"} // 0
$self->slots->{"_row_number"}
Truth table0%0/2
A
A= $self->slots->{"_row_number"}
Aresult
11
00
1888 },
1889 2 );
1890  
1891 $reader_class->methods->{skip_lines} = native_function(
1892 name => 'skip_lines',
1893 0 ▶ native => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
1894 0 my ( $self, $count ) = @_;
1895 0 ▶ my $n = int( $count // 0 );
Condition0%0/2
$count // 0
l!l
00
MC/DC0%0/1
$count // 0
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
1896 0 ▶ while ( $n > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 0 0 0
1897 0 ▶ last if !defined scalar readline( $self->slots->{_fh} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined scalar readline $self->slots->{"_fh"} 0 0 0
1898 0 ▶ $self->slots->{_line_number} = ( $self->slots->{_line_number} // 0 ) + 1;
Condition0%0/2
$self->slots->{"_line_number"} // 0
l!l
00
MC/DC0%0/1
$self->slots->{"_line_number"} // 0
$self->slots->{"_line_number"}
Truth table0%0/2
A
A= $self->slots->{"_line_number"}
Aresult
11
00
1899 0 $n--;
1900 }
1901 0 return $self;
1902 },
1903 2 );
1904  
1905 $reader_class->methods->{errors} = native_function(
1906 name => 'errors',
1907 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1908 0 my ( $self ) = @_;
1909 0 ▶ return perl_to_zuzu( $self->slots->{_errors} // [] );
Condition0%0/2
$self->slots->{"_errors"} // []
l!l
00
MC/DC0%0/1
$self->slots->{"_errors"} // []
$self->slots->{"_errors"}
Truth table0%0/2
A
A= $self->slots->{"_errors"}
Aresult
11
00
1910 },
1911 2 );
1912  
1913 $reader_class->methods->{close} = native_function(
1914 name => 'close',
1915 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1916 0 my ( $self ) = @_;
1917 0 ▶ if ( !zuzu_bool( $self->slots->{_closed}, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (zuzu_bool($self->slots->{"_closed"}, 0)) 0 0 0
1918 0 close( $self->slots->{_fh} );
1919 0 $self->slots->{_closed} = 1;
1920 }
1921 0 return $self;
1922 },
1923 2 );
1924  
1925 $reader_class->methods->{to_Iterator} = native_function(
1926 name => 'to_Iterator',
1927 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1928 0 my ( $self ) = @_;
1929 0 return _reader_to_iterator( $runtime, $self );
1930 },
1931 2 );
1932  
1933 $writer_class->methods->{columns} = native_function(
1934 name => 'columns',
1935 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1936 0 my ( $self ) = @_;
1937 0 ▶ return perl_to_zuzu( $self->slots->{_columns} // [] );
Condition0%0/2
$self->slots->{"_columns"} // []
l!l
00
MC/DC0%0/1
$self->slots->{"_columns"} // []
$self->slots->{"_columns"}
Truth table0%0/2
A
A= $self->slots->{"_columns"}
Aresult
11
00
1938 },
1939 2 );
1940  
1941 $writer_class->methods->{write_header} = native_function(
1942 name => 'write_header',
1943 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1944 0 my ( $self, $columns ) = @_;
1945 0 my $cols = _array_from_value($columns);
1946 0 ▶ $self->slots->{_columns} = [ map { defined $_ ? "$_" : '' } @{$cols} ]
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
1947 0 ▶ if @{$cols};
Branch: 1 branch
BranchTrueFalseTotal
if @{$cols;} 0 0 0
1948 0 _writer_maybe_header( $runtime, $self, $self->slots->{_columns} );
1949 0 ▶ return perl_to_zuzu( $self->slots->{_columns} // [] );
Condition0%0/2
$self->slots->{"_columns"} // []
l!l
00
MC/DC0%0/1
$self->slots->{"_columns"} // []
$self->slots->{"_columns"}
Truth table0%0/2
A
A= $self->slots->{"_columns"}
Aresult
11
00
1950 },
1951 2 );
1952  
1953 $writer_class->methods->{write_row} = native_function(
1954 name => 'write_row',
1955 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1956 0 my ( $self, $row ) = @_;
1957 0 return _writer_write_row( $runtime, $self, zuzu_to_perl($row) );
1958 },
1959 2 );
1960  
1961 $writer_class->methods->{print_row} = native_function(
1962 name => 'print_row',
1963 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1964 0 my ( $self, $row ) = @_;
1965 0 return _writer_write_row( $runtime, $self, zuzu_to_perl($row) );
1966 },
1967 2 );
1968  
1969 $writer_class->methods->{row_number} = native_function(
1970 name => 'row_number',
1971 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1972 0 my ( $self ) = @_;
1973 0 ▶ return 0 + ( $self->slots->{_row_number} // 0 );
Condition0%0/2
$self->slots->{"_row_number"} // 0
l!l
00
MC/DC0%0/1
$self->slots->{"_row_number"} // 0
$self->slots->{"_row_number"}
Truth table0%0/2
A
A= $self->slots->{"_row_number"}
Aresult
11
00
1974 },
1975 2 );
1976  
1977 $writer_class->methods->{close} = native_function(
1978 name => 'close',
1979 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1980 0 my ( $self ) = @_;
1981 0 ▶ if ( !zuzu_bool( $self->slots->{_closed}, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (zuzu_bool($self->slots->{"_closed"}, 0)) 0 0 0
1982 0 close( $self->slots->{_fh} );
1983 0 $self->slots->{_closed} = 1;
1984 }
1985 0 return $self;
1986 },
1987 2 );
1988  
1989 return {
1990 2 CSV => $csv_class,
1991 CSVReader => $reader_class,
1992 CSVWriter => $writer_class,
1993 };
1994 }
1995  
1996 2 1;
1997  
1998 =pod
1999  
2000 =head1 NAME
2001  
2002 Zuzu::Module::CSV - std/data/csv bindings for ZuzuScript.
2003  
2004 =head1 DESCRIPTION
2005  
2006 Implements the runtime side of C<std/data/csv> using L<Text::CSV_XS>.
2007 The module exposes:
2008  
2009 =over
2010  
2011 =item * C<CSV>
2012  
2013 A configurable codec/IO helper for CSV-like formats.
2014  
2015 =item * C<CSVReader>
2016  
2017 A streaming reader with iterator support for C<for> loops.
2018  
2019 =item * C<CSVWriter>
2020  
2021 A streaming writer for large exports.
2022  
2023 =back
2024  
2025 =head1 COPYRIGHT AND LICENCE
2026  
2027 B<< Zuzu::Module::CSV >> is copyright Toby Inkster.
2028  
2029 It is free software; you may redistribute it and/or modify it under
2030 the terms of either the Artistic License 1.0 or the GNU General Public
2031 License version 2.
2032  
2033 =cut
« blib/lib/Zuzu/Module/CLib.pm ↑ Summary blib/lib/Zuzu/Module/Math.pm »