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 { |
52 | my ($self, $sql, $order, $rows, $offset ) = @_; |
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 | |
6553ac38 |
58 | # get the order_by only (or make up an order if none exists) |
59 | my $order_by = $self->_order_by( |
84ddb3da |
60 | (delete $order->{order_by}) || $self->_rno_default_order |
6553ac38 |
61 | ); |
6f4ddea1 |
62 | |
cb478051 |
63 | # whatever is left of the order_by |
6553ac38 |
64 | my $group_having = $self->_order_by($order); |
6f4ddea1 |
65 | |
1f36ab67 |
66 | my $qalias = $self->_quote ($self->{_dbic_rs_attrs}{alias}); |
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 | |
6553ac38 |
87 | # Crappy Top based Limit/Offset support. Legacy from MSSQL. |
15827712 |
88 | sub _Top { |
89 | my ( $self, $sql, $order, $rows, $offset ) = @_; |
90 | |
b1e1d073 |
91 | # mangle the input sql so it can be properly aliased in the outer queries |
92 | $sql =~ s/^ \s* SELECT \s+ (.+?) \s+ (?=FROM)//ix |
93 | or croak "Unrecognizable SELECT: $sql"; |
42e5b103 |
94 | my $sql_select = $1; |
95 | my @sql_select = split (/\s*,\s*/, $sql_select); |
96 | |
97 | # we can't support subqueries (in fact MSSQL can't) - croak |
98 | if (@sql_select != @{$self->{_dbic_rs_attrs}{select}}) { |
99 | croak (sprintf ( |
100 | 'SQL SELECT did not parse cleanly - retrieved %d comma separated elements, while ' |
101 | . 'the resultset select attribure contains %d elements: %s', |
102 | scalar @sql_select, |
103 | scalar @{$self->{_dbic_rs_attrs}{select}}, |
104 | $sql_select, |
105 | )); |
106 | } |
b1e1d073 |
107 | |
42e5b103 |
108 | my $name_sep = $self->name_sep || '.'; |
ac93965c |
109 | my $esc_name_sep = "\Q$name_sep\E"; |
110 | my $col_re = qr/ ^ (?: (.+) $esc_name_sep )? ([^$esc_name_sep]+) $ /x; |
111 | |
112 | my $rs_alias = $self->{_dbic_rs_attrs}{alias}; |
113 | my $quoted_rs_alias = $self->_quote ($rs_alias); |
b1e1d073 |
114 | |
42e5b103 |
115 | # construct the new select lists, rename(alias) some columns if necessary |
116 | my (@outer_select, @inner_select, %seen_names, %col_aliases, %outer_col_aliases); |
b1e1d073 |
117 | |
42e5b103 |
118 | for (@{$self->{_dbic_rs_attrs}{select}}) { |
119 | next if ref $_; |
120 | my ($table, $orig_colname) = ( $_ =~ $col_re ); |
121 | next unless $table; |
122 | $seen_names{$orig_colname}++; |
123 | } |
b1e1d073 |
124 | |
42e5b103 |
125 | for my $i (0 .. $#sql_select) { |
b1e1d073 |
126 | |
42e5b103 |
127 | my $colsel_arg = $self->{_dbic_rs_attrs}{select}[$i]; |
128 | my $colsel_sql = $sql_select[$i]; |
b1e1d073 |
129 | |
42e5b103 |
130 | # this may or may not work (in case of a scalarref or something) |
131 | my ($table, $orig_colname) = ( $colsel_arg =~ $col_re ); |
b1e1d073 |
132 | |
42e5b103 |
133 | my $quoted_alias; |
134 | # do not attempt to understand non-scalar selects - alias numerically |
135 | if (ref $colsel_arg) { |
136 | $quoted_alias = $self->_quote ('column_' . (@inner_select + 1) ); |
137 | } |
138 | # column name seen more than once - alias it |
ed4cdb4f |
139 | elsif ($orig_colname && |
140 | ($seen_names{$orig_colname} && $seen_names{$orig_colname} > 1) ) { |
42e5b103 |
141 | $quoted_alias = $self->_quote ("${table}__${orig_colname}"); |
142 | } |
b1e1d073 |
143 | |
42e5b103 |
144 | # we did rename - make a record and adjust |
145 | if ($quoted_alias) { |
146 | # alias inner |
147 | push @inner_select, "$colsel_sql AS $quoted_alias"; |
148 | |
149 | # push alias to outer |
150 | push @outer_select, $quoted_alias; |
151 | |
152 | # Any aliasing accumulated here will be considered |
153 | # both for inner and outer adjustments of ORDER BY |
154 | $self->__record_alias ( |
155 | \%col_aliases, |
156 | $quoted_alias, |
157 | $colsel_arg, |
158 | $table ? $orig_colname : undef, |
159 | ); |
b1e1d073 |
160 | } |
161 | |
42e5b103 |
162 | # otherwise just leave things intact inside, and use the abbreviated one outside |
163 | # (as we do not have table names anymore) |
164 | else { |
165 | push @inner_select, $colsel_sql; |
166 | |
167 | my $outer_quoted = $self->_quote ($orig_colname); # it was not a duplicate so should just work |
168 | push @outer_select, $outer_quoted; |
169 | $self->__record_alias ( |
170 | \%outer_col_aliases, |
171 | $outer_quoted, |
172 | $colsel_arg, |
173 | $table ? $orig_colname : undef, |
174 | ); |
175 | } |
b1e1d073 |
176 | } |
177 | |
178 | my $outer_select = join (', ', @outer_select ); |
42e5b103 |
179 | my $inner_select = join (', ', @inner_select ); |
b1e1d073 |
180 | |
42e5b103 |
181 | %outer_col_aliases = (%outer_col_aliases, %col_aliases); |
b1e1d073 |
182 | |
183 | # deal with order |
15827712 |
184 | croak '$order supplied to SQLAHacks limit emulators must be a hash' |
185 | if (ref $order ne 'HASH'); |
186 | |
8f6dbee9 |
187 | $order = { %$order }; #copy |
188 | |
02d133f0 |
189 | my $req_order = $order->{order_by}; |
15827712 |
190 | |
e2db8319 |
191 | # examine normalized version, collapses nesting |
192 | my $limit_order; |
193 | if (scalar $self->_order_by_chunks ($req_order)) { |
194 | $limit_order = $req_order; |
195 | } |
196 | else { |
e2db8319 |
197 | $limit_order = [ map |
198 | { join ('', $rs_alias, $name_sep, $_ ) } |
199 | ( $self->{_dbic_rs_attrs}{_source_handle}->resolve->primary_columns ) |
200 | ]; |
201 | } |
1cbd3034 |
202 | |
42e5b103 |
203 | my ( $order_by_inner, $order_by_outer ) = $self->_order_directions($limit_order); |
204 | my $order_by_requested = $self->_order_by ($req_order); |
15827712 |
205 | |
64c7c000 |
206 | # generate the rest |
e2db8319 |
207 | delete $order->{order_by}; |
8f6dbee9 |
208 | my $grpby_having = $self->_order_by ($order); |
209 | |
64c7c000 |
210 | # short circuit for counts - the ordering complexity is needless |
211 | if ($self->{_dbic_rs_attrs}{-for_count_only}) { |
212 | return "SELECT TOP $rows $inner_select $sql $grpby_having $order_by_outer"; |
213 | } |
214 | |
42e5b103 |
215 | # we can't really adjust the order_by columns, as introspection is lacking |
216 | # resort to simple substitution |
217 | for my $col (keys %outer_col_aliases) { |
218 | for ($order_by_requested, $order_by_outer) { |
219 | $_ =~ s/\s+$col\s+/ $outer_col_aliases{$col} /g; |
b1e1d073 |
220 | } |
221 | } |
42e5b103 |
222 | for my $col (keys %col_aliases) { |
9c1ffdde |
223 | $order_by_inner =~ s/\s+$col\s+/ $col_aliases{$col} /g; |
42e5b103 |
224 | } |
15827712 |
225 | |
15827712 |
226 | |
42e5b103 |
227 | my $inner_lim = $rows + $offset; |
15827712 |
228 | |
618a0fe3 |
229 | $sql = "SELECT TOP $inner_lim $inner_select $sql $grpby_having $order_by_inner"; |
42e5b103 |
230 | |
231 | if ($offset) { |
232 | $sql = <<"SQL"; |
b1e1d073 |
233 | |
234 | SELECT TOP $rows $outer_select FROM |
15827712 |
235 | ( |
42e5b103 |
236 | $sql |
ac93965c |
237 | ) $quoted_rs_alias |
15827712 |
238 | $order_by_outer |
b1e1d073 |
239 | SQL |
240 | |
42e5b103 |
241 | } |
15827712 |
242 | |
42e5b103 |
243 | if ($order_by_requested) { |
b1e1d073 |
244 | $sql = <<"SQL"; |
15827712 |
245 | |
42e5b103 |
246 | SELECT $outer_select FROM |
ac93965c |
247 | ( $sql ) $quoted_rs_alias |
248 | $order_by_requested |
15827712 |
249 | SQL |
b1e1d073 |
250 | |
251 | } |
252 | |
ac93965c |
253 | $sql =~ s/\s*\n\s*/ /g; # parsing out multiline statements is harder than a single line |
b1e1d073 |
254 | return $sql; |
15827712 |
255 | } |
256 | |
42e5b103 |
257 | # action at a distance to shorten Top code above |
258 | sub __record_alias { |
259 | my ($self, $register, $alias, $fqcol, $col) = @_; |
260 | |
261 | # record qualified name |
262 | $register->{$fqcol} = $alias; |
263 | $register->{$self->_quote($fqcol)} = $alias; |
264 | |
265 | return unless $col; |
266 | |
b6d347e0 |
267 | # record unqualified name, undef (no adjustment) if a duplicate is found |
42e5b103 |
268 | if (exists $register->{$col}) { |
269 | $register->{$col} = undef; |
270 | } |
271 | else { |
272 | $register->{$col} = $alias; |
273 | } |
274 | |
275 | $register->{$self->_quote($col)} = $register->{$col}; |
15827712 |
276 | } |
277 | |
278 | |
6f4ddea1 |
279 | |
280 | # While we're at it, this should make LIMIT queries more efficient, |
281 | # without digging into things too deeply |
6f4ddea1 |
282 | sub _find_syntax { |
283 | my ($self, $syntax) = @_; |
ac788c46 |
284 | return $self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax); |
6f4ddea1 |
285 | } |
286 | |
ec7cfd36 |
287 | my $for_syntax = { |
288 | update => 'FOR UPDATE', |
289 | shared => 'FOR SHARE', |
290 | }; |
998373c2 |
291 | # Quotes table names, handles "limit" dialects (e.g. where rownum between x and |
292 | # y), supports SELECT ... FOR UPDATE and SELECT ... FOR SHARE. |
6f4ddea1 |
293 | sub select { |
294 | my ($self, $table, $fields, $where, $order, @rest) = @_; |
1cbd3034 |
295 | |
296 | $self->{"${_}_bind"} = [] for (qw/having from order/); |
db56cf3d |
297 | |
c2b7c5dc |
298 | if (not ref($table) or ref($table) eq 'SCALAR') { |
6f4ddea1 |
299 | $table = $self->_quote($table); |
300 | } |
c2b7c5dc |
301 | |
6f4ddea1 |
302 | local $self->{rownum_hack_count} = 1 |
303 | if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum'); |
304 | @rest = (-1) unless defined $rest[0]; |
e8fcf76f |
305 | croak "LIMIT 0 Does Not Compute" if $rest[0] == 0; |
6f4ddea1 |
306 | # and anyway, SQL::Abstract::Limit will cause a barf if we don't first |
db56cf3d |
307 | my ($sql, @where_bind) = $self->SUPER::select( |
6f4ddea1 |
308 | $table, $self->_recurse_fields($fields), $where, $order, @rest |
309 | ); |
ec7cfd36 |
310 | if (my $for = delete $self->{_dbic_rs_attrs}{for}) { |
311 | $sql .= " $for_syntax->{$for}" if $for_syntax->{$for}; |
312 | } |
313 | |
1cbd3034 |
314 | return wantarray ? ($sql, @{$self->{from_bind}}, @where_bind, @{$self->{having_bind}}, @{$self->{order_bind}} ) : $sql; |
6f4ddea1 |
315 | } |
316 | |
998373c2 |
317 | # Quotes table names, and handles default inserts |
6f4ddea1 |
318 | sub insert { |
319 | my $self = shift; |
320 | my $table = shift; |
c2b7c5dc |
321 | $table = $self->_quote($table); |
7a72e5a5 |
322 | |
323 | # SQLA will emit INSERT INTO $table ( ) VALUES ( ) |
324 | # which is sadly understood only by MySQL. Change default behavior here, |
325 | # until SQLA2 comes with proper dialect support |
326 | if (! $_[0] or (ref $_[0] eq 'HASH' and !keys %{$_[0]} ) ) { |
327 | return "INSERT INTO ${table} DEFAULT VALUES" |
328 | } |
329 | |
6f4ddea1 |
330 | $self->SUPER::insert($table, @_); |
331 | } |
332 | |
998373c2 |
333 | # Just quotes table names. |
6f4ddea1 |
334 | sub update { |
335 | my $self = shift; |
336 | my $table = shift; |
c2b7c5dc |
337 | $table = $self->_quote($table); |
6f4ddea1 |
338 | $self->SUPER::update($table, @_); |
339 | } |
340 | |
998373c2 |
341 | # Just quotes table names. |
6f4ddea1 |
342 | sub delete { |
343 | my $self = shift; |
344 | my $table = shift; |
c2b7c5dc |
345 | $table = $self->_quote($table); |
6f4ddea1 |
346 | $self->SUPER::delete($table, @_); |
347 | } |
348 | |
349 | sub _emulate_limit { |
350 | my $self = shift; |
351 | if ($_[3] == -1) { |
352 | return $_[1].$self->_order_by($_[2]); |
353 | } else { |
354 | return $self->SUPER::_emulate_limit(@_); |
355 | } |
356 | } |
357 | |
358 | sub _recurse_fields { |
359 | my ($self, $fields, $params) = @_; |
360 | my $ref = ref $fields; |
361 | return $self->_quote($fields) unless $ref; |
362 | return $$fields if $ref eq 'SCALAR'; |
363 | |
364 | if ($ref eq 'ARRAY') { |
365 | return join(', ', map { |
366 | $self->_recurse_fields($_) |
367 | .(exists $self->{rownum_hack_count} && !($params && $params->{no_rownum_hack}) |
368 | ? ' AS col'.$self->{rownum_hack_count}++ |
369 | : '') |
370 | } @$fields); |
83e09b5b |
371 | } |
372 | elsif ($ref eq 'HASH') { |
373 | my %hash = %$fields; |
83e09b5b |
374 | |
50136dd9 |
375 | my $as = delete $hash{-as}; # if supplied |
376 | |
377 | my ($func, $args) = each %hash; |
378 | delete $hash{$func}; |
379 | |
380 | if (lc ($func) eq 'distinct' && ref $args eq 'ARRAY' && @$args > 1) { |
381 | croak ( |
382 | 'The select => { distinct => ... } syntax is not supported for multiple columns.' |
383 | .' Instead please use { group_by => [ qw/' . (join ' ', @$args) . '/ ] }' |
384 | .' or { select => [ qw/' . (join ' ', @$args) . '/ ], distinct => 1 }' |
83e09b5b |
385 | ); |
6f4ddea1 |
386 | } |
83e09b5b |
387 | |
50136dd9 |
388 | my $select = sprintf ('%s( %s )%s', |
389 | $self->_sqlcase($func), |
390 | $self->_recurse_fields($args), |
391 | $as |
324bc214 |
392 | ? sprintf (' %s %s', $self->_sqlcase('as'), $as) |
50136dd9 |
393 | : '' |
394 | ); |
395 | |
83e09b5b |
396 | # there should be nothing left |
397 | if (keys %hash) { |
398 | croak "Malformed select argument - too many keys in hash: " . join (',', keys %$fields ); |
6f4ddea1 |
399 | } |
83e09b5b |
400 | |
83e09b5b |
401 | return $select; |
6f4ddea1 |
402 | } |
403 | # Is the second check absolutely necessary? |
404 | elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) { |
db56cf3d |
405 | return $self->_fold_sqlbind( $fields ); |
6f4ddea1 |
406 | } |
407 | else { |
e8fcf76f |
408 | croak($ref . qq{ unexpected in _recurse_fields()}) |
6f4ddea1 |
409 | } |
410 | } |
411 | |
412 | sub _order_by { |
1cbd3034 |
413 | my ($self, $arg) = @_; |
15827712 |
414 | |
1cbd3034 |
415 | if (ref $arg eq 'HASH' and keys %$arg and not grep { $_ =~ /^-(?:desc|asc)/i } keys %$arg ) { |
416 | |
417 | my $ret = ''; |
418 | |
83e09b5b |
419 | if (my $g = $self->_recurse_fields($arg->{group_by}, { no_rownum_hack => 1 }) ) { |
420 | $ret = $self->_sqlcase(' group by ') . $g; |
6f4ddea1 |
421 | } |
15827712 |
422 | |
1cbd3034 |
423 | if (defined $arg->{having}) { |
424 | my ($frag, @bind) = $self->_recurse_where($arg->{having}); |
425 | push(@{$self->{having_bind}}, @bind); |
6f4ddea1 |
426 | $ret .= $self->_sqlcase(' having ').$frag; |
427 | } |
15827712 |
428 | |
1cbd3034 |
429 | if (defined $arg->{order_by}) { |
430 | my ($frag, @bind) = $self->SUPER::_order_by($arg->{order_by}); |
431 | push(@{$self->{order_bind}}, @bind); |
432 | $ret .= $frag; |
6f4ddea1 |
433 | } |
15827712 |
434 | |
1cbd3034 |
435 | return $ret; |
fde3719a |
436 | } |
1cbd3034 |
437 | else { |
438 | my ($sql, @bind) = $self->SUPER::_order_by ($arg); |
439 | push(@{$self->{order_bind}}, @bind); |
440 | return $sql; |
fd4cb60a |
441 | } |
6f4ddea1 |
442 | } |
443 | |
1cbd3034 |
444 | sub _order_directions { |
fd4cb60a |
445 | my ($self, $order) = @_; |
15827712 |
446 | |
1cbd3034 |
447 | # strip bind values - none of the current _order_directions users support them |
448 | return $self->SUPER::_order_directions( [ map |
449 | { ref $_ ? $_->[0] : $_ } |
450 | $self->_order_by_chunks ($order) |
451 | ]); |
fd4cb60a |
452 | } |
453 | |
6f4ddea1 |
454 | sub _table { |
455 | my ($self, $from) = @_; |
456 | if (ref $from eq 'ARRAY') { |
457 | return $self->_recurse_from(@$from); |
458 | } elsif (ref $from eq 'HASH') { |
459 | return $self->_make_as($from); |
460 | } else { |
461 | return $from; # would love to quote here but _table ends up getting called |
462 | # twice during an ->select without a limit clause due to |
463 | # the way S::A::Limit->select works. should maybe consider |
464 | # bypassing this and doing S::A::select($self, ...) in |
465 | # our select method above. meantime, quoting shims have |
466 | # been added to select/insert/update/delete here |
467 | } |
468 | } |
469 | |
470 | sub _recurse_from { |
471 | my ($self, $from, @join) = @_; |
472 | my @sqlf; |
473 | push(@sqlf, $self->_make_as($from)); |
474 | foreach my $j (@join) { |
475 | my ($to, $on) = @$j; |
476 | |
aa82ce29 |
477 | |
6f4ddea1 |
478 | # check whether a join type exists |
6f4ddea1 |
479 | my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to; |
aa82ce29 |
480 | my $join_type; |
481 | if (ref($to_jt) eq 'HASH' and defined($to_jt->{-join_type})) { |
482 | $join_type = $to_jt->{-join_type}; |
483 | $join_type =~ s/^\s+ | \s+$//xg; |
6f4ddea1 |
484 | } |
aa82ce29 |
485 | |
de5f71ef |
486 | $join_type = $self->{_default_jointype} if not defined $join_type; |
aa82ce29 |
487 | |
488 | my $join_clause = sprintf ('%s JOIN ', |
489 | $join_type ? ' ' . uc($join_type) : '' |
490 | ); |
491 | push @sqlf, $join_clause; |
6f4ddea1 |
492 | |
493 | if (ref $to eq 'ARRAY') { |
494 | push(@sqlf, '(', $self->_recurse_from(@$to), ')'); |
495 | } else { |
496 | push(@sqlf, $self->_make_as($to)); |
497 | } |
498 | push(@sqlf, ' ON ', $self->_join_condition($on)); |
499 | } |
500 | return join('', @sqlf); |
501 | } |
502 | |
db56cf3d |
503 | sub _fold_sqlbind { |
504 | my ($self, $sqlbind) = @_; |
69989ea9 |
505 | |
506 | my @sqlbind = @$$sqlbind; # copy |
507 | my $sql = shift @sqlbind; |
508 | push @{$self->{from_bind}}, @sqlbind; |
509 | |
db56cf3d |
510 | return $sql; |
6f4ddea1 |
511 | } |
512 | |
513 | sub _make_as { |
514 | my ($self, $from) = @_; |
db56cf3d |
515 | return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ |
516 | : ref $_ eq 'REF' ? $self->_fold_sqlbind($_) |
517 | : $self->_quote($_)) |
6f4ddea1 |
518 | } reverse each %{$self->_skip_options($from)}); |
519 | } |
520 | |
521 | sub _skip_options { |
522 | my ($self, $hash) = @_; |
523 | my $clean_hash = {}; |
524 | $clean_hash->{$_} = $hash->{$_} |
525 | for grep {!/^-/} keys %$hash; |
526 | return $clean_hash; |
527 | } |
528 | |
529 | sub _join_condition { |
530 | my ($self, $cond) = @_; |
531 | if (ref $cond eq 'HASH') { |
532 | my %j; |
533 | for (keys %$cond) { |
534 | my $v = $cond->{$_}; |
535 | if (ref $v) { |
e8fcf76f |
536 | croak (ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'}) |
6f4ddea1 |
537 | if ref($v) ne 'SCALAR'; |
538 | $j{$_} = $v; |
539 | } |
540 | else { |
541 | my $x = '= '.$self->_quote($v); $j{$_} = \$x; |
542 | } |
543 | }; |
544 | return scalar($self->_recurse_where(\%j)); |
545 | } elsif (ref $cond eq 'ARRAY') { |
546 | return join(' OR ', map { $self->_join_condition($_) } @$cond); |
547 | } else { |
548 | die "Can't handle this yet!"; |
549 | } |
550 | } |
551 | |
552 | sub _quote { |
553 | my ($self, $label) = @_; |
554 | return '' unless defined $label; |
c2b7c5dc |
555 | return $$label if ref($label) eq 'SCALAR'; |
6f4ddea1 |
556 | return "*" if $label eq '*'; |
557 | return $label unless $self->{quote_char}; |
558 | if(ref $self->{quote_char} eq "ARRAY"){ |
559 | return $self->{quote_char}->[0] . $label . $self->{quote_char}->[1] |
560 | if !defined $self->{name_sep}; |
561 | my $sep = $self->{name_sep}; |
562 | return join($self->{name_sep}, |
563 | map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1] } |
564 | split(/\Q$sep\E/,$label)); |
565 | } |
566 | return $self->SUPER::_quote($label); |
567 | } |
568 | |
569 | sub limit_dialect { |
570 | my $self = shift; |
571 | $self->{limit_dialect} = shift if @_; |
572 | return $self->{limit_dialect}; |
573 | } |
574 | |
998373c2 |
575 | # Set to an array-ref to specify separate left and right quotes for table names. |
576 | # A single scalar is equivalen to [ $char, $char ] |
6f4ddea1 |
577 | sub quote_char { |
578 | my $self = shift; |
579 | $self->{quote_char} = shift if @_; |
580 | return $self->{quote_char}; |
581 | } |
582 | |
998373c2 |
583 | # Character separating quoted table names. |
6f4ddea1 |
584 | sub name_sep { |
585 | my $self = shift; |
586 | $self->{name_sep} = shift if @_; |
587 | return $self->{name_sep}; |
588 | } |
589 | |
590 | 1; |