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 # FIXME: this used to be a sub-type of inherited ( to see run:
29 # `git log -Sinherited_ro_instance lib/DBIx/Class/ResultSourceProxy.pm` )
30 # however given the lack of any sync effort as described above *anyway*,
31 # it makes no sense to guard for erroneous use at a non-trivial cost in
32 # performance (and may end up in the way of future optimizations as per
33 # https://github.com/vovkasm/Class-Accessor-Inherited-XS/issues/2#issuecomment-243246924 )
34 __PACKAGE__->mk_group_accessors( inherited => 'source_name');
36 # The marking with indirect_sugar will cause warnings to be issued in darkpan code
37 # (though extremely unlikely)
38 sub get_inherited_ro_instance :DBIC_method_is_indirect_sugar {
39 DBIx::Class::Exception->throw(
40 "The 'inherted_ro_instance' CAG group has been retired - use 'inherited' instead"
43 sub set_inherited_ro_instance :DBIC_method_is_indirect_sugar {
44 DBIx::Class::Exception->throw(
45 "The 'inherted_ro_instance' CAG group has been retired - use 'inherited' instead"
49 sub add_columns :DBIC_method_is_bypassable_resultsource_proxy {
50 my ($class, @cols) = @_;
51 my $source = $class->result_source;
52 local $source->{__callstack_includes_rsrc_proxy_method} = "add_columns";
54 $source->add_columns(@cols);
57 foreach my $c (grep { !ref } @cols) {
58 # If this is an augment definition get the real colname.
61 $class->register_column(
63 ( $colinfos ||= $source->columns_info )->{$c}
68 sub add_column :DBIC_method_is_indirect_sugar {
69 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
70 shift->add_columns(@_)
73 sub add_relationship :DBIC_method_is_bypassable_resultsource_proxy {
74 my ($class, $rel, @rest) = @_;
75 my $source = $class->result_source;
76 local $source->{__callstack_includes_rsrc_proxy_method} = "add_relationship";
78 $source->add_relationship($rel => @rest);
79 $class->register_relationship($rel => $source->relationship_info($rel));
83 # legacy resultset_class accessor, seems to be used by cdbi only
84 sub iterator_class :DBIC_method_is_indirect_sugar {
85 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
86 shift->result_source->resultset_class(@_)
89 for my $method_to_proxy (qw/
103 column_info_from_storage
109 add_unique_constraint
110 add_unique_constraints
113 unique_constraint_names
114 unique_constraint_columns
120 my $qsub_opts = { attributes => [
123 attributes::get( \&{"DBIx::Class::ResultSource::$method_to_proxy"} );
127 # bypassable default for backcompat, except for indirect methods
128 # ( those will simply warn during the sanheck )
130 { $_ eq 'DBIC_method_is_indirect_sugar' }
131 @{ $qsub_opts->{attributes} }
133 push @wrap_rsrc_methods, $method_to_proxy;
134 push @{ $qsub_opts->{atributes} }, 'DBIC_method_is_bypassable_resultsource_proxy';
137 quote_sub __PACKAGE__."::$method_to_proxy", sprintf( <<'EOC', $method_to_proxy ), {}, $qsub_opts;
138 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
140 my $rsrc = shift->result_source;
141 local $rsrc->{__callstack_includes_rsrc_proxy_method} = q(%1$s);
147 # This is where the "magic" of detecting/invoking the proper overridden
148 # Result method takes place. It isn't implemented as a stateless out-of-band
149 # SanityCheck as invocation requires certain state in the $rsrc object itself
150 # in order not to loop over itself. It is not in ResultSource.pm either
151 # because of load order and because the entire stack is just terrible :/
153 # The code is not easily readable, as it it optimized for execution time
154 # (this stuff will be run all the time across the entire install base :/ )
157 our %__rsrc_proxy_meta_cache;
159 sub DBIx::Class::__RsrcProxy_iThreads_handler__::CLONE {
160 # recreating this cache is pretty cheap: just blow it away
161 %__rsrc_proxy_meta_cache = ();
164 for my $method_to_wrap (@wrap_rsrc_methods) {
167 perlstring $method_to_wrap,
172 \&{"DBIx::Class::ResultSource::$method_to_wrap"}
175 my %unclassified_override_warn_emitted;
179 # ref to hashref, this is how S::Q works
180 '$rsrc_proxy_meta_cache' => \\%__rsrc_proxy_meta_cache,
181 '$unclassified_override_warn_emitted' => \\%unclassified_override_warn_emitted,
184 { attributes => [ attributes::get($orig) ] }
187 quote_sub "DBIx::Class::ResultSource::$method_to_wrap", sprintf( <<'EOC', @src_args ), @qsub_args;
189 my $overridden_proxy_cref;
191 # fall through except when...
192 return &$orig unless (
194 # FIXME - this may be necessary some day, but skip the hit for now
195 # Scalar::Util::reftype $_[0] eq 'HASH'
198 # there is a class to check in the first place
199 defined $_[0]->{result_class}
202 # we are not in a reinvoked callstack
204 ( $_[0]->{__callstack_includes_rsrc_proxy_method} || '' )
210 # there is a proxied method in the first place
212 ( $rsrc_proxy_meta_cache->{address}{%1$s} ||= 0 + (
213 DBIx::Class::ResultSourceProxy->can(%1$s)
222 # the proxied method *is overridden*
224 $rsrc_proxy_meta_cache->{address}{%1$s}
226 # the can() should not be able to fail in theory, but the
227 # result class may not inherit from ::Core *at all*
228 # hence we simply ||ourselves to paper over this eventuality
230 ( $overridden_proxy_cref = $_[0]->{result_class}->can(%1$s) )
232 $rsrc_proxy_meta_cache->{address}{%1$s}
237 # no short-circuiting atributes
242 # - Override is not something DBIC plastered on top of things
243 # One would think this is crazy, yet there it is... sigh:
244 # https://metacpan.org/source/KARMAN/DBIx-Class-RDBOHelpers-0.12/t/lib/MyDBIC/Schema/Cd.pm#L26-27
246 # - And is not an m2m crapfest
248 # - And is not something marked as bypassable
250 $_ =~ / ^ DBIC_method_is_ (?:
251 generated_from_resultsource_metadata
253 m2m_ (?: extra_)? sugar (?:_with_attrs)?
255 bypassable_resultsource_proxy
258 keys %%{ $rsrc_proxy_meta_cache->{attrs}{$overridden_proxy_cref} ||= {
259 map { $_ => 1 } attributes::get($overridden_proxy_cref)
264 # Getting this far means that there *is* an override
265 # and it is *not* marked for a skip
267 # we were asked to loop back through the Result override
269 $rsrc_proxy_meta_cache->{attrs}
270 {$overridden_proxy_cref}
271 {DBIC_method_is_mandatory_resultsource_proxy}
273 local $_[0]->{__callstack_includes_rsrc_proxy_method} = %1$s;
275 # replace $self without compromising aliasing
276 splice @_, 0, 1, $_[0]->{result_class};
278 return &$overridden_proxy_cref;
280 # complain (sparsely) and carry on
283 # FIXME!!! - terrible, need to swap for something saner later
284 my ($cs) = DBIx::Class::Carp::__find_caller( __PACKAGE__ );
286 my $key = $cs . $overridden_proxy_cref;
288 unless( $unclassified_override_warn_emitted->{$key} ) {
290 # find the real origin
291 my @meth_stack = @{ DBIx::Class::_Util::describe_class_methods(
292 ref $_[0]->{result_class} || $_[0]->{result_class}
293 )->{methods}{%1$s} };
295 my $in_class = (shift @meth_stack)->{via_class};
301 $meth_stack[0]{via_class} ne __PACKAGE__
303 push @$possible_supers, (shift @meth_stack)->{via_class};
306 $possible_supers = $possible_supers
308 ' ( and possible SUPERs: %%s )',
310 { join '::', $_, %1$s }
316 my $fqmeth = $in_class . '::' . %1$s . '()';
318 DBIx::Class::_Util::emit_loud_diag(
320 # Repurpose the assertion envvar ( the override-check is independent
321 # from the schema san-checker, but the spirit is the same )
322 confess => $ENV{DBIC_ASSERT_NO_FAILING_SANITY_CHECKS},
325 "The override method $fqmeth$possible_supers has been bypassed "
327 . "In order to silence this warning you must tag the "
328 . "definition of $fqmeth with one of the attributes "
329 . "':DBIC_method_is_bypassable_resultsource_proxy' or "
330 . "':DBIC_method_is_mandatory_resultsource_proxy' ( see "
331 . "https://is.gd/dbic_rsrcproxy_methodattr for more info )\n"
334 # only set if we didn't throw
335 $unclassified_override_warn_emitted->{$key} = 1;
344 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
347 # CI sanity check that all annotations make sense
349 DBIx::Class::_ENV_::ASSERT_NO_ERRONEOUS_METAINSTANCE_USE
351 # no point taxing 5.8 with this
352 ! DBIx::Class::_ENV_::OLD_MRO
355 my ( $rsrc_methods, $rsrc_proxy_methods, $base_methods ) = map {
356 describe_class_methods($_)->{methods}
358 DBIx::Class::ResultSource
359 DBIx::Class::ResultSourceProxy
363 delete $rsrc_methods->{$_}, delete $rsrc_proxy_methods->{$_}
364 for keys %$base_methods;
369 ! $rsrc_proxy_methods->{$_}[0]{attributes}{DBIC_method_is_indirect_sugar}
372 delete $rsrc_proxy_methods->{$_}
373 for keys %$rsrc_proxy_methods;
375 # see fat FIXME at top of file
376 delete @{$rsrc_proxy_methods}{qw( source_name _source_name_accessor )};
379 ( my $proxied = join "\n", map "\t$_", sort keys %$rsrc_proxy_methods )
381 ( my $wrapped = join "\n", map "\t$_", sort @wrap_rsrc_methods )
384 "Unexpected mismatch between the list of proxied methods:\n\n$proxied"
385 . "\n\nand the list of wrapped rsrc methods:\n\n$wrapped\n\n"