blib/lib/DBIO/Relationship/ManyToMany.pm

filter: Statementstmt 67.0% 106 / 158 Branchbran 47.2% 34 / 72 Conditioncond 23.5% 8 / 34 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 80.9% 17 / 21 Podpod 100% 1 / 1 totaltotal 54.9% 168 / 306 CC 318 SCAR 25.6
CC
318
Cov
56%
many_to_many
21.3
__ANON__
18.3
add_to_artists
18.3
CRAP 12.9
« blib/lib/DBIO/Carp.pm ↑ Summary blib/lib/DBIO/Extension/Protocol.pm »
1 package DBIO::Relationship::ManyToMany;
2 # ABSTRACT: Declare a many-to-many relationship via a bridge table
3  
4 378 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 378 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 318 our $VERSION = '0.900007';
8  
9 378 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 378 ▶use Sub::Util 'set_subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 378 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 378 ▶use DBIO::Util qw(assert_no_list_context);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 378 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 318 our %_pod_inherit_config =
16 (
17 class_map => { 'DBIO::Relationship::ManyToMany' => 'DBIO::Relationship' }
18 );
19  
20  
21 3591 ▶sub many_to_many {
Subroutine many_to_many: called CC=10 SCAR=30.6
Pod documented
22 3591 my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_;
23  
24 3591 ▶ $class->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $rel 0 3591 3591
25 "missing relation in many-to-many"
26 ) unless $rel;
27  
28 3591 ▶ $class->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $f_rel 0 3591 3591
29 "missing foreign relation in many-to-many"
30 ) unless $f_rel;
31  
32 3591 ▶ $class->mk_classdata('_m2m_metadata' => {})
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("_m2m_metadata") 2053 1538 3591
33 unless $class->can('_m2m_metadata');
34  
35 3591 my $store = $class->_m2m_metadata;
36 carp("Overwriting existing many-to-many metadata for '$meth'")
37 3591 ▶ if exists $store->{$meth};
Branch: 1 branch
BranchTrueFalseTotal
if exists $store->{$meth} 1 3590 3591
38  
39 3591 ▶ $class->_m2m_metadata({
Branch: 1 branch
BranchTrueFalseTotal
@_ > 4 ? : 769 2822 3591
40 %$store,
41 $meth => {
42 accessor => $meth,
43 relation => $rel,
44 foreign_relation => $f_rel,
45 (@_ > 4 ? (attrs => $rel_attrs) : ()),
46 rs_method => "${meth}_rs",
47 add_method => "add_to_${meth}",
48 set_method => "set_${meth}",
49 remove_method => "remove_from_${meth}",
50 },
51 });
52  
53 {
54 378 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 378 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 3591 my $add_meth = "add_to_${meth}";
58 3591 my $remove_meth = "remove_from_${meth}";
59 3591 my $set_meth = "set_${meth}";
60 3591 my $rs_meth = "${meth}_rs";
61  
62 3591 for ($add_meth, $remove_meth, $set_meth, $rs_meth) {
63 14364 ▶ if ( $class->can ($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can($_)) 6 14358 14364
64 6 ▶ carp (<<"EOW") unless $ENV{DBIO_OVERWRITE_HELPER_METHODS_OK};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DBIO_OVERWRITE_HELPER_METHODS_OK"} 1 5 6
65  
66 ***************************************************************************
67 The many-to-many relationship '$meth' is trying to create a utility method
68 called $_.
69 This will completely overwrite one such already existing method on class
70 $class.
71  
72 You almost certainly want to rename your method or the many-to-many
73 relationship, as the functionality of the original method will not be
74 accessible anymore.
75  
76 To disable this warning set to a true value the environment variable
77 DBIO_OVERWRITE_HELPER_METHODS_OK
78  
79 ***************************************************************************
80 EOW
81 }
82 }
83  
84 3591 ▶ $rel_attrs->{alias} ||= $f_rel;
Condition33%1/3
$rel_attrs->{"alias"} ||= $f_rel
l!l&&r!l&&!r
035910
MC/DC0%0/2
$rel_attrs->{"alias"} ||= $f_rel
$rel_attrs->{"alias"} $f_rel
Truth table33%1/3
A ||= B
A= $rel_attrs->{"alias"}B= $f_rel
ABresult
1X1
011
000
85  
86 3591 my $rs_meth_name = join '::', $class, $rs_meth;
87 0 ▶ *$rs_meth_name = set_subname $rs_meth_name, sub {
Subroutine objects_rs: not called CC=5 SCAR=26.7
Subroutine fourkeys_rs: called CC=5 SCAR=26.7
Subroutine producers_sorted_rs: not called CC=5 SCAR=26.7
88 0 my $self = shift;
89 0 ▶ my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
Branch: 3 branches
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 0 0
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 2 2
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 0 0
Condition16%1/6
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
000
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
600
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table0%0/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
90 my $rs = $self->search_related($rel)->search_related(
91 0 ▶ $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }
Branch: 2 branches
BranchTrueFalseTotal
@_ > 0 ? : 0 0 0
@_ > 0 ? : 5 1 6
Condition20%1/5
$rel_attrs || {}
l!l
00
$rel_attrs || {}
l!l
700
MC/DC0%0/1
$rel_attrs || {}
$rel_attrs
MC/DC100%1/1
$rel_attrs || {}
$rel_attrs
Truth table0%0/2
A
A= $rel_attrs
Aresult
11
00
Truth table100%2/2
A
A= $rel_attrs
Aresult
11
00
92 );
93 0 return $rs;
94 3591 };
95  
96 3591 my $meth_name = join '::', $class, $meth;
97 2 ▶ *$meth_name = set_subname $meth_name, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
98 2 my $self = shift;
99 # ADR 0042: the m2m accessor used to return the rows in list context.
100 # It now always returns the ResultSet and refuses list context outright.
101 2 ▶ assert_no_list_context("relationship accessor '$meth'") if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 1 2
102 1 return $self->$rs_meth( @_ );
103 3591 };
104  
105 3591 my $add_meth_name = join '::', $class, $add_meth;
106 0 ▶ *$add_meth_name = set_subname $add_meth_name, sub {
Subroutine add_to_artworks: not called CC=8 SCAR=29.0
Subroutine add_to_collections: called CC=8 SCAR=29.0
107 0 my $self = shift;
108 0 ▶ @_ > 0 or $self->throw_exception(
Branch: 2 branches
BranchTrueFalseTotal
@_ > 0 or $self->throw_exception("$add_meth needs an object or hashref") 0 0 0
@_ > 0 or $self->throw_exception("$add_meth needs an object or hashref") 2 3 5
109 "${add_meth} needs an object or hashref"
110 );
111 0 my $source = $self->result_source;
112 0 my $schema = $source->schema;
113 0 my $rel_source_name = $source->relationship_info($rel)->{source};
114 0 my $rel_source = $schema->resultset($rel_source_name)->result_source;
115 0 my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source};
116 0 ▶ my $f_rel_rs = $schema->resultset($f_rel_source_name)->search({}, $rel_attrs||{});
Condition20%1/5
$rel_attrs || {}
l!l
00
$rel_attrs || {}
l!l
300
MC/DC0%0/1
$rel_attrs || {}
$rel_attrs
MC/DC0%0/1
$rel_attrs || {}
$rel_attrs
Truth table0%0/2
A
A= $rel_attrs
Aresult
11
00
Truth table50%1/2
A
A= $rel_attrs
Aresult
11
00
117  
118 # ADR 0045 / karr k325: the row found or created here is returned, and it
119 # is derived from this one -- called on a context-bound row it keeps the
120 # pin. A row object passed in is handed back as it came.
121 0 ▶ $f_rel_rs->{_async_context} = $self->{_async_context} if $self->{_async_context};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"_async_context"} 0 0 0
if $self->{"_async_context"} 7 0 7
122  
123 # a row the data identifies is the row to link to only when the link's
124 # relationship is a belongs_to; any other data is created (ADR 0071)
125 0 my $find = $rel_source->_relationship_depends_on($f_rel);
126 7 ▶ my $find_or_create = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
127 7 ▶ ( $find && $f_rel_rs->_find_identified($_[0]) ) || $f_rel_rs->new_result($_[0])->insert
Branch: 1 branch
BranchTrueFalseTotal
$find and $f_rel_rs->_find_identified($_[0]) or $f_rel_rs->new_result($_[0])->insert 1 6 7
Condition66%2/3
$find and $f_rel_rs->_find_identified($_[0])
!ll&&!rl&&r
061
MC/DC50%1/2
$find and $f_rel_rs->_find_identified($_[0])
$find $f_rel_rs->_find_identified($_[0])
Truth table66%2/3
A and B
A= $findB= $f_rel_rs->_find_identified($_[0])
ABresult
0X0
100
111
128 0 };
129  
130 0 my $obj;
131 0 ▶ if (ref $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
if (ref $_[0]) { } 5 2 7
132 0 ▶ if (ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
if (ref $_[0] eq 'HASH') { } 2 5 7
133 0 $obj = $find_or_create->($_[0]);
134 } else {
135 0 $obj = $_[0];
136 }
137 } else {
138 0 $obj = $find_or_create->({@_});
139 }
140  
141 0 ▶ my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 0 0
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 2 0 2
Condition16%1/6
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
000
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table0%0/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
142 0 my $link = $self->search_related($rel)->new_result($link_vals);
143 0 $link->set_from_related($f_rel, $obj);
144 0 $link->insert();
145 0 return $obj;
146 3591 };
147  
148 3591 my $set_meth_name = join '::', $class, $set_meth;
149 2 ▶ *$set_meth_name = set_subname $set_meth_name, sub {
Subroutine set_artworks: called CC=7 SCAR=22.9
Subroutine set_collections: called CC=7 SCAR=22.9
150 2 my $self = shift;
151 2 ▶ @_ > 0 or $self->throw_exception(
Branch: 2 branches
BranchTrueFalseTotal
@_ > 0 or $self->throw_exception("{$set_meth} needs a list of objects or hashrefs") 2 0 2
@_ > 0 or $self->throw_exception("{$set_meth} needs a list of objects or hashrefs") 1 0 1
152 "{$set_meth} needs a list of objects or hashrefs"
153 );
154 2 ▶ my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_);
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 2 0 2
ref $_[0] eq 'ARRAY' ? : 0 1 1
155 # if there is a where clause in the attributes, ensure we only delete
156 # rows that are within the where restriction
157 # the delete and the adds commit together or not at all
158 2 my $guard = $self->result_source->schema->txn_scope_guard;
159 2 ▶ if ($rel_attrs && $rel_attrs->{where}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rel_attrs and $rel_attrs->{'where'}) { } 0 2 2
if ($rel_attrs and $rel_attrs->{'where'}) { } 0 1 1
Condition16%1/6
$rel_attrs and $rel_attrs->{"where"}
!ll&&!rl&&r
020
$rel_attrs and $rel_attrs->{"where"}
!ll&&!rl&&r
000
MC/DC0%0/2
$rel_attrs and $rel_attrs->{"where"}
$rel_attrs $rel_attrs->{"where"}
MC/DC0%0/2
$rel_attrs and $rel_attrs->{"where"}
$rel_attrs $rel_attrs->{"where"}
Truth table33%1/3
A and B
A= $rel_attrsB= $rel_attrs->{"where"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $rel_attrsB= $rel_attrs->{"where"}
ABresult
0X0
100
111
160 0 $self->search_related( $rel, $rel_attrs->{where},{join => $f_rel})->delete;
161 } else {
162 2 $self->search_related( $rel, {} )->delete;
163 }
164 # add in the set rel objects
165 2 ▶ $self->$add_meth($_, ref($_[1]) ? $_[1] : {}) for (@to_set);
Branch: 2 branches
BranchTrueFalseTotal
ref $_[1] ? : 0 2 2
ref $_[1] ? : 0 0 0
166 0 $guard->commit;
167 0 return;
168 3591 };
169  
170 3591 my $remove_meth_name = join '::', $class, $remove_meth;
171 0 ▶ *$remove_meth_name = set_subname $remove_meth_name, sub {
Subroutine remove_from_producers: not called CC=3 SCAR=24.8
Subroutine remove_from_collections: called CC=3 SCAR=24.8
172 0 my ($self, $obj) = @_;
173 0 ▶ $self->throw_exception("${remove_meth} needs an object")
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($obj) 0 0 0
unless builtin::blessed($obj) 0 0 0
174 unless blessed ($obj);
175 0 my $rel_source = $self->search_related($rel)->result_source;
176 0 my $cond = $rel_source->relationship_info($f_rel)->{cond};
177 0 my ($link_cond, $crosstable) = $rel_source->_resolve_condition(
178 $cond, $obj, $f_rel, $f_rel
179 );
180  
181 0 ▶ $self->throw_exception(
Branch: 2 branches
BranchTrueFalseTotal
if $crosstable 0 0 0
if $crosstable 0 0 0
182 "Relationship '$rel' does not resolve to a join-free condition, "
183 ."unable to use with the ManyToMany helper '$f_rel'"
184 ) if $crosstable;
185  
186 0 $self->search_related($rel, $link_cond)->delete;
187 3591 };
188  
189 }
190 }
191  
192 318 1;
193  
194 __END__
« blib/lib/DBIO/Carp.pm ↑ Summary blib/lib/DBIO/Extension/Protocol.pm »