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