Greatly improve diagnostic messages of _resolve_relationship_condition
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / _Util.pm
CommitLineData
b1dbf716 1package # hide from PAUSE
2 DBIx::Class::_Util;
3
4use warnings;
5use strict;
6
7use constant SPURIOUS_VERSION_CHECK_WARNINGS => ($] < 5.010 ? 1 : 0);
8
37873f78 9BEGIN {
10 package # hide from pause
11 DBIx::Class::_ENV_;
12
13 use Config;
14
15 use constant {
16
17 # but of course
18 BROKEN_FORK => ($^O eq 'MSWin32') ? 1 : 0,
19
20 HAS_ITHREADS => $Config{useithreads} ? 1 : 0,
21
22 # ::Runmode would only be loaded by DBICTest, which in turn implies t/
23 DBICTEST => eval { DBICTest::RunMode->is_author } ? 1 : 0,
24
25 # During 5.13 dev cycle HELEMs started to leak on copy
6cfb1d2f 26 # add an escape for these perls ON SMOKERS - a user will still get death
27 PEEPEENESS => ( eval { DBICTest::RunMode->is_smoker } && ($] >= 5.013005 and $] <= 5.013006) ),
37873f78 28
1b658919 29 SHUFFLE_UNORDERED_RESULTSETS => $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} ? 1 : 0,
30
37873f78 31 ASSERT_NO_INTERNAL_WANTARRAY => $ENV{DBIC_ASSERT_NO_INTERNAL_WANTARRAY} ? 1 : 0,
32
33 IV_SIZE => $Config{ivsize},
00882d2c 34
35 OS_NAME => $^O,
37873f78 36 };
37
38 if ($] < 5.009_005) {
39 require MRO::Compat;
40 constant->import( OLD_MRO => 1 );
41 }
42 else {
43 require mro;
44 constant->import( OLD_MRO => 0 );
45 }
46}
47
841efcb3 48# FIXME - this is not supposed to be here
49# Carp::Skip to the rescue soon
50use DBIx::Class::Carp '^DBIx::Class|^DBICTest';
51
52use Carp 'croak';
bf302897 53use Scalar::Util qw(weaken blessed reftype);
3705e3b2 54use List::Util qw(first);
b1dbf716 55
56use base 'Exporter';
3705e3b2 57our @EXPORT_OK = qw(
58 sigwarn_silencer modver_gt_or_eq fail_on_internal_wantarray
8433421f 59 refdesc refcount hrefaddr is_exception
facd0e8e 60 UNRESOLVABLE_CONDITION
3705e3b2 61);
052a832c 62
facd0e8e 63use constant UNRESOLVABLE_CONDITION => \ '1 = 0';
64
bf302897 65sub sigwarn_silencer ($) {
052a832c 66 my $pattern = shift;
67
68 croak "Expecting a regexp" if ref $pattern ne 'Regexp';
69
70 my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) };
71
72 return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
73}
b1dbf716 74
8433421f 75sub hrefaddr ($) { sprintf '0x%x', &Scalar::Util::refaddr||0 }
76
77sub refdesc ($) {
78 croak "Expecting a reference" if ! length ref $_[0];
79
80 # be careful not to trigger stringification,
81 # reuse @_ as a scratch-pad
82 sprintf '%s%s(0x%x)',
83 ( defined( $_[1] = blessed $_[0]) ? "$_[1]=" : '' ),
84 reftype $_[0],
85 Scalar::Util::refaddr($_[0]),
86 ;
87}
bf302897 88
89sub refcount ($) {
dac7972a 90 croak "Expecting a reference" if ! length ref $_[0];
91
92 require B;
93 # No tempvars - must operate on $_[0], otherwise the pad
94 # will count as an extra ref
95 B::svref_2object($_[0])->REFCNT;
96}
97
841efcb3 98sub is_exception ($) {
99 my $e = $_[0];
100
a0414138 101 # this is not strictly correct - an eval setting $@ to undef
102 # is *not* the same as an eval setting $@ to ''
103 # but for the sake of simplicity assume the following for
104 # the time being
105 return 0 unless defined $e;
106
841efcb3 107 my ($not_blank, $suberror);
108 {
109 local $@;
110 eval {
111 $not_blank = ($e ne '') ? 1 : 0;
112 1;
113 } or $suberror = $@;
114 }
115
116 if (defined $suberror) {
117 if (length (my $class = blessed($e) )) {
118 carp_unique( sprintf(
9bea2000 119 'External exception class %s implements partial (broken) overloading '
120 . 'preventing its instances from being used in simple ($x eq $y) '
841efcb3 121 . 'comparisons. Given Perl\'s "globally cooperative" exception '
122 . 'handling this type of brokenness is extremely dangerous on '
123 . 'exception objects, as it may (and often does) result in silent '
124 . '"exception substitution". DBIx::Class tries to work around this '
125 . 'as much as possible, but other parts of your software stack may '
126 . 'not be even aware of this. Please submit a bugreport against the '
127 . 'distribution containing %s and in the meantime apply a fix similar '
128 . 'to the one shown at %s, in order to ensure your exception handling '
129 . 'is saner application-wide. What follows is the actual error text '
130 . "as generated by Perl itself:\n\n%s\n ",
9bea2000 131 $class,
841efcb3 132 $class,
133 'http://v.gd/DBIC_overload_tempfix/',
134 $suberror,
135 ));
136
137 # workaround, keeps spice flowing
138 $not_blank = ("$e" ne '') ? 1 : 0;
139 }
140 else {
141 # not blessed yet failed the 'ne'... this makes 0 sense...
142 # just throw further
143 die $suberror
144 }
145 }
146
147 return $not_blank;
148}
149
bf302897 150sub modver_gt_or_eq ($$) {
b1dbf716 151 my ($mod, $ver) = @_;
152
153 croak "Nonsensical module name supplied"
154 if ! defined $mod or ! length $mod;
155
156 croak "Nonsensical minimum version supplied"
157 if ! defined $ver or $ver =~ /[^0-9\.\_]/;
158
052a832c 159 local $SIG{__WARN__} = sigwarn_silencer( qr/\Qisn't numeric in subroutine entry/ )
160 if SPURIOUS_VERSION_CHECK_WARNINGS;
b1dbf716 161
56270bba 162 croak "$mod does not seem to provide a version (perhaps it never loaded)"
163 unless $mod->VERSION;
164
b1dbf716 165 local $@;
166 eval { $mod->VERSION($ver) } ? 1 : 0;
167}
168
a9da9b6a 169{
170 my $list_ctx_ok_stack_marker;
171
e89c7968 172 sub fail_on_internal_wantarray () {
a9da9b6a 173 return if $list_ctx_ok_stack_marker;
174
175 if (! defined wantarray) {
176 croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard');
177 }
178
179 my $cf = 1;
180 while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?:
181
182 # these are public API parts that alter behavior on wantarray
183 search | search_related | slice | search_literal
184
185 |
186
187 # these are explicitly prefixed, since we only recognize them as valid
188 # escapes when they come from the guts of CDBICompat
189 CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all )
190
191 ) $/x ) {
192 $cf++;
193 }
194
e89c7968 195 my ($fr, $want, $argdesc);
196 {
197 package DB;
198 $fr = [ caller($cf) ];
199 $want = ( caller($cf-1) )[5];
200 $argdesc = ref $DB::args[0]
201 ? DBIx::Class::_Util::refdesc($DB::args[0])
202 : 'non '
203 ;
204 };
205
a9da9b6a 206 if (
e89c7968 207 $want and $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/
a9da9b6a 208 ) {
a9da9b6a 209 DBIx::Class::Exception->throw( sprintf (
e89c7968 210 "Improper use of %s instance in list context at %s line %d\n\n Stacktrace starts",
211 $argdesc, @{$fr}[1,2]
a9da9b6a 212 ), 'with_stacktrace');
213 }
214
215 my $mark = [];
216 weaken ( $list_ctx_ok_stack_marker = $mark );
217 $mark;
218 }
219}
220
b1dbf716 2211;