Commit | Line | Data |
6f4ddea1 |
1 | package # Hide from PAUSE |
855c6fd0 |
2 | DBIx::Class::SQLAHacks; |
6f4ddea1 |
3 | |
998373c2 |
4 | # This module is a subclass of SQL::Abstract::Limit and includes a number |
5 | # of DBIC-specific workarounds, not yet suitable for inclusion into the |
6 | # SQLA core |
7 | |
6f4ddea1 |
8 | use base qw/SQL::Abstract::Limit/; |
e3764383 |
9 | use strict; |
10 | use warnings; |
b2b22cd6 |
11 | use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/; |
8637bb24 |
12 | use Sub::Name(); |
b2b22cd6 |
13 | |
14 | BEGIN { |
15 | # reinstall the carp()/croak() functions imported into SQL::Abstract |
16 | # as Carp and Carp::Clan do not like each other much |
17 | no warnings qw/redefine/; |
18 | no strict qw/refs/; |
19 | for my $f (qw/carp croak/) { |
329d7385 |
20 | |
b2b22cd6 |
21 | my $orig = \&{"SQL::Abstract::$f"}; |
8637bb24 |
22 | *{"SQL::Abstract::$f"} = Sub::Name::subname "SQL::Abstract::$f" => |
23 | sub { |
24 | if (Carp::longmess() =~ /DBIx::Class::SQLAHacks::[\w]+ .+? called \s at/x) { |
25 | __PACKAGE__->can($f)->(@_); |
26 | } |
27 | else { |
28 | goto $orig; |
29 | } |
30 | }; |
b2b22cd6 |
31 | } |
32 | } |
6f4ddea1 |
33 | |
998373c2 |
34 | |
35 | # Tries to determine limit dialect. |
36 | # |
6f4ddea1 |
37 | sub new { |
38 | my $self = shift->SUPER::new(@_); |
39 | |
40 | # This prevents the caching of $dbh in S::A::L, I believe |
41 | # If limit_dialect is a ref (like a $dbh), go ahead and replace |
42 | # it with what it resolves to: |
43 | $self->{limit_dialect} = $self->_find_syntax($self->{limit_dialect}) |
44 | if ref $self->{limit_dialect}; |
45 | |
46 | $self; |
47 | } |
48 | |
6f4ddea1 |
49 | |
6553ac38 |
50 | # ANSI standard Limit/Offset implementation. DB2 and MSSQL use this |
6f4ddea1 |
51 | sub _RowNumberOver { |
a6b68a60 |
52 | my ($self, $sql, $rs_attrs, $rows, $offset ) = @_; |
6f4ddea1 |
53 | |
cb478051 |
54 | # get the select to make the final amount of columns equal the original one |
55 | my ($select) = $sql =~ /^ \s* SELECT \s+ (.+?) \s+ FROM/ix |
56 | or croak "Unrecognizable SELECT: $sql"; |
57 | |
a6b68a60 |
58 | # make up an order if none exists |
6553ac38 |
59 | my $order_by = $self->_order_by( |
a6b68a60 |
60 | (delete $rs_attrs->{order_by}) || $self->_rno_default_order |
6553ac38 |
61 | ); |
6f4ddea1 |
62 | |
cb478051 |
63 | # whatever is left of the order_by |
a6b68a60 |
64 | my $group_having = $self->_parse_rs_attrs($rs_attrs); |
6f4ddea1 |
65 | |
a6b68a60 |
66 | my $qalias = $self->_quote ($rs_attrs->{alias}); |
1f36ab67 |
67 | |
cb478051 |
68 | $sql = sprintf (<<EOS, $offset + 1, $offset + $rows, ); |
6f4ddea1 |
69 | |
cb478051 |
70 | SELECT $select FROM ( |
71 | SELECT $qalias.*, ROW_NUMBER() OVER($order_by ) AS rno__row__index FROM ( |
72 | ${sql}${group_having} |
73 | ) $qalias |
74 | ) $qalias WHERE rno__row__index BETWEEN %d AND %d |
6553ac38 |
75 | |
76 | EOS |
77 | |
78 | $sql =~ s/\s*\n\s*/ /g; # easier to read in the debugger |
6f4ddea1 |
79 | return $sql; |
80 | } |
81 | |
84ddb3da |
82 | # some databases are happy with OVER (), some need OVER (ORDER BY (SELECT (1)) ) |
83 | sub _rno_default_order { |
84 | return undef; |
85 | } |
86 | |
193590c2 |
87 | # Informix specific limit, almost like LIMIT/OFFSET |
88 | sub _SkipFirst { |
a6b68a60 |
89 | my ($self, $sql, $rs_attrs, $rows, $offset) = @_; |
193590c2 |
90 | |
91 | $sql =~ s/^ \s* SELECT \s+ //ix |
92 | or croak "Unrecognizable SELECT: $sql"; |
93 | |
94 | return sprintf ('SELECT %s%s%s%s', |
95 | $offset |
96 | ? sprintf ('SKIP %d ', $offset) |
97 | : '' |
98 | , |
99 | sprintf ('FIRST %d ', $rows), |
100 | $sql, |
a6b68a60 |
101 | $self->_parse_rs_attrs ($rs_attrs), |
193590c2 |
102 | ); |
103 | } |
104 | |
145b2a3d |
105 | # Firebird specific limit, reverse of _SkipFirst for Informix |
106 | sub _FirstSkip { |
a6b68a60 |
107 | my ($self, $sql, $rs_attrs, $rows, $offset) = @_; |
145b2a3d |
108 | |
109 | $sql =~ s/^ \s* SELECT \s+ //ix |
110 | or croak "Unrecognizable SELECT: $sql"; |
111 | |
112 | return sprintf ('SELECT %s%s%s%s', |
113 | sprintf ('FIRST %d ', $rows), |
114 | $offset |
115 | ? sprintf ('SKIP %d ', $offset) |
116 | : '' |
117 | , |
118 | $sql, |
a6b68a60 |
119 | $self->_parse_rs_attrs ($rs_attrs), |
145b2a3d |
120 | ); |
121 | } |
122 | |
6553ac38 |
123 | # Crappy Top based Limit/Offset support. Legacy from MSSQL. |
15827712 |
124 | sub _Top { |
a6b68a60 |
125 | my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_; |
15827712 |
126 | |
b1e1d073 |
127 | # mangle the input sql so it can be properly aliased in the outer queries |
128 | $sql =~ s/^ \s* SELECT \s+ (.+?) \s+ (?=FROM)//ix |
129 | or croak "Unrecognizable SELECT: $sql"; |
42e5b103 |
130 | my $sql_select = $1; |
131 | my @sql_select = split (/\s*,\s*/, $sql_select); |
132 | |
133 | # we can't support subqueries (in fact MSSQL can't) - croak |
a6b68a60 |
134 | if (@sql_select != @{$rs_attrs->{select}}) { |
42e5b103 |
135 | croak (sprintf ( |
136 | 'SQL SELECT did not parse cleanly - retrieved %d comma separated elements, while ' |
137 | . 'the resultset select attribure contains %d elements: %s', |
138 | scalar @sql_select, |
a6b68a60 |
139 | scalar @{$rs_attrs->{select}}, |
42e5b103 |
140 | $sql_select, |
141 | )); |
142 | } |
b1e1d073 |
143 | |
42e5b103 |
144 | my $name_sep = $self->name_sep || '.'; |
ac93965c |
145 | my $esc_name_sep = "\Q$name_sep\E"; |
146 | my $col_re = qr/ ^ (?: (.+) $esc_name_sep )? ([^$esc_name_sep]+) $ /x; |
147 | |
a6b68a60 |
148 | my $rs_alias = $rs_attrs->{alias}; |
ac93965c |
149 | my $quoted_rs_alias = $self->_quote ($rs_alias); |
b1e1d073 |
150 | |
42e5b103 |
151 | # construct the new select lists, rename(alias) some columns if necessary |
152 | my (@outer_select, @inner_select, %seen_names, %col_aliases, %outer_col_aliases); |
b1e1d073 |
153 | |
a6b68a60 |
154 | for (@{$rs_attrs->{select}}) { |
42e5b103 |
155 | next if ref $_; |
156 | my ($table, $orig_colname) = ( $_ =~ $col_re ); |
157 | next unless $table; |
158 | $seen_names{$orig_colname}++; |
159 | } |
b1e1d073 |
160 | |
42e5b103 |
161 | for my $i (0 .. $#sql_select) { |
b1e1d073 |
162 | |
a6b68a60 |
163 | my $colsel_arg = $rs_attrs->{select}[$i]; |
42e5b103 |
164 | my $colsel_sql = $sql_select[$i]; |
b1e1d073 |
165 | |
42e5b103 |
166 | # this may or may not work (in case of a scalarref or something) |
167 | my ($table, $orig_colname) = ( $colsel_arg =~ $col_re ); |
b1e1d073 |
168 | |
42e5b103 |
169 | my $quoted_alias; |
170 | # do not attempt to understand non-scalar selects - alias numerically |
171 | if (ref $colsel_arg) { |
172 | $quoted_alias = $self->_quote ('column_' . (@inner_select + 1) ); |
173 | } |
174 | # column name seen more than once - alias it |
ed4cdb4f |
175 | elsif ($orig_colname && |
176 | ($seen_names{$orig_colname} && $seen_names{$orig_colname} > 1) ) { |
42e5b103 |
177 | $quoted_alias = $self->_quote ("${table}__${orig_colname}"); |
178 | } |
b1e1d073 |
179 | |
42e5b103 |
180 | # we did rename - make a record and adjust |
181 | if ($quoted_alias) { |
182 | # alias inner |
183 | push @inner_select, "$colsel_sql AS $quoted_alias"; |
184 | |
185 | # push alias to outer |
186 | push @outer_select, $quoted_alias; |
187 | |
188 | # Any aliasing accumulated here will be considered |
189 | # both for inner and outer adjustments of ORDER BY |
190 | $self->__record_alias ( |
191 | \%col_aliases, |
192 | $quoted_alias, |
193 | $colsel_arg, |
194 | $table ? $orig_colname : undef, |
195 | ); |
b1e1d073 |
196 | } |
197 | |
42e5b103 |
198 | # otherwise just leave things intact inside, and use the abbreviated one outside |
199 | # (as we do not have table names anymore) |
200 | else { |
201 | push @inner_select, $colsel_sql; |
202 | |
203 | my $outer_quoted = $self->_quote ($orig_colname); # it was not a duplicate so should just work |
204 | push @outer_select, $outer_quoted; |
205 | $self->__record_alias ( |
206 | \%outer_col_aliases, |
207 | $outer_quoted, |
208 | $colsel_arg, |
209 | $table ? $orig_colname : undef, |
210 | ); |
211 | } |
b1e1d073 |
212 | } |
213 | |
214 | my $outer_select = join (', ', @outer_select ); |
42e5b103 |
215 | my $inner_select = join (', ', @inner_select ); |
b1e1d073 |
216 | |
42e5b103 |
217 | %outer_col_aliases = (%outer_col_aliases, %col_aliases); |
b1e1d073 |
218 | |
219 | # deal with order |
a6b68a60 |
220 | croak '$order/attr container supplied to SQLAHacks limit emulators must be a hash' |
221 | if (ref $rs_attrs ne 'HASH'); |
15827712 |
222 | |
a6b68a60 |
223 | my $req_order = $rs_attrs->{order_by}; |
15827712 |
224 | |
e2db8319 |
225 | # examine normalized version, collapses nesting |
a6b68a60 |
226 | my $limit_order = scalar $self->_order_by_chunks ($req_order) |
227 | ? $req_order |
228 | : [ map |
e2db8319 |
229 | { join ('', $rs_alias, $name_sep, $_ ) } |
a6b68a60 |
230 | ( $rs_attrs->{_rsroot_source_handle}->resolve->primary_columns ) |
231 | ] |
232 | ; |
1cbd3034 |
233 | |
42e5b103 |
234 | my ( $order_by_inner, $order_by_outer ) = $self->_order_directions($limit_order); |
235 | my $order_by_requested = $self->_order_by ($req_order); |
15827712 |
236 | |
64c7c000 |
237 | # generate the rest |
a6b68a60 |
238 | delete $rs_attrs->{order_by}; |
239 | my $grpby_having = $self->_parse_rs_attrs ($rs_attrs); |
8f6dbee9 |
240 | |
64c7c000 |
241 | # short circuit for counts - the ordering complexity is needless |
a6b68a60 |
242 | if ($rs_attrs->{-for_count_only}) { |
64c7c000 |
243 | return "SELECT TOP $rows $inner_select $sql $grpby_having $order_by_outer"; |
244 | } |
245 | |
42e5b103 |
246 | # we can't really adjust the order_by columns, as introspection is lacking |
247 | # resort to simple substitution |
248 | for my $col (keys %outer_col_aliases) { |
249 | for ($order_by_requested, $order_by_outer) { |
250 | $_ =~ s/\s+$col\s+/ $outer_col_aliases{$col} /g; |
b1e1d073 |
251 | } |
252 | } |
42e5b103 |
253 | for my $col (keys %col_aliases) { |
9c1ffdde |
254 | $order_by_inner =~ s/\s+$col\s+/ $col_aliases{$col} /g; |
42e5b103 |
255 | } |
15827712 |
256 | |
15827712 |
257 | |
42e5b103 |
258 | my $inner_lim = $rows + $offset; |
15827712 |
259 | |
618a0fe3 |
260 | $sql = "SELECT TOP $inner_lim $inner_select $sql $grpby_having $order_by_inner"; |
42e5b103 |
261 | |
262 | if ($offset) { |
263 | $sql = <<"SQL"; |
b1e1d073 |
264 | |
265 | SELECT TOP $rows $outer_select FROM |
15827712 |
266 | ( |
42e5b103 |
267 | $sql |
ac93965c |
268 | ) $quoted_rs_alias |
15827712 |
269 | $order_by_outer |
b1e1d073 |
270 | SQL |
271 | |
42e5b103 |
272 | } |
15827712 |
273 | |
42e5b103 |
274 | if ($order_by_requested) { |
b1e1d073 |
275 | $sql = <<"SQL"; |
15827712 |
276 | |
42e5b103 |
277 | SELECT $outer_select FROM |
ac93965c |
278 | ( $sql ) $quoted_rs_alias |
279 | $order_by_requested |
15827712 |
280 | SQL |
b1e1d073 |
281 | |
282 | } |
283 | |
ac93965c |
284 | $sql =~ s/\s*\n\s*/ /g; # parsing out multiline statements is harder than a single line |
b1e1d073 |
285 | return $sql; |
15827712 |
286 | } |
287 | |
42e5b103 |
288 | # action at a distance to shorten Top code above |
289 | sub __record_alias { |
290 | my ($self, $register, $alias, $fqcol, $col) = @_; |
291 | |
292 | # record qualified name |
293 | $register->{$fqcol} = $alias; |
294 | $register->{$self->_quote($fqcol)} = $alias; |
295 | |
296 | return unless $col; |
297 | |
b6d347e0 |
298 | # record unqualified name, undef (no adjustment) if a duplicate is found |
42e5b103 |
299 | if (exists $register->{$col}) { |
300 | $register->{$col} = undef; |
301 | } |
302 | else { |
303 | $register->{$col} = $alias; |
304 | } |
305 | |
306 | $register->{$self->_quote($col)} = $register->{$col}; |
15827712 |
307 | } |
308 | |
309 | |
6f4ddea1 |
310 | |
311 | # While we're at it, this should make LIMIT queries more efficient, |
312 | # without digging into things too deeply |
6f4ddea1 |
313 | sub _find_syntax { |
314 | my ($self, $syntax) = @_; |
ac788c46 |
315 | return $self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax); |
6f4ddea1 |
316 | } |
317 | |
998373c2 |
318 | # Quotes table names, handles "limit" dialects (e.g. where rownum between x and |
a6b68a60 |
319 | # y) |
6f4ddea1 |
320 | sub select { |
a6b68a60 |
321 | my ($self, $table, $fields, $where, $rs_attrs, @rest) = @_; |
1cbd3034 |
322 | |
323 | $self->{"${_}_bind"} = [] for (qw/having from order/); |
db56cf3d |
324 | |
c2b7c5dc |
325 | if (not ref($table) or ref($table) eq 'SCALAR') { |
6f4ddea1 |
326 | $table = $self->_quote($table); |
327 | } |
c2b7c5dc |
328 | |
6f4ddea1 |
329 | local $self->{rownum_hack_count} = 1 |
330 | if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum'); |
331 | @rest = (-1) unless defined $rest[0]; |
e8fcf76f |
332 | croak "LIMIT 0 Does Not Compute" if $rest[0] == 0; |
6f4ddea1 |
333 | # and anyway, SQL::Abstract::Limit will cause a barf if we don't first |
a6b68a60 |
334 | |
db56cf3d |
335 | my ($sql, @where_bind) = $self->SUPER::select( |
a6b68a60 |
336 | $table, $self->_recurse_fields($fields), $where, $rs_attrs, @rest |
6f4ddea1 |
337 | ); |
1cbd3034 |
338 | return wantarray ? ($sql, @{$self->{from_bind}}, @where_bind, @{$self->{having_bind}}, @{$self->{order_bind}} ) : $sql; |
6f4ddea1 |
339 | } |
340 | |
998373c2 |
341 | # Quotes table names, and handles default inserts |
6f4ddea1 |
342 | sub insert { |
343 | my $self = shift; |
344 | my $table = shift; |
c2b7c5dc |
345 | $table = $self->_quote($table); |
7a72e5a5 |
346 | |
347 | # SQLA will emit INSERT INTO $table ( ) VALUES ( ) |
348 | # which is sadly understood only by MySQL. Change default behavior here, |
349 | # until SQLA2 comes with proper dialect support |
350 | if (! $_[0] or (ref $_[0] eq 'HASH' and !keys %{$_[0]} ) ) { |
28d28903 |
351 | my $sql = "INSERT INTO ${table} DEFAULT VALUES"; |
352 | |
ef8d02eb |
353 | if (my $ret = ($_[1]||{})->{returning} ) { |
354 | $sql .= $self->_insert_returning ($ret); |
28d28903 |
355 | } |
356 | |
357 | return $sql; |
7a72e5a5 |
358 | } |
359 | |
6f4ddea1 |
360 | $self->SUPER::insert($table, @_); |
361 | } |
362 | |
998373c2 |
363 | # Just quotes table names. |
6f4ddea1 |
364 | sub update { |
365 | my $self = shift; |
366 | my $table = shift; |
c2b7c5dc |
367 | $table = $self->_quote($table); |
6f4ddea1 |
368 | $self->SUPER::update($table, @_); |
369 | } |
370 | |
998373c2 |
371 | # Just quotes table names. |
6f4ddea1 |
372 | sub delete { |
373 | my $self = shift; |
374 | my $table = shift; |
c2b7c5dc |
375 | $table = $self->_quote($table); |
6f4ddea1 |
376 | $self->SUPER::delete($table, @_); |
377 | } |
378 | |
379 | sub _emulate_limit { |
380 | my $self = shift; |
a6b68a60 |
381 | # my ( $syntax, $sql, $order, $rows, $offset ) = @_; |
382 | |
6f4ddea1 |
383 | if ($_[3] == -1) { |
a6b68a60 |
384 | return $_[1] . $self->_parse_rs_attrs($_[2]); |
6f4ddea1 |
385 | } else { |
386 | return $self->SUPER::_emulate_limit(@_); |
387 | } |
388 | } |
389 | |
390 | sub _recurse_fields { |
391 | my ($self, $fields, $params) = @_; |
392 | my $ref = ref $fields; |
393 | return $self->_quote($fields) unless $ref; |
394 | return $$fields if $ref eq 'SCALAR'; |
395 | |
396 | if ($ref eq 'ARRAY') { |
397 | return join(', ', map { |
398 | $self->_recurse_fields($_) |
399 | .(exists $self->{rownum_hack_count} && !($params && $params->{no_rownum_hack}) |
400 | ? ' AS col'.$self->{rownum_hack_count}++ |
401 | : '') |
402 | } @$fields); |
83e09b5b |
403 | } |
404 | elsif ($ref eq 'HASH') { |
405 | my %hash = %$fields; |
83e09b5b |
406 | |
50136dd9 |
407 | my $as = delete $hash{-as}; # if supplied |
408 | |
409 | my ($func, $args) = each %hash; |
410 | delete $hash{$func}; |
411 | |
412 | if (lc ($func) eq 'distinct' && ref $args eq 'ARRAY' && @$args > 1) { |
413 | croak ( |
414 | 'The select => { distinct => ... } syntax is not supported for multiple columns.' |
415 | .' Instead please use { group_by => [ qw/' . (join ' ', @$args) . '/ ] }' |
416 | .' or { select => [ qw/' . (join ' ', @$args) . '/ ], distinct => 1 }' |
83e09b5b |
417 | ); |
6f4ddea1 |
418 | } |
83e09b5b |
419 | |
50136dd9 |
420 | my $select = sprintf ('%s( %s )%s', |
421 | $self->_sqlcase($func), |
422 | $self->_recurse_fields($args), |
423 | $as |
0491b597 |
424 | ? sprintf (' %s %s', $self->_sqlcase('as'), $self->_quote ($as) ) |
50136dd9 |
425 | : '' |
426 | ); |
427 | |
83e09b5b |
428 | # there should be nothing left |
429 | if (keys %hash) { |
430 | croak "Malformed select argument - too many keys in hash: " . join (',', keys %$fields ); |
6f4ddea1 |
431 | } |
83e09b5b |
432 | |
83e09b5b |
433 | return $select; |
6f4ddea1 |
434 | } |
435 | # Is the second check absolutely necessary? |
436 | elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) { |
db56cf3d |
437 | return $self->_fold_sqlbind( $fields ); |
6f4ddea1 |
438 | } |
439 | else { |
e8fcf76f |
440 | croak($ref . qq{ unexpected in _recurse_fields()}) |
6f4ddea1 |
441 | } |
442 | } |
443 | |
a6b68a60 |
444 | my $for_syntax = { |
445 | update => 'FOR UPDATE', |
446 | shared => 'FOR SHARE', |
447 | }; |
448 | |
449 | # this used to be a part of _order_by but is broken out for clarity. |
450 | # What we have been doing forever is hijacking the $order arg of |
451 | # SQLA::select to pass in arbitrary pieces of data (first the group_by, |
452 | # then pretty much the entire resultset attr-hash, as more and more |
453 | # things in the SQLA space need to have mopre info about the $rs they |
454 | # create SQL for. The alternative would be to keep expanding the |
455 | # signature of _select with more and more positional parameters, which |
456 | # is just gross. All hail SQLA2! |
457 | sub _parse_rs_attrs { |
1cbd3034 |
458 | my ($self, $arg) = @_; |
15827712 |
459 | |
a6b68a60 |
460 | my $sql = ''; |
1cbd3034 |
461 | |
a6b68a60 |
462 | if (my $g = $self->_recurse_fields($arg->{group_by}, { no_rownum_hack => 1 }) ) { |
463 | $sql .= $self->_sqlcase(' group by ') . $g; |
464 | } |
1cbd3034 |
465 | |
a6b68a60 |
466 | if (defined $arg->{having}) { |
467 | my ($frag, @bind) = $self->_recurse_where($arg->{having}); |
468 | push(@{$self->{having_bind}}, @bind); |
469 | $sql .= $self->_sqlcase(' having ') . $frag; |
470 | } |
15827712 |
471 | |
a6b68a60 |
472 | if (defined $arg->{order_by}) { |
473 | $sql .= $self->_order_by ($arg->{order_by}); |
474 | } |
15827712 |
475 | |
a6b68a60 |
476 | if (my $for = $arg->{for}) { |
477 | $sql .= " $for_syntax->{$for}" if $for_syntax->{$for}; |
478 | } |
479 | |
480 | return $sql; |
481 | } |
482 | |
483 | sub _order_by { |
484 | my ($self, $arg) = @_; |
15827712 |
485 | |
a6b68a60 |
486 | # check that we are not called in legacy mode (order_by as 4th argument) |
487 | if (ref $arg eq 'HASH' and not grep { $_ =~ /^-(?:desc|asc)/i } keys %$arg ) { |
488 | return $self->_parse_rs_attrs ($arg); |
fde3719a |
489 | } |
1cbd3034 |
490 | else { |
491 | my ($sql, @bind) = $self->SUPER::_order_by ($arg); |
a6b68a60 |
492 | push @{$self->{order_bind}}, @bind; |
1cbd3034 |
493 | return $sql; |
fd4cb60a |
494 | } |
6f4ddea1 |
495 | } |
496 | |
1cbd3034 |
497 | sub _order_directions { |
fd4cb60a |
498 | my ($self, $order) = @_; |
15827712 |
499 | |
1cbd3034 |
500 | # strip bind values - none of the current _order_directions users support them |
501 | return $self->SUPER::_order_directions( [ map |
502 | { ref $_ ? $_->[0] : $_ } |
503 | $self->_order_by_chunks ($order) |
504 | ]); |
fd4cb60a |
505 | } |
506 | |
6f4ddea1 |
507 | sub _table { |
508 | my ($self, $from) = @_; |
509 | if (ref $from eq 'ARRAY') { |
510 | return $self->_recurse_from(@$from); |
511 | } elsif (ref $from eq 'HASH') { |
512 | return $self->_make_as($from); |
513 | } else { |
514 | return $from; # would love to quote here but _table ends up getting called |
515 | # twice during an ->select without a limit clause due to |
516 | # the way S::A::Limit->select works. should maybe consider |
517 | # bypassing this and doing S::A::select($self, ...) in |
518 | # our select method above. meantime, quoting shims have |
519 | # been added to select/insert/update/delete here |
520 | } |
521 | } |
522 | |
b8391c87 |
523 | sub _generate_join_clause { |
524 | my ($self, $join_type) = @_; |
525 | |
526 | return sprintf ('%s JOIN ', |
527 | $join_type ? ' ' . uc($join_type) : '' |
528 | ); |
529 | } |
530 | |
6f4ddea1 |
531 | sub _recurse_from { |
532 | my ($self, $from, @join) = @_; |
533 | my @sqlf; |
534 | push(@sqlf, $self->_make_as($from)); |
535 | foreach my $j (@join) { |
536 | my ($to, $on) = @$j; |
537 | |
aa82ce29 |
538 | |
6f4ddea1 |
539 | # check whether a join type exists |
6f4ddea1 |
540 | my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to; |
aa82ce29 |
541 | my $join_type; |
542 | if (ref($to_jt) eq 'HASH' and defined($to_jt->{-join_type})) { |
543 | $join_type = $to_jt->{-join_type}; |
544 | $join_type =~ s/^\s+ | \s+$//xg; |
6f4ddea1 |
545 | } |
aa82ce29 |
546 | |
de5f71ef |
547 | $join_type = $self->{_default_jointype} if not defined $join_type; |
aa82ce29 |
548 | |
b8391c87 |
549 | push @sqlf, $self->_generate_join_clause( $join_type ); |
6f4ddea1 |
550 | |
551 | if (ref $to eq 'ARRAY') { |
552 | push(@sqlf, '(', $self->_recurse_from(@$to), ')'); |
553 | } else { |
554 | push(@sqlf, $self->_make_as($to)); |
555 | } |
556 | push(@sqlf, ' ON ', $self->_join_condition($on)); |
557 | } |
558 | return join('', @sqlf); |
559 | } |
560 | |
db56cf3d |
561 | sub _fold_sqlbind { |
562 | my ($self, $sqlbind) = @_; |
69989ea9 |
563 | |
564 | my @sqlbind = @$$sqlbind; # copy |
565 | my $sql = shift @sqlbind; |
566 | push @{$self->{from_bind}}, @sqlbind; |
567 | |
db56cf3d |
568 | return $sql; |
6f4ddea1 |
569 | } |
570 | |
571 | sub _make_as { |
572 | my ($self, $from) = @_; |
db56cf3d |
573 | return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ |
574 | : ref $_ eq 'REF' ? $self->_fold_sqlbind($_) |
575 | : $self->_quote($_)) |
6f4ddea1 |
576 | } reverse each %{$self->_skip_options($from)}); |
577 | } |
578 | |
579 | sub _skip_options { |
580 | my ($self, $hash) = @_; |
581 | my $clean_hash = {}; |
582 | $clean_hash->{$_} = $hash->{$_} |
583 | for grep {!/^-/} keys %$hash; |
584 | return $clean_hash; |
585 | } |
586 | |
587 | sub _join_condition { |
588 | my ($self, $cond) = @_; |
589 | if (ref $cond eq 'HASH') { |
590 | my %j; |
591 | for (keys %$cond) { |
592 | my $v = $cond->{$_}; |
593 | if (ref $v) { |
e8fcf76f |
594 | croak (ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'}) |
6f4ddea1 |
595 | if ref($v) ne 'SCALAR'; |
596 | $j{$_} = $v; |
597 | } |
598 | else { |
599 | my $x = '= '.$self->_quote($v); $j{$_} = \$x; |
600 | } |
601 | }; |
602 | return scalar($self->_recurse_where(\%j)); |
603 | } elsif (ref $cond eq 'ARRAY') { |
604 | return join(' OR ', map { $self->_join_condition($_) } @$cond); |
605 | } else { |
606 | die "Can't handle this yet!"; |
607 | } |
608 | } |
609 | |
6f4ddea1 |
610 | sub limit_dialect { |
611 | my $self = shift; |
b2c7cf74 |
612 | if (@_) { |
613 | $self->{limit_dialect} = shift; |
614 | undef $self->{_cached_syntax}; |
615 | } |
6f4ddea1 |
616 | return $self->{limit_dialect}; |
617 | } |
618 | |
998373c2 |
619 | # Set to an array-ref to specify separate left and right quotes for table names. |
620 | # A single scalar is equivalen to [ $char, $char ] |
6f4ddea1 |
621 | sub quote_char { |
622 | my $self = shift; |
623 | $self->{quote_char} = shift if @_; |
624 | return $self->{quote_char}; |
625 | } |
626 | |
998373c2 |
627 | # Character separating quoted table names. |
6f4ddea1 |
628 | sub name_sep { |
629 | my $self = shift; |
630 | $self->{name_sep} = shift if @_; |
631 | return $self->{name_sep}; |
632 | } |
633 | |
634 | 1; |