DBIx/Class/ResultSource/RowParser/Util.pm

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