blib/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm

filter: Statementstmt 91.9% 402 / 437 Branchbran 79.0% 158 / 200 Conditioncond 70.2% 104 / 148 MC/DCmcdc 53.3% 48 / 90 Subroutinesub 91.8% 34 / 37 Podpod 100% 1 / 1 totaltotal 81.8% 747 / 913 CC 175 SCAR 39.2
CC
175
Cov
85%
_update_relation
86.1
recursive_update
68.1
_delete_empty_auto_increment
30.0
CRAP 50.5
↑ Summary
1 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package DBIx::Class::ResultSet::RecursiveUpdate;
5 10 $DBIx::Class::ResultSet::RecursiveUpdate::VERSION = '0.45';
6 # ABSTRACT: like update_or_create - but recursive
7  
8 10 ▶use base qw(DBIx::Class::ResultSet);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 75 ▶sub recursive_update {
Subroutine recursive_update: called CC=5 SCAR=16.1
Pod documented
11 75 my ( $self, $updates, $attrs ) = @_;
12  
13 75 my $fixed_fields;
14 my $unknown_params_ok;
15 75 my $m2m_force_set_rel;
16  
17 # 0.21+ api
18 75 ▶ if ( defined $attrs && ref $attrs eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $attrs and ref $attrs eq 'HASH') { } 7 68 75
elsif (defined $attrs and ref $attrs eq 'ARRAY') { } 1 134 135
Condition100%3/3
defined $attrs and ref $attrs eq "HASH"
!ll&&!rl&&r
6717
MC/DC100%2/2
defined $attrs and ref $attrs eq "HASH"
defined $attrs ref $attrs eq "HASH"
Truth table100%3/3
A and B
A= defined $attrsB= ref $attrs eq "HASH"
ABresult
0X0
100
111
19 7 $fixed_fields = $attrs->{fixed_fields};
20 7 $unknown_params_ok = $attrs->{unknown_params_ok};
21 7 $m2m_force_set_rel = $attrs->{m2m_force_set_rel};
22 }
23  
24 # pre 0.21 api
25 ▶ elsif ( defined $attrs && ref $attrs eq 'ARRAY' ) {
Condition66%2/3
defined $attrs and ref $attrs eq "ARRAY"
!ll&&!rl&&r
6701
MC/DC50%1/2
defined $attrs and ref $attrs eq "ARRAY"
defined $attrs ref $attrs eq "ARRAY"
Truth table66%2/3
A and B
A= defined $attrsB= ref $attrs eq "ARRAY"
ABresult
0X0
100
111
26 1 $fixed_fields = $attrs;
27 }
28  
29 75 return DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
30 resultset => $self,
31 updates => $updates,
32 fixed_fields => $fixed_fields,
33 unknown_params_ok => $unknown_params_ok,
34 m2m_force_set_rel => $m2m_force_set_rel,
35 );
36 }
37  
38 package DBIx::Class::ResultSet::RecursiveUpdate::Functions;
39 10 $DBIx::Class::ResultSet::RecursiveUpdate::Functions::VERSION = '0.45';
40 10 ▶use v5.14; # for non-destructive substitution regex modifier
Subroutine BEGIN: called CC=1 SCAR=0.0
41 10 ▶use Carp::Clan qw/^DBIx::Class|^HTML::FormHandler|^Try::Tiny/;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 10 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 10 ▶use List::Util qw/ any all none /;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 10 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 10 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 10 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 277 ▶sub recursive_update {
Subroutine recursive_update: called CC=55 SCAR=42.2
50 277 my %params = @_;
51 my ( $self, $updates, $fixed_fields, $object, $resolved, $if_not_submitted,
52 $unknown_params_ok, $m2m_force_set_rel )
53 = @params{
54 277 qw/resultset updates fixed_fields object resolved if_not_submitted unknown_params_ok m2m_force_set_rel/
55 };
56 277 ▶ $resolved ||= {};
Condition100%2/2
$resolved ||= {}
l!l
154123
MC/DC100%1/1
$resolved ||= {}
$resolved
Truth table100%2/2
A
A= $resolved
Aresult
11
00
57 277 $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1;
58  
59 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
60 0 my $level = 0;
61 0 my $i = 0;
62 0 while (1) {
63 0 ▶ my $sub = (caller($i++))[3] or last;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = (caller $i++)[3] or last 0 0 0
64 0 ▶ $level++ if $sub =~ /(?:recursive_update|_update_relation)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /(?:recursive_update|_update_relation)$/u 0 0 0
65 }
66 0 warn map { s/^/" "x$level/egmr } @_;
67 277 } if DEBUG;
68  
69 277 my $source = $self->result_source;
70  
71 277 ▶ croak "first parameter needs to be defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $updates 0 277 277
72 unless defined $updates;
73  
74 277 ▶ croak "first parameter needs to be a hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $updates eq "HASH" 0 277 277
75 unless ref($updates) eq 'HASH';
76  
77 277 ▶ croak 'fixed fields needs to be an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_fields and ref $fixed_fields ne "ARRAY" 0 277 277
Condition66%2/3
defined $fixed_fields and ref $fixed_fields ne "ARRAY"
!ll&&!rl&&r
27520
MC/DC0%0/2
defined $fixed_fields and ref $fixed_fields ne "ARRAY"
defined $fixed_fields ref $fixed_fields ne "ARRAY"
Truth table66%2/3
A and B
A= defined $fixed_fieldsB= ref $fixed_fields ne "ARRAY"
ABresult
0X0
100
111
78 if defined $fixed_fields && ref $fixed_fields ne 'ARRAY';
79  
80 277 DEBUG and warn "recursive_update: " . $source->name . "\n";
81 277 ▶ DEBUG and warn "object passed, skipping find" .
Branch: 1 branch
BranchTrueFalseTotal
if defined $object 116 161 277
82 (defined $object->id
83 ? " (id " . $object->id . ")\n"
84 : "\n")
85 if defined $object;
86  
87 # always warn about additional parameters if storage debugging is enabled
88 277 ▶ $unknown_params_ok = 0
Branch: 1 branch
BranchTrueFalseTotal
if $source->storage->debug 147 130 277
89 if $source->storage->debug;
90  
91 277 ▶ if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($updates) and $updates->isa("DBIx::Class::Row")) 0 277 277
Condition33%1/3
builtin::blessed($updates) and $updates->isa("DBIx::Class::Row")
!ll&&!rl&&r
27700
MC/DC0%0/2
builtin::blessed($updates) and $updates->isa("DBIx::Class::Row")
builtin::blessed($updates) $updates->isa("DBIx::Class::Row")
Truth table33%1/3
A and B
A= builtin::blessed($updates)B= $updates->isa("DBIx::Class::Row")
ABresult
0X0
100
111
92 0 return $updates;
93 }
94  
95 277 my @pks = $source->primary_columns;
96 277 my %pk_kvs;
97 277 for my $colname (@pks) {
98 369 ▶ if (exists $updates->{$colname} && defined $updates->{$colname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $updates->{$colname} and defined $updates->{$colname}) 177 192 369
Condition66%2/3
exists $updates->{$colname} and defined $updates->{$colname}
!ll&&!rl&&r
1920177
MC/DC50%1/2
exists $updates->{$colname} and defined $updates->{$colname}
exists $updates->{$colname} defined $updates->{$colname}
Truth table66%2/3
A and B
A= exists $updates->{$colname}B= defined $updates->{$colname}
ABresult
0X0
100
111
99 177 $pk_kvs{$colname} = $updates->{$colname};
100 177 next;
101 }
102 $pk_kvs{$colname} = $resolved->{$colname}
103 192 ▶ if exists $resolved->{$colname} && defined $resolved->{$colname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $resolved->{$colname} and defined $resolved->{$colname} 89 103 192
Condition66%2/3
exists $resolved->{$colname} and defined $resolved->{$colname}
!ll&&!rl&&r
103089
MC/DC50%1/2
exists $resolved->{$colname} and defined $resolved->{$colname}
exists $resolved->{$colname} defined $resolved->{$colname}
Truth table66%2/3
A and B
A= exists $resolved->{$colname}B= defined $resolved->{$colname}
ABresult
0X0
100
111
104 }
105 # support the special case where a method on the related row
106 # populates one or more primary key columns and we don't have
107 # all primary key values already
108 # see DBSchema::Result::DVD relationship keysbymethod
109 277 DEBUG and warn "pk columns so far: " . join (', ',
110 sort keys %pk_kvs) . "\n";
111 my @non_pk_columns = grep {
112 277 my $colname = $_;
113 493 ▶ none { $colname eq $_ } keys %pk_kvs
Subroutine __ANON__: called CC=1 SCAR=0.0
114 }
115 sort keys %$updates;
116 277 DEBUG and warn "non-pk columns: " . join (', ',
117 @non_pk_columns) . "\n";
118 277 ▶ if ( scalar keys %pk_kvs != scalar @pks && @non_pk_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %pk_kvs != scalar @pks and @non_pk_columns) 101 176 277
Condition66%2/3
scalar keys %pk_kvs != scalar @pks and @non_pk_columns
!ll&&!rl&&r
1760101
MC/DC50%1/2
scalar keys %pk_kvs != scalar @pks and @non_pk_columns
scalar keys %pk_kvs != scalar @pks @non_pk_columns
Truth table66%2/3
A and B
A= scalar keys %pk_kvs != scalar @pksB= @non_pk_columns
ABresult
0X0
100
111
119 101 DEBUG and warn "not all primary keys available, trying " .
120 "object creation\n";
121 # new_result throws exception if non column values are passed
122 # because we want to also support e.g. a BUILDARGS method that
123 # populates primary key columns from an additional value
124 # filter out all relationships
125 my @non_rel_columns = grep {
126 101 ▶ !is_m2m( $self, $_ )
Condition100%3/3
is_m2m($self, $_) || $source->has_relationship($_)
l!l&&r!l&&!r
379124
MC/DC100%2/2
is_m2m($self, $_) || $source->has_relationship($_)
is_m2m($self, $_) $source->has_relationship($_)
Truth table100%3/3
A || B
A= is_m2m($self, $_)B= $source->has_relationship($_)
ABresult
1X1
011
000
127 && !$source->has_relationship($_)
128 }
129 sort keys %$updates;
130 my %non_rel_updates = map {
131 101 $_ => $updates->{$_}
132 } @non_rel_columns;
133 # transform columns specified by their accessor name
134 101 my %columns_by_accessor = _get_columns_by_accessor($self);
135 101 for my $accessor_name (sort keys %columns_by_accessor) {
136 404 my $colname = $columns_by_accessor{$accessor_name}->{name};
137 404 ▶ if ($accessor_name ne $colname
Branch: 1 branch
BranchTrueFalseTotal
if ($accessor_name ne $colname and exists $non_rel_updates{$accessor_name}) 12 392 404
Condition100%3/3
$accessor_name ne $colname and exists $non_rel_updates{$accessor_name}
!ll&&!rl&&r
3801212
MC/DC100%2/2
$accessor_name ne $colname and exists $non_rel_updates{$accessor_name}
$accessor_name ne $colname exists $non_rel_updates{$accessor_name}
Truth table100%3/3
A and B
A= $accessor_name ne $colnameB= exists $non_rel_updates{$accessor_name}
ABresult
0X0
100
111
138 && exists $non_rel_updates{$accessor_name}) {
139 12 DEBUG and warn "renaming column accessor " .
140 "'$accessor_name' to column name '$colname'\n";
141 $non_rel_updates{$colname} = delete
142 12 $non_rel_updates{$accessor_name};
143 }
144 }
145 101 DEBUG and warn "using all non-rel updates for object " .
146 "construction: " . Dumper(\%non_rel_updates);
147 # the object creation might fail because of non-column and
148 # non-constructor handled parameters which shouldn't break RU
149 101 ▶ try {
Subroutine __ANON__: called CC=5 SCAR=16.1
150 101 my $row = $self->new_result(\%non_rel_updates);
151 98 for my $colname (@pks) {
152 next
153 122 ▶ if exists $pk_kvs{$colname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pk_kvs{$colname} 22 100 122
154  
155 100 ▶ if ($row->can($colname)
Branch: 1 branch
BranchTrueFalseTotal
if ($row->can($colname) and defined $row->$colname) { } 6 94 100
Condition66%2/3
$row->can($colname) and defined $row->$colname
!ll&&!rl&&r
0946
MC/DC50%1/2
$row->can($colname) and defined $row->$colname
$row->can($colname) defined $row->$colname
Truth table66%2/3
A and B
A= $row->can($colname)B= defined $row->$colname
ABresult
0X0
100
111
156 && defined $row->$colname) {
157 6 DEBUG and warn "missing pk column $colname exists " .
158 "and defined on object\n";
159 6 $pk_kvs{$colname} = $row->$colname;
160 }
161 else {
162 94 DEBUG and warn "missing pk column $colname doesn't "
163 . "exist or isn't defined on object, aborting\n";
164 94 last;
165 }
166 }
167 }
168 3 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
169 3 DEBUG and warn "object construction failed, ignoring:
170 $_\n";
171 101 };
172 }
173  
174 # check if row can be found in resultset cache
175 277 ▶ if ( !defined $object && scalar keys %pk_kvs == scalar @pks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $object and scalar keys %pk_kvs == scalar @pks) 106 171 277
Condition100%3/3
not defined $object and scalar keys %pk_kvs == scalar @pks
!ll&&!rl&&r
11655106
MC/DC100%2/2
not defined $object and scalar keys %pk_kvs == scalar @pks
not defined $object scalar keys %pk_kvs == scalar @pks
Truth table100%3/3
not(A) and B
A= not defined $objectB= scalar keys %pk_kvs == scalar @pks
ABresult
0X0
100
111
176 106 my $cached_rows = $self->get_cache;
177 106 ▶ if (defined $cached_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cached_rows) 4 102 106
178 4 DEBUG and warn "find in cache\n";
179 4 $object = _get_matching_row(\%pk_kvs, $cached_rows)
180 }
181 }
182  
183 277 $updates = { %$updates, %$resolved };
184  
185 277 my %fixed_fields = map { $_ => 1 } @$fixed_fields;
186  
187 # add the resolved columns to the updates hashref
188 277 my %all_pks = ( %pk_kvs, %fixed_fields );
189  
190 277 ▶ if ( !defined $object && scalar keys %all_pks == scalar @pks) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $object and scalar keys %all_pks == scalar @pks) 102 175 277
Condition100%3/3
not defined $object and scalar keys %all_pks == scalar @pks
!ll&&!rl&&r
12055102
MC/DC100%2/2
not defined $object and scalar keys %all_pks == scalar @pks
not defined $object scalar keys %all_pks == scalar @pks
Truth table100%3/3
not(A) and B
A= not defined $objectB= scalar keys %all_pks == scalar @pks
ABresult
0X0
100
111
191 102 DEBUG and warn "find by pk\n";
192 102 $object = $self->find( \%all_pks, { key => 'primary' } );
193 }
194  
195 277 ▶ unless (defined $object) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 67 210 277
196 67 DEBUG and warn "create new row\n";
197 67 $object = $self->new_result( {} );
198 }
199  
200 # direct column accessors
201 277 my %columns;
202  
203 # relations that that should be done before the row is inserted into the
204 # database like belongs_to
205 my %pre_updates;
206  
207 # relations that that should be done after the row is inserted into the
208 # database like has_many, might_have and has_one
209 277 my %post_updates;
210 277 my %other_methods;
211 277 my %m2m_accessors;
212 277 my %columns_by_accessor = _get_columns_by_accessor($self);
213  
214 # this section determines to what each key/value pair maps to,
215 # column or relationship
216 277 for my $name ( sort keys %$updates ) {
217 DEBUG and warn "updating $name to "
218 615 . ($updates->{$name} // '[undef]') . "\n";
219 # columns
220 615 ▶ if ( exists $columns_by_accessor{$name} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $columns_by_accessor{$name} and not $source->has_relationship($name) && ref $updates->{$name}) 443 172 615
Condition100%6/6
exists $columns_by_accessor{$name} and not $source->has_relationship($name) && ref $updates->{$name}
!ll&&!rl&&r
11656443
$source->has_relationship($name) && ref $updates->{$name}
!ll&&!rl&&r
30114256
MC/DC100%3/3
exists $columns_by_accessor{$name} and not $source->has_relationship($name) && ref $updates->{$name}
exists $columns_by_accessor{$name} $source->has_relationship($name) ref $updates->{$name}
Truth table100%4/4
A and not(B && C)
A= exists $columns_by_accessor{$name}B= $source->has_relationship($name)C= ref $updates->{$name}
ABCresult
0XX0
1110
10X1
1101
221 !( $source->has_relationship($name) && ref( $updates->{$name} ) ) ) {
222 443 $columns{$name} = $updates->{$name};
223 443 next;
224 }
225  
226 # relationships
227 172 ▶ if ( $source->has_relationship($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->has_relationship($name)) 130 42 172
228 130 ▶ if ( _master_relation_cond( $self, $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_master_relation_cond($self, $name)) { } 82 48 130
229 82 $pre_updates{$name} = $updates->{$name};
230 82 next;
231 }
232 else {
233 48 $post_updates{$name} = $updates->{$name};
234 48 next;
235 }
236 }
237  
238 # many-to-many helper accessors
239 42 ▶ if ( is_m2m( $self, $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_m2m($self, $name)) 37 5 42
240 37 DEBUG and warn "is m2m\n";
241 # Transform m2m data into recursive has_many data
242 # if IntrospectableM2M is in use.
243 #
244 # This removes the overhead related to deleting and
245 # re-adding all relationships.
246 37 ▶ if ( !$m2m_force_set_rel && $source->result_class->can('_m2m_metadata') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $m2m_force_set_rel and $source->result_class->can('_m2m_metadata')) { } 27 10 37
Condition100%3/3
not $m2m_force_set_rel and $source->result_class->can("_m2m_metadata")
!ll&&!rl&&r
4627
MC/DC100%2/2
not $m2m_force_set_rel and $source->result_class->can("_m2m_metadata")
not $m2m_force_set_rel $source->result_class->can("_m2m_metadata")
Truth table100%3/3
not(A) and B
A= not $m2m_force_set_relB= $source->result_class->can("_m2m_metadata")
ABresult
0X0
100
111
247 27 my $meta = $source->result_class->_m2m_metadata->{$name};
248 27 my $bridge_rel = $meta->{relation};
249 27 my $foreign_rel = $meta->{foreign_relation};
250  
251 $post_updates{$bridge_rel} = [
252 map {
253 70 { $foreign_rel => $_ }
254 27 } @{ $updates->{$name} }
255 ];
256 DEBUG and warn "m2m '$name' transformed to:\n$bridge_rel => " .
257 27 Dumper($post_updates{$bridge_rel}) . "\n";
258 }
259 # Fall back to set_$rel if IntrospectableM2M
260 # is not available. (removing and re-adding all relationships)
261 else {
262 10 $m2m_accessors{$name} = $updates->{$name};
263 }
264  
265 37 next;
266 }
267  
268 # accessors
269 5 ▶ if ( $object->can($name) && not $source->has_relationship($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can($name) and not $source->has_relationship($name)) 2 3 5
Condition66%2/3
$object->can($name) and not $source->has_relationship($name)
!ll&&!rl&&r
302
MC/DC50%1/2
$object->can($name) and not $source->has_relationship($name)
$object->can($name) not $source->has_relationship($name)
Truth table66%2/3
A and not(B)
A= $object->can($name)B= not $source->has_relationship($name)
ABresult
0X0
100
111
270 2 $other_methods{$name} = $updates->{$name};
271 2 next;
272 }
273  
274 # unknown
275  
276 # don't throw a warning instead of an exception to give users
277 # time to adapt to the new API
278 carp(
279 3 ▶ "No such column, relationship, many-to-many helper accessor or " .
Branch: 1 branch
BranchTrueFalseTotal
unless $unknown_params_ok 2 1 3
280 "generic accessor '$name' on '" . $source->name . "'"
281 ) unless $unknown_params_ok;
282  
283 }
284  
285 # first update columns and other accessors
286 # so that later related records can be found
287 277 for my $name ( sort keys %columns ) {
288 431 $object->$name( $columns{$name} );
289 }
290 271 for my $name ( sort keys %other_methods ) {
291 2 $object->$name( $other_methods{$name} );
292 }
293 271 for my $name ( sort keys %pre_updates ) {
294 79 _update_relation( $self, $name, $pre_updates{$name}, $object, $if_not_submitted, 0 );
295 }
296  
297 # $self->_delete_empty_auto_increment($object);
298 # don't allow insert to recurse to related objects
299 # do the recursion ourselves
300 # $object->{_rel_in_storage} = 1;
301 # Update if %other_methods because of possible custom update method
302 267 my $in_storage = $object->in_storage;
303  
304 # preserve related resultsets as DBIx::Class::Row->update clears them
305 # yes, this directly accesses a row attribute, but no API exists and in
306 # the hope to get the recursive_update feature into core DBIx::Class this
307 # is the easiest solution
308 267 my $related_resultsets = $object->{related_resultsets};
309  
310 267 DEBUG and warn "before update_or_insert\n";
311 267 ▶ $object->update_or_insert if ( $object->is_changed || keys %other_methods );
Branch: 1 branch
BranchTrueFalseTotal
if $object->is_changed or keys %other_methods 152 115 267
Condition66%2/3
$object->is_changed or keys %other_methods
l!l&&r!l&&!r
1520115
MC/DC50%1/2
$object->is_changed or keys %other_methods
$object->is_changed keys %other_methods
Truth table66%2/3
A or B
A= $object->is_changedB= keys %other_methods
ABresult
1X1
011
000
312 267 DEBUG and warn "after update_or_insert\n";
313  
314 # restore related resultsets
315 267 $object->{related_resultsets} = $related_resultsets;
316  
317 # updating many_to_many
318 267 for my $name ( sort keys %m2m_accessors ) {
319 10 DEBUG and warn "updating m2m $name\n";
320 10 my $value = $m2m_accessors{$name};
321  
322 # TODO: only first pk col is used
323 10 my ($pk) = _get_pk_for_related( $self, $name );
324 10 my @rows;
325 10 my $rel_source = $object->$name->result_source;
326 10 my @updates;
327 10 ▶ if ( defined $value && ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $value and ref $value eq 'ARRAY') { } 10 0 10
elsif (defined $value and not ref $value) { } 0 0 0
elsif (defined $value) { } 0 0 0
Condition33%1/3
defined $value and ref $value eq "ARRAY"
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $value and ref $value eq "ARRAY"
defined $value ref $value eq "ARRAY"
Truth table33%1/3
A and B
A= defined $valueB= ref $value eq "ARRAY"
ABresult
0X0
100
111
328 10 @updates = @{$value};
329 }
330 ▶ elsif ( defined $value && !ref $value ) {
Condition0%0/3
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table0%0/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
331 0 @updates = ($value);
332 }
333 elsif ( defined $value ) {
334 0 carp "value of many-to-many rel '$name' must be an arrayref or scalar: $value";
335 }
336 10 for my $elem (@updates) {
337 23 ▶ if ( blessed($elem) && $elem->isa('DBIx::Class::Row') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($elem) and $elem->isa('DBIx::Class::Row')) { } 0 23 23
elsif (ref $elem eq 'HASH') { } 5 18 23
Condition33%1/3
builtin::blessed($elem) and $elem->isa("DBIx::Class::Row")
!ll&&!rl&&r
2300
MC/DC0%0/2
builtin::blessed($elem) and $elem->isa("DBIx::Class::Row")
builtin::blessed($elem) $elem->isa("DBIx::Class::Row")
Truth table33%1/3
A and B
A= builtin::blessed($elem)B= $elem->isa("DBIx::Class::Row")
ABresult
0X0
100
111
338 0 push @rows, $elem;
339 }
340 elsif ( ref $elem eq 'HASH' ) {
341 5 push @rows,
342 recursive_update(
343 resultset => $rel_source->resultset,
344 updates => $elem
345 );
346 }
347 else {
348 18 push @rows, $rel_source->resultset->find( { $pk => $elem } );
349 }
350 }
351 10 my $set_meth = 'set_' . $name;
352 10 $object->$set_meth( \@rows );
353 }
354 267 for my $name ( sort keys %post_updates ) {
355 67 _update_relation( $self, $name, $post_updates{$name}, $object, $if_not_submitted, $in_storage );
356 }
357 262 delete $ENV{DBIC_NULLABLE_KEY_NOWARN};
358 262 return $object;
359 }
360  
361 # returns DBIx::Class::ResultSource::column_info as a hash indexed by column accessor || name
362 400 ▶sub _get_columns_by_accessor {
Subroutine _get_columns_by_accessor: called CC=3 SCAR=11.0
363 400 my $self = shift;
364 400 my $source = $self->result_source;
365 400 my %columns;
366 400 for my $name ( $source->columns ) {
367 1548 my $info = $source->column_info($name);
368 1548 ▶ $columns{ $info->{accessor} || $name } = { %$info, name => $name };
Condition66%2/3
$info->{'accessor'} || $name
l!l&&r!l&&!r
7214760
MC/DC0%0/2
$info->{'accessor'} || $name
$info->{'accessor'} $name
Truth table66%2/3
A || B
A= $info->{'accessor'}B= $name
ABresult
1X1
011
000
369 }
370 400 return %columns;
371 }
372  
373 100 ▶sub _get_matching_row {
Subroutine _get_matching_row: called CC=12 SCAR=25.7
374 100 my ($kvs, $rows) = @_;
375  
376 return
377 100 ▶ unless defined $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rows 0 100 100
378  
379 100 ▶ croak 'key/value need to be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $kvs eq "HASH" 0 100 100
380 unless ref $kvs eq 'HASH';
381  
382 100 ▶ croak 'key/value needs to have at least one pair'
Branch: 1 branch
BranchTrueFalseTotal
if keys %$kvs == 0 0 100 100
383 if keys %$kvs == 0;
384  
385 100 ▶ croak 'rows need to be an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" 0 100 100
386 unless ref $rows eq 'ARRAY';
387  
388 100 ▶ unless ($rows) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rows) 0 100 100
389 0 DEBUG and warn "skipping because no rows passed\n";
390 0 return;
391 }
392  
393 100 my $matching_row;
394  
395 my @matching_rows;
396 100 for my $row (@$rows) {
397 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
398 push @matching_rows, $row
399 444 ▶ if all { $kvs->{$_} eq $row->get_column($_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
400 239 ▶ grep { !ref $kvs->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { $kvs->{$_} eq $row->get_column($_); } , grep({not ref $kvs->{$_};} sort(keys %$kvs))) 62 177 239
401 sort keys %$kvs;
402 }
403 100 ▶ DEBUG and warn "multiple matching rows: " . scalar @matching_rows . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if @matching_rows > 1 0 100 100
404 if @matching_rows > 1;
405 100 ▶ $matching_row = $matching_rows[0]
Branch: 1 branch
BranchTrueFalseTotal
if scalar @matching_rows == 1 62 38 100
406 if scalar @matching_rows == 1;
407 DEBUG and warn "matching row found for: " . Dumper($kvs) . " in " .
408 100 ▶ Dumper([map { { $_->get_columns } } @$rows]) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $matching_row 62 38 100
409 if defined $matching_row;
410 DEBUG and warn "matching row not found for: " . Dumper($kvs) . " in " .
411 100 ▶ Dumper([map { { $_->get_columns } } @$rows]) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matching_row 38 62 100
412 unless defined $matching_row;
413  
414 100 return $matching_row;
415 }
416  
417 # Arguments: $rs, $name, $updates, $row, $if_not_submitted, $row_existed
418 146 ▶sub _update_relation {
Subroutine _update_relation: called CC=74 SCAR=44.6
419 146 my ( $self, $name, $updates, $object, $if_not_submitted, $row_existed ) = @_;
420  
421 # this should never happen because we're checking the paramters passed to
422 # recursive_update, but just to be sure...
423 146 ▶ $object->throw_exception("No such relationship '$name'")
Branch: 1 branch
BranchTrueFalseTotal
unless $object->has_relationship($name) 0 146 146
424 unless $object->has_relationship($name);
425  
426 146 DEBUG and warn "_update_relation: $name\n";
427  
428 146 my $info = $object->result_source->relationship_info($name);
429 146 my $attrs = $info->{attrs};
430  
431 # get a related resultset without a condition
432 146 my $related_source = $self->related_resultset($name)->result_source;
433 146 my $related_resultset = $related_source->resultset;
434 146 ▶ $self->throw_exception("result_source must support _resolve_condition")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->result_source->can("_resolve_condition") 0 146 146
435 unless $self->result_source->can('_resolve_condition');
436 146 my $resolved = $self->result_source->_resolve_condition( $info->{cond}, $name, $object, $name );
437  
438 146 ▶ $resolved = {}
Branch: 1 branch
BranchTrueFalseTotal
if defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION and $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved 46 100 146
Condition66%2/3
defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION and $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved
!ll&&!rl&&r
010046
MC/DC50%1/2
defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION and $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved
defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved
Truth table66%2/3
A and B
A= defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITIONB= $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved
ABresult
0X0
100
111
439 if defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION &&
440 $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved;
441  
442 # This is a hack. I'm not sure that this will handle most
443 # custom code conditions yet. This needs tests.
444 146 my @rel_cols;
445 146 ▶ if ( ref $info->{cond} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info->{'cond'} eq 'CODE') { } 0 146 146
446 0 my $new_resolved;
447 # remove 'me.' from keys in returned hashref
448 0 ▶ while ( my ( $key, $value ) = each %$resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$resolved) 0 0 0
449 0 $key =~ s/^me\.//;
450 0 $new_resolved->{$key} = $value;
451 0 push @rel_cols, $key;
452 }
453 0 $resolved = $new_resolved;
454 }
455 else {
456 146 @rel_cols = sort keys %{ $info->{cond} };
457 146 map { s/^foreign\.// } @rel_cols;
458 }
459  
460 # find out if all related columns are nullable
461 146 my $all_fks_nullable = 1;
462 146 for my $rel_col (@rel_cols) {
463 $all_fks_nullable = 0
464 146 ▶ unless $related_resultset->result_source->column_info($rel_col)->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
unless $related_resultset->result_source->column_info($rel_col)->{"is_nullable"} 141 5 146
465 }
466  
467 146 ▶ $if_not_submitted = $all_fks_nullable ? 'set_to_null' : 'delete'
Branch: 2 branches
BranchTrueFalseTotal
unless defined $if_not_submitted 144 2 146
$all_fks_nullable ? : 3 141 144
468 unless defined $if_not_submitted;
469  
470 # the only valid datatype for a has_many rels is an arrayref
471 146 ▶ if ( $attrs->{accessor} eq 'multi' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attrs->{'accessor'} eq 'multi') { } 59 87 146
elsif ($attrs->{'accessor'} eq 'single' or $attrs->{'accessor'} eq 'filter') { } 87 0 87
472 59 DEBUG and warn "has_many: $name\n";
473  
474 # handle undef like empty arrayref
475 59 ▶ $updates = []
Branch: 1 branch
BranchTrueFalseTotal
unless defined $updates 1 58 59
476 unless defined $updates;
477 59 ▶ $self->throw_exception("data for has_many relationship '$name' must be an arrayref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $updates eq "ARRAY" 0 59 59
478 unless ref $updates eq 'ARRAY';
479  
480 59 my @updated_objs;
481 my @related_rows;
482 # newly created rows can't have related rows
483 59 ▶ if ($row_existed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_existed) 38 21 59
484 38 @related_rows = $object->$name;
485 38 DEBUG and warn "got related rows: " . scalar @related_rows . "\n";
486 }
487 59 my $related_result_source = $related_resultset->result_source;
488 59 my @pks = $related_result_source->primary_columns;
489  
490 59 for my $sub_updates ( @{$updates} ) {
491 116 DEBUG and warn "updating related row: " . Dumper($sub_updates)
492 . "\n";
493 116 my %pk_kvs;
494 # detect the special case where the primary key of a currently not
495 # related row is passed in the updates hash
496 # let the resolved column values fill any missing primary key
497 # columns but not overwrite them
498 116 for my $colname (@pks) {
499 204 ▶ if (exists $sub_updates->{$colname}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sub_updates->{$colname} and defined $sub_updates->{$colname}) 69 135 204
Condition66%2/3
exists $sub_updates->{$colname} and defined $sub_updates->{$colname}
!ll&&!rl&&r
135069
MC/DC50%1/2
exists $sub_updates->{$colname} and defined $sub_updates->{$colname}
exists $sub_updates->{$colname} defined $sub_updates->{$colname}
Truth table66%2/3
A and B
A= exists $sub_updates->{$colname}B= defined $sub_updates->{$colname}
ABresult
0X0
100
111
500 && defined $sub_updates->{$colname}) {
501 # $sub_updates->{$colname} might be a hashref if a
502 # relationship is named the same as a foreign key column
503 69 ▶ if (ref $sub_updates->{$colname} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub_updates->{$colname} eq 'HASH') { } 29 40 69
504 29 ▶ if ($related_source->has_relationship($colname)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($related_source->has_relationship($colname)) { } 29 0 29
505 29 my $rel_info = $related_source
506 ->relationship_info($colname);
507 29 my @rel_cols = sort keys %{ $rel_info->{cond} };
508 29 map { s/^foreign\.// } @rel_cols;
509 29 ▶ $self->throw_exception("passing a hashref for " .
Branch: 1 branch
BranchTrueFalseTotal
if scalar @rel_cols != 1 0 29 29
510 "a multi-column relationship named the " .
511 "same as a column ('$colname') is not " .
512 "implemented")
513 if scalar @rel_cols != 1;
514 29 DEBUG and warn "using '$rel_cols[0]' in hashref " .
515 "for primary key column '$colname'\n";
516 $pk_kvs{$colname} = $sub_updates->{$colname}
517 29 ->{$rel_cols[0]};
518 }
519 else {
520 0 $self->throw_exception(
521 "data for $colname is a hashref but no " .
522 "relationship with that name exists");
523 }
524 }
525 else {
526 40 $pk_kvs{$colname} = $sub_updates->{$colname};
527 }
528 69 next;
529 }
530 $pk_kvs{$colname} = $resolved->{$colname}
531 if exists $resolved->{$colname}
532 135 ▶ && defined $resolved->{$colname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $resolved->{$colname} and defined $resolved->{$colname} 83 52 135
Condition66%2/3
exists $resolved->{$colname} and defined $resolved->{$colname}
!ll&&!rl&&r
52083
MC/DC50%1/2
exists $resolved->{$colname} and defined $resolved->{$colname}
exists $resolved->{$colname} defined $resolved->{$colname}
Truth table66%2/3
A and B
A= exists $resolved->{$colname}B= defined $resolved->{$colname}
ABresult
0X0
100
111
533 }
534 116 my $related_object;
535  
536 my @missing_pks = grep {
537 116 my $pkcolname = $_;
538 204 ▶ none { $_ eq $pkcolname } keys %pk_kvs;
Subroutine __ANON__: called CC=1 SCAR=0.0
539 } @pks;
540  
541 # support the special case where a method on the related row
542 # populates one or more primary key columns and we don't have
543 # all primary key values already
544 # see DBSchema::Result::DVD relationship keysbymethod
545 116 DEBUG and warn "pk columns so far: " . join (', ',
546 sort keys %pk_kvs) . "\n";
547 116 DEBUG and warn "pk columns missing: " . join (', ',
548 sort @missing_pks) . "\n";
549 my @non_pk_columns = grep {
550 116 my $colname = $_;
551 143 ▶ none { $colname eq $_ } keys %pk_kvs
Subroutine __ANON__: called CC=1 SCAR=0.0
552 }
553 sort keys %$sub_updates;
554 116 DEBUG and warn "non-pk columns: " . join (', ',
555 @non_pk_columns) . "\n";
556 116 ▶ if ( scalar keys %pk_kvs != scalar @pks && @non_pk_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %pk_kvs != scalar @pks and @non_pk_columns) 51 65 116
Condition66%2/3
scalar keys %pk_kvs != scalar @pks and @non_pk_columns
!ll&&!rl&&r
65051
MC/DC50%1/2
scalar keys %pk_kvs != scalar @pks and @non_pk_columns
scalar keys %pk_kvs != scalar @pks @non_pk_columns
Truth table66%2/3
A and B
A= scalar keys %pk_kvs != scalar @pksB= @non_pk_columns
ABresult
0X0
100
111
557 51 DEBUG and warn "not all primary keys available, trying " .
558 "relationships\n";
559 my @rel_names = grep {
560 51 $related_result_source->has_relationship($_)
561 } sort keys %$sub_updates;
562  
563 51 for my $relname (@rel_names) {
564 34 my $rel_info = $related_result_source
565 ->relationship_info($relname);
566 next
567 unless $rel_info->{attrs}->{accessor} eq 'single'
568 34 ▶ && $rel_info->{attrs}->{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $rel_info->{"attrs"}{"accessor"} eq "single" and $rel_info->{"attrs"}{"is_foreign_key_constraint"} 3 31 34
Condition100%3/3
$rel_info->{"attrs"}{"accessor"} eq "single" and $rel_info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
2131
MC/DC100%2/2
$rel_info->{"attrs"}{"accessor"} eq "single" and $rel_info->{"attrs"}{"is_foreign_key_constraint"}
$rel_info->{"attrs"}{"accessor"} eq "single" $rel_info->{"attrs"}{"is_foreign_key_constraint"}
Truth table100%3/3
A and B
A= $rel_info->{"attrs"}{"accessor"} eq "single"B= $rel_info->{"attrs"}{"is_foreign_key_constraint"}
ABresult
0X0
100
111
569  
570 31 for my $f_key ( sort keys %{$rel_info->{cond}} ) {
571 31 my $col = $rel_info->{cond}->{$f_key};
572 31 $col =~ s/^self\.//;
573 31 $f_key =~ s/^foreign\.//;
574  
575 next
576 31 ▶ unless any { $col eq $_ } @missing_pks;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $col eq $_; } , @missing_pks) 1 30 31
577  
578 30 DEBUG and warn "populating primary key column " .
579 "'$col' from relationship '$relname'\n";
580 # rel holds a scalar value
581 30 ▶ if (keys %{$rel_info->{cond}} == 1 && !ref $sub_updates->{$relname}) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$$rel_info{'cond'};} == 1 and not ref $sub_updates->{$relname}) { } 10 20 30
elsif (ref $sub_updates->{$relname} eq 'HASH') { } 20 0 20
Condition66%2/3
keys %{$$rel_info{"cond"};} == 1 and not ref $sub_updates->{$relname}
!ll&&!rl&&r
02010
MC/DC50%1/2
keys %{$$rel_info{"cond"};} == 1 and not ref $sub_updates->{$relname}
keys %{$$rel_info{"cond"};} == 1 not ref $sub_updates->{$relname}
Truth table66%2/3
A and not(B)
A= keys %{$$rel_info{"cond"};} == 1B= not ref $sub_updates->{$relname}
ABresult
0X0
100
111
582 10 $pk_kvs{$col} = delete $sub_updates->{$relname};
583 # optimization to not update the related row
584 # when only the primary key of it is given
585 10 $sub_updates->{$col} = $pk_kvs{$col};
586 }
587 # rel holds a hashref
588 elsif (ref $sub_updates->{$relname} eq 'HASH') {
589 $pk_kvs{$col} =
590 20 $sub_updates->{$relname}->{$f_key};
591 }
592 else {
593 0 $self->throw_exception("passing anything else " .
594 "but a scalar or a hashref is not " .
595 "implemented");
596 }
597 }
598 }
599 }
600  
601 116 ▶ if ( scalar keys %pk_kvs != scalar @pks && @non_pk_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %pk_kvs != scalar @pks and @non_pk_columns) 21 95 116
Condition66%2/3
scalar keys %pk_kvs != scalar @pks and @non_pk_columns
!ll&&!rl&&r
95021
MC/DC50%1/2
scalar keys %pk_kvs != scalar @pks and @non_pk_columns
scalar keys %pk_kvs != scalar @pks @non_pk_columns
Truth table66%2/3
A and B
A= scalar keys %pk_kvs != scalar @pksB= @non_pk_columns
ABresult
0X0
100
111
602 21 DEBUG and warn "not all primary keys available, trying " .
603 "object creation\n";
604 # new_result throws exception if non column values are passed
605 # because we want to also support e.g. a BUILDARGS method that
606 # populates primary key columns from an additional value
607 # filter out all relationships
608 my @non_rel_columns = grep {
609 21 ▶ !is_m2m( $related_resultset, $_ )
Condition100%3/3
is_m2m($related_resultset, $_) || $related_result_source->has_relationship($_)
l!l&&r!l&&!r
2435
MC/DC100%2/2
is_m2m($related_resultset, $_) || $related_result_source->has_relationship($_)
is_m2m($related_resultset, $_) $related_result_source->has_relationship($_)
Truth table100%3/3
A || B
A= is_m2m($related_resultset, $_)B= $related_result_source->has_relationship($_)
ABresult
1X1
011
000
610 && !$related_result_source->has_relationship($_)
611 }
612 sort keys %$sub_updates;
613 my %non_rel_updates = map {
614 21 $_ => $sub_updates->{$_}
615 } @non_rel_columns;
616 # transform columns specified by their accessor name
617 21 my %columns_by_accessor = _get_columns_by_accessor($related_resultset);
618 21 for my $accessor_name (sort keys %columns_by_accessor) {
619 107 my $colname = $columns_by_accessor{$accessor_name}->{name};
620 107 ▶ if ($accessor_name ne $colname
Branch: 1 branch
BranchTrueFalseTotal
if ($accessor_name ne $colname and exists $non_rel_updates{$accessor_name}) 10 97 107
Condition100%3/3
$accessor_name ne $colname and exists $non_rel_updates{$accessor_name}
!ll&&!rl&&r
871010
MC/DC100%2/2
$accessor_name ne $colname and exists $non_rel_updates{$accessor_name}
$accessor_name ne $colname exists $non_rel_updates{$accessor_name}
Truth table100%3/3
A and B
A= $accessor_name ne $colnameB= exists $non_rel_updates{$accessor_name}
ABresult
0X0
100
111
621 && exists $non_rel_updates{$accessor_name}) {
622 10 DEBUG and warn "renaming column accessor " .
623 "'$accessor_name' to column name '$colname'\n";
624 $non_rel_updates{$colname} = delete
625 10 $non_rel_updates{$accessor_name};
626 }
627 }
628 21 DEBUG and warn "using all non-rel updates for object " .
629 "construction: " . Dumper(\%non_rel_updates);
630 # the object creation might fail because of non-column and
631 # non-constructor handled parameters which shouldn't break RU
632 21 ▶ try {
Subroutine __ANON__: called CC=5 SCAR=16.1
633 21 my $related_row = $related_resultset
634 ->new_result(\%non_rel_updates);
635 21 for my $colname (@pks) {
636 next
637 23 ▶ if exists $pk_kvs{$colname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pk_kvs{$colname} 1 22 23
638  
639 22 ▶ if ($related_row->can($colname)
Branch: 1 branch
BranchTrueFalseTotal
if ($related_row->can($colname) and defined $related_row->$colname) { } 2 20 22
Condition66%2/3
$related_row->can($colname) and defined $related_row->$colname
!ll&&!rl&&r
0202
MC/DC50%1/2
$related_row->can($colname) and defined $related_row->$colname
$related_row->can($colname) defined $related_row->$colname
Truth table66%2/3
A and B
A= $related_row->can($colname)B= defined $related_row->$colname
ABresult
0X0
100
111
640 && defined $related_row->$colname) {
641 2 DEBUG and warn "missing pk column $colname exists " .
642 "and defined on object\n";
643 2 $pk_kvs{$colname} = $related_row->$colname;
644 }
645 else {
646 20 DEBUG and warn "missing pk column $colname doesn't "
647 . "exist or isn't defined on object, aborting\n";
648 20 last;
649 }
650 }
651 }
652 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
653 0 DEBUG and warn "object construction failed, ignoring:
654 $_\n";
655 21 };
656 }
657  
658 # The only reasons to let recursive_update search for an existing
659 # row (= not passing a new result to it) is relinking of existing
660 # rows.
661 # Relinking is only possible if all primary key column values are
662 # known and only required if at least one of the foreign row
663 # columns, which are part of the relationship, differ between
664 # current and target ones.
665 # There are two different cases:
666 # The foreign row columns are part of the foreign primary key.
667 # An example is the dvdtags relationship of Dvd.
668 # Or one or more non primary key form the relationship.
669 # An example is the owned_dvds relationship of User.
670 116 my $relink = 0;
671  
672 116 ▶ if ( scalar keys %pk_kvs == scalar @pks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %pk_kvs == scalar @pks) 96 20 116
673 96 DEBUG and warn "all primary keys available, " .
674 "searching for row in currently related rows\n";
675 # the lookup can fail if the primary key of a currently not
676 # related row is passed in the updates hash
677 96 $related_object = _get_matching_row(\%pk_kvs, \@related_rows);
678 # %pk_kvs contains the scalar value instead of a hashref
679 # when a column and relationship are named the same so
680 # overwrite the hashref in $sub_updates with that
681 # don't include %$resolved as well as that contains target data
682 96 my %current_data = (%$sub_updates, %pk_kvs);
683 96 DEBUG and warn "current data: " . Dumper(\%current_data);
684 96 DEBUG and warn "target data: " . Dumper($resolved);
685  
686 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
687  
688 # If the row can't be found by _get_matching_row it is
689 # currently not linked or doesn't even exist. In this case we
690 # must execute a sql select to find it.
691 $relink = 1
692 if (not defined $related_object)
693 38 ▶ && (any { $resolved->{$_} ne $current_data{$_} }
Subroutine __ANON__: called CC=1 SCAR=0.0
694 96 ▶ keys %$resolved);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $related_object and &any(sub { $resolved->{$_} ne $current_data{$_}; } , keys %$resolved) 2 94 96
Condition100%3/3
not defined $related_object and &any(sub { $resolved->{$_} ne $current_data{$_}; } , keys %$resolved)
!ll&&!rl&&r
58362
MC/DC100%2/2
not defined $related_object and &any(sub { $resolved->{$_} ne $current_data{$_}; } , keys %$resolved)
not defined $related_object &any(sub { $resolved->{$_} ne $current_data{$_}; } , keys %$resolved)
Truth table100%3/3
not(A) and B
A= not defined $related_objectB= &any(sub { $resolved->{$_} ne $current_data{$_}; } , keys %$resolved)
ABresult
0X0
100
111
695 }
696 116 DEBUG and warn "relink: $relink\n";
697  
698 # pass an empty object if no related row found and it's not the
699 # special case where the primary key of a currently not related
700 # row is passed in the updates hash to prevent the find by pk in
701 # recursive_update to happen
702 116 ▶ if ((not defined $related_object) && (not $relink)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $related_object or $relink) 56 60 116
Condition100%3/3
defined $related_object or $relink
l!l&&r!l&&!r
58256
MC/DC100%2/2
defined $related_object or $relink
defined $related_object $relink
Truth table100%3/3
A or B
A= defined $related_objectB= $relink
ABresult
1X1
011
000
703 56 DEBUG and warn "passing empty row to prevent find by pk\n";
704 56 $related_object = $related_resultset->new_result({});
705 }
706  
707 116 my $sub_object = recursive_update(
708 resultset => $related_resultset,
709 updates => $sub_updates,
710 resolved => $resolved,
711 # pass prefetched object if found
712 object => $related_object,
713 );
714  
715 111 push @updated_objs, $sub_object;
716 }
717  
718 # determine if a removal query is required
719 my @remove_rows = grep {
720 54 my $existing_row = $_;
721 159 ▶ none { $existing_row->ID eq $_->ID } @updated_objs
Subroutine __ANON__: called CC=1 SCAR=0.0
722 102 } @related_rows;
723 54 DEBUG and warn "rows for removal: " . join(', ', map { $_->ID }
724 @remove_rows) . "\n";
725  
726 54 ▶ if (scalar @remove_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @remove_rows) 21 33 54
727 21 my $rs_rel_delist = $object->$name;
728  
729 # foreign table has a single pk column
730 21 ▶ if (scalar @pks == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pks == 1) { } 6 15 21
731 6 DEBUG and warn "delete in not_in\n";
732 6 $rs_rel_delist = $rs_rel_delist->search_rs(
733 {
734 $self->current_source_alias . "." .
735 $pks[0] => { -not_in => [ map ( $_->id, @updated_objs ) ] }
736 }
737 );
738 }
739  
740 # foreign table has multiple pk columns
741 else {
742 15 my @cond;
743 15 for my $obj (@updated_objs) {
744 21 my %cond_for_obj;
745 21 for my $col (@pks) {
746 42 $cond_for_obj{ $self->current_source_alias . ".$col" } =
747 $obj->get_column($col);
748  
749 }
750 21 push @cond, \%cond_for_obj;
751 }
752  
753 # only limit resultset if there are related rows left
754 15 ▶ if (scalar @cond) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @cond) 11 4 15
755 11 $rs_rel_delist = $rs_rel_delist->search_rs({ -not => [ @cond ] });
756 }
757 }
758  
759 21 ▶ if ($if_not_submitted eq 'delete') {
Branch: 2 branches
BranchTrueFalseTotal
if ($if_not_submitted eq 'delete') { } 19 2 21
elsif ($if_not_submitted eq 'set_to_null') { } 2 0 2
760 19 $rs_rel_delist->delete;
761 }
762 elsif ($if_not_submitted eq 'set_to_null') {
763 2 my %update = map {$_ => undef} @rel_cols;
764 2 $rs_rel_delist->update(\%update);
765 }
766 }
767 }
768 ▶ elsif ( $attrs->{accessor} eq 'single' ||
Condition66%2/3
$attrs->{"accessor"} eq "single" or $attrs->{"accessor"} eq "filter"
l!l&&r!l&&!r
46410
MC/DC0%0/2
$attrs->{"accessor"} eq "single" or $attrs->{"accessor"} eq "filter"
$attrs->{"accessor"} eq "single" $attrs->{"accessor"} eq "filter"
Truth table66%2/3
A or B
A= $attrs->{"accessor"} eq "single"B= $attrs->{"accessor"} eq "filter"
ABresult
1X1
011
000
769 $attrs->{accessor} eq 'filter' ) {
770 DEBUG and warn "has_one, might_have, belongs_to (" .
771 87 $attrs->{accessor} . "): $name\n";
772  
773 87 my $sub_object;
774 87 ▶ if ( ref $updates ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $updates) { } 86 1 87
775 86 my $existing_row = 0;
776 86 my @pks = $related_resultset->result_source->primary_columns;
777 86 ▶ if ( all { exists $updates->{$_} && defined $updates->{$_} } @pks ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if (&all(sub { exists $updates->{$_} and defined $updates->{$_}; } , @pks)) 37 49 86
exists $updates->{$_} and defined $updates->{$_} 37 49 86
778 37 $existing_row = 1;
779 }
780 86 DEBUG and warn $existing_row ? "existing row\n" : "new row\n";
781 # newly created rows can't have related rows
782 86 my $related_row;
783 86 ▶ if ($row_existed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_existed) 2 84 86
784 2 $related_row = $object->$name;
785 2 DEBUG and warn "got related row\n";
786 }
787 86 ▶ if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($updates) and $updates->isa('DBIx::Class::Row')) { } 10 76 86
elsif ($attrs->{'accessor'} eq 'single' and defined $related_row) { } 1 75 76
Condition66%2/3
builtin::blessed($updates) and $updates->isa("DBIx::Class::Row")
!ll&&!rl&&r
76010
MC/DC50%1/2
builtin::blessed($updates) and $updates->isa("DBIx::Class::Row")
builtin::blessed($updates) $updates->isa("DBIx::Class::Row")
Truth table66%2/3
A and B
A= builtin::blessed($updates)B= $updates->isa("DBIx::Class::Row")
ABresult
0X0
100
111
788 10 $sub_object = $updates;
789 }
790 ▶ elsif ( $attrs->{accessor} eq 'single' && defined $related_row )
Condition100%3/3
$attrs->{"accessor"} eq "single" and defined $related_row
!ll&&!rl&&r
38371
MC/DC100%2/2
$attrs->{"accessor"} eq "single" and defined $related_row
$attrs->{"accessor"} eq "single" defined $related_row
Truth table100%3/3
A and B
A= $attrs->{"accessor"} eq "single"B= defined $related_row
ABresult
0X0
100
111
791 {
792 1 ▶ $sub_object = recursive_update(
Branch: 1 branch
BranchTrueFalseTotal
$existing_row ? : 0 1 1
793 resultset => $related_resultset,
794 updates => $updates,
795 $existing_row ? () : (object => $object->$name),
796 );
797 }
798 else {
799 75 ▶ $sub_object = recursive_update(
Branch: 1 branch
BranchTrueFalseTotal
$existing_row ? : 37 38 75
800 resultset => $related_resultset,
801 updates => $updates,
802 $existing_row ? () : (resolved => $resolved),
803 );
804 }
805 }
806 else {
807 $sub_object = $related_resultset->find($updates)
808 unless (
809 !$updates &&
810 ( exists $attrs->{join_type} &&
811 1 ▶ $attrs->{join_type} eq 'LEFT' )
Branch: 1 branch
BranchTrueFalseTotal
unless not $updates and (exists $attrs->{"join_type"} and $attrs->{"join_type"} eq "LEFT") 0 1 1
Condition33%2/6
not $updates and (exists $attrs->{"join_type"} and $attrs->{"join_type"} eq "LEFT")
!ll&&!rl&&r
001
exists $attrs->{"join_type"} and $attrs->{"join_type"} eq "LEFT"
!ll&&!rl&&r
001
MC/DC0%0/3
not $updates and (exists $attrs->{"join_type"} and $attrs->{"join_type"} eq "LEFT")
not $updates exists $attrs->{"join_type"} $attrs->{"join_type"} eq "LEFT"
Truth table25%1/4
not(A) and B and C
A= not $updatesB= exists $attrs->{"join_type"}C= $attrs->{"join_type"} eq "LEFT"
ABCresult
0XX0
10X0
1100
1111
812 );
813 }
814 83 ▶ my $join_type = $attrs->{join_type} || '';
Condition100%2/2
$attrs->{'join_type'} || ''
l!l
1271
MC/DC100%1/1
$attrs->{'join_type'} || ''
$attrs->{'join_type'}
Truth table100%2/2
A
A= $attrs->{'join_type'}
Aresult
11
00
815 # unmarked 'LEFT' join for belongs_to
816 my $might_belong_to =
817 ( $attrs->{accessor} eq 'single' || $attrs->{accessor} eq 'filter' ) &&
818 83 ▶ $attrs->{is_foreign_key_constraint};
Condition66%4/6
($attrs->{'accessor'} eq 'single' || $attrs->{'accessor'} eq 'filter') && $attrs->{'is_foreign_key_constraint'}
!ll&&!rl&&r
0875
$attrs->{"accessor"} eq "single" or $attrs->{"accessor"} eq "filter"
l!l&&r!l&&!r
45380
MC/DC33%1/3
($attrs->{'accessor'} eq 'single' || $attrs->{'accessor'} eq 'filter') && $attrs->{'is_foreign_key_constraint'}
$attrs->{"accessor"} eq "single" $attrs->{"accessor"} eq "filter" $attrs->{'is_foreign_key_constraint'}
Truth table60%3/5
A or B && C
A= $attrs->{"accessor"} eq "single"B= $attrs->{"accessor"} eq "filter"C= $attrs->{'is_foreign_key_constraint'}
ABCresult
00X0
1X00
0100
1X11
0111
819 # adding check for custom condition that's a coderef
820 # this 'set_from_related' should probably not be called in lots of other
821 # situations too, but until that's worked out, kludge it
822 83 ▶ if ( ( $sub_object || $updates || $might_belong_to || $join_type eq 'LEFT' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_object || $updates || $might_belong_to || $join_type eq "LEFT" and ref $info->{"cond"} ne "CODE") 83 0 83
Condition58%7/12
$sub_object || $updates || $might_belong_to || $join_type eq "LEFT" and ref $info->{"cond"} ne "CODE"
!ll&&!rl&&r
0083
$sub_object or $updates or $might_belong_to or $join_type eq "LEFT"
l!l&&r!l&&!r
8300
$sub_object or $updates or $might_belong_to
l!l&&r!l&&!r
8210
$sub_object or $updates
l!l&&r!l&&!r
8111
MC/DC0%0/5
$sub_object || $updates || $might_belong_to || $join_type eq "LEFT" and ref $info->{"cond"} ne "CODE"
$sub_object $updates $might_belong_to $join_type eq "LEFT" ref $info->{"cond"} ne "CODE"
Truth table33%3/9
A or B or C or D and E
A= $sub_objectB= $updatesC= $might_belong_toD= $join_type eq "LEFT"E= ref $info->{"cond"} ne "CODE"
ABCDEresult
0000X0
1XXX00
01XX00
001X00
000100
1XXX11
01XX11
001X11
000111
823 ref $info->{cond} ne 'CODE' ) {
824 83 $object->$name($sub_object);
825 }
826 }
827 else {
828 $self->throw_exception(
829 "recursive_update doesn't now how to handle relationship '$name' with accessor " .
830 0 $info->{attrs}{accessor} );
831 }
832  
833 137 DEBUG and warn "_update_relation end\n";
834 }
835  
836 392 ▶sub is_m2m {
Subroutine is_m2m: called CC=5 SCAR=16.1
837 392 my ( $self, $relation ) = @_;
838 392 my $rclass = $self->result_class;
839  
840 # DBIx::Class::IntrospectableM2M
841 392 ▶ if ( $rclass->can('_m2m_metadata') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rclass->can("_m2m_metadata")) 70 322 392
842 70 return $rclass->_m2m_metadata->{$relation};
843 }
844 322 my $object = $self->new_result( {} );
845 322 ▶ if ( $object->can($relation) and
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can($relation) and not $self->result_source->has_relationship($relation) and $object->can('set_' . $relation)) 12 310 322
Condition100%6/6
$object->can($relation) and not $self->result_source->has_relationship($relation) and $object->can('set_' . $relation)
!ll&&!rl&&r
16314712
$object->can($relation) and not $self->result_source->has_relationship($relation)
!ll&&!rl&&r
6157159
MC/DC100%3/3
$object->can($relation) and not $self->result_source->has_relationship($relation) and $object->can('set_' . $relation)
$object->can($relation) not $self->result_source->has_relationship($relation) $object->can('set_' . $relation)
Truth table100%4/4
A and not(B) and C
A= $object->can($relation)B= not $self->result_source->has_relationship($relation)C= $object->can('set_' . $relation)
ABCresult
0XX0
10X0
1100
1111
846 !$self->result_source->has_relationship($relation) and
847 $object->can( 'set_' . $relation ) ) {
848 12 return 1;
849 }
850 310 return;
851 }
852  
853 10 ▶sub get_m2m_source {
Subroutine get_m2m_source: called CC=2 SCAR=6.9
854 10 my ( $self, $relation ) = @_;
855 10 my $rclass = $self->result_class;
856  
857 # DBIx::Class::IntrospectableM2M
858 10 ▶ if ( $rclass->can('_m2m_metadata') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rclass->can("_m2m_metadata")) 4 6 10
859 return $self->result_source->related_source(
860 $rclass->_m2m_metadata->{$relation}{relation} )
861 4 ->related_source( $rclass->_m2m_metadata->{$relation}{foreign_relation} );
862 }
863 6 my $object = $self->new_result( {} );
864 6 my $r = $object->$relation;
865 6 return $r->result_source;
866 }
867  
868 0 ▶sub _delete_empty_auto_increment {
Subroutine _delete_empty_auto_increment: not called CC=5 SCAR=34.0
869 0 my ( $self, $object ) = @_;
870 0 for my $col ( sort keys %{ $object->{_column_data} } ) {
871 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($object->result_source->column_info($col)->{"is_auto_increment"} and !defined($object->{"_column_data"}{$col}) || $object->{"_column_data"}{$col} eq "") 0 0 0
Condition0%0/6
$object->result_source->column_info($col)->{"is_auto_increment"} and !defined($object->{"_column_data"}{$col}) || $object->{"_column_data"}{$col} eq ""
!ll&&!rl&&r
000
not defined $object->{"_column_data"}{$col} or $object->{"_column_data"}{$col} eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
$object->result_source->column_info($col)->{"is_auto_increment"} and !defined($object->{"_column_data"}{$col}) || $object->{"_column_data"}{$col} eq ""
$object->result_source->column_info($col)->{"is_auto_increment"} not defined $object->{"_column_data"}{$col} $object->{"_column_data"}{$col} eq ""
Truth table0%0/4
A and not(B) or C
A= $object->result_source->column_info($col)->{"is_auto_increment"}B= not defined $object->{"_column_data"}{$col}C= $object->{"_column_data"}{$col} eq ""
ABCresult
0XX0
1000
11X1
1011
872 $object->result_source->column_info($col)->{is_auto_increment} and
873 ( !defined $object->{_column_data}{$col} or
874 $object->{_column_data}{$col} eq '' )
875 ) {
876 0 delete $object->{_column_data}{$col};
877 }
878 }
879 }
880  
881 103 ▶sub _get_pk_for_related {
Subroutine _get_pk_for_related: called CC=3 SCAR=11.0
882 103 my ( $self, $relation ) = @_;
883 103 my $source;
884 103 ▶ if ( $self->result_source->has_relationship($relation) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->result_source->has_relationship($relation)) 93 10 103
885 93 $source = $self->result_source->related_source($relation);
886 }
887  
888 # many to many case
889 103 ▶ if ( is_m2m( $self, $relation ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_m2m($self, $relation)) 10 93 103
890 10 $source = get_m2m_source( $self, $relation );
891 }
892 103 return $source->primary_columns;
893 }
894  
895 # This function determines whether a relationship should be done before or
896 # after the row is inserted into the database
897 # relationships before: belongs_to
898 # relationships after: has_many, might_have and has_one
899 # true means before, false after
900 132 ▶sub _master_relation_cond {
Subroutine _master_relation_cond: called CC=6 SCAR=19.9
901 132 my ( $self, $name ) = @_;
902  
903 132 my $source = $self->result_source;
904 132 my $info = $source->relationship_info($name);
905  
906 # has_many rels are always after
907 return 0
908 132 ▶ if $info->{attrs}->{accessor} eq 'multi';
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"attrs"}{"accessor"} eq "multi" 39 93 132
909  
910 93 my @foreign_ids = _get_pk_for_related( $self, $name );
911  
912 93 my $cond = $info->{cond};
913  
914 93 ▶ sub _inner {
Subroutine _inner: called CC=4 SCAR=14.0
915 93 my ( $source, $cond, @foreign_ids ) = @_;
916  
917 93 for my $f_key ( sort keys %$cond ) {
918 93 my $col = $cond->{$f_key};
919 # might_have is not master
920 93 $col =~ s/^self\.//;
921 93 $f_key =~ s/^foreign\.//;
922 93 ▶ if ( $source->column_info($col)->{is_auto_increment} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->column_info($col)->{"is_auto_increment"}) 9 84 93
923 9 return 0;
924 }
925 84 ▶ if ( any { $_ eq $f_key } @foreign_ids ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $f_key; } , @foreign_ids)) 84 0 84
926 84 return 1;
927 }
928 }
929 0 return 0;
930 }
931  
932 93 ▶ if ( ref $cond eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $cond eq 'HASH') { } 93 0 93
elsif (ref $cond eq 'ARRAY') { } 0 0 0
elsif (ref $cond eq 'CODE') { } 0 0 0
933 93 return _inner( $source, $cond, @foreign_ids );
934 }
935  
936 # arrayref of hashrefs
937 elsif ( ref $cond eq 'ARRAY' ) {
938 0 for my $new_cond (@$cond) {
939 0 return _inner( $source, $new_cond, @foreign_ids );
940 }
941 }
942  
943 # we have a custom join condition, so update afterward
944 elsif ( ref $cond eq 'CODE' ) {
945 0 return 0;
946 }
947  
948 else {
949 0 $source->throw_exception( "unhandled relation condition " . ref($cond) );
950 }
951 0 return;
952 }
953  
954 10 1;
955  
956 __END__
↑ Summary