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

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