blib/lib/DBIO/ResultSource/RowParser.pm

filter: Statementstmt 93.0% 174 / 187 Branchbran 78.5% 66 / 84 Conditioncond 71.4% 55 / 77 MC/DCmcdc 45.2% 19 / 42 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 81.1% 328 / 404 CC 86 SCAR 40.7
CC
86
Cov
85%
_resolve_collapse
90.2
_resolve_prefetch
12.6
_mk_row_parser
9.1
CRAP 58.3
« blib/lib/DBIO/ResultClass/HashRefInflator.pm ↑ Summary blib/lib/DBIO/EncodedColumn.pm »
1 package DBIO::ResultSource::RowParser;
2 # ABSTRACT: Build row-inflating parsers from result source metadata
3  
4 383 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 383 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 379 our $VERSION = '0.900007';
8  
9 383 ▶use base 'DBIO::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 380 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 380 ▶use DBIO::ResultSource::RowParser::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 assemble_simple_parser
15 assemble_collapsing_parser
16 380 );
17 380 ▶use DBIO::Util qw(stresstest_utf8_upgrade_generated_collapser_source);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 379 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Accepts one or more relationships for the current source and returns an
22 # array of column names for each of those relationships. Column names are
23 # prefixed relative to the current source, in accordance with where they appear
24 # in the supplied relationships.
25  
26 122 ▶sub _resolve_prefetch {
Subroutine _resolve_prefetch: called CC=12 SCAR=25.4
27 122 my ($self, $pre, $alias, $alias_map, $order, $pref_path) = @_;
28 122 ▶ $pref_path ||= [];
Condition100%2/2
$pref_path ||= []
l!l
7349
MC/DC100%1/1
$pref_path ||= []
$pref_path
Truth table100%2/2
A
A= $pref_path
Aresult
11
00
29  
30 122 ▶ if (not defined $pre or not length $pre) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $pre && length $pre) { } 1 121 122
elsif (ref $pre eq 'ARRAY') { } 50 71 121
elsif (ref $pre eq 'HASH') { } 9 62 71
elsif (ref $pre) { } 0 62 62
Condition66%2/3
defined $pre && length $pre
!ll&&!rl&&r
10121
MC/DC50%1/2
defined $pre && length $pre
defined $pre length $pre
Truth table66%2/3
A && B
A= defined $preB= length $pre
ABresult
0X0
100
111
31 1 return ();
32 }
33 elsif( ref $pre eq 'ARRAY' ) {
34 return
35 50 map { $self->_resolve_prefetch( $_, $alias, $alias_map, $order, [ @$pref_path ] ) }
36 @$pre;
37 }
38 elsif( ref $pre eq 'HASH' ) {
39 my @ret =
40 map {
41 9 $self->_resolve_prefetch($_, $alias, $alias_map, $order, [ @$pref_path ] ),
42 $self->related_source($_)->_resolve_prefetch(
43 8 $pre->{$_}, "${alias}.$_", $alias_map, $order, [ @$pref_path, $_] )
44 } keys %$pre;
45 9 return @ret;
46 }
47 elsif( ref $pre ) {
48 0 $self->throw_exception(
49 "don't know how to resolve prefetch reftype ".ref($pre));
50 }
51 else {
52 62 my $p = $alias_map;
53 62 $p = $p->{$_} for (@$pref_path, $pre);
54  
55 $self->throw_exception (
56 "Unable to resolve prefetch '$pre' - join alias map does not contain an entry for path: "
57 . join (' -> ', @$pref_path, $pre)
58 62 ▶ ) if (ref $p->{-join_aliases} ne 'ARRAY' or not @{$p->{-join_aliases}} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $p->{"-join_aliases"} ne "ARRAY" or not @{$p->{"-join_aliases"};} 0 62 62
Condition33%1/3
ref $p->{"-join_aliases"} ne "ARRAY" or not @{$p->{"-join_aliases"};}
l!l&&r!l&&!r
0062
MC/DC0%0/2
ref $p->{"-join_aliases"} ne "ARRAY" or not @{$p->{"-join_aliases"};}
ref $p->{"-join_aliases"} ne "ARRAY" not @{$p->{"-join_aliases"};}
Truth table33%1/3
A or not(B)
A= ref $p->{"-join_aliases"} ne "ARRAY"B= not @{$p->{"-join_aliases"};}
ABresult
1X1
011
000
59  
60 62 my $as = shift @{$p->{-join_aliases}};
61  
62 62 my $rel_info = $self->relationship_info( $pre );
63 62 ▶ $self->throw_exception( $self->source_name . " has no such relationship '$pre'" )
Branch: 1 branch
BranchTrueFalseTotal
unless $rel_info 0 62 62
64 unless $rel_info;
65  
66 62 ▶ my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : '');
Branch: 1 branch
BranchTrueFalseTotal
$alias =~ /^.*?\.(.+)$/ ? : 5 57 62
67  
68 62 return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
69 $self->related_source($pre)->columns;
70 }
71 }
72  
73 # Takes an arrayref of {as} dbic column aliases and the collapse and select
74 # attributes from the same $rs (the selector requirement is a temporary
75 # workaround... I hope), and returns a coderef capable of:
76 # my $me_pref_clps = $coderef->([$rs->cursor->next/all])
77 # Where the $me_pref_clps arrayref is the future argument to inflate_result()
78 #
79 # For an example of this coderef in action (and to see its guts) look at
80 # t/resultset/rowparser_internals.t
81 #
82 # This is a huge performance win, as we call the same code for every row
83 # returned from the db, thus avoiding repeated method lookups when traversing
84 # relationships
85 #
86 # Also since the coderef is completely stateless (the returned structure is
87 # always fresh on every new invocation) this is a very good opportunity for
88 # memoization if further speed improvements are needed
89 #
90 # The way we construct this coderef is somewhat fugly, although the result is
91 # really worth it. The final coderef does not perform any kind of recursion -
92 # the entire nested structure constructor is rolled out into a single scope.
93 #
94 # In any case - the output of this thing is meticulously micro-tested, so
95 # any sort of adjustment/rewrite should be relatively easy (fsvo relatively)
96 #
97  
98 23 ▶sub _mk_row_parser {
Subroutine _mk_row_parser: called CC=9 SCAR=22.1
99 # $args and $attrs are separated to delineate what is core collapser stuff and
100 # what is dbic $rs specific
101 23 my ($self, $args, $attrs) = @_;
102  
103 die "HRI without pruning makes zero sense"
104 23 ▶ if ( $args->{hri_style} && ! $args->{prune_null_branches} );
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"hri_style"} and not $args->{"prune_null_branches"} 0 23 23
Condition66%2/3
$args->{"hri_style"} and not $args->{"prune_null_branches"}
!ll&&!rl&&r
2030
MC/DC0%0/2
$args->{"hri_style"} and not $args->{"prune_null_branches"}
$args->{"hri_style"} not $args->{"prune_null_branches"}
Truth table66%2/3
A and not(B)
A= $args->{"hri_style"}B= not $args->{"prune_null_branches"}
ABresult
0X0
100
111
105  
106 my %common = (
107 hri_style => $args->{hri_style},
108 prune_null_branches => $args->{prune_null_branches},
109 val_index => { map
110 200 { $args->{inflate_map}[$_] => $_ }
111 23 ( 0 .. $#{$args->{inflate_map}} )
112 },
113 );
114  
115 23 my $check_null_columns;
116  
117 23 ▶ my $src = (! $args->{collapse} ) ? assemble_simple_parser(\%common) : do {
Branch: 1 branch
BranchTrueFalseTotal
!$args->{'collapse'} ? : 7 16 23
118 my $collapse_map = $self->_resolve_collapse ({
119 # FIXME
120 # only consider real columns (not functions) during collapse resolution
121 # this check shouldn't really be here, as fucktards are not supposed to
122 # alias random crap to existing column names anyway, but still - just in
123 # case
124 # FIXME !!!! - this does not yet deal with unbalanced selectors correctly
125 # (it is now trivial as the attrs specify where things go out of sync
126 # needs MOAR tests)
127 as => { map
128 152 ▶ { ref $attrs->{select}[$common{val_index}{$_}] ? () : ( $_ => $common{val_index}{$_} ) }
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs->{'select'}[$common{'val_index'}{$_}] ? : 0 152 152
129 16 keys %{$common{val_index}}
130 },
131 premultiplied => $args->{premultiplied},
132 16 });
133  
134 $check_null_columns = $collapse_map->{-identifying_columns}
135 16 ▶ if @{$collapse_map->{-identifying_columns}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$collapse_map->{"-identifying_columns"};} 14 2 16
136  
137 16 assemble_collapsing_parser({
138 %common,
139 collapse_map => $collapse_map,
140 });
141 };
142  
143 23 ▶ utf8::upgrade($src)
Branch: 1 branch
BranchTrueFalseTotal
if &stresstest_utf8_upgrade_generated_collapser_source() 0 23 23
144 if stresstest_utf8_upgrade_generated_collapser_source;
145  
146 return (
147 23 ▶ $args->{eval} ? ( eval "sub $src" || die $@ ) : $src,
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$args->{'eval'} ? : 13 10 23
Condition50%1/2
eval "sub $src" or die $@
l!l
130
MC/DC0%0/1
eval "sub $src" or die $@
eval "sub $src"
Truth table50%1/2
A
A= eval "sub $src"
Aresult
11
00
148 $check_null_columns,
149 );
150 }
151  
152  
153 # Takes an arrayref selection list and generates a collapse-map representing
154 # row-object fold-points. Every relationship is assigned a set of unique,
155 # non-nullable columns (which may *not even be* from the same resultset)
156 # and the collapser will use this information to correctly distinguish
157 # data of individual to-be-row-objects. See t/resultset/rowparser_internals.t
158 # for extensive RV examples
159  
160 107 ▶sub _resolve_collapse {
Subroutine _resolve_collapse: called CC=67 SCAR=45.0
161 107 my ($self, $args, $common_args) = @_;
162  
163 # for comprehensible error messages put ourselves at the head of the relationship chain
164 107 ▶ $args->{_rel_chain} ||= [ $self->source_name ];
Condition100%2/2
$args->{"_rel_chain"} ||= [$self->source_name]
l!l
8819
MC/DC100%1/1
$args->{"_rel_chain"} ||= [$self->source_name]
$args->{"_rel_chain"}
Truth table100%2/2
A
A= $args->{"_rel_chain"}
Aresult
11
00
165  
166 # record top-level fully-qualified column index, signify toplevelness
167 107 ▶ unless ($common_args->{_as_fq_idx}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($common_args->{"_as_fq_idx"}) 19 88 107
168 19 $common_args->{_as_fq_idx} = { %{$args->{as}} };
169 19 $args->{_is_top_level} = 1;
170 };
171  
172 107 my ($my_cols, $rel_cols);
173 107 for (keys %{$args->{as}}) {
174 511 ▶ if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^ ([^\.]+) \. (.+) /x) { } 318 193 511
175 318 $rel_cols->{$1}{$2} = 1;
176 }
177 else {
178 193 $my_cols->{$_} = {}; # important for ||='s below
179 }
180 }
181  
182 107 my $relinfo;
183 # run through relationships, collect metadata
184 107 for my $rel (keys %$rel_cols) {
185 85 my $inf = $self->relationship_info ($rel);
186  
187 $relinfo->{$rel} = {
188 is_single => ( $inf->{attrs}{accessor} && $inf->{attrs}{accessor} ne 'multi' ),
189 85 ▶ is_inner => ( ( $inf->{attrs}{join_type} || '' ) !~ /^left/i),
Condition80%4/5
$inf->{'attrs'}{'accessor'} && $inf->{'attrs'}{'accessor'} ne 'multi'
!ll&&!rl&&r
04342
$inf->{'attrs'}{'join_type'} || ''
l!l
5233
MC/DC50%1/2
$inf->{'attrs'}{'accessor'} && $inf->{'attrs'}{'accessor'} ne 'multi'
$inf->{'attrs'}{'accessor'} $inf->{'attrs'}{'accessor'} ne 'multi'
MC/DC100%1/1
$inf->{'attrs'}{'join_type'} || ''
$inf->{'attrs'}{'join_type'}
Truth table66%2/3
A && B
A= $inf->{'attrs'}{'accessor'}B= $inf->{'attrs'}{'accessor'} ne 'multi'
ABresult
0X0
100
111
Truth table100%2/2
A
A= $inf->{'attrs'}{'join_type'}
Aresult
11
00
190 rsrc => $self->related_source($rel),
191 };
192  
193 # FIME - need to use _resolve_cond here instead
194 85 my $cond = $inf->{cond};
195  
196 85 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cond eq "HASH" and keys %$cond and not grep({not $_ =~ /^foreign\./;} keys %$cond) and not grep({not $_ =~ /^self\./;} values %$cond)) 85 0 85
Condition33%3/9
ref $cond eq "HASH" and keys %$cond and not grep({not $_ =~ /^foreign\./;} keys %$cond) and not grep({not $_ =~ /^self\./;} values %$cond)
!ll&&!rl&&r
0085
ref $cond eq "HASH" and keys %$cond and not grep({not $_ =~ /^foreign\./;} keys %$cond)
!ll&&!rl&&r
0085
ref $cond eq "HASH" and keys %$cond
!ll&&!rl&&r
0085
MC/DC0%0/4
ref $cond eq "HASH" and keys %$cond and not grep({not $_ =~ /^foreign\./;} keys %$cond) and not grep({not $_ =~ /^self\./;} values %$cond)
ref $cond eq "HASH" keys %$cond not grep({not $_ =~ /^foreign\./;} keys %$cond) not grep({not $_ =~ /^self\./;} values %$cond)
Truth table20%1/5
A and B and not(C) and not(D)
A= ref $cond eq "HASH"B= keys %$condC= not grep({not $_ =~ /^foreign\./;} keys %$cond)D= not grep({not $_ =~ /^self\./;} values %$cond)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
197 ref $cond eq 'HASH'
198 and
199 keys %$cond
200 and
201 85 ! grep { $_ !~ /^foreign\./ } (keys %$cond)
202 and
203 85 ! grep { $_ !~ /^self\./ } (values %$cond)
204 ) {
205 85 for my $f (keys %$cond) {
206 85 my $s = $cond->{$f};
207 85 $_ =~ s/^ (?: foreign | self ) \.//x for ($f, $s);
208 85 $relinfo->{$rel}{fk_map}{$s} = $f;
209 }
210 }
211 }
212  
213 # inject non-left fk-bridges from *INNER-JOINED* children (if any)
214 107 for my $rel (grep { $relinfo->{$_}{is_inner} } keys %$relinfo) {
215 36 my $ri = $relinfo->{$rel};
216 36 for (keys %{$ri->{fk_map}} ) {
217 # need to know source from *our* pov, hence $rel.col
218 $my_cols->{$_} ||= { via_fk => "$rel.$ri->{fk_map}{$_}" }
219 36 ▶ if defined $rel_cols->{$rel}{$ri->{fk_map}{$_}} # in fact selected
Branch: 1 branch
BranchTrueFalseTotal
if defined $rel_cols->{$rel}{$ri->{"fk_map"}{$_}} 18 18 36
Condition50%1/2
$my_cols->{$_} ||= {"via_fk", qq[$rel.$ri->{"fk_map"}{$_}]}
l!l
018
MC/DC0%0/1
$my_cols->{$_} ||= {"via_fk", qq[$rel.$ri->{"fk_map"}{$_}]}
$my_cols->{$_}
Truth table50%1/2
A
A= $my_cols->{$_}
Aresult
11
00
220 }
221 }
222  
223 # if the parent is already defined *AND* we have an inner reverse relationship
224 # (i.e. do not exist without it) , assume all of its related FKs are selected
225 # (even if they in fact are NOT in the select list). Keep a record of what we
226 # assumed, and if any such phantom-column becomes part of our own collapser,
227 # throw everything assumed-from-parent away and replace with the collapser of
228 # the parent (whatever it may be)
229 107 my $assumed_from_parent;
230 107 ▶ if ( ! $args->{_parent_info}{underdefined} and ! $args->{_parent_info}{rev_rel_is_optional} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"_parent_info"}{"underdefined"} or $args->{"_parent_info"}{"rev_rel_is_optional"}) 59 48 107
Condition100%3/3
$args->{"_parent_info"}{"underdefined"} or $args->{"_parent_info"}{"rev_rel_is_optional"}
l!l&&r!l&&!r
212759
MC/DC100%2/2
$args->{"_parent_info"}{"underdefined"} or $args->{"_parent_info"}{"rev_rel_is_optional"}
$args->{"_parent_info"}{"underdefined"} $args->{"_parent_info"}{"rev_rel_is_optional"}
Truth table100%3/3
A or B
A= $args->{"_parent_info"}{"underdefined"}B= $args->{"_parent_info"}{"rev_rel_is_optional"}
ABresult
1X1
011
000
231 59 ▶ for my $col ( values %{$args->{_parent_info}{rel_condition} || {}} ) {
Condition100%2/2
$args->{'_parent_info'}{'rel_condition'} || {}
l!l
4019
MC/DC100%1/1
$args->{'_parent_info'}{'rel_condition'} || {}
$args->{'_parent_info'}{'rel_condition'}
Truth table100%2/2
A
A= $args->{'_parent_info'}{'rel_condition'}
Aresult
11
00
232 40 ▶ next if exists $my_cols->{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $my_cols->{$col} 16 24 40
233 24 $my_cols->{$col} = { via_collapse => $args->{_parent_info}{collapse_on_idcols} };
234 24 $assumed_from_parent->{columns}{$col}++;
235 }
236 }
237  
238 # get colinfo for everything
239 107 ▶ if ($my_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($my_cols) 98 9 107
240 98 my $ci = $self->columns_info;
241 98 $my_cols->{$_}{colinfo} = $ci->{$_} for keys %$my_cols;
242 }
243  
244 107 my $collapse_map;
245  
246 # first try to reuse the parent's collapser (i.e. reuse collapser over 1:1)
247 # (makes for a leaner coderef later)
248 107 ▶ unless ($collapse_map->{-identifying_columns}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($collapse_map->{"-identifying_columns"}) 107 0 107
249 $collapse_map->{-identifying_columns} = $args->{_parent_info}{collapse_on_idcols}
250 107 ▶ if $args->{_parent_info}{collapser_reusable};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"_parent_info"}{"collapser_reusable"} 21 86 107
251 }
252  
253 # Still don't know how to collapse - try to resolve based on our columns (plus already inserted FK bridges)
254 107 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $collapse_map->{"-identifying_columns"} and $my_cols and my $idset = $self->_identifying_column_set({map({$_, $my_cols->{$_}{"colinfo"};} keys %$my_cols)})) 68 39 107
Condition100%6/6
not $collapse_map->{"-identifying_columns"} and $my_cols and my $idset = $self->_identifying_column_set({map({$_, $my_cols->{$_}{"colinfo"};} keys %$my_cols)})
!ll&&!rl&&r
271268
not $collapse_map->{"-identifying_columns"} and $my_cols
!ll&&!rl&&r
21680
MC/DC100%3/3
not $collapse_map->{"-identifying_columns"} and $my_cols and my $idset = $self->_identifying_column_set({map({$_, $my_cols->{$_}{"colinfo"};} keys %$my_cols)})
not $collapse_map->{"-identifying_columns"} $my_cols my $idset = $self->_identifying_column_set({map({$_, $my_cols->{$_}{"colinfo"};} keys %$my_cols)})
Truth table100%4/4
not(A) and B and C
A= not $collapse_map->{"-identifying_columns"}B= $my_colsC= my $idset = $self->_identifying_column_set({map({$_, $my_cols->{$_}{"colinfo"};} keys %$my_cols)})
ABCresult
0XX0
10X0
1100
1111
255 ! $collapse_map->{-identifying_columns}
256 and
257 $my_cols
258 and
259 217 my $idset = $self->_identifying_column_set ({map { $_ => $my_cols->{$_}{colinfo} } keys %$my_cols})
260 ) {
261 # see if the resulting collapser relies on any implied columns,
262 # and fix stuff up if this is the case
263 68 my @reduced_set = grep { ! $assumed_from_parent->{columns}{$_} } @$idset;
264  
265 $collapse_map->{-identifying_columns} = [ __unique_numlist(
266 68 ▶ @{ $args->{_parent_info}{collapse_on_idcols}||[] },
Condition100%2/2
$args->{'_parent_info'}{'collapse_on_idcols'} || []
l!l
4325
MC/DC100%1/1
$args->{'_parent_info'}{'collapse_on_idcols'} || []
$args->{'_parent_info'}{'collapse_on_idcols'}
Truth table100%2/2
A
A= $args->{'_parent_info'}{'collapse_on_idcols'}
Aresult
11
00
267  
268 (map
269 {
270 68 my $fqc = join ('.',
271 80 ▶ @{$args->{_rel_chain}}[1 .. $#{$args->{_rel_chain}}],
Condition66%2/3
$my_cols->{$_}{'via_fk'} || $_
l!l&&r!l&&!r
3770
MC/DC0%0/2
$my_cols->{$_}{'via_fk'} || $_
$my_cols->{$_}{'via_fk'} $_
Truth table66%2/3
A || B
A= $my_cols->{$_}{'via_fk'}B= $_
ABresult
1X1
011
000
272 80 ( $my_cols->{$_}{via_fk} || $_ ),
273 );
274  
275 80 $common_args->{_as_fq_idx}->{$fqc};
276 }
277 @reduced_set
278 ),
279 )];
280 }
281  
282 # Stil don't know how to collapse - keep descending down 1:1 chains - if
283 # a related non-LEFT 1:1 is resolvable - its condition will collapse us
284 # too
285 107 ▶ unless ($collapse_map->{-identifying_columns}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($collapse_map->{"-identifying_columns"}) 18 89 107
286 18 my @candidates;
287  
288 18 for my $rel (keys %$relinfo) {
289 24 ▶ next unless ($relinfo->{$rel}{is_single} && $relinfo->{$rel}{is_inner});
Branch: 1 branch
BranchTrueFalseTotal
unless $relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"} 9 15 24
Condition100%3/3
$relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"}
!ll&&!rl&&r
6315
MC/DC100%2/2
$relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"}
$relinfo->{$rel}{"is_single"} $relinfo->{$rel}{"is_inner"}
Truth table100%3/3
A and B
A= $relinfo->{$rel}{"is_single"}B= $relinfo->{$rel}{"is_inner"}
ABresult
0X0
100
111
290  
291 15 ▶ if ( my $rel_collapse = $relinfo->{$rel}{rsrc}->_resolve_collapse ({
Branch: 1 branch
BranchTrueFalseTotal
if (my $rel_collapse = $relinfo->{$rel}{"rsrc"}->_resolve_collapse({"as", $rel_cols->{$rel}, "_rel_chain", [@{$args->{"_rel_chain"};}, $rel], "_parent_info", {"underdefined", 1}}, $common_args)) 15 0 15
292 as => $rel_cols->{$rel},
293 15 _rel_chain => [ @{$args->{_rel_chain}}, $rel ],
294 _parent_info => { underdefined => 1 },
295 }, $common_args)) {
296 15 push @candidates, $rel_collapse->{-identifying_columns};
297 }
298 }
299  
300 # get the set with least amount of columns
301 # FIXME - maybe need to implement a data type order as well (i.e. prefer several ints
302 # to a single varchar)
303 18 ▶ if (@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates) 15 3 18
304 0 ($collapse_map->{-identifying_columns}) = sort { scalar @$a <=> scalar @$b } (@candidates);
305 }
306 }
307  
308 # Stil don't know how to collapse, and we are the root node. Last ditch
309 # effort in case we are *NOT* premultiplied.
310 # Run through *each multi* all the way down, left or not, and all
311 # *left* singles (a single may become a multi underneath) . When everything
312 # gets back see if all the rels link to us definitively. If this is the
313 # case we are good - either one of them will define us, or if all are NULLs
314 # we know we are "unique" due to the "non-premultiplied" check
315 107 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $collapse_map->{"-identifying_columns"} || $args->{"premultiplied"} and $args->{"_is_top_level"}) 3 104 107
Condition66%4/6
not $collapse_map->{"-identifying_columns"} || $args->{"premultiplied"} and $args->{"_is_top_level"}
!ll&&!rl&&r
10403
$collapse_map->{'-identifying_columns'} || $args->{'premultiplied'}
l!l&&r!l&&!r
10403
MC/DC33%1/3
not $collapse_map->{"-identifying_columns"} || $args->{"premultiplied"} and $args->{"_is_top_level"}
$collapse_map->{'-identifying_columns'} $args->{'premultiplied'} $args->{"_is_top_level"}
Truth table50%2/4
not(A || B) and C
A= $collapse_map->{'-identifying_columns'}B= $args->{'premultiplied'}C= $args->{"_is_top_level"}
ABCresult
1XX0
01X0
0000
0011
316 ! $collapse_map->{-identifying_columns}
317 and
318 ! $args->{premultiplied}
319 and
320 $args->{_is_top_level}
321 ) {
322 3 my (@collapse_sets, $uncollapsible_chain);
323  
324 3 for my $rel (keys %$relinfo) {
325  
326 # we already looked at these higher up
327 6 ▶ next if ($relinfo->{$rel}{is_single} && $relinfo->{$rel}{is_inner});
Branch: 1 branch
BranchTrueFalseTotal
if $relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"} 0 6 6
Condition66%2/3
$relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"}
!ll&&!rl&&r
330
MC/DC0%0/2
$relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"}
$relinfo->{$rel}{"is_single"} $relinfo->{$rel}{"is_inner"}
Truth table66%2/3
A and B
A= $relinfo->{$rel}{"is_single"}B= $relinfo->{$rel}{"is_inner"}
ABresult
0X0
100
111
328  
329 6 ▶ if (my $clps = $relinfo->{$rel}{rsrc}->_resolve_collapse ({
Branch: 1 branch
BranchTrueFalseTotal
if (my $clps = $relinfo->{$rel}{'rsrc'}->_resolve_collapse({'as', $rel_cols->{$rel}, '_rel_chain', [@{$args->{'_rel_chain'};}, $rel], '_parent_info', {'underdefined', 1}}, $common_args)) { } 6 0 6
330 as => $rel_cols->{$rel},
331 6 _rel_chain => [ @{$args->{_rel_chain}}, $rel ],
332 _parent_info => { underdefined => 1 },
333 }, $common_args) ) {
334  
335 # for singles use the idcols wholesale (either there or not)
336 6 ▶ if ($relinfo->{$rel}{is_single}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($relinfo->{$rel}{'is_single'}) { } 3 3 6
elsif (not $relinfo->{$rel}{'fk_map'}) { } 0 3 3
337 3 push @collapse_sets, $clps->{-identifying_columns};
338 }
339 elsif (! $relinfo->{$rel}{fk_map}) {
340 0 $uncollapsible_chain = 1;
341 0 last;
342 }
343 else {
344 3 my $defined_cols_parent_side;
345  
346 3 for my $fq_col ( grep { /^$rel\.[^\.]+$/ } keys %{$args->{as}} ) {
347 6 my ($col) = $fq_col =~ /([^\.]+)$/;
348  
349 6 $defined_cols_parent_side->{$_} = $args->{as}{$fq_col} for grep
350 6 { $relinfo->{$rel}{fk_map}{$_} eq $col }
351 6 keys %{$relinfo->{$rel}{fk_map}}
352 ;
353 }
354  
355 3 ▶ if (my $set = $self->_identifying_column_set([ keys %$defined_cols_parent_side ]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set = $self->_identifying_column_set([keys %$defined_cols_parent_side])) { } 3 0 3
356 3 push @collapse_sets, [ sort map { $defined_cols_parent_side->{$_} } @$set ];
357 }
358 else {
359 0 $uncollapsible_chain = 1;
360 0 last;
361 }
362 }
363 }
364 else {
365 0 $uncollapsible_chain = 1;
366 0 last;
367 }
368 }
369  
370 3 ▶ unless ($uncollapsible_chain) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uncollapsible_chain) 3 0 3
371 # if we got here - we are good to go, but the construction is tricky
372 # since our children will want to include our collapse criteria - we
373 # don't give them anything (safe, since they are all collapsible on their own)
374 # in addition we record the individual collapse possibilities
375 # of all left children node collapsers, and merge them in the rowparser
376 # coderef later
377 3 $collapse_map->{-identifying_columns} = [];
378 $collapse_map->{-identifying_columns_variants} = [ sort {
379 3 (scalar @$a) <=> (scalar @$b)
380 or
381 (
382 # Poor man's max()
383 0 ( sort { $b <=> $a } @$a )[0]
384 <=>
385 0 ▶ ( sort { $b <=> $a } @$b )[0]
Condition33%1/3
scalar @$a <=> scalar @$b || (sort {$b <=> $a;} @$a)[0] <=> (sort {$b <=> $a;} @$b)[0]
l!l&&r!l&&!r
030
MC/DC0%0/2
scalar @$a <=> scalar @$b || (sort {$b <=> $a;} @$a)[0] <=> (sort {$b <=> $a;} @$b)[0]
scalar @$a <=> scalar @$b (sort {$b <=> $a;} @$a)[0] <=> (sort {$b <=> $a;} @$b)[0]
Truth table33%1/3
A || B
A= scalar @$a <=> scalar @$bB= (sort {$b <=> $a;} @$a)[0] <=> (sort {$b <=> $a;} @$b)[0]
ABresult
1X1
011
000
386 )
387 } @collapse_sets ];
388 }
389 }
390  
391 # stop descending into children if we were called by a parent for first-pass
392 # and don't despair if nothing was found (there may be other parallel branches
393 # to dive into)
394 107 ▶ if ($args->{_parent_info}{underdefined}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'_parent_info'}{'underdefined'}) { } 21 86 107
elsif (not $collapse_map->{'-identifying_columns'}) { } 0 86 86
395 21 ▶ return $collapse_map->{-identifying_columns} ? $collapse_map : undef
Branch: 1 branch
BranchTrueFalseTotal
$collapse_map->{'-identifying_columns'} ? : 21 0 21
396 }
397 # nothing down the chain resolved - can't calculate a collapse-map
398 elsif (! $collapse_map->{-identifying_columns}) {
399 $self->throw_exception ( sprintf
400 "Unable to calculate a definitive collapse column set for %s%s: fetch more unique non-nullable columns",
401 $self->source_name,
402 0 @{$args->{_rel_chain}} > 1
403 0 ▶ ? sprintf (' (last member of the %s chain)', join ' -> ', @{$args->{_rel_chain}} )
Branch: 1 branch
BranchTrueFalseTotal
@{$args->{'_rel_chain'};} > 1 ? : 0 0 0
404 : ''
405 ,
406 );
407 }
408  
409 # If we got that far - we are collapsable - GREAT! Now go down all children
410 # a second time, and fill in the rest
411  
412 $collapse_map->{-identifying_columns} = [ __unique_numlist(
413 86 ▶ @{ $args->{_parent_info}{collapse_on_idcols}||[] },
Condition100%2/2
$args->{'_parent_info'}{'collapse_on_idcols'} || []
l!l
6719
MC/DC100%1/1
$args->{'_parent_info'}{'collapse_on_idcols'} || []
$args->{'_parent_info'}{'collapse_on_idcols'}
Truth table100%2/2
A
A= $args->{'_parent_info'}{'collapse_on_idcols'}
Aresult
11
00
414 86 @{ $collapse_map->{-identifying_columns} },
415 )];
416  
417 86 my @id_sets;
418 86 for my $rel (sort keys %$relinfo) {
419  
420 $collapse_map->{$rel} = $relinfo->{$rel}{rsrc}->_resolve_collapse ({
421 246 as => { map { $_ => 1 } ( keys %{$rel_cols->{$rel}} ) },
422 67 _rel_chain => [ @{$args->{_rel_chain}}, $rel],
423 _parent_info => {
424 # shallow copy
425 67 ▶ collapse_on_idcols => [ @{$collapse_map->{-identifying_columns}} ],
Branch: 1 branch
BranchTrueFalseTotal
grep({ref $_->{'cond'} eq 'HASH' and not +($_->{'attrs'}{'join_type'} || '') =~ /^left/i;} values %{$self->reverse_relationship_info($rel);}) ? : 40 27 67
426  
427 rel_condition => $relinfo->{$rel}{fk_map},
428  
429 is_optional => ! $relinfo->{$rel}{is_inner},
430  
431 # if there is at least one *inner* reverse relationship which is HASH-based (equality only)
432 # we can safely assume that the child can not exist without us
433 rev_rel_is_optional => ( grep
434 119 ▶ { ref $_->{cond} eq 'HASH' and ($_->{attrs}{join_type}||'') !~ /^left/i }
Condition80%4/5
ref $_->{'cond'} eq 'HASH' && !(($_->{'attrs'}{'join_type'} || '') =~ /^left/i)
!ll&&!rl&&r
04574
$_->{'attrs'}{'join_type'} || ''
l!l
4574
MC/DC50%1/2
ref $_->{'cond'} eq 'HASH' && !(($_->{'attrs'}{'join_type'} || '') =~ /^left/i)
ref $_->{'cond'} eq 'HASH' $_->{'attrs'}{'join_type'}
Truth table66%2/3
A && not(B)
A= ref $_->{'cond'} eq 'HASH'B= $_->{'attrs'}{'join_type'}
ABresult
0X0
110
101
435 67 ▶ values %{ $self->reverse_relationship_info($rel) },
Branch: 1 branch
BranchTrueFalseTotal
$relinfo->{$rel}{'is_single'} && $relinfo->{$rel}{'is_inner'} && @{$collapse_map->{'-identifying_columns'};} ? : 21 46 67
Condition83%5/6
$relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"} and @{$collapse_map->{"-identifying_columns"};}
!ll&&!rl&&r
46021
$relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"}
!ll&&!rl&&r
37921
MC/DC66%2/3
$relinfo->{$rel}{"is_single"} and $relinfo->{$rel}{"is_inner"} and @{$collapse_map->{"-identifying_columns"};}
$relinfo->{$rel}{"is_single"} $relinfo->{$rel}{"is_inner"} @{$collapse_map->{"-identifying_columns"};}
Truth table75%3/4
A and B and C
A= $relinfo->{$rel}{"is_single"}B= $relinfo->{$rel}{"is_inner"}C= @{$collapse_map->{"-identifying_columns"};}
ABCresult
0XX0
10X0
1100
1111
436 ) ? 0 : 1,
437  
438 # if this is a 1:1 our own collapser can be used as a collapse-map
439 # (regardless of left or not)
440 collapser_reusable => (
441 $relinfo->{$rel}{is_single}
442 &&
443 $relinfo->{$rel}{is_inner}
444 &&
445 67 @{$collapse_map->{-identifying_columns}}
446 ) ? 1 : 0,
447 },
448 }, $common_args );
449  
450 67 ▶ $collapse_map->{$rel}{-is_single} = 1 if $relinfo->{$rel}{is_single};
Branch: 1 branch
BranchTrueFalseTotal
if $relinfo->{$rel}{"is_single"} 30 37 67
451 67 ▶ $collapse_map->{$rel}{-is_optional} ||= 1 unless $relinfo->{$rel}{is_inner};
Branch: 1 branch
BranchTrueFalseTotal
unless $relinfo->{$rel}{"is_inner"} 43 24 67
Condition50%1/2
$collapse_map->{$rel}{"-is_optional"} ||= 1
l!l
043
MC/DC0%0/1
$collapse_map->{$rel}{"-is_optional"} ||= 1
$collapse_map->{$rel}{"-is_optional"}
Truth table50%1/2
A
A= $collapse_map->{$rel}{"-is_optional"}
Aresult
11
00
452 }
453  
454 86 return $collapse_map;
455 }
456  
457 # adding a dep on MoreUtils *just* for this is retarded
458  
459 154 ▶sub __unique_numlist {
Subroutine __unique_numlist: called CC=1 SCAR=0.0
460 154 sort { $a <=> $b } keys %{ {map { $_ => 1 } @_ }}
461 }
462  
463 379 1;
464  
465 __END__
« blib/lib/DBIO/ResultClass/HashRefInflator.pm ↑ Summary blib/lib/DBIO/EncodedColumn.pm »