Commit | Line | Data |
d5dedbd6 |
1 | package DBIx::Class::SQLMaker; |
6f4ddea1 |
2 | |
d5dedbd6 |
3 | =head1 NAME |
4 | |
5 | DBIx::Class::SQLMaker - An SQL::Abstract-based SQL maker class |
6 | |
7 | =head1 DESCRIPTION |
8 | |
9 | This module is a subclass of L<SQL::Abstract> and includes a number of |
10 | DBIC-specific workarounds, not yet suitable for inclusion into the |
11 | L<SQL::Abstract> core. It also provides all (and more than) the functionality |
12 | of L<SQL::Abstract::Limit>, see L<DBIx::Class::SQLMaker::LimitDialects> for |
13 | more info. |
14 | |
15 | Currently the enhancements to L<SQL::Abstract> are: |
16 | |
17 | =over |
18 | |
19 | =item * Support for C<JOIN> statements (via extended C<table/from> support) |
20 | |
21 | =item * Support of functions in C<SELECT> lists |
22 | |
23 | =item * C<GROUP BY>/C<HAVING> support (via extensions to the order_by parameter) |
24 | |
25 | =item * Support of C<...FOR UPDATE> type of select statement modifiers |
26 | |
27 | =back |
28 | |
29 | =cut |
6a247f33 |
30 | |
31 | use base qw/ |
d5dedbd6 |
32 | DBIx::Class::SQLMaker::LimitDialects |
6a247f33 |
33 | SQL::Abstract |
34 | Class::Accessor::Grouped |
35 | /; |
36 | use mro 'c3'; |
e3764383 |
37 | use strict; |
38 | use warnings; |
6298a324 |
39 | use Sub::Name 'subname'; |
ac322978 |
40 | use Carp::Clan qw/^DBIx::Class|^SQL::Abstract|^Try::Tiny/; |
e8fc51c7 |
41 | use namespace::clean; |
b2b22cd6 |
42 | |
6a247f33 |
43 | __PACKAGE__->mk_group_accessors (simple => qw/quote_char name_sep limit_dialect/); |
44 | |
b2b22cd6 |
45 | BEGIN { |
46 | # reinstall the carp()/croak() functions imported into SQL::Abstract |
47 | # as Carp and Carp::Clan do not like each other much |
48 | no warnings qw/redefine/; |
49 | no strict qw/refs/; |
50 | for my $f (qw/carp croak/) { |
329d7385 |
51 | |
b2b22cd6 |
52 | my $orig = \&{"SQL::Abstract::$f"}; |
e8fc51c7 |
53 | my $clan_import = \&{$f}; |
6298a324 |
54 | *{"SQL::Abstract::$f"} = subname "SQL::Abstract::$f" => |
8637bb24 |
55 | sub { |
d5dedbd6 |
56 | if (Carp::longmess() =~ /DBIx::Class::SQLMaker::[\w]+ .+? called \s at/x) { |
e8fc51c7 |
57 | $clan_import->(@_); |
8637bb24 |
58 | } |
59 | else { |
60 | goto $orig; |
61 | } |
62 | }; |
b2b22cd6 |
63 | } |
64 | } |
6f4ddea1 |
65 | |
e9657379 |
66 | # the "oh noes offset/top without limit" constant |
6a247f33 |
67 | # limited to 32 bits for sanity (and consistency, |
68 | # since it is ultimately handed to sprintf %u) |
69 | # Implemented as a method, since ::Storage::DBI also |
70 | # refers to it (i.e. for the case of software_limit or |
71 | # as the value to abuse with MSSQL ordered subqueries) |
e9657379 |
72 | sub __max_int { 0xFFFFFFFF }; |
73 | |
6a247f33 |
74 | # Handle limit-dialect selection |
6f4ddea1 |
75 | sub select { |
6a247f33 |
76 | my ($self, $table, $fields, $where, $rs_attrs, $limit, $offset) = @_; |
77 | |
78 | |
79 | $fields = $self->_recurse_fields($fields); |
80 | |
81 | if (defined $offset) { |
82 | croak ('A supplied offset must be a non-negative integer') |
83 | if ( $offset =~ /\D/ or $offset < 0 ); |
84 | } |
85 | $offset ||= 0; |
1cbd3034 |
86 | |
6a247f33 |
87 | if (defined $limit) { |
88 | croak ('A supplied limit must be a positive integer') |
89 | if ( $limit =~ /\D/ or $limit <= 0 ); |
90 | } |
91 | elsif ($offset) { |
92 | $limit = $self->__max_int; |
6f4ddea1 |
93 | } |
c2b7c5dc |
94 | |
a6b68a60 |
95 | |
6a247f33 |
96 | my ($sql, @bind); |
97 | if ($limit) { |
98 | # this is legacy code-flow from SQLA::Limit, it is not set in stone |
99 | |
100 | ($sql, @bind) = $self->next::method ($table, $fields, $where); |
101 | |
102 | my $limiter = |
103 | $self->can ('emulate_limit') # also backcompat hook from SQLA::Limit |
104 | || |
105 | do { |
106 | my $dialect = $self->limit_dialect |
107 | or croak "Unable to generate SQL-limit - no limit dialect specified on $self, and no emulate_limit method found"; |
108 | $self->can ("_$dialect") |
d5dedbd6 |
109 | or croak (__PACKAGE__ . " does not implement the requested dialect '$dialect'"); |
6a247f33 |
110 | } |
111 | ; |
112 | |
113 | $sql = $self->$limiter ($sql, $rs_attrs, $limit, $offset); |
114 | } |
115 | else { |
116 | ($sql, @bind) = $self->next::method ($table, $fields, $where, $rs_attrs); |
117 | } |
118 | |
49afd714 |
119 | push @{$self->{where_bind}}, @bind; |
583a0c65 |
120 | |
121 | # this *must* be called, otherwise extra binds will remain in the sql-maker |
49afd714 |
122 | my @all_bind = $self->_assemble_binds; |
583a0c65 |
123 | |
e5372da4 |
124 | $sql .= $self->_lock_select ($rs_attrs->{for}) |
125 | if $rs_attrs->{for}; |
126 | |
49afd714 |
127 | return wantarray ? ($sql, @all_bind) : $sql; |
583a0c65 |
128 | } |
129 | |
130 | sub _assemble_binds { |
131 | my $self = shift; |
132 | return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/from where having order/); |
6f4ddea1 |
133 | } |
134 | |
e5372da4 |
135 | my $for_syntax = { |
136 | update => 'FOR UPDATE', |
137 | shared => 'FOR SHARE', |
138 | }; |
139 | sub _lock_select { |
140 | my ($self, $type) = @_; |
141 | my $sql = $for_syntax->{$type} || croak "Unknown SELECT .. FOR type '$type' requested"; |
142 | return " $sql"; |
143 | } |
144 | |
6a247f33 |
145 | # Handle default inserts |
6f4ddea1 |
146 | sub insert { |
6a247f33 |
147 | # optimized due to hotttnesss |
148 | # my ($self, $table, $data, $options) = @_; |
7a72e5a5 |
149 | |
150 | # SQLA will emit INSERT INTO $table ( ) VALUES ( ) |
151 | # which is sadly understood only by MySQL. Change default behavior here, |
152 | # until SQLA2 comes with proper dialect support |
6a247f33 |
153 | if (! $_[2] or (ref $_[2] eq 'HASH' and !keys %{$_[2]} ) ) { |
154 | my $sql = "INSERT INTO $_[1] DEFAULT VALUES"; |
28d28903 |
155 | |
6a247f33 |
156 | if (my $ret = ($_[3]||{})->{returning} ) { |
157 | $sql .= $_[0]->_insert_returning ($ret); |
28d28903 |
158 | } |
159 | |
160 | return $sql; |
7a72e5a5 |
161 | } |
162 | |
6a247f33 |
163 | next::method(@_); |
6f4ddea1 |
164 | } |
165 | |
166 | sub _recurse_fields { |
81446c4f |
167 | my ($self, $fields) = @_; |
6f4ddea1 |
168 | my $ref = ref $fields; |
169 | return $self->_quote($fields) unless $ref; |
170 | return $$fields if $ref eq 'SCALAR'; |
171 | |
172 | if ($ref eq 'ARRAY') { |
81446c4f |
173 | return join(', ', map { $self->_recurse_fields($_) } @$fields); |
83e09b5b |
174 | } |
175 | elsif ($ref eq 'HASH') { |
81446c4f |
176 | my %hash = %$fields; # shallow copy |
83e09b5b |
177 | |
50136dd9 |
178 | my $as = delete $hash{-as}; # if supplied |
179 | |
81446c4f |
180 | my ($func, $args, @toomany) = %hash; |
181 | |
182 | # there should be only one pair |
183 | if (@toomany) { |
184 | croak "Malformed select argument - too many keys in hash: " . join (',', keys %$fields ); |
185 | } |
50136dd9 |
186 | |
187 | if (lc ($func) eq 'distinct' && ref $args eq 'ARRAY' && @$args > 1) { |
188 | croak ( |
189 | 'The select => { distinct => ... } syntax is not supported for multiple columns.' |
190 | .' Instead please use { group_by => [ qw/' . (join ' ', @$args) . '/ ] }' |
191 | .' or { select => [ qw/' . (join ' ', @$args) . '/ ], distinct => 1 }' |
83e09b5b |
192 | ); |
6f4ddea1 |
193 | } |
83e09b5b |
194 | |
50136dd9 |
195 | my $select = sprintf ('%s( %s )%s', |
196 | $self->_sqlcase($func), |
197 | $self->_recurse_fields($args), |
198 | $as |
0491b597 |
199 | ? sprintf (' %s %s', $self->_sqlcase('as'), $self->_quote ($as) ) |
50136dd9 |
200 | : '' |
201 | ); |
202 | |
83e09b5b |
203 | return $select; |
6f4ddea1 |
204 | } |
205 | # Is the second check absolutely necessary? |
206 | elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) { |
db56cf3d |
207 | return $self->_fold_sqlbind( $fields ); |
6f4ddea1 |
208 | } |
209 | else { |
e8fcf76f |
210 | croak($ref . qq{ unexpected in _recurse_fields()}) |
6f4ddea1 |
211 | } |
212 | } |
213 | |
a6b68a60 |
214 | |
215 | # this used to be a part of _order_by but is broken out for clarity. |
216 | # What we have been doing forever is hijacking the $order arg of |
217 | # SQLA::select to pass in arbitrary pieces of data (first the group_by, |
218 | # then pretty much the entire resultset attr-hash, as more and more |
219 | # things in the SQLA space need to have mopre info about the $rs they |
220 | # create SQL for. The alternative would be to keep expanding the |
221 | # signature of _select with more and more positional parameters, which |
222 | # is just gross. All hail SQLA2! |
223 | sub _parse_rs_attrs { |
1cbd3034 |
224 | my ($self, $arg) = @_; |
15827712 |
225 | |
a6b68a60 |
226 | my $sql = ''; |
1cbd3034 |
227 | |
b03f30a3 |
228 | if (my $g = $self->_recurse_fields($arg->{group_by}) ) { |
a6b68a60 |
229 | $sql .= $self->_sqlcase(' group by ') . $g; |
230 | } |
1cbd3034 |
231 | |
a6b68a60 |
232 | if (defined $arg->{having}) { |
233 | my ($frag, @bind) = $self->_recurse_where($arg->{having}); |
234 | push(@{$self->{having_bind}}, @bind); |
235 | $sql .= $self->_sqlcase(' having ') . $frag; |
236 | } |
15827712 |
237 | |
a6b68a60 |
238 | if (defined $arg->{order_by}) { |
239 | $sql .= $self->_order_by ($arg->{order_by}); |
240 | } |
15827712 |
241 | |
a6b68a60 |
242 | return $sql; |
243 | } |
244 | |
245 | sub _order_by { |
246 | my ($self, $arg) = @_; |
15827712 |
247 | |
a6b68a60 |
248 | # check that we are not called in legacy mode (order_by as 4th argument) |
249 | if (ref $arg eq 'HASH' and not grep { $_ =~ /^-(?:desc|asc)/i } keys %$arg ) { |
250 | return $self->_parse_rs_attrs ($arg); |
fde3719a |
251 | } |
1cbd3034 |
252 | else { |
6a247f33 |
253 | my ($sql, @bind) = $self->next::method($arg); |
a6b68a60 |
254 | push @{$self->{order_bind}}, @bind; |
1cbd3034 |
255 | return $sql; |
fd4cb60a |
256 | } |
6f4ddea1 |
257 | } |
258 | |
259 | sub _table { |
6a247f33 |
260 | # optimized due to hotttnesss |
261 | # my ($self, $from) = @_; |
262 | if (my $ref = ref $_[1] ) { |
263 | if ($ref eq 'ARRAY') { |
264 | return $_[0]->_recurse_from(@{$_[1]}); |
265 | } |
266 | elsif ($ref eq 'HASH') { |
267 | return $_[0]->_make_as($_[1]); |
268 | } |
6f4ddea1 |
269 | } |
6a247f33 |
270 | |
271 | return $_[0]->next::method ($_[1]); |
6f4ddea1 |
272 | } |
273 | |
b8391c87 |
274 | sub _generate_join_clause { |
275 | my ($self, $join_type) = @_; |
276 | |
277 | return sprintf ('%s JOIN ', |
278 | $join_type ? ' ' . uc($join_type) : '' |
279 | ); |
280 | } |
281 | |
6f4ddea1 |
282 | sub _recurse_from { |
283 | my ($self, $from, @join) = @_; |
284 | my @sqlf; |
285 | push(@sqlf, $self->_make_as($from)); |
286 | foreach my $j (@join) { |
287 | my ($to, $on) = @$j; |
288 | |
aa82ce29 |
289 | |
6f4ddea1 |
290 | # check whether a join type exists |
6f4ddea1 |
291 | my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to; |
aa82ce29 |
292 | my $join_type; |
293 | if (ref($to_jt) eq 'HASH' and defined($to_jt->{-join_type})) { |
294 | $join_type = $to_jt->{-join_type}; |
295 | $join_type =~ s/^\s+ | \s+$//xg; |
6f4ddea1 |
296 | } |
aa82ce29 |
297 | |
de5f71ef |
298 | $join_type = $self->{_default_jointype} if not defined $join_type; |
aa82ce29 |
299 | |
b8391c87 |
300 | push @sqlf, $self->_generate_join_clause( $join_type ); |
6f4ddea1 |
301 | |
302 | if (ref $to eq 'ARRAY') { |
303 | push(@sqlf, '(', $self->_recurse_from(@$to), ')'); |
304 | } else { |
305 | push(@sqlf, $self->_make_as($to)); |
306 | } |
307 | push(@sqlf, ' ON ', $self->_join_condition($on)); |
308 | } |
309 | return join('', @sqlf); |
310 | } |
311 | |
db56cf3d |
312 | sub _fold_sqlbind { |
313 | my ($self, $sqlbind) = @_; |
69989ea9 |
314 | |
315 | my @sqlbind = @$$sqlbind; # copy |
316 | my $sql = shift @sqlbind; |
317 | push @{$self->{from_bind}}, @sqlbind; |
318 | |
db56cf3d |
319 | return $sql; |
6f4ddea1 |
320 | } |
321 | |
322 | sub _make_as { |
323 | my ($self, $from) = @_; |
db56cf3d |
324 | return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ |
325 | : ref $_ eq 'REF' ? $self->_fold_sqlbind($_) |
326 | : $self->_quote($_)) |
6f4ddea1 |
327 | } reverse each %{$self->_skip_options($from)}); |
328 | } |
329 | |
330 | sub _skip_options { |
331 | my ($self, $hash) = @_; |
332 | my $clean_hash = {}; |
333 | $clean_hash->{$_} = $hash->{$_} |
334 | for grep {!/^-/} keys %$hash; |
335 | return $clean_hash; |
336 | } |
337 | |
338 | sub _join_condition { |
339 | my ($self, $cond) = @_; |
340 | if (ref $cond eq 'HASH') { |
341 | my %j; |
342 | for (keys %$cond) { |
343 | my $v = $cond->{$_}; |
344 | if (ref $v) { |
e8fcf76f |
345 | croak (ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'}) |
6f4ddea1 |
346 | if ref($v) ne 'SCALAR'; |
347 | $j{$_} = $v; |
348 | } |
349 | else { |
350 | my $x = '= '.$self->_quote($v); $j{$_} = \$x; |
351 | } |
352 | }; |
353 | return scalar($self->_recurse_where(\%j)); |
354 | } elsif (ref $cond eq 'ARRAY') { |
355 | return join(' OR ', map { $self->_join_condition($_) } @$cond); |
356 | } else { |
b48d3b4e |
357 | croak "Can't handle this yet!"; |
6f4ddea1 |
358 | } |
359 | } |
360 | |
6f4ddea1 |
361 | 1; |
d5dedbd6 |
362 | |
363 | =head1 AUTHORS |
364 | |
365 | See L<DBIx::Class/CONTRIBUTORS>. |
366 | |
367 | =head1 LICENSE |
368 | |
369 | You may distribute this code under the same terms as Perl itself. |
370 | |
371 | =cut |