blib/lib/DBIO/ResultSource/RowParser/Util.pm

filter: Statementstmt 99.2% 135 / 136 Branchbran 94.2% 49 / 52 Conditioncond 82.6% 19 / 23 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 94.0% 223 / 237 CC 42 SCAR 26.7
CC
42
Cov
96%
__visit_infmap_collapse
23.0
__visit_infmap_simple
12.0
assemble_collapsing_parser
8.0
CRAP 14.4
« blib/lib/DBIO/Util.pm ↑ Summary blib/lib/DBIO/Test/Kubernetes.pm »
1 package DBIO::ResultSource::RowParser::Util;
2 # ABSTRACT: Assemble row-parser source code for simple and collapsing queries
3  
4 379 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 379 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 379 our $VERSION = '0.900007';
8  
9 379 ▶use DBIO::Util 'perlstring';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 379 ▶use constant HAS_DOR => ( $] < 5.010 ? 0 : 1 );
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.01 ? : 0 379 379
12  
13 379 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 379 our @EXPORT_OK = qw(
15 assemble_simple_parser
16 assemble_collapsing_parser
17 );
18  
19 # working title - we are hoping to extract this eventually...
20 379 our $null_branch_class = 'DBIO::ResultParser::RelatedNullBranch';
21  
22  
23 23 ▶sub __wrap_in_strictured_scope {
Subroutine __wrap_in_strictured_scope: called CC=1 SCAR=0.0
24 23 " { use strict; use warnings; use warnings FATAL => 'uninitialized';\n$_[0]\n }"
25 }
26  
27  
28 7 ▶sub assemble_simple_parser {
Subroutine assemble_simple_parser: called CC=1 SCAR=0.0
Pod documented
29 #my ($args) = @_;
30  
31 # the non-collapsing assembler is easy
32 # FIXME SUBOPTIMAL there could be a yet faster way to do things here, but
33 # need to try an actual implementation and benchmark it:
34 #
35 # <timbunce_> First setup the nested data structure you want for each row
36 # Then call bind_col() to alias the row fields into the right place in
37 # the data structure, then to fetch the data do:
38 # push @rows, dclone($row_data_struct) while ($sth->fetchrow);
39 #
40 7 my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) );
41  
42 # change the quoted placeholders to unquoted alias-references
43 7 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
44  
45 7 __wrap_in_strictured_scope($parser_src);
46 }
47  
48 # the simple non-collapsing nested structure recursor
49  
50 28 ▶sub __visit_infmap_simple {
Subroutine __visit_infmap_simple: called CC=12 SCAR=24.9
51 28 my $args = shift;
52  
53 28 my $my_cols = {};
54 28 my $rel_cols;
55 28 for (keys %{$args->{val_index}}) {
56 97 ▶ if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^ ([^\.]+) \. (.+) /x) { } 49 48 97
57 49 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
58 }
59 else {
60 48 $my_cols->{$_} = $args->{val_index}{$_};
61 }
62 }
63  
64 28 my @relperl;
65 28 for my $rel (sort keys %$rel_cols) {
66  
67 my $rel_struct = __visit_infmap_simple({ %$args,
68 21 val_index => $rel_cols->{$rel},
69 });
70  
71 21 ▶ if (keys %$my_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$my_cols) 13 8 21
72  
73 my $branch_null_checks = join ' && ', map
74 29 { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" }
75 13 sort { $a <=> $b } values %{$rel_cols->{$rel}}
76 ;
77  
78 13 ▶ if ($args->{prune_null_branches}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'prune_null_branches'}) { } 9 4 13
79 9 $rel_struct = sprintf ( '( (%s) ? undef : %s )',
80 $branch_null_checks,
81 $rel_struct,
82 );
83 }
84 else {
85 4 $rel_struct = sprintf ( '( (%s) ? bless( (%s), %s ) : %s )',
86 $branch_null_checks,
87 $rel_struct,
88 perlstring($null_branch_class),
89 $rel_struct,
90 );
91 }
92 }
93  
94 21 push @relperl, sprintf '( %s => %s )',
95 perlstring($rel),
96 $rel_struct,
97 ;
98  
99 }
100  
101 28 my $me_struct;
102 28 ▶ $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$my_cols 20 8 28
103  
104 28 ▶ if ($args->{hri_style}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'hri_style'}) { } 6 22 28
105 6 ▶ $me_struct =~ s/^ \s* \{ | \} \s* $//gx
Branch: 1 branch
BranchTrueFalseTotal
if $me_struct 4 2 6
106 if $me_struct;
107  
108 6 ▶ return sprintf '{ %s }', join (', ', $me_struct||(), @relperl);
Condition66%2/3
$me_struct || ()
l!l&&r!l&&!r
420
MC/DC0%0/2
$me_struct || ()
$me_struct ()
Truth table66%2/3
A || B
A= $me_structB= ()
ABresult
1X1
011
000
109 }
110 else {
111 22 ▶ return sprintf '[%s]', join (',',
Branch: 1 branch
BranchTrueFalseTotal
@relperl ? : 16 6 22
Condition100%2/2
$me_struct || 'undef'
l!l
166
MC/DC100%1/1
$me_struct || 'undef'
$me_struct
Truth table100%2/2
A
A= $me_struct
Aresult
11
00
112 $me_struct || 'undef',
113 @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
114 );
115 }
116 }
117  
118  
119 16 ▶sub assemble_collapsing_parser {
Subroutine assemble_collapsing_parser: called CC=8 SCAR=20.8
Pod documented
120 16 my $args = shift;
121  
122 # it may get unset further down
123 16 my $no_rowid_container = $args->{prune_null_branches};
124  
125 16 my ($top_node_key, $top_node_key_assembler);
126  
127 16 ▶ if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @{$args->{'collapse_map'}{'-identifying_columns'};}) { } 14 2 16
elsif (my(@variants) = @{$args->{'collapse_map'}{'-identifying_columns_variants'};}) { } 2 0 2
128 $top_node_key = join ('', map
129 16 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
130 14 @{$args->{collapse_map}{-identifying_columns}}
131 );
132 }
133 2 elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
134  
135 2 my @path_parts = map { sprintf
136 "( ( defined '\xFF__VALPOS__%d__\xFF' ) && (join qq(\xFF), '', %s, '') )",
137 $_->[0], # checking just first is enough - one ID defined, all defined
138 4 ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ),
139 } @variants;
140  
141 2 my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
142  
143 2 $top_node_key = "{'\xFF__IDVALPOS__${virtual_column_idx}__\xFF'}";
144  
145 2 $top_node_key_assembler = sprintf "'\xFF__IDVALPOS__%d__\xFF' = (%s);",
146 $virtual_column_idx,
147 "\n" . join( "\n or\n", @path_parts, qq{"\0\$rows_pos\0"} )
148 ;
149  
150 $args->{collapse_map} = {
151 2 %{$args->{collapse_map}},
152 -custom_node_key => $top_node_key,
153 };
154  
155 2 $no_rowid_container = 0;
156 }
157 else {
158 0 die('Unexpected collapse map contents');
159 }
160  
161 16 my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
162  
163 my @idcol_args = $no_rowid_container ? ('', '') : (
164 ', %cur_row_ids', # only declare the variable if we'll use it
165 join ("\n", map {
166 21 my $quoted_null_val = qq( "\0NULL\xFF\${rows_pos}\xFF${_}\0" );
167 qq(\$cur_row_ids{$_} = ) . (
168 # in case we prune - we will never hit these undefs
169 21 ▶ $args->{prune_null_branches} ? qq( \$cur_row_data->[$_]; )
Branch: 1 branch
BranchTrueFalseTotal
$args->{'prune_null_branches'} ? : 5 16 21
170 : HAS_DOR ? qq( \$cur_row_data->[$_] // $quoted_null_val; )
171 : qq( defined(\$cur_row_data->[$_]) ? \$cur_row_data->[$_] : $quoted_null_val; )
172 )
173 16 ▶ } sort { $a <=> $b } keys %{ $stats->{idcols_seen} } ),
Branch: 1 branch
BranchTrueFalseTotal
$no_rowid_container ? : 12 4 16
174 );
175  
176 16 ▶ my $parser_src = sprintf (<<'EOS', @idcol_args, $top_node_key_assembler||'', $top_node_key, join( "\n", @{$data_assemblers||[]} ) );
Condition75%3/4
$top_node_key_assembler || ''
l!l
214
$data_assemblers || []
l!l
160
MC/DC100%1/1
$top_node_key_assembler || ''
$top_node_key_assembler
MC/DC0%0/1
$data_assemblers || []
$data_assemblers
Truth table100%2/2
A
A= $top_node_key_assembler
Aresult
11
00
Truth table50%1/2
A
A= $data_assemblers
Aresult
11
00
177 ### BEGIN LITERAL STRING EVAL
178 my $rows_pos = 0;
179 my ($result_pos, @collapse_idx, $cur_row_data %1$s);
180  
181 # this loop is a bit arcane - the rationale is that the passed in
182 # $_[0] will either have only one row (->next) or will have all
183 # rows already pulled in (->all and/or unordered). Given that the
184 # result can be rather large - we reuse the same already allocated
185 # array, since the collapsed prefetch is smaller by definition.
186 # At the end we cut the leftovers away and move on.
187 while ($cur_row_data = (
188 ( $rows_pos >= 0 and $_[0][$rows_pos++] )
189 or
190 ( $_[1] and $rows_pos = -1 and $_[1]->() )
191 ) ) {
192  
193 # this code exists only when we are using a cur_row_ids
194 # furthermore the undef checks may or may not be there
195 # depending on whether we prune or not
196 #
197 # due to left joins some of the ids may be NULL/undef, and
198 # won't play well when used as hash lookups
199 # we also need to differentiate NULLs on per-row/per-col basis
200 # (otherwise folding of optional 1:1s will be greatly confused
201 %2$s
202  
203 # in the case of an underdefined root - calculate the virtual id (otherwise no code at all)
204 %3$s
205  
206 # if we were supplied a coderef - we are collapsing lazily (the set
207 # is ordered properly)
208 # as long as we have a result already and the next result is new we
209 # return the pre-read data and bail
210 $_[1] and $result_pos and ! $collapse_idx[0]%4$s and (unshift @{$_[2]}, $cur_row_data) and last;
211  
212 # the rel assemblers
213 %5$s
214  
215 }
216  
217 $#{$_[0]} = $result_pos - 1; # truncate the passed in array to where we filled it with results
218 ### END LITERAL STRING EVAL
219 EOS
220  
221 # !!! note - different var than the one above
222 # change the quoted placeholders to unquoted alias-references
223 16 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex;
224 16 $parser_src =~ s/
225 \' \xFF__IDVALPOS__(\d+)__\xFF \'
226 /
227 350 ▶ $no_rowid_container ? "\$cur_row_data->[$1]" : "\$cur_row_ids{$1}"
Branch: 1 branch
BranchTrueFalseTotal
$no_rowid_container ? : 183 167 350
228 /gex;
229  
230 16 __wrap_in_strictured_scope($parser_src);
231 }
232  
233  
234 # the collapsing nested structure recursor
235  
236 64 ▶sub __visit_infmap_collapse {
Subroutine __visit_infmap_collapse: called CC=23 SCAR=31.4
237 64 my $args = {%{ shift() }};
238  
239 64 ▶ my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
Condition100%2/2
$args->{"-node_idx_counter"} ||= \do { my $x = 0 }
l!l
4816
MC/DC100%1/1
$args->{"-node_idx_counter"} ||= \do { my $x = 0 }
$args->{"-node_idx_counter"}
Truth table100%2/2
A
A= $args->{"-node_idx_counter"}
Aresult
11
00
240  
241 64 my ($my_cols, $rel_cols) = {};
242 64 for ( keys %{$args->{val_index}} ) {
243 336 ▶ if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^ ([^\.]+) \. (.+) /x) { } 184 152 336
244 184 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
245 }
246 else {
247 152 $my_cols->{$_} = $args->{val_index}{$_};
248 }
249 }
250  
251  
252 64 ▶ if ($args->{hri_style}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"hri_style"}) 13 51 64
253 13 delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols;
254 }
255  
256 64 my $me_struct;
257 64 ▶ $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$my_cols 52 12 64
258  
259 $me_struct = sprintf( '[ %s ]', $me_struct||'' )
260 64 ▶ unless $args->{hri_style};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"hri_style"} 51 13 64
Condition100%2/2
$me_struct || ''
l!l
429
MC/DC100%1/1
$me_struct || ''
$me_struct
Truth table100%2/2
A
A= $me_struct
Aresult
11
00
261  
262  
263 my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
264 126 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
265 64 ▶ @{$args->{collapse_map}->{-identifying_columns}}
Condition66%2/3
$args->{'collapse_map'}{'-custom_node_key'} || join('', map({"{'\377__IDVALPOS__${_}__\377'}";} @{$$args{'collapse_map'}{'-identifying_columns'};}))
l!l&&r!l&&!r
2620
MC/DC0%0/2
$args->{'collapse_map'}{'-custom_node_key'} || join('', map({"{'\377__IDVALPOS__${_}__\377'}";} @{$$args{'collapse_map'}{'-identifying_columns'};}))
$args->{'collapse_map'}{'-custom_node_key'} join('', map({"{'\377__IDVALPOS__${_}__\377'}";} @{$$args{'collapse_map'}{'-identifying_columns'};}))
Truth table66%2/3
A || B
A= $args->{'collapse_map'}{'-custom_node_key'}B= join('', map({"{'\377__IDVALPOS__${_}__\377'}";} @{$$args{'collapse_map'}{'-identifying_columns'};}))
ABresult
1X1
011
000
266 );
267 64 my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
268  
269  
270 64 my @src;
271  
272 64 ▶ if ($cur_node_idx == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_node_idx == 0) { } 16 48 64
273 16 ▶ push @src, sprintf( '%s %s $_[0][$result_pos++] = %s;',
Condition50%1/2
$me_struct || '{}'
l!l
160
MC/DC0%0/1
$me_struct || '{}'
$me_struct
Truth table50%1/2
A
A= $me_struct
Aresult
11
00
274 $node_idx_slot,
275 (HAS_DOR ? '//=' : '||='),
276 $me_struct || '{}',
277 );
278 }
279 else {
280 my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s%s{%s}',
281 48 ▶ @{$args}{qw/-parent_node_idx -parent_node_key/},
Branch: 1 branch
BranchTrueFalseTotal
$args->{'hri_style'} ? : 11 37 48
282 $args->{hri_style} ? '' : '[1]',
283 48 perlstring($args->{-node_rel_name}),
284 );
285  
286 48 ▶ if ($args->{collapse_map}->{-is_single}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'collapse_map'}{'-is_single'}) { } 20 28 48
287 20 ▶ push @src, sprintf ( '%s %s %s%s;',
Branch: 1 branch
BranchTrueFalseTotal
$me_struct ? : 17 3 20
288 $parent_attach_slot,
289 (HAS_DOR ? '//=' : '||='),
290 $node_idx_slot,
291 $me_struct ? " = $me_struct" : '',
292 );
293 }
294 else {
295 28 ▶ push @src, sprintf('(! %s) and push @{%s}, %s%s;',
Branch: 1 branch
BranchTrueFalseTotal
$me_struct ? : 28 0 28
296 $node_idx_slot,
297 $parent_attach_slot,
298 $node_idx_slot,
299 $me_struct ? " = $me_struct" : '',
300 );
301 }
302 }
303  
304 64 my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} };
305 64 my ($stats, $rel_src);
306  
307 64 for my $rel (sort keys %$rel_cols) {
308  
309 48 my $relinfo = $args->{collapse_map}{$rel};
310  
311 ($rel_src, $stats->{$rel}) = __visit_infmap_collapse({ %$args,
312 48 val_index => $rel_cols->{$rel},
313 collapse_map => $relinfo,
314 -parent_node_idx => $cur_node_idx,
315 -parent_node_key => $node_key,
316 -node_rel_name => $rel,
317 });
318  
319 48 my $rel_src_pos = $#src + 1;
320 48 push @src, @$rel_src;
321  
322 48 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($relinfo->{"-is_optional"} and scalar(my($first_distinct_child_idcol) = grep({not $known_present_ids->{$_};} @{$$relinfo{"-identifying_columns"};}))) 32 16 48
Condition100%2/2
$relinfo->{"-is_optional"} and scalar(my($first_distinct_child_idcol) = grep({not $known_present_ids->{$_};} @{$$relinfo{"-identifying_columns"};}))
!ll
1632
MC/DC100%1/1
$relinfo->{"-is_optional"} and scalar(my($first_distinct_child_idcol) = grep({not $known_present_ids->{$_};} @{$$relinfo{"-identifying_columns"};}))
$relinfo->{"-is_optional"}
Truth table100%2/2
A
A= $relinfo->{"-is_optional"}
Aresult
00
11
323 $relinfo->{-is_optional}
324 and
325 scalar( my ($first_distinct_child_idcol) = grep
326 80 { ! $known_present_ids->{$_} }
327 32 @{$relinfo->{-identifying_columns}}
328 )
329 ) {
330  
331 32 ▶ if ($args->{prune_null_branches}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'prune_null_branches'}) { } 21 11 32
332  
333 # start of wrap of the entire chain in a conditional
334 splice @src, $rel_src_pos, 0, sprintf "( ! defined %s )\n ? %s%s{%s} = %s\n : do {",
335 "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
336 $node_idx_slot,
337 $args->{hri_style} ? '' : '[1]',
338 perlstring($rel),
339 21 ▶ ($args->{hri_style} && $relinfo->{-is_single}) ? 'undef' : '[]'
Branch: 2 branches
BranchTrueFalseTotal
$args->{'hri_style'} ? : 7 14 21
$args->{'hri_style'} && $relinfo->{'-is_single'} ? : 2 19 21
Condition100%3/3
$args->{"hri_style"} and $relinfo->{"-is_single"}
!ll&&!rl&&r
1452
MC/DC100%2/2
$args->{"hri_style"} and $relinfo->{"-is_single"}
$args->{"hri_style"} $relinfo->{"-is_single"}
Truth table100%3/3
A and B
A= $args->{"hri_style"}B= $relinfo->{"-is_single"}
ABresult
0X0
100
111
340 ;
341  
342 # end of wrap
343 21 push @src, '};'
344 }
345 else {
346  
347 11 splice @src, $rel_src_pos + 1, 0, sprintf ( '(defined %s) or bless (%s[1]{%s}, %s);',
348 "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
349 $node_idx_slot,
350 perlstring($rel),
351 perlstring($null_branch_class),
352 );
353 }
354 }
355 }
356  
357 return (
358 \@src,
359 {
360 idcols_seen => {
361 48 ( map { %{ $_->{idcols_seen} } } values %$stats ),
362 64 ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
363 }
364 }
365 );
366 }
367  
368  
369 72 ▶sub __result_struct_to_source {
Subroutine __result_struct_to_source: called CC=1 SCAR=0.0
370 sprintf( '{ %s }', join (', ', map
371 200 { sprintf "%s => '\xFF__VALPOS__%d__\xFF'", perlstring($_), $_[0]{$_} }
372 72 sort keys %{$_[0]}
373 ));
374 }
375  
376 379 1;
377  
378 __END__
« blib/lib/DBIO/Util.pm ↑ Summary blib/lib/DBIO/Test/Kubernetes.pm »