DBIx/Class/ResultSource/RowParser/Util.pm

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