Refactor ::DBIHacks::_extract_fixed_condition_columns (sequel to 8d005ad9)
[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);
55use overload ();
b1dbf716 56
57use base 'Exporter';
3705e3b2 58our @EXPORT_OK = qw(
59 sigwarn_silencer modver_gt_or_eq fail_on_internal_wantarray
60 refcount hrefaddr is_exception
61 is_plain_value is_literal_value
facd0e8e 62 UNRESOLVABLE_CONDITION
3705e3b2 63);
052a832c 64
facd0e8e 65use constant UNRESOLVABLE_CONDITION => \ '1 = 0';
66
bf302897 67sub sigwarn_silencer ($) {
052a832c 68 my $pattern = shift;
69
70 croak "Expecting a regexp" if ref $pattern ne 'Regexp';
71
72 my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) };
73
74 return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
75}
b1dbf716 76
bf302897 77sub hrefaddr ($) { sprintf '0x%x', &Scalar::Util::refaddr }
78
79sub refcount ($) {
dac7972a 80 croak "Expecting a reference" if ! length ref $_[0];
81
82 require B;
83 # No tempvars - must operate on $_[0], otherwise the pad
84 # will count as an extra ref
85 B::svref_2object($_[0])->REFCNT;
86}
87
841efcb3 88sub is_exception ($) {
89 my $e = $_[0];
90
a0414138 91 # this is not strictly correct - an eval setting $@ to undef
92 # is *not* the same as an eval setting $@ to ''
93 # but for the sake of simplicity assume the following for
94 # the time being
95 return 0 unless defined $e;
96
841efcb3 97 my ($not_blank, $suberror);
98 {
99 local $@;
100 eval {
101 $not_blank = ($e ne '') ? 1 : 0;
102 1;
103 } or $suberror = $@;
104 }
105
106 if (defined $suberror) {
107 if (length (my $class = blessed($e) )) {
108 carp_unique( sprintf(
bf302897 109 'External exception object %s=%s(%s) implements partial (broken) '
841efcb3 110 . 'overloading preventing it from being used in simple ($x eq $y) '
111 . 'comparisons. Given Perl\'s "globally cooperative" exception '
112 . 'handling this type of brokenness is extremely dangerous on '
113 . 'exception objects, as it may (and often does) result in silent '
114 . '"exception substitution". DBIx::Class tries to work around this '
115 . 'as much as possible, but other parts of your software stack may '
116 . 'not be even aware of this. Please submit a bugreport against the '
117 . 'distribution containing %s and in the meantime apply a fix similar '
118 . 'to the one shown at %s, in order to ensure your exception handling '
119 . 'is saner application-wide. What follows is the actual error text '
120 . "as generated by Perl itself:\n\n%s\n ",
121 $class,
122 reftype $e,
bf302897 123 hrefaddr $e,
841efcb3 124 $class,
125 'http://v.gd/DBIC_overload_tempfix/',
126 $suberror,
127 ));
128
129 # workaround, keeps spice flowing
130 $not_blank = ("$e" ne '') ? 1 : 0;
131 }
132 else {
133 # not blessed yet failed the 'ne'... this makes 0 sense...
134 # just throw further
135 die $suberror
136 }
137 }
138
139 return $not_blank;
140}
141
bf302897 142sub modver_gt_or_eq ($$) {
b1dbf716 143 my ($mod, $ver) = @_;
144
145 croak "Nonsensical module name supplied"
146 if ! defined $mod or ! length $mod;
147
148 croak "Nonsensical minimum version supplied"
149 if ! defined $ver or $ver =~ /[^0-9\.\_]/;
150
052a832c 151 local $SIG{__WARN__} = sigwarn_silencer( qr/\Qisn't numeric in subroutine entry/ )
152 if SPURIOUS_VERSION_CHECK_WARNINGS;
b1dbf716 153
56270bba 154 croak "$mod does not seem to provide a version (perhaps it never loaded)"
155 unless $mod->VERSION;
156
b1dbf716 157 local $@;
158 eval { $mod->VERSION($ver) } ? 1 : 0;
159}
160
3705e3b2 161sub is_literal_value ($) {
162 (
163 ref $_[0] eq 'SCALAR'
164 or
165 ( ref $_[0] eq 'REF' and ref ${$_[0]} eq 'ARRAY' )
166 ) ? 1 : 0;
167}
168
169# FIXME XSify - this can be done so much more efficiently
170sub is_plain_value ($) {
171 no strict 'refs';
172 (
173 # plain scalar
174 (! length ref $_[0])
175 or
176 (
177 blessed $_[0]
178 and
179 # deliberately not using Devel::OverloadInfo - the checks we are
180 # intersted in are much more limited than the fullblown thing, and
181 # this is a relatively hot piece of code
182 (
7cbd6cbd 183 # FIXME - DBI needs fixing to stringify regardless of DBD
184 #
185 # either has stringification which DBI SHOULD prefer out of the box
3705e3b2 186 #first { *{$_ . '::(""'}{CODE} } @{ mro::get_linear_isa( ref $_[0] ) }
187 overload::Method($_[0], '""')
188 or
189 # has nummification and fallback is *not* disabled
190 (
191 $_[1] = first { *{"${_}::(0+"}{CODE} } @{ mro::get_linear_isa( ref $_[0] ) }
192 and
193 ( ! defined ${"$_[1]::()"} or ${"$_[1]::()"} )
194 )
195 )
196 )
197 ) ? 1 : 0;
198}
199
a9da9b6a 200{
201 my $list_ctx_ok_stack_marker;
202
203 sub fail_on_internal_wantarray {
204 return if $list_ctx_ok_stack_marker;
205
206 if (! defined wantarray) {
207 croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard');
208 }
209
210 my $cf = 1;
211 while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?:
212
213 # these are public API parts that alter behavior on wantarray
214 search | search_related | slice | search_literal
215
216 |
217
218 # these are explicitly prefixed, since we only recognize them as valid
219 # escapes when they come from the guts of CDBICompat
220 CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all )
221
222 ) $/x ) {
223 $cf++;
224 }
225
226 if (
227 (caller($cf))[0] =~ /^(?:DBIx::Class|DBICx::)/
228 ) {
229 my $obj = shift;
230
231 DBIx::Class::Exception->throw( sprintf (
bf302897 232 "Improper use of %s(%s) instance in list context at %s line %d\n\n\tStacktrace starts",
233 ref($obj), hrefaddr($obj), (caller($cf))[1,2]
a9da9b6a 234 ), 'with_stacktrace');
235 }
236
237 my $mark = [];
238 weaken ( $list_ctx_ok_stack_marker = $mark );
239 $mark;
240 }
241}
242
b1dbf716 2431;