1 package # hide from PAUSE
2 DBIx::Class::ResultSourceProxy;
7 use base 'DBIx::Class';
9 # ! LOAD ORDER SENSITIVE !
10 # needs to be loaded early to query method attributes below
11 # and to do the around()s properly
12 use DBIx::Class::ResultSource;
13 my @wrap_rsrc_methods = qw(
18 use DBIx::Class::_Util qw(
19 quote_sub perlstring fail_on_internal_call describe_class_methods
23 # FIXME: this is truly bizarre, not sure why it is this way since 93405cf0
24 # This value *IS* *DIFFERENT* from source_name in the underlying rsrc
25 # instance, and there is *ZERO EFFORT* made to synchronize them...
26 # FIXME: Due to the above marking this as a rsrc_proxy method is also out
28 __PACKAGE__->mk_group_accessors('inherited_ro_instance' => 'source_name');
30 sub get_inherited_ro_instance { $_[0]->get_inherited($_[1]) }
32 sub set_inherited_ro_instance {
33 $_[0]->throw_exception ("Cannot set '$_[1]' on an instance")
36 $_[0]->set_inherited( $_[1], $_[2] );
39 sub add_columns :DBIC_method_is_bypassable_resultsource_proxy {
40 my ($class, @cols) = @_;
41 my $source = $class->result_source;
42 local $source->{__callstack_includes_rsrc_proxy_method} = "add_columns";
44 $source->add_columns(@cols);
47 foreach my $c (grep { !ref } @cols) {
48 # If this is an augment definition get the real colname.
51 $class->register_column(
53 ( $colinfos ||= $source->columns_info )->{$c}
58 sub add_column :DBIC_method_is_indirect_sugar {
59 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
60 shift->add_columns(@_)
63 sub add_relationship :DBIC_method_is_bypassable_resultsource_proxy {
64 my ($class, $rel, @rest) = @_;
65 my $source = $class->result_source;
66 local $source->{__callstack_includes_rsrc_proxy_method} = "add_relationship";
68 $source->add_relationship($rel => @rest);
69 $class->register_relationship($rel => $source->relationship_info($rel));
73 # legacy resultset_class accessor, seems to be used by cdbi only
74 sub iterator_class :DBIC_method_is_indirect_sugar {
75 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
76 shift->result_source->resultset_class(@_)
79 for my $method_to_proxy (qw/
93 column_info_from_storage
100 add_unique_constraints
103 unique_constraint_names
104 unique_constraint_columns
110 my $qsub_opts = { attributes => [
113 attributes::get( \&{"DBIx::Class::ResultSource::$method_to_proxy"} );
117 # bypassable default for backcompat, except for indirect methods
118 # ( those will simply warn during the sanheck )
120 { $_ eq 'DBIC_method_is_indirect_sugar' }
121 @{ $qsub_opts->{attributes} }
123 push @wrap_rsrc_methods, $method_to_proxy;
124 push @{ $qsub_opts->{atributes} }, 'DBIC_method_is_bypassable_resultsource_proxy';
127 quote_sub __PACKAGE__."::$method_to_proxy", sprintf( <<'EOC', $method_to_proxy ), {}, $qsub_opts;
128 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
130 my $rsrc = shift->result_source;
131 local $rsrc->{__callstack_includes_rsrc_proxy_method} = q(%1$s);
137 # This is where the "magic" of detecting/invoking the proper overridden
138 # Result method takes place. It isn't implemented as a stateless out-of-band
139 # SanityCheck as invocation requires certain state in the $rsrc object itself
140 # in order not to loop over itself. It is not in ResultSource.pm either
141 # because of load order and because the entire stack is just terrible :/
143 # The code is not easily readable, as it it optimized for execution time
144 # (this stuff will be run all the time across the entire install base :/ )
147 our %__rsrc_proxy_meta_cache;
149 sub DBIx::Class::__RsrcProxy_iThreads_handler__::CLONE {
150 # recreating this cache is pretty cheap: just blow it away
151 %__rsrc_proxy_meta_cache = ();
154 for my $method_to_wrap (@wrap_rsrc_methods) {
157 perlstring $method_to_wrap,
162 \&{"DBIx::Class::ResultSource::$method_to_wrap"}
165 my %unclassified_override_warn_emitted;
169 # ref to hashref, this is how S::Q works
170 '$rsrc_proxy_meta_cache' => \\%__rsrc_proxy_meta_cache,
171 '$unclassified_override_warn_emitted' => \\%unclassified_override_warn_emitted,
174 { attributes => [ attributes::get($orig) ] }
177 quote_sub "DBIx::Class::ResultSource::$method_to_wrap", sprintf( <<'EOC', @src_args ), @qsub_args;
179 my $overridden_proxy_cref;
181 # fall through except when...
182 return &$orig unless (
184 # FIXME - this may be necessary some day, but skip the hit for now
185 # Scalar::Util::reftype $_[0] eq 'HASH'
188 # there is a class to check in the first place
189 defined $_[0]->{result_class}
192 # we are not in a reinvoked callstack
194 ( $_[0]->{__callstack_includes_rsrc_proxy_method} || '' )
200 # there is a proxied method in the first place
202 ( $rsrc_proxy_meta_cache->{address}{%1$s} ||= 0 + (
203 DBIx::Class::ResultSourceProxy->can(%1$s)
212 # the proxied method *is overridden*
214 $rsrc_proxy_meta_cache->{address}{%1$s}
216 # the can() should not be able to fail in theory, but the
217 # result class may not inherit from ::Core *at all*
218 # hence we simply ||ourselves to paper over this eventuality
220 ( $overridden_proxy_cref = $_[0]->{result_class}->can(%1$s) )
222 $rsrc_proxy_meta_cache->{address}{%1$s}
227 # no short-circuiting atributes
232 # - Override is not something DBIC plastered on top of things
233 # One would think this is crazy, yet there it is... sigh:
234 # https://metacpan.org/source/KARMAN/DBIx-Class-RDBOHelpers-0.12/t/lib/MyDBIC/Schema/Cd.pm#L26-27
236 # - And is not an m2m crapfest
238 # - And is not something marked as bypassable
240 $_ =~ / ^ DBIC_method_is_ (?:
241 generated_from_resultsource_metadata
243 m2m_ (?: extra_)? sugar (?:_with_attrs)?
245 bypassable_resultsource_proxy
248 keys %%{ $rsrc_proxy_meta_cache->{attrs}{$overridden_proxy_cref} ||= {
249 map { $_ => 1 } attributes::get($overridden_proxy_cref)
254 # Getting this far means that there *is* an override
255 # and it is *not* marked for a skip
257 # we were asked to loop back through the Result override
259 $rsrc_proxy_meta_cache->{attrs}
260 {$overridden_proxy_cref}
261 {DBIC_method_is_mandatory_resultsource_proxy}
263 local $_[0]->{__callstack_includes_rsrc_proxy_method} = %1$s;
265 # replace $self without compromising aliasing
266 splice @_, 0, 1, $_[0]->{result_class};
268 return &$overridden_proxy_cref;
270 # complain (sparsely) and carry on
273 # FIXME!!! - terrible, need to swap for something saner later
274 my ($cs) = DBIx::Class::Carp::__find_caller( __PACKAGE__ );
276 my $key = $cs . $overridden_proxy_cref;
278 unless( $unclassified_override_warn_emitted->{$key} ) {
280 # find the real origin
281 my @meth_stack = @{ DBIx::Class::_Util::describe_class_methods(
282 ref $_[0]->{result_class} || $_[0]->{result_class}
283 )->{methods}{%1$s} };
285 my $in_class = (shift @meth_stack)->{via_class};
291 $meth_stack[0]{via_class} ne __PACKAGE__
293 push @$possible_supers, (shift @meth_stack)->{via_class};
296 $possible_supers = $possible_supers
298 ' ( and possible SUPERs: %%s )',
300 { join '::', $_, %1$s }
306 my $fqmeth = $in_class . '::' . %1$s . '()';
308 DBIx::Class::_Util::emit_loud_diag(
309 # Repurpose the assertion envvar ( the override-check is independent
310 # from the schema san-checker, but the spirit is the same )
311 confess => $ENV{DBIC_ASSERT_NO_FAILING_SANITY_CHECKS},
313 "The override method $fqmeth$possible_supers has been bypassed "
315 . "In order to silence this warning you must tag the "
316 . "definition of $fqmeth with one of the attributes "
317 . "':DBIC_method_is_bypassable_resultsource_proxy' or "
318 . "':DBIC_method_is_mandatory_resultsource_proxy' ( see "
319 . "https://is.gd/dbic_rsrcproxy_methodattr for more info )\n"
322 # only set if we didn't throw
323 $unclassified_override_warn_emitted->{$key} = 1;
332 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
335 # CI sanity check that all annotations make sense
337 DBIx::Class::_ENV_::ASSERT_NO_ERRONEOUS_METAINSTANCE_USE
339 # no point taxing 5.8 with this
340 ! DBIx::Class::_ENV_::OLD_MRO
343 my ( $rsrc_methods, $rsrc_proxy_methods, $base_methods ) = map {
344 describe_class_methods($_)->{methods}
346 DBIx::Class::ResultSource
347 DBIx::Class::ResultSourceProxy
351 delete $rsrc_methods->{$_}, delete $rsrc_proxy_methods->{$_}
352 for keys %$base_methods;
357 ! $rsrc_proxy_methods->{$_}[0]{attributes}{DBIC_method_is_indirect_sugar}
360 delete $rsrc_proxy_methods->{$_}
361 for keys %$rsrc_proxy_methods;
363 # see fat FIXME at top of file
364 delete @{$rsrc_proxy_methods}{qw( source_name _source_name_accessor )};
367 ( my $proxied = join "\n", map "\t$_", sort keys %$rsrc_proxy_methods )
369 ( my $wrapped = join "\n", map "\t$_", sort @wrap_rsrc_methods )
372 "Unexpected mismatch between the list of proxied methods:\n\n$proxied"
373 . "\n\nand the list of wrapped rsrc methods:\n\n$wrapped\n\n"