blib/lib/DBIO/Relationship/Base.pm

filter: Statementstmt 79.8% 87 / 109 Branchbran 63.6% 28 / 44 Conditioncond 51.7% 15 / 29 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 79.1% 19 / 24 Podpod 100% 14 / 14 totaltotal 70.3% 166 / 236 CC 37 SCAR 33.5
CC
37
Cov
71%
related_resultset
63.7
new_related
8.9
__ANON__
6.0
CRAP 28.6
« blib/lib/DBIO/Optional/Dependencies.pm ↑ Summary blib/lib/DBIO/Ordered.pm »
1 package DBIO::Relationship::Base;
2 # ABSTRACT: Inter-table relationships
3  
4 378 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 378 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 378 our $VERSION = '0.900007';
8  
9 378 ▶use base qw/DBIO::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 378 ▶use Scalar::Util qw/weaken blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 378 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 378 ▶use DBIO::Util qw(UNRESOLVABLE_CONDITION assert_no_list_context);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 378 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 22702 ▶sub register_relationship { }
Subroutine register_relationship: called CC=1 SCAR=0.0
Pod documented
18  
19  
20 2139 ▶sub related_resultset {
Subroutine related_resultset: called CC=25 SCAR=41.5
Pod documented
21 2139 my $self = shift;
22  
23 2139 ▶ $self->throw_exception("Can't call *_related as class methods")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 2139 2139
24 unless ref $self;
25  
26 2139 my $rel = shift;
27  
28 my $rrs = defined $self->{related_resultsets}{$rel}
29 ? $self->{related_resultsets}{$rel}
30 2139 ▶ : ( $self->{related_resultsets}{$rel} = do {
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'related_resultsets'}{$rel} ? : 40 2099 2139
31  
32 2099 my $rsrc = $self->result_source;
33  
34 2099 ▶ my $rel_info = $rsrc->relationship_info($rel)
Branch: 1 branch
BranchTrueFalseTotal
my $rel_info = $rsrc->relationship_info($rel) or $self->throw_exception("No such relationship '${rel}'") 2099 0 2099
35 or $self->throw_exception( "No such relationship '$rel'" );
36  
37 2099 ▶ my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 2099 2099
Condition66%2/3
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
209810
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table66%2/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
38 2099 ▶ $attrs = { %{$rel_info->{attrs} || {}}, %$attrs };
Condition50%1/2
$rel_info->{'attrs'} || {}
l!l
20990
MC/DC0%0/1
$rel_info->{'attrs'} || {}
$rel_info->{'attrs'}
Truth table50%1/2
A
A= $rel_info->{'attrs'}
Aresult
11
00
39  
40 2099 ▶ $self->throw_exception( "Invalid query: an odd number of arguments" )
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 and @_ % 2 == 1 1 2098 2099
Condition66%2/3
@_ > 1 and @_ % 2 == 1
!ll&&!rl&&r
209801
MC/DC50%1/2
@_ > 1 and @_ % 2 == 1
@_ > 1 @_ % 2 == 1
Truth table66%2/3
A and B
A= @_ > 1B= @_ % 2 == 1
ABresult
0X0
100
111
41 if (@_ > 1 && (@_ % 2 == 1));
42 2098 ▶ my $query = ((@_ > 1) ? {@_} : shift);
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 2098 2098
43  
44 # condition resolution may fail if an incomplete master-object prefetch
45 # is encountered - that is ok during prefetch construction (not yet in_storage)
46 2098 ▶ my ($cond, $is_crosstable) = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 2098 $rsrc->_resolve_condition( $rel_info->{cond}, $rel, $self, $rel )
48 }
49 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
50 0 ▶ $self->throw_exception ($_) if $self->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_storage 0 0 0
51 0 UNRESOLVABLE_CONDITION; # RV, no return()
52 2098 };
53  
54 # keep in mind that the following if() block is part of a do{} - no return()s!!!
55 2098 ▶ if ($is_crosstable and ref $rel_info->{cond} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_crosstable and ref $rel_info->{'cond'} eq 'CODE') { } 0 2098 2098
Condition33%1/3
$is_crosstable and ref $rel_info->{"cond"} eq "CODE"
!ll&&!rl&&r
209800
MC/DC0%0/2
$is_crosstable and ref $rel_info->{"cond"} eq "CODE"
$is_crosstable ref $rel_info->{"cond"} eq "CODE"
Truth table33%1/3
A and B
A= $is_crosstableB= ref $rel_info->{"cond"} eq "CODE"
ABresult
0X0
100
111
56  
57 # A WHOREIFFIC hack to reinvoke the entire condition resolution
58 # with the correct alias. Another way of doing this involves a
59 # lot of state passing around, and the @_ positions are already
60 # mapped out, making this crap a less icky option.
61 #
62 # The point of this exercise is to retain the spirit of the original
63 # $obj->search_related($rel) where the resulting rset will have the
64 # root alias as 'me', instead of $rel (as opposed to invoking
65 # $rs->search_related)
66  
67 # make the fake 'me' rel
68 local $rsrc->{_relationships}{me} = {
69 0 %{ $rsrc->{_relationships}{$rel} },
70 _original_name => $rel,
71 };
72  
73 0 my $obj_table_alias = lc($rsrc->source_name) . '__row';
74 0 $obj_table_alias =~ s/\W+/_/g;
75  
76 0 $rsrc->resultset->search(
77 $self->ident_condition($obj_table_alias),
78 { alias => $obj_table_alias },
79 )->search_related('me', $query, $attrs)
80 }
81 else {
82 # FIXME - this conditional doesn't seem correct - got to figure out
83 # at some point what it does. Also the entire UNRESOLVABLE_CONDITION
84 # business seems shady - we could simply not query *at all*
85 2098 ▶ if ($cond eq UNRESOLVABLE_CONDITION) {
Branch: 3 branches
BranchTrueFalseTotal
if ($cond eq \'1 = 0') { } 0 2098 2098
elsif (ref $cond eq 'ARRAY') { } 15 2083 2098
elsif (ref $cond eq 'HASH') { } 2083 0 2083
86 0 my $reverse = $rsrc->reverse_relationship_info($rel);
87 0 foreach my $rev_rel (keys %$reverse) {
88 0 ▶ if ($reverse->{$rev_rel}{attrs}{accessor} && $reverse->{$rev_rel}{attrs}{accessor} eq 'multi') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse->{$rev_rel}{'attrs'}{'accessor'} and $reverse->{$rev_rel}{'attrs'}{'accessor'} eq 'multi') { } 0 0 0
Condition0%0/3
$reverse->{$rev_rel}{"attrs"}{"accessor"} and $reverse->{$rev_rel}{"attrs"}{"accessor"} eq "multi"
!ll&&!rl&&r
000
MC/DC0%0/2
$reverse->{$rev_rel}{"attrs"}{"accessor"} and $reverse->{$rev_rel}{"attrs"}{"accessor"} eq "multi"
$reverse->{$rev_rel}{"attrs"}{"accessor"} $reverse->{$rev_rel}{"attrs"}{"accessor"} eq "multi"
Truth table0%0/3
A and B
A= $reverse->{$rev_rel}{"attrs"}{"accessor"}B= $reverse->{$rev_rel}{"attrs"}{"accessor"} eq "multi"
ABresult
0X0
100
111
89 0 weaken($attrs->{related_objects}{$rev_rel}[0] = $self);
90 } else {
91 0 weaken($attrs->{related_objects}{$rev_rel} = $self);
92 }
93 }
94 }
95 elsif (ref $cond eq 'ARRAY') {
96 $cond = [ map {
97 15 ▶ if (ref $_ eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 30 0 30
98 30 my $hash;
99 30 foreach my $key (keys %$_) {
100 30 ▶ my $newkey = $key !~ /\./ ? "me.$key" : $key;
Branch: 1 branch
BranchTrueFalseTotal
!($key =~ /\./) ? : 0 30 30
101 30 $hash->{$newkey} = $_->{$key};
102 }
103 30 $hash;
104 } else {
105 0 $_;
106 }
107 } @$cond ];
108 }
109 elsif (ref $cond eq 'HASH') {
110 2083 foreach my $key (grep { ! /\./ } keys %$cond) {
111 2083 $cond->{"me.$key"} = delete $cond->{$key};
112 }
113 }
114  
115 2098 ▶ $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
Branch: 1 branch
BranchTrueFalseTotal
$query ? : 0 2098 2098
116 2098 $rsrc->related_source($rel)->resultset->search(
117 $query, $attrs
118 );
119 }
120 } );
121  
122 # ADR 0045 / karr k195: a related resultset derived from a context-bound row
123 # inherits the pin, so $row->search_related(...) and the generated
124 # ${rel}_async accessor route onto the pinned connection instead of the pool.
125 $rrs->{_async_context} = $self->{_async_context}
126 2138 ▶ if $self->{_async_context} and blessed $rrs;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_async_context"} and builtin::blessed($rrs) 18 2120 2138
Condition66%2/3
$self->{"_async_context"} and builtin::blessed($rrs)
!ll&&!rl&&r
2120018
MC/DC50%1/2
$self->{"_async_context"} and builtin::blessed($rrs)
$self->{"_async_context"} builtin::blessed($rrs)
Truth table66%2/3
A and B
A= $self->{"_async_context"}B= builtin::blessed($rrs)
ABresult
0X0
100
111
127  
128 2138 return $rrs;
129 }
130  
131  
132 2092 ▶sub search_related {
Subroutine search_related: called CC=2 SCAR=6.9
Pod documented
133 2092 ▶ assert_no_list_context('->search_related') if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 2091 2092
134 2091 return shift->related_resultset(shift)->search_rs(@_);
135 }
136  
137  
138 20 ▶sub search_related_rs {
Subroutine search_related_rs: called CC=1 SCAR=0.0
Pod documented
139 20 return shift->related_resultset(shift)->search_rs(@_);
140 }
141  
142  
143 0 ▶sub count_related {
Subroutine count_related: not called CC=1 SCAR=6.9
Pod documented
144 0 shift->search_related(@_)->count;
145 }
146  
147  
148 1491 ▶sub new_related {
Subroutine new_related: called CC=8 SCAR=21.8
Pod documented
149 1491 my ($self, $rel, $data) = @_;
150  
151 1491 my $rsrc = $self->result_source;
152 1491 my %resolve = (
153 rel_name => $rel,
154 self_result_object => $self,
155 foreign_alias => $rel,
156 self_alias => 'me',
157 );
158  
159 # ADR 0071: the foreign key the parent gives is inherited, the keys of $data
160 # are the caller's assignment and may not contradict it
161 1491 ▶ if (ref $data eq 'HASH' and %$data and ref( ($rsrc->relationship_info($rel) || {})->{cond} ) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and %$data and ref +($rsrc->relationship_info($rel) || {})->{"cond"} eq "HASH") 1479 12 1491
Condition62%5/8
ref $data eq "HASH" and %$data and ref +($rsrc->relationship_info($rel) || {})->{"cond"} eq "HASH"
!ll&&!rl&&r
1201479
ref $data eq "HASH" and %$data
!ll&&!rl&&r
0121479
$rsrc->relationship_info($rel) || {}
l!l
14790
MC/DC33%1/3
ref $data eq "HASH" and %$data and ref +($rsrc->relationship_info($rel) || {})->{"cond"} eq "HASH"
ref $data eq "HASH" %$data $rsrc->relationship_info($rel)
Truth table50%2/4
A and B and C
A= ref $data eq "HASH"B= %$dataC= $rsrc->relationship_info($rel)
ABCresult
0XX0
10X0
1100
1111
162 $rsrc->related_source($rel)->_assert_scope_agrees(
163 2963 ▶ { map { ref $data->{$_} ? () : ( $_ => $data->{$_} ) } keys %$data },
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$_} ? : 0 2963 2963
Condition50%1/2
$rsrc->_resolve_relationship_condition('infer_values_based_on', {}, %resolve)->{'inferred_values'} || {}
l!l
14790
MC/DC0%0/1
$rsrc->_resolve_relationship_condition('infer_values_based_on', {}, %resolve)->{'inferred_values'} || {}
$rsrc->_resolve_relationship_condition('infer_values_based_on', {}, %resolve)->{'inferred_values'}
Truth table50%1/2
A
A= $rsrc->_resolve_relationship_condition('infer_values_based_on', {}, %resolve)->{'inferred_values'}
Aresult
11
00
164 $rsrc->_resolve_relationship_condition(infer_values_based_on => {}, %resolve)->{inferred_values} || {},
165 1479 );
166 }
167  
168 1488 my $rs = $self->search_related($rel);
169  
170 # new_result reads which keys these are from the resultset (a new object)
171 1488 ▶ $rs->{_caller_keys} = [ keys %$data ] if ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "HASH" 1488 0 1488
172  
173 return $rs->new_result( $rsrc->_resolve_relationship_condition (
174 infer_values_based_on => $data,
175 %resolve,
176 1488 )->{inferred_values} );
177 }
178  
179  
180 1476 ▶sub create_related {
Subroutine create_related: called CC=1 SCAR=0.0
Pod documented
181 1476 my $self = shift;
182 1476 my $rel = shift;
183 1476 my $obj = $self->new_related($rel, @_)->insert;
184 1474 delete $self->{related_resultsets}->{$rel};
185 1474 return $obj;
186 }
187  
188  
189 15 ▶sub find_related {
Subroutine find_related: called CC=1 SCAR=0.0
Pod documented
190 #my ($self, $rel, @args) = @_;
191 15 return shift->search_related(shift)->find(@_);
192 }
193  
194  
195 4 ▶sub find_or_new_related {
Subroutine find_or_new_related: called CC=1 SCAR=0.0
Pod documented
196 4 my ($self, $rel) = (shift, shift);
197  
198 4 return $self->related_resultset($rel)->find_or_new(@_);
199 }
200  
201  
202 4 ▶sub find_or_create_related {
Subroutine find_or_create_related: called CC=1 SCAR=0.0
Pod documented
203 4 my ($self, $rel) = (shift, shift);
204  
205 4 my $obj = $self->related_resultset($rel)->find_or_create(@_);
206 2 delete $self->{related_resultsets}{$rel};
207  
208 2 return $obj;
209 }
210  
211  
212 0 ▶sub update_or_create_related {
Subroutine update_or_create_related: not called CC=1 SCAR=6.9
Pod documented
213 #my ($self, $rel, @args) = @_;
214 0 shift->related_resultset(shift)->update_or_create(@_);
215 }
216  
217  
218 131 ▶sub set_from_related {
Subroutine set_from_related: called CC=1 SCAR=0.0
Pod documented
219 131 my ($self, $rel, $f_obj) = @_;
220  
221 131 $self->set_columns($self->_inferred_from_related($rel, $f_obj));
222  
223 126 return 1;
224 }
225  
226 # The column values set_from_related sets on this row for a related object (or
227 # a hash of its columns, or undef): { column => value }.
228 193 ▶sub _inferred_from_related {
Subroutine _inferred_from_related: called CC=4 SCAR=13.9
229 193 my ($self, $rel, $f_obj) = @_;
230  
231 my $inferred = $self->result_source->_resolve_relationship_condition (
232 infer_values_based_on => {},
233 rel_name => $rel,
234 foreign_values => $f_obj,
235 foreign_alias => $rel,
236 self_alias => 'me',
237 193 )->{inferred_values};
238  
239 # A plain hashref of the related row's own columns can resolve to a condition
240 # that does not pin this row's columns -- e.g. a might_have searched by a
241 # non-key column infers { self_pk => undef }. Applying that would clobber real
242 # key columns with an undef foreign key. Refuse it rather than corrupt the row
243 # (ADR 0034). A blessed related object or an explicit undef (disassociation)
244 # is left to resolve as before.
245 193 ▶ if (ref $f_obj eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f_obj eq "HASH") 31 162 193
246 31 my @unresolved = sort grep { ! defined $inferred->{$_} } keys %$inferred;
247  
248 31 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if @unresolved 9 22 31
249 "Relationship '$rel' does not resolve to a concrete column condition"
250 9 . " (column(s) '@{[ join q{, }, @unresolved ]}' would be set to undef)"
251 ) if @unresolved;
252 }
253  
254 184 ▶ return $inferred || {};
Condition50%1/2
$inferred || {}
l!l
1840
MC/DC0%0/1
$inferred || {}
$inferred
Truth table50%1/2
A
A= $inferred
Aresult
11
00
255 }
256  
257  
258 0 ▶sub update_from_related {
Subroutine update_from_related: not called CC=1 SCAR=6.9
Pod documented
259 0 my $self = shift;
260 0 $self->set_from_related(@_);
261 0 $self->update;
262 }
263  
264  
265 0 ▶sub delete_related {
Subroutine delete_related: not called CC=1 SCAR=6.9
Pod documented
266 0 my $self = shift;
267 0 my $obj = $self->search_related(@_)->delete;
268 0 delete $self->{related_resultsets}->{$_[0]};
269 0 return $obj;
270 }
271  
272  
273 378 1;
274  
275 __END__
« blib/lib/DBIO/Optional/Dependencies.pm ↑ Summary blib/lib/DBIO/Ordered.pm »