c7cde16d79f4673c05938af99096ea014634889c
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / ManyToMany.pm
1 package # hide from PAUSE
2     DBIx::Class::Relationship::ManyToMany;
3
4 use strict;
5 use warnings;
6
7 use DBIx::Class::Carp;
8 use DBIx::Class::_Util qw( quote_sub perlstring );
9
10 # FIXME - this souldn't be needed
11 my $cu;
12 BEGIN { $cu = \&carp_unique }
13
14 use namespace::clean;
15
16 our %_pod_inherit_config =
17   (
18    class_map => { 'DBIx::Class::Relationship::ManyToMany' => 'DBIx::Class::Relationship' }
19   );
20
21 sub many_to_many {
22   my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_;
23
24   $class->throw_exception(
25     "missing relation in many-to-many"
26   ) unless $rel;
27
28   $class->throw_exception(
29     "missing foreign relation in many-to-many"
30   ) unless $f_rel;
31
32     my $add_meth = "add_to_${meth}";
33     my $remove_meth = "remove_from_${meth}";
34     my $set_meth = "set_${meth}";
35     my $rs_meth = "${meth}_rs";
36
37     for ($add_meth, $remove_meth, $set_meth, $rs_meth) {
38       if ( $class->can ($_) ) {
39         carp (<<"EOW") unless $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK};
40
41 ***************************************************************************
42 The many-to-many relationship '$meth' is trying to create a utility method
43 called $_.
44 This will completely overwrite one such already existing method on class
45 $class.
46
47 You almost certainly want to rename your method or the many-to-many
48 relationship, as the functionality of the original method will not be
49 accessible anymore.
50
51 To disable this warning set to a true value the environment variable
52 DBIC_OVERWRITE_HELPER_METHODS_OK
53
54 ***************************************************************************
55 EOW
56       }
57     }
58
59     my $qsub_attrs = {
60       '$rel_attrs' => \{ alias => $f_rel, %{ $rel_attrs||{} } },
61       '$carp_unique' => \$cu,
62     };
63
64     quote_sub "${class}::${rs_meth}", sprintf( <<'EOC', map { perlstring $_ } ( "${class}::${meth}", $rel, $f_rel ) ), $qsub_attrs;
65
66       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS
67         and
68       # allow nested calls from our ->many_to_many, see comment below
69       ( (CORE::caller(1))[3] ne %s )
70         and
71       DBIx::Class::_Util::fail_on_internal_call;
72
73       # this little horror is there replicating a deprecation from
74       # within search_rs() itself
75       shift->related_resultset( %s )
76             ->related_resultset( %s )
77              ->search_rs (
78                undef,
79                ( @_ > 1 and ref $_[-1] eq 'HASH' )
80                  ? { %%$rel_attrs, %%{ pop @_ } }
81                  : $rel_attrs
82              )->search_rs(@_)
83       ;
84 EOC
85
86
87     quote_sub "${class}::${meth}", sprintf( <<'EOC', $rs_meth );
88
89       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
90       DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
91
92       my $rs = shift->%s( @_ );
93
94       wantarray ? $rs->all : $rs;
95 EOC
96
97
98     quote_sub "${class}::${add_meth}", sprintf( <<'EOC', $add_meth, $rel, $f_rel ), $qsub_attrs;
99
100       ( @_ >= 2 and @_ <= 3 ) or $_[0]->throw_exception(
101         "'%1$s' expects an object or hashref to link to, and an optional hashref of link data"
102       );
103
104       $_[0]->throw_exception(
105         "The optional link data supplied to '%1$s' is not a hashref (it was previously ignored)"
106       ) if $_[2] and ref $_[2] ne 'HASH';
107
108       my( $self, $far_obj ) = @_;
109
110       my $guard;
111
112       # the API needs is always expected to return the far object, possibly
113       # creating it in the process
114       if( not defined Scalar::Util::blessed( $far_obj ) ) {
115
116         $guard = $self->result_source->schema->storage->txn_scope_guard;
117
118         # reify the hash into an actual object
119         $far_obj = $self->result_source
120                          ->related_source( q{%2$s} )
121                           ->related_source( q{%3$s} )
122                            ->resultset
123                             ->search_rs( undef, $rel_attrs )
124                              ->find_or_create( $far_obj );
125       }
126
127       my $link = $self->new_related(
128         q{%2$s},
129         $_[2] || {},
130       );
131
132       $link->set_from_related( q{%3$s}, $far_obj );
133
134       $link->insert();
135
136       $guard->commit if $guard;
137
138       $far_obj;
139 EOC
140
141
142     quote_sub "${class}::${set_meth}", sprintf( <<'EOC', $set_meth, $add_meth, $rel, $f_rel ), $qsub_attrs;
143
144       my $self = shift;
145
146       my $set_to = ( ref $_[0] eq 'ARRAY' )
147         ? ( shift @_ )
148         : do {
149           $carp_unique->(
150             "Calling '%1$s' with a list of items to link to is deprecated, use an arrayref instead"
151           );
152
153           # gobble up everything from @_ into a new arrayref
154           [ splice @_ ]
155         }
156       ;
157
158       # make sure folks are not invoking a bizarre mix of deprecated and curent syntax
159       $self->throw_exception(
160         "'%1$s' expects an arrayref of objects or hashrefs to link to, and an optional hashref of link data"
161       ) if (
162         @_ > 1
163           or
164         ( defined $_[0] and ref $_[0] ne 'HASH' )
165       );
166
167       my $guard;
168
169       # there will only be a single delete() op, unless we have what to set to
170       $guard = $self->result_source->schema->storage->txn_scope_guard
171         if @$set_to;
172
173       # if there is a where clause in the attributes, ensure we only delete
174       # rows that are within the where restriction
175       $self->related_resultset( q{%3$s} )
176             ->search_rs(
177               ( $rel_attrs->{where}
178                 ? ( $rel_attrs->{where}, { join => q{%4$s} } )
179                 : ()
180               )
181             )->delete;
182
183       # add in the set rel objects
184       $self->%2$s(
185         $_,
186         @_, # at this point @_ is either empty or contains a lone link-data hash
187       ) for @$set_to;
188
189       $guard->commit if $guard;
190 EOC
191
192
193     quote_sub "${class}::${remove_meth}", sprintf( <<'EOC', $remove_meth, $rel, $f_rel );
194
195       $_[0]->throw_exception("'%1$s' expects an object")
196         unless defined Scalar::Util::blessed( $_[1] );
197
198       $_[0]->related_resultset( q{%2$s} )
199             ->search_rs( $_[1]->ident_condition( q{%3$s} ), { join => q{%3$s} } )
200              ->delete;
201 EOC
202
203 }
204
205 1;