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