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

filter: Statementstmt 91.9% 400 / 435 Branchbran 79.0% 158 / 200 Conditioncond 70.2% 104 / 148 MC/DCmcdc 53.3% 48 / 90 Subroutinesub 91.6% 33 / 36 Podpod 100% 1 / 1 totaltotal 81.7% 744 / 910 CC 175 SCAR 39.3
CC
175
Cov
85%
_update_relation
86.1
recursive_update
68.1
_delete_empty_auto_increment
30.0
CRAP 50.8
↑ 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.42';
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 sub recursive_update {
11 75 ▶ my ( $self, $updates, $attrs ) = @_;
Subroutine recursive_update: called CC=5 SCAR=16.1
Pod documented
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.42';
40 10 ▶use Carp::Clan qw/^DBIx::Class|^HTML::FormHandler|^Try::Tiny/;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 10 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 10 ▶use List::MoreUtils qw/ any all none /;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 10 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 10 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 10 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 sub recursive_update {
49 277 ▶ my %params = @_;
Subroutine recursive_update: called CC=55 SCAR=42.2
50 my ( $self, $updates, $fixed_fields, $object, $resolved, $if_not_submitted,
51 $unknown_params_ok, $m2m_force_set_rel )
52 = @params{
53 277 qw/resultset updates fixed_fields object resolved if_not_submitted unknown_params_ok m2m_force_set_rel/
54 };
55 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
56 277 $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1;
57  
58 local $SIG{__WARN__} = sub {
59 0 ▶ my $level = 0;
Subroutine __ANON__: not called CC=3 SCAR=24.8
60 0 my $i = 0;
61 0 while (1) {
62 0 ▶ my $sub = (caller($i++))[3] or last;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = (caller $i++)[3] or last 0 0 0
63 0 ▶ $level++ if $sub =~ /(?:recursive_update|_update_relation)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /(?:recursive_update|_update_relation)$/ 0 0 0
64 }
65 0 warn map { s/^/" "x$level/egmr } @_;
66 277 } if DEBUG;
67  
68 277 my $source = $self->result_source;
69  
70 277 ▶ croak "first parameter needs to be defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $updates 0 277 277
71 unless defined $updates;
72  
73 277 ▶ croak "first parameter needs to be a hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $updates eq "HASH" 0 277 277
74 unless ref($updates) eq 'HASH';
75  
76 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
77 if defined $fixed_fields && ref $fixed_fields ne 'ARRAY';
78  
79 277 DEBUG and warn "recursive_update: " . $source->name . "\n";
80 277 ▶ DEBUG and warn "object passed, skipping find" .
Branch: 1 branch
BranchTrueFalseTotal
if defined $object 116 161 277
81 (defined $object->id
82 ? " (id " . $object->id . ")\n"
83 : "\n")
84 if defined $object;
85  
86 # always warn about additional parameters if storage debugging is enabled
87 277 ▶ $unknown_params_ok = 0
Branch: 1 branch
BranchTrueFalseTotal
if $source->storage->debug 147 130 277
88 if $source->storage->debug;
89  
90 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
91 0 return $updates;
92 }
93  
94 277 my @pks = $source->primary_columns;
95 277 my %pk_kvs;
96 277 for my $colname (@pks) {
97 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
98 177 $pk_kvs{$colname} = $updates->{$colname};
99 177 next;
100 }
101 $pk_kvs{$colname} = $resolved->{$colname}
102 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
103 }
104 # support the special case where a method on the related row
105 # populates one or more primary key columns and we don't have
106 # all primary key values already
107 # see DBSchema::Result::DVD relationship keysbymethod
108 277 DEBUG and warn "pk columns so far: " . join (', ',
109 sort keys %pk_kvs) . "\n";
110 my @non_pk_columns = grep {
111 277 my $colname = $_;
112 493 ▶ none { $colname eq $_ } keys %pk_kvs
Subroutine __ANON__: called CC=1 SCAR=0.0
113 }
114 sort keys %$updates;
115 277 DEBUG and warn "non-pk columns: " . join (', ',
116 @non_pk_columns) . "\n";
117 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
118 101 DEBUG and warn "not all primary keys available, trying " .
119 "object creation\n";
120 # new_result throws exception if non column values are passed
121 # because we want to also support e.g. a BUILDARGS method that
122 # populates primary key columns from an additional value
123 # filter out all relationships
124 my @non_rel_columns = grep {
125 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
126 && !$source->has_relationship($_)
127 }
128 sort keys %$updates;
129 my %non_rel_updates = map {
130 101 $_ => $updates->{$_}
131 } @non_rel_columns;
132 # transform columns specified by their accessor name
133 101 my %columns_by_accessor = _get_columns_by_accessor($self);
134 101 for my $accessor_name (sort keys %columns_by_accessor) {
135 404 my $colname = $columns_by_accessor{$accessor_name}->{name};
136 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
137 && exists $non_rel_updates{$accessor_name}) {
138 12 DEBUG and warn "renaming column accessor " .
139 "'$accessor_name' to column name '$colname'\n";
140 $non_rel_updates{$colname} = delete
141 12 $non_rel_updates{$accessor_name};
142 }
143 }
144 101 DEBUG and warn "using all non-rel updates for object " .
145 "construction: " . Dumper(\%non_rel_updates);
146 # the object creation might fail because of non-column and
147 # non-constructor handled parameters which shouldn't break RU
148 try {
149 101 ▶ my $row = $self->new_result(\%non_rel_updates);
Subroutine __ANON__: called CC=5 SCAR=16.1
150 98 for my $colname (@pks) {
151 next
152 122 ▶ if exists $pk_kvs{$colname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pk_kvs{$colname} 22 100 122
153  
154 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
155 && defined $row->$colname) {
156 6 DEBUG and warn "missing pk column $colname exists " .
157 "and defined on object\n";
158 6 $pk_kvs{$colname} = $row->$colname;
159 }
160 else {
161 94 DEBUG and warn "missing pk column $colname doesn't "
162 . "exist or isn't defined on object, aborting\n";
163 94 last;
164 }
165 }
166 }
167 catch {
168 3 ▶ DEBUG and warn "object construction failed, ignoring:
Subroutine __ANON__: called CC=1 SCAR=0.0
169 $_\n";
170 101 };
171 }
172  
173 # check if row can be found in resultset cache
174 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
175 106 my $cached_rows = $self->get_cache;
176 106 ▶ if (defined $cached_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cached_rows) 4 102 106
177 4 DEBUG and warn "find in cache\n";
178 4 $object = _get_matching_row(\%pk_kvs, $cached_rows)
179 }
180 }
181  
182 277 $updates = { %$updates, %$resolved };
183  
184 277 my %fixed_fields = map { $_ => 1 } @$fixed_fields;
185  
186 # add the resolved columns to the updates hashref
187 277 my %all_pks = ( %pk_kvs, %fixed_fields );
188  
189 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
190 102 DEBUG and warn "find by pk\n";
191 102 $object = $self->find( \%all_pks, { key => 'primary' } );
192 }
193  
194 277 ▶ unless (defined $object) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 67 210 277
195 67 DEBUG and warn "create new row\n";
196 67 $object = $self->new_result( {} );
197 }
198  
199 # direct column accessors
200 277 my %columns;
201  
202 # relations that that should be done before the row is inserted into the
203 # database like belongs_to
204 my %pre_updates;
205  
206 # relations that that should be done after the row is inserted into the
207 # database like has_many, might_have and has_one
208 277 my %post_updates;
209 277 my %other_methods;
210 277 my %m2m_accessors;
211 277 my %columns_by_accessor = _get_columns_by_accessor($self);
212  
213 # this section determines to what each key/value pair maps to,
214 # column or relationship
215 277 for my $name ( sort keys %$updates ) {
216 DEBUG and warn "updating $name to "
217 615 . ($updates->{$name} // '[undef]') . "\n";
218 # columns
219 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
220 !( $source->has_relationship($name) && ref( $updates->{$name} ) ) ) {
221 443 $columns{$name} = $updates->{$name};
222 443 next;
223 }
224  
225 # relationships
226 172 ▶ if ( $source->has_relationship($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->has_relationship($name)) 130 42 172
227 130 ▶ if ( _master_relation_cond( $self, $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_master_relation_cond($self, $name)) { } 82 48 130
228 82 $pre_updates{$name} = $updates->{$name};
229 82 next;
230 }
231 else {
232 48 $post_updates{$name} = $updates->{$name};
233 48 next;
234 }
235 }
236  
237 # many-to-many helper accessors
238 42 ▶ if ( is_m2m( $self, $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_m2m($self, $name)) 37 5 42
239 37 DEBUG and warn "is m2m\n";
240 # Transform m2m data into recursive has_many data
241 # if IntrospectableM2M is in use.
242 #
243 # This removes the overhead related to deleting and
244 # re-adding all relationships.
245 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
246 27 my $meta = $source->result_class->_m2m_metadata->{$name};
247 27 my $bridge_rel = $meta->{relation};
248 27 my $foreign_rel = $meta->{foreign_relation};
249  
250 $post_updates{$bridge_rel} = [
251 map {
252 70 { $foreign_rel => $_ }
253 27 } @{ $updates->{$name} }
254 ];
255 DEBUG and warn "m2m '$name' transformed to:\n$bridge_rel => " .
256 27 Dumper($post_updates{$bridge_rel}) . "\n";
257 }
258 # Fall back to set_$rel if IntrospectableM2M
259 # is not available. (removing and re-adding all relationships)
260 else {
261 10 $m2m_accessors{$name} = $updates->{$name};
262 }
263  
264 37 next;
265 }
266  
267 # accessors
268 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
269 2 $other_methods{$name} = $updates->{$name};
270 2 next;
271 }
272  
273 # unknown
274  
275 # don't throw a warning instead of an exception to give users
276 # time to adapt to the new API
277 carp(
278 3 ▶ "No such column, relationship, many-to-many helper accessor or " .
Branch: 1 branch
BranchTrueFalseTotal
unless $unknown_params_ok 2 1 3
279 "generic accessor '$name' on '" . $source->name . "'"
280 ) unless $unknown_params_ok;
281  
282 }
283  
284 # first update columns and other accessors
285 # so that later related records can be found
286 277 for my $name ( sort keys %columns ) {
287 431 $object->$name( $columns{$name} );
288 }
289 271 for my $name ( sort keys %other_methods ) {
290 2 $object->$name( $other_methods{$name} );
291 }
292 271 for my $name ( sort keys %pre_updates ) {
293 79 _update_relation( $self, $name, $pre_updates{$name}, $object, $if_not_submitted, 0 );
294 }
295  
296 # $self->_delete_empty_auto_increment($object);
297 # don't allow insert to recurse to related objects
298 # do the recursion ourselves
299 # $object->{_rel_in_storage} = 1;
300 # Update if %other_methods because of possible custom update method
301 267 my $in_storage = $object->in_storage;
302  
303 # preserve related resultsets as DBIx::Class::Row->update clears them
304 # yes, this directly accesses a row attribute, but no API exists and in
305 # the hope to get the recursive_update feature into core DBIx::Class this
306 # is the easiest solution
307 267 my $related_resultsets = $object->{related_resultsets};
308  
309 267 DEBUG and warn "before update_or_insert\n";
310 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
311 267 DEBUG and warn "after update_or_insert\n";
312  
313 # restore related resultsets
314 267 $object->{related_resultsets} = $related_resultsets;
315  
316 # updating many_to_many
317 267 for my $name ( sort keys %m2m_accessors ) {
318 10 DEBUG and warn "updating m2m $name\n";
319 10 my $value = $m2m_accessors{$name};
320  
321 # TODO: only first pk col is used
322 10 my ($pk) = _get_pk_for_related( $self, $name );
323 10 my @rows;
324 10 my $rel_source = $object->$name->result_source;
325 10 my @updates;
326 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
327 10 @updates = @{$value};
328 }
329 ▶ 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
330 0 @updates = ($value);
331 }
332 elsif ( defined $value ) {
333 0 carp "value of many-to-many rel '$name' must be an arrayref or scalar: $value";
334 }
335 10 for my $elem (@updates) {
336 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
337 0 push @rows, $elem;
338 }
339 elsif ( ref $elem eq 'HASH' ) {
340 5 push @rows,
341 recursive_update(
342 resultset => $rel_source->resultset,
343 updates => $elem
344 );
345 }
346 else {
347 18 push @rows, $rel_source->resultset->find( { $pk => $elem } );
348 }
349 }
350 10 my $set_meth = 'set_' . $name;
351 10 $object->$set_meth( \@rows );
352 }
353 267 for my $name ( sort keys %post_updates ) {
354 67 _update_relation( $self, $name, $post_updates{$name}, $object, $if_not_submitted, $in_storage );
355 }
356 262 delete $ENV{DBIC_NULLABLE_KEY_NOWARN};
357 262 return $object;
358 }
359  
360 # returns DBIx::Class::ResultSource::column_info as a hash indexed by column accessor || name
361 sub _get_columns_by_accessor {
362 400 ▶ my $self = shift;
Subroutine _get_columns_by_accessor: called CC=3 SCAR=11.0
363 400 my $source = $self->result_source;
364 400 my %columns;
365 400 for my $name ( $source->columns ) {
366 1548 my $info = $source->column_info($name);
367 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
368 }
369 400 return %columns;
370 }
371  
372 sub _get_matching_row {
373 100 ▶ my ($kvs, $rows) = @_;
Subroutine _get_matching_row: called CC=12 SCAR=25.7
374  
375 return
376 100 ▶ unless defined $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rows 0 100 100
377  
378 100 ▶ croak 'key/value need to be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $kvs eq "HASH" 0 100 100
379 unless ref $kvs eq 'HASH';
380  
381 100 ▶ croak 'key/value needs to have at least one pair'
Branch: 1 branch
BranchTrueFalseTotal
if keys %$kvs == 0 0 100 100
382 if keys %$kvs == 0;
383  
384 100 ▶ croak 'rows need to be an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" 0 100 100
385 unless ref $rows eq 'ARRAY';
386  
387 100 ▶ unless ($rows) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rows) 0 100 100
388 0 DEBUG and warn "skipping because no rows passed\n";
389 0 return;
390 }
391  
392 100 my $matching_row;
393  
394 my @matching_rows;
395 100 for my $row (@$rows) {
396 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
397 push @matching_rows, $row
398 444 ▶ if all { $kvs->{$_} eq $row->get_column($_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
399 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
400 sort keys %$kvs;
401 }
402 100 ▶ DEBUG and warn "multiple matching rows: " . scalar @matching_rows . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if @matching_rows > 1 0 100 100
403 if @matching_rows > 1;
404 100 ▶ $matching_row = $matching_rows[0]
Branch: 1 branch
BranchTrueFalseTotal
if scalar @matching_rows == 1 62 38 100
405 if scalar @matching_rows == 1;
406 DEBUG and warn "matching row found for: " . Dumper($kvs) . " in " .
407 100 ▶ Dumper([map { { $_->get_columns } } @$rows]) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $matching_row 62 38 100
408 if defined $matching_row;
409 DEBUG and warn "matching row not found for: " . Dumper($kvs) . " in " .
410 100 ▶ Dumper([map { { $_->get_columns } } @$rows]) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matching_row 38 62 100
411 unless defined $matching_row;
412  
413 100 return $matching_row;
414 }
415  
416 # Arguments: $rs, $name, $updates, $row, $if_not_submitted, $row_existed
417 sub _update_relation {
418 146 ▶ my ( $self, $name, $updates, $object, $if_not_submitted, $row_existed ) = @_;
Subroutine _update_relation: called CC=74 SCAR=44.6
419  
420 # this should never happen because we're checking the paramters passed to
421 # recursive_update, but just to be sure...
422 146 ▶ $object->throw_exception("No such relationship '$name'")
Branch: 1 branch
BranchTrueFalseTotal
unless $object->has_relationship($name) 0 146 146
423 unless $object->has_relationship($name);
424  
425 146 DEBUG and warn "_update_relation: $name\n";
426  
427 146 my $info = $object->result_source->relationship_info($name);
428 146 my $attrs = $info->{attrs};
429  
430 # get a related resultset without a condition
431 146 my $related_source = $self->related_resultset($name)->result_source;
432 146 my $related_resultset = $related_source->resultset;
433 146 ▶ $self->throw_exception("result_source must support _resolve_condition")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->result_source->can("_resolve_condition") 0 146 146
434 unless $self->result_source->can('_resolve_condition');
435 146 my $resolved = $self->result_source->_resolve_condition( $info->{cond}, $name, $object, $name );
436  
437 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
438 if defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION &&
439 $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved;
440  
441 # This is a hack. I'm not sure that this will handle most
442 # custom code conditions yet. This needs tests.
443 146 my @rel_cols;
444 146 ▶ if ( ref $info->{cond} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info->{'cond'} eq 'CODE') { } 0 146 146
445 0 my $new_resolved;
446 # remove 'me.' from keys in returned hashref
447 0 ▶ while ( my ( $key, $value ) = each %$resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$resolved) 0 0 0
448 0 $key =~ s/^me\.//;
449 0 $new_resolved->{$key} = $value;
450 0 push @rel_cols, $key;
451 }
452 0 $resolved = $new_resolved;
453 }
454 else {
455 146 @rel_cols = sort keys %{ $info->{cond} };
456 146 map { s/^foreign\.// } @rel_cols;
457 }
458  
459 # find out if all related columns are nullable
460 146 my $all_fks_nullable = 1;
461 146 for my $rel_col (@rel_cols) {
462 $all_fks_nullable = 0
463 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
464 }
465  
466 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
467 unless defined $if_not_submitted;
468  
469 # the only valid datatype for a has_many rels is an arrayref
470 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
471 59 DEBUG and warn "has_many: $name\n";
472  
473 # handle undef like empty arrayref
474 59 ▶ $updates = []
Branch: 1 branch
BranchTrueFalseTotal
unless defined $updates 1 58 59
475 unless defined $updates;
476 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
477 unless ref $updates eq 'ARRAY';
478  
479 59 my @updated_objs;
480 my @related_rows;
481 # newly created rows can't have related rows
482 59 ▶ if ($row_existed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_existed) 38 21 59
483 38 @related_rows = $object->$name;
484 38 DEBUG and warn "got related rows: " . scalar @related_rows . "\n";
485 }
486 59 my $related_result_source = $related_resultset->result_source;
487 59 my @pks = $related_result_source->primary_columns;
488  
489 59 for my $sub_updates ( @{$updates} ) {
490 116 DEBUG and warn "updating related row: " . Dumper($sub_updates)
491 . "\n";
492 116 my %pk_kvs;
493 # detect the special case where the primary key of a currently not
494 # related row is passed in the updates hash
495 # let the resolved column values fill any missing primary key
496 # columns but not overwrite them
497 116 for my $colname (@pks) {
498 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
499 && defined $sub_updates->{$colname}) {
500 # $sub_updates->{$colname} might be a hashref if a
501 # relationship is named the same as a foreign key column
502 69 ▶ if (ref $sub_updates->{$colname} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub_updates->{$colname} eq 'HASH') { } 29 40 69
503 29 ▶ if ($related_source->has_relationship($colname)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($related_source->has_relationship($colname)) { } 29 0 29
504 29 my $rel_info = $related_source
505 ->relationship_info($colname);
506 29 my @rel_cols = sort keys %{ $rel_info->{cond} };
507 29 map { s/^foreign\.// } @rel_cols;
508 29 ▶ $self->throw_exception("passing a hashref for " .
Branch: 1 branch
BranchTrueFalseTotal
if scalar @rel_cols != 1 0 29 29
509 "a multi-column relationship named the " .
510 "same as a column ('$colname') is not " .
511 "implemented")
512 if scalar @rel_cols != 1;
513 29 DEBUG and warn "using '$rel_cols[0]' in hashref " .
514 "for primary key column '$colname'\n";
515 $pk_kvs{$colname} = $sub_updates->{$colname}
516 29 ->{$rel_cols[0]};
517 }
518 else {
519 0 $self->throw_exception(
520 "data for $colname is a hashref but no " .
521 "relationship with that name exists");
522 }
523 }
524 else {
525 40 $pk_kvs{$colname} = $sub_updates->{$colname};
526 }
527 69 next;
528 }
529 $pk_kvs{$colname} = $resolved->{$colname}
530 if exists $resolved->{$colname}
531 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
532 }
533 116 my $related_object;
534  
535 my @missing_pks = grep {
536 116 my $pkcolname = $_;
537 204 ▶ none { $_ eq $pkcolname } keys %pk_kvs;
Subroutine __ANON__: called CC=1 SCAR=0.0
538 } @pks;
539  
540 # support the special case where a method on the related row
541 # populates one or more primary key columns and we don't have
542 # all primary key values already
543 # see DBSchema::Result::DVD relationship keysbymethod
544 116 DEBUG and warn "pk columns so far: " . join (', ',
545 sort keys %pk_kvs) . "\n";
546 116 DEBUG and warn "pk columns missing: " . join (', ',
547 sort @missing_pks) . "\n";
548 my @non_pk_columns = grep {
549 116 my $colname = $_;
550 143 ▶ none { $colname eq $_ } keys %pk_kvs
Subroutine __ANON__: called CC=1 SCAR=0.0
551 }
552 sort keys %$sub_updates;
553 116 DEBUG and warn "non-pk columns: " . join (', ',
554 @non_pk_columns) . "\n";
555 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
556 51 DEBUG and warn "not all primary keys available, trying " .
557 "relationships\n";
558 my @rel_names = grep {
559 51 $related_result_source->has_relationship($_)
560 } sort keys %$sub_updates;
561  
562 51 for my $relname (@rel_names) {
563 34 my $rel_info = $related_result_source
564 ->relationship_info($relname);
565 next
566 unless $rel_info->{attrs}->{accessor} eq 'single'
567 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
568  
569 31 for my $f_key ( sort keys %{$rel_info->{cond}} ) {
570 31 my $col = $rel_info->{cond}->{$f_key};
571 31 $col =~ s/^self\.//;
572 31 $f_key =~ s/^foreign\.//;
573  
574 next
575 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
576  
577 30 DEBUG and warn "populating primary key column " .
578 "'$col' from relationship '$relname'\n";
579 # rel holds a scalar value
580 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
581 10 $pk_kvs{$col} = delete $sub_updates->{$relname};
582 # optimization to not update the related row
583 # when only the primary key of it is given
584 10 $sub_updates->{$col} = $pk_kvs{$col};
585 }
586 # rel holds a hashref
587 elsif (ref $sub_updates->{$relname} eq 'HASH') {
588 $pk_kvs{$col} =
589 20 $sub_updates->{$relname}->{$f_key};
590 }
591 else {
592 0 $self->throw_exception("passing anything else " .
593 "but a scalar or a hashref is not " .
594 "implemented");
595 }
596 }
597 }
598 }
599  
600 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
601 21 DEBUG and warn "not all primary keys available, trying " .
602 "object creation\n";
603 # new_result throws exception if non column values are passed
604 # because we want to also support e.g. a BUILDARGS method that
605 # populates primary key columns from an additional value
606 # filter out all relationships
607 my @non_rel_columns = grep {
608 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
609 && !$related_result_source->has_relationship($_)
610 }
611 sort keys %$sub_updates;
612 my %non_rel_updates = map {
613 21 $_ => $sub_updates->{$_}
614 } @non_rel_columns;
615 # transform columns specified by their accessor name
616 21 my %columns_by_accessor = _get_columns_by_accessor($related_resultset);
617 21 for my $accessor_name (sort keys %columns_by_accessor) {
618 107 my $colname = $columns_by_accessor{$accessor_name}->{name};
619 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
620 && exists $non_rel_updates{$accessor_name}) {
621 10 DEBUG and warn "renaming column accessor " .
622 "'$accessor_name' to column name '$colname'\n";
623 $non_rel_updates{$colname} = delete
624 10 $non_rel_updates{$accessor_name};
625 }
626 }
627 21 DEBUG and warn "using all non-rel updates for object " .
628 "construction: " . Dumper(\%non_rel_updates);
629 # the object creation might fail because of non-column and
630 # non-constructor handled parameters which shouldn't break RU
631 try {
632 21 ▶ my $related_row = $related_resultset
Subroutine __ANON__: called CC=5 SCAR=16.1
633 ->new_result(\%non_rel_updates);
634 21 for my $colname (@pks) {
635 next
636 23 ▶ if exists $pk_kvs{$colname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pk_kvs{$colname} 1 22 23
637  
638 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
639 && defined $related_row->$colname) {
640 2 DEBUG and warn "missing pk column $colname exists " .
641 "and defined on object\n";
642 2 $pk_kvs{$colname} = $related_row->$colname;
643 }
644 else {
645 20 DEBUG and warn "missing pk column $colname doesn't "
646 . "exist or isn't defined on object, aborting\n";
647 20 last;
648 }
649 }
650 }
651 catch {
652 0 ▶ DEBUG and warn "object construction failed, ignoring:
Subroutine __ANON__: not called CC=1 SCAR=6.9
653 $_\n";
654 21 };
655 }
656  
657 # The only reasons to let recursive_update search for an existing
658 # row (= not passing a new result to it) is relinking of existing
659 # rows.
660 # Relinking is only possible if all primary key column values are
661 # known and only required if at least one of the foreign row
662 # columns, which are part of the relationship, differ between
663 # current and target ones.
664 # There are two different cases:
665 # The foreign row columns are part of the foreign primary key.
666 # An example is the dvdtags relationship of Dvd.
667 # Or one or more non primary key form the relationship.
668 # An example is the owned_dvds relationship of User.
669 116 my $relink = 0;
670  
671 116 ▶ if ( scalar keys %pk_kvs == scalar @pks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %pk_kvs == scalar @pks) 96 20 116
672 96 DEBUG and warn "all primary keys available, " .
673 "searching for row in currently related rows\n";
674 # the lookup can fail if the primary key of a currently not
675 # related row is passed in the updates hash
676 96 $related_object = _get_matching_row(\%pk_kvs, \@related_rows);
677 # %pk_kvs contains the scalar value instead of a hashref
678 # when a column and relationship are named the same so
679 # overwrite the hashref in $sub_updates with that
680 # don't include %$resolved as well as that contains target data
681 96 my %current_data = (%$sub_updates, %pk_kvs);
682 96 DEBUG and warn "current data: " . Dumper(\%current_data);
683 96 DEBUG and warn "target data: " . Dumper($resolved);
684  
685 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
686  
687 # If the row can't be found by _get_matching_row it is
688 # currently not linked or doesn't even exist. In this case we
689 # must execute a sql select to find it.
690 $relink = 1
691 if (not defined $related_object)
692 38 ▶ && (any { $resolved->{$_} ne $current_data{$_} }
Subroutine __ANON__: called CC=1 SCAR=0.0
693 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 {
!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
694 }
695 116 DEBUG and warn "relink: $relink\n";
696  
697 # pass an empty object if no related row found and it's not the
698 # special case where the primary key of a currently not related
699 # row is passed in the updates hash to prevent the find by pk in
700 # recursive_update to happen
701 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
702 56 DEBUG and warn "passing empty row to prevent find by pk\n";
703 56 $related_object = $related_resultset->new_result({});
704 }
705  
706 116 my $sub_object = recursive_update(
707 resultset => $related_resultset,
708 updates => $sub_updates,
709 resolved => $resolved,
710 # pass prefetched object if found
711 object => $related_object,
712 );
713  
714 111 push @updated_objs, $sub_object;
715 }
716  
717 # determine if a removal query is required
718 my @remove_rows = grep {
719 54 my $existing_row = $_;
720 159 ▶ none { $existing_row->ID eq $_->ID } @updated_objs
Subroutine __ANON__: called CC=1 SCAR=0.0
721 102 } @related_rows;
722 54 DEBUG and warn "rows for removal: " . join(', ', map { $_->ID }
723 @remove_rows) . "\n";
724  
725 54 ▶ if (scalar @remove_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @remove_rows) 21 33 54
726 21 my $rs_rel_delist = $object->$name;
727  
728 # foreign table has a single pk column
729 21 ▶ if (scalar @pks == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pks == 1) { } 6 15 21
730 6 DEBUG and warn "delete in not_in\n";
731 6 $rs_rel_delist = $rs_rel_delist->search_rs(
732 {
733 $self->current_source_alias . "." .
734 $pks[0] => { -not_in => [ map ( $_->id, @updated_objs ) ] }
735 }
736 );
737 }
738  
739 # foreign table has multiple pk columns
740 else {
741 15 my @cond;
742 15 for my $obj (@updated_objs) {
743 21 my %cond_for_obj;
744 21 for my $col (@pks) {
745 42 $cond_for_obj{ $self->current_source_alias . ".$col" } =
746 $obj->get_column($col);
747  
748 }
749 21 push @cond, \%cond_for_obj;
750 }
751  
752 # only limit resultset if there are related rows left
753 15 ▶ if (scalar @cond) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @cond) 11 4 15
754 11 $rs_rel_delist = $rs_rel_delist->search_rs({ -not => [ @cond ] });
755 }
756 }
757  
758 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
759 19 $rs_rel_delist->delete;
760 }
761 elsif ($if_not_submitted eq 'set_to_null') {
762 2 my %update = map {$_ => undef} @rel_cols;
763 2 $rs_rel_delist->update(\%update);
764 }
765 }
766 }
767 ▶ 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
768 $attrs->{accessor} eq 'filter' ) {
769 DEBUG and warn "has_one, might_have, belongs_to (" .
770 87 $attrs->{accessor} . "): $name\n";
771  
772 87 my $sub_object;
773 87 ▶ if ( ref $updates ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $updates) { } 86 1 87
774 86 my $existing_row = 0;
775 86 my @pks = $related_resultset->result_source->primary_columns;
776 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
777 37 $existing_row = 1;
778 }
779 86 DEBUG and warn $existing_row ? "existing row\n" : "new row\n";
780 # newly created rows can't have related rows
781 86 my $related_row;
782 86 ▶ if ($row_existed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_existed) 2 84 86
783 2 $related_row = $object->$name;
784 2 DEBUG and warn "got related row\n";
785 }
786 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
787 10 $sub_object = $updates;
788 }
789 ▶ 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
790 {
791 1 ▶ $sub_object = recursive_update(
Branch: 1 branch
BranchTrueFalseTotal
$existing_row ? : 0 1 1
792 resultset => $related_resultset,
793 updates => $updates,
794 $existing_row ? () : (object => $object->$name),
795 );
796 }
797 else {
798 75 ▶ $sub_object = recursive_update(
Branch: 1 branch
BranchTrueFalseTotal
$existing_row ? : 37 38 75
799 resultset => $related_resultset,
800 updates => $updates,
801 $existing_row ? () : (resolved => $resolved),
802 );
803 }
804 }
805 else {
806 $sub_object = $related_resultset->find($updates)
807 unless (
808 !$updates &&
809 ( exists $attrs->{join_type} &&
810 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
811 );
812 }
813 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
814 # unmarked 'LEFT' join for belongs_to
815 my $might_belong_to =
816 ( $attrs->{accessor} eq 'single' || $attrs->{accessor} eq 'filter' ) &&
817 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
818 # adding check for custom condition that's a coderef
819 # this 'set_from_related' should probably not be called in lots of other
820 # situations too, but until that's worked out, kludge it
821 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
822 ref $info->{cond} ne 'CODE' ) {
823 83 $object->$name($sub_object);
824 }
825 }
826 else {
827 $self->throw_exception(
828 "recursive_update doesn't now how to handle relationship '$name' with accessor " .
829 0 $info->{attrs}{accessor} );
830 }
831  
832 137 DEBUG and warn "_update_relation end\n";
833 }
834  
835 sub is_m2m {
836 392 ▶ my ( $self, $relation ) = @_;
Subroutine is_m2m: called CC=5 SCAR=16.1
837 392 my $rclass = $self->result_class;
838  
839 # DBIx::Class::IntrospectableM2M
840 392 ▶ if ( $rclass->can('_m2m_metadata') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rclass->can("_m2m_metadata")) 70 322 392
841 70 return $rclass->_m2m_metadata->{$relation};
842 }
843 322 my $object = $self->new_result( {} );
844 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
845 !$self->result_source->has_relationship($relation) and
846 $object->can( 'set_' . $relation ) ) {
847 12 return 1;
848 }
849 310 return;
850 }
851  
852 sub get_m2m_source {
853 10 ▶ my ( $self, $relation ) = @_;
Subroutine get_m2m_source: called CC=2 SCAR=6.9
854 10 my $rclass = $self->result_class;
855  
856 # DBIx::Class::IntrospectableM2M
857 10 ▶ if ( $rclass->can('_m2m_metadata') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rclass->can("_m2m_metadata")) 4 6 10
858 return $self->result_source->related_source(
859 $rclass->_m2m_metadata->{$relation}{relation} )
860 4 ->related_source( $rclass->_m2m_metadata->{$relation}{foreign_relation} );
861 }
862 6 my $object = $self->new_result( {} );
863 6 my $r = $object->$relation;
864 6 return $r->result_source;
865 }
866  
867 sub _delete_empty_auto_increment {
868 0 ▶ my ( $self, $object ) = @_;
Subroutine _delete_empty_auto_increment: not called CC=5 SCAR=34.0
869 0 for my $col ( sort keys %{ $object->{_column_data} } ) {
870 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
871 $object->result_source->column_info($col)->{is_auto_increment} and
872 ( !defined $object->{_column_data}{$col} or
873 $object->{_column_data}{$col} eq '' )
874 ) {
875 0 delete $object->{_column_data}{$col};
876 }
877 }
878 }
879  
880 sub _get_pk_for_related {
881 103 ▶ my ( $self, $relation ) = @_;
Subroutine _get_pk_for_related: called CC=3 SCAR=11.0
882 103 my $source;
883 103 ▶ if ( $self->result_source->has_relationship($relation) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->result_source->has_relationship($relation)) 93 10 103
884 93 $source = $self->result_source->related_source($relation);
885 }
886  
887 # many to many case
888 103 ▶ if ( is_m2m( $self, $relation ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_m2m($self, $relation)) 10 93 103
889 10 $source = get_m2m_source( $self, $relation );
890 }
891 103 return $source->primary_columns;
892 }
893  
894 # This function determines whether a relationship should be done before or
895 # after the row is inserted into the database
896 # relationships before: belongs_to
897 # relationships after: has_many, might_have and has_one
898 # true means before, false after
899 sub _master_relation_cond {
900 132 ▶ my ( $self, $name ) = @_;
Subroutine _master_relation_cond: called CC=6 SCAR=19.9
901  
902 132 my $source = $self->result_source;
903 132 my $info = $source->relationship_info($name);
904  
905 # has_many rels are always after
906 return 0
907 132 ▶ if $info->{attrs}->{accessor} eq 'multi';
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"attrs"}{"accessor"} eq "multi" 39 93 132
908  
909 93 my @foreign_ids = _get_pk_for_related( $self, $name );
910  
911 93 my $cond = $info->{cond};
912  
913 sub _inner {
914 93 ▶ my ( $source, $cond, @foreign_ids ) = @_;
Subroutine _inner: called CC=4 SCAR=14.0
915  
916 93 for my $f_key ( sort keys %$cond ) {
917 93 my $col = $cond->{$f_key};
918 # might_have is not master
919 93 $col =~ s/^self\.//;
920 93 $f_key =~ s/^foreign\.//;
921 93 ▶ if ( $source->column_info($col)->{is_auto_increment} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->column_info($col)->{"is_auto_increment"}) 9 84 93
922 9 return 0;
923 }
924 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
925 84 return 1;
926 }
927 }
928 0 return 0;
929 }
930  
931 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
932 93 return _inner( $source, $cond, @foreign_ids );
933 }
934  
935 # arrayref of hashrefs
936 elsif ( ref $cond eq 'ARRAY' ) {
937 0 for my $new_cond (@$cond) {
938 0 return _inner( $source, $new_cond, @foreign_ids );
939 }
940 }
941  
942 # we have a custom join condition, so update afterward
943 elsif ( ref $cond eq 'CODE' ) {
944 0 return 0;
945 }
946  
947 else {
948 0 $source->throw_exception( "unhandled relation condition " . ref($cond) );
949 }
950 0 return;
951 }
952  
953 10 1;
954  
955 __END__
956  
957 =pod
958  
959 =encoding UTF-8
960  
961 =head1 NAME
962  
963 DBIx::Class::ResultSet::RecursiveUpdate - like update_or_create - but recursive
964  
965 =head1 VERSION
966  
967 version 0.42
968  
969 =head1 SYNOPSIS
970  
971 # The functional interface:
972  
973 my $schema = MyDB::Schema->connect();
974 my $new_item = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
975 resultset => $schema->resultset('User'),
976 updates => {
977 id => 1,
978 owned_dvds => [
979 {
980 title => "One Flew Over the Cuckoo's Nest"
981 }
982 ]
983 },
984 unknown_params_ok => 1,
985 );
986  
987  
988 # As ResultSet subclass:
989  
990 __PACKAGE__->load_namespaces( default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate' );
991  
992 # in the Schema file (see t/lib/DBSchema.pm). Or appropriate 'use base' in the ResultSet classes.
993  
994 my $user = $schema->resultset('User')->recursive_update({
995 id => 1,
996 owned_dvds => [
997 {
998 title => "One Flew Over the Cuckoo's Nest"
999 }
1000 ]
1001 }, {
1002 unknown_params_ok => 1,
1003 });
1004  
1005 # You'll get a warning if you pass non-result specific data to
1006 # recursive_update. See L</"Additional data in the updates hashref">
1007 # for more information how to prevent this.
1008  
1009 =head1 DESCRIPTION
1010  
1011 You can feed the ->create method of DBIx::Class with a recursive datastructure
1012 and have the related records created. Unfortunately you cannot do a similar
1013 thing with update_or_create. This module tries to fill that void until
1014 L<DBIx::Class> has an api itself.
1015  
1016 The functional interface can be used without modifications of the model,
1017 for example by form processors like L<HTML::FormHandler::Model::DBIC>.
1018  
1019 It is a base class for L<DBIx::Class::ResultSet>s providing the method
1020 recursive_update which works just like update_or_create but can recursively
1021 update or create result objects composed of multiple rows. All rows need to be
1022 identified by primary keys so you need to provide them in the update structure
1023 (unless they can be deduced from the parent row. For example a related row of
1024 a belongs_to relationship). If any of the primary key columns are missing,
1025 a new row will be created, with the expectation that the missing columns will
1026 be filled by it (as in the case of auto_increment primary keys).
1027  
1028 If the resultset itself stores an assignment for the primary key,
1029 like in the case of:
1030  
1031 my $restricted_rs = $user_rs->search( { id => 1 } );
1032  
1033 you need to inform recursive_update about the additional predicate with the fixed_fields attribute:
1034  
1035 my $user = $restricted_rs->recursive_update( {
1036 owned_dvds => [
1037 {
1038 title => 'One Flew Over the Cuckoo's Nest'
1039 }
1040 ]
1041 },
1042 {
1043 fixed_fields => [ 'id' ],
1044 }
1045 );
1046  
1047 For a many_to_many (pseudo) relation you can supply a list of primary keys
1048 from the other table and it will link the record at hand to those and
1049 only those records identified by them. This is convenient for handling web
1050 forms with check boxes (or a select field with multiple choice) that lets you
1051 update such (pseudo) relations.
1052  
1053 For a description how to set up base classes for ResultSets see
1054 L<DBIx::Class::Schema/load_namespaces>.
1055  
1056 =head2 Additional data in the updates hashref
1057  
1058 If you pass additional data to recursive_update which doesn't match a column
1059 name, column accessor, relationship or many-to-many helper accessor, it will
1060 throw a warning by default. To disable this behaviour you can set the
1061 unknown_params_ok attribute to a true value.
1062  
1063 The warning thrown is:
1064 "No such column, relationship, many-to-many helper accessor or generic accessor '$key'"
1065  
1066 When used by L<HTML::FormHandler::Model::DBIC> this can happen if you have
1067 additional form fields that aren't relevant to the database but don't have the
1068 noupdate attribute set to a true value.
1069  
1070 NOTE: in a future version this behaviour will change and throw an exception
1071 instead of a warning!
1072  
1073 =head1 DESIGN CHOICES
1074  
1075 Columns and relationships which are excluded from the updates hashref aren't
1076 touched at all.
1077  
1078 =head2 Treatment of belongs_to relations
1079  
1080 In case the relationship is included but undefined in the updates hashref,
1081 all columns forming the relationship will be set to null.
1082 If not all of them are nullable, DBIx::Class will throw an error.
1083  
1084 Updating the relationship:
1085  
1086 my $dvd = $dvd_rs->recursive_update( {
1087 id => 1,
1088 owner => $user->id,
1089 });
1090  
1091 Clearing the relationship (only works if cols are nullable!):
1092  
1093 my $dvd = $dvd_rs->recursive_update( {
1094 id => 1,
1095 owner => undef,
1096 });
1097  
1098 Updating a relationship including its (full) primary key:
1099  
1100 my $dvd = $dvd_rs->recursive_update( {
1101 id => 1,
1102 owner => {
1103 id => 2,
1104 name => "George",
1105 },
1106 });
1107  
1108 =head2 Treatment of might_have relationships
1109  
1110 In case the relationship is included but undefined in the updates hashref,
1111 all columns forming the relationship will be set to null.
1112  
1113 Updating the relationship:
1114  
1115 my $user = $user_rs->recursive_update( {
1116 id => 1,
1117 address => {
1118 street => "101 Main Street",
1119 city => "Podunk",
1120 state => "New York",
1121 }
1122 });
1123  
1124 Clearing the relationship:
1125  
1126 my $user = $user_rs->recursive_update( {
1127 id => 1,
1128 address => undef,
1129 });
1130  
1131 =head2 Treatment of has_many relations
1132  
1133 If a relationship key is included in the data structure with a value of undef
1134 or an empty array, all existing related rows will be deleted, or their foreign
1135 key columns will be set to null.
1136  
1137 The exact behaviour depends on the nullability of the foreign key columns and
1138 the value of the "if_not_submitted" parameter. The parameter defaults to
1139 undefined which neither nullifies nor deletes.
1140  
1141 When the array contains elements they are updated if they exist, created when
1142 not and deleted if not included.
1143  
1144 =head3 All foreign table columns are nullable
1145  
1146 In this case recursive_update defaults to nullifying the foreign columns.
1147  
1148 =head3 Not all foreign table columns are nullable
1149  
1150 In this case recursive_update deletes the foreign rows.
1151  
1152 Updating the relationship:
1153  
1154 Passing ids:
1155  
1156 my $user = $user_rs->recursive_update( {
1157 id => 1,
1158 owned_dvds => [1, 2],
1159 });
1160  
1161 Passing hashrefs:
1162  
1163 my $user = $user_rs->recursive_update( {
1164 id => 1,
1165 owned_dvds => [
1166 {
1167 name => 'temp name 1',
1168 },
1169 {
1170 name => 'temp name 2',
1171 },
1172 ],
1173 });
1174  
1175 Passing objects:
1176  
1177 my $user = $user_rs->recursive_update( {
1178 id => 1,
1179 owned_dvds => [ $dvd1, $dvd2 ],
1180 });
1181  
1182 You can even mix them:
1183  
1184 my $user = $user_rs->recursive_update( {
1185 id => 1,
1186 owned_dvds => [ 1, { id => 2 } ],
1187 });
1188  
1189 Clearing the relationship:
1190  
1191 my $user = $user_rs->recursive_update( {
1192 id => 1,
1193 owned_dvds => undef,
1194 });
1195  
1196 This is the same as passing an empty array:
1197  
1198 my $user = $user_rs->recursive_update( {
1199 id => 1,
1200 owned_dvds => [],
1201 });
1202  
1203 =head2 Treatment of many-to-many pseudo relations
1204  
1205 If a many-to-many accessor key is included in the data structure with a value
1206 of undef or an empty array, all existing related rows are unlinked.
1207  
1208 When the array contains elements they are updated if they exist, created when
1209 not and deleted if not included.
1210  
1211 RecursiveUpdate defaults to
1212 calling 'set_$rel' to update many-to-many relationships.
1213 See L<DBIx::Class::Relationship/many_to_many> for details.
1214 set_$rel effectively removes and re-adds all relationship data,
1215 even if the set of related items did not change at all.
1216  
1217 If L<DBIx::Class::IntrospectableM2M> is in use, RecursiveUpdate will
1218 look up the corresponding has_many relationship and use this to recursively
1219 update the many-to-many relationship.
1220  
1221 While both mechanisms have the same final result, deleting and re-adding
1222 all relationship data can have unwanted consequences if triggers or
1223 method modifiers are defined or logging modules like L<DBIx::Class::AuditLog>
1224 are in use.
1225  
1226 The traditional "set_$rel" behaviour can be forced by passing
1227 "m2m_force_set_rel => 1" to recursive_update.
1228  
1229 See L</is_m2m> for many-to-many pseudo relationship detection.
1230  
1231 Updating the relationship:
1232  
1233 Passing ids:
1234  
1235 my $dvd = $dvd_rs->recursive_update( {
1236 id => 1,
1237 tags => [1, 2],
1238 });
1239  
1240 Passing hashrefs:
1241  
1242 my $dvd = $dvd_rs->recursive_update( {
1243 id => 1,
1244 tags => [
1245 {
1246 id => 1,
1247 file => 'file0'
1248 },
1249 {
1250 id => 2,
1251 file => 'file1',
1252 },
1253 ],
1254 });
1255  
1256 Passing objects:
1257  
1258 my $dvd = $dvd_rs->recursive_update( {
1259 id => 1,
1260 tags => [ $tag1, $tag2 ],
1261 });
1262  
1263 You can even mix them:
1264  
1265 my $dvd = $dvd_rs->recursive_update( {
1266 id => 1,
1267 tags => [ 2, { id => 3 } ],
1268 });
1269  
1270 Clearing the relationship:
1271  
1272 my $dvd = $dvd_rs->recursive_update( {
1273 id => 1,
1274 tags => undef,
1275 });
1276  
1277 This is the same as passing an empty array:
1278  
1279 my $dvd = $dvd_rs->recursive_update( {
1280 id => 1,
1281 tags => [],
1282 });
1283  
1284 Make sure that set_$rel used to update many-to-many relationships
1285 even if IntrospectableM2M is loaded:
1286  
1287 my $dvd = $dvd_rs->recursive_update( {
1288 id => 1,
1289 tags => [1, 2],
1290 },
1291 { m2m_force_set_rel => 1 },
1292 );
1293  
1294 =head1 INTERFACE
1295  
1296 =head1 METHODS
1297  
1298 =head2 recursive_update
1299  
1300 The method that does the work here.
1301  
1302 =head2 is_m2m
1303  
1304 =over 4
1305  
1306 =item Arguments: $name
1307  
1308 =item Return Value: true, if $name is a many to many pseudo-relationship
1309  
1310 =back
1311  
1312 The function gets the information about m2m relations from
1313 L<DBIx::Class::IntrospectableM2M>. If it isn't loaded in the ResultSource
1314 class, the code relies on the fact:
1315  
1316 if($object->can($name) and
1317 !$object->result_source->has_relationship($name) and
1318 $object->can( 'set_' . $name )
1319 )
1320  
1321 to identify a many to many pseudo relationship. In a similar ugly way the
1322 ResultSource of that many to many pseudo relationship is detected.
1323  
1324 So if you need many to many pseudo relationship support, it's strongly
1325 recommended to load L<DBIx::Class::IntrospectableM2M> in your ResultSource
1326 class!
1327  
1328 =head2 get_m2m_source
1329  
1330 =over 4
1331  
1332 =item Arguments: $name
1333  
1334 =item Return Value: $result_source
1335  
1336 =back
1337  
1338 =head1 CONFIGURATION AND ENVIRONMENT
1339  
1340 DBIx::Class::RecursiveUpdate requires no configuration files or environment variables.
1341  
1342 =head1 DEPENDENCIES
1343  
1344 DBIx::Class
1345  
1346 optional but recommended:
1347 DBIx::Class::IntrospectableM2M
1348  
1349 =head1 INCOMPATIBILITIES
1350  
1351 None reported.
1352  
1353 =head1 BUGS AND LIMITATIONS
1354  
1355 The list of reported bugs can be viewed at L<http://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Class-ResultSet-RecursiveUpdate>.
1356  
1357 Please report any bugs or feature requests to
1358 C<bug-DBIx-Class-ResultSet-RecursiveUpdate@rt.cpan.org>, or through the web interface at
1359 L<http://rt.cpan.org>.
1360  
1361 =head1 AUTHORS
1362  
1363 =over 4
1364  
1365 =item *
1366  
1367 Zbigniew Lukasiak <zby@cpan.org>
1368  
1369 =item *
1370  
1371 John Napiorkowski <jjnapiork@cpan.org>
1372  
1373 =item *
1374  
1375 Alexander Hartmaier <abraxxa@cpan.org>
1376  
1377 =item *
1378  
1379 Gerda Shank <gshank@cpan.org>
1380  
1381 =back
1382  
1383 =head1 COPYRIGHT AND LICENSE
1384  
1385 This software is copyright (c) 2020 by Zbigniew Lukasiak, John Napiorkowski, Alexander Hartmaier.
1386  
1387 This is free software; you can redistribute it and/or modify it under
1388 the same terms as the Perl 5 programming language system itself.
1389  
1390 =cut
↑ Summary