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