X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSQLAHacks.pm;h=c362e91133faad6a04e3ee6d938ffab11d374895;hb=e326a7c23be1ed24387395a95ea4ecf48faed7f0;hp=f3fa03012c10859bc2b0f236cfd26a54cd8188f5;hpb=a54bd479dbbeb16e6641140586b626e5502ff631;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index f3fa030..c362e91 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -132,11 +132,7 @@ sub _subqueried_limit_attrs { my %extra_order_sel; if ($scan_order) { - for my $chunk ($self->_order_by_chunks ( - ref $rs_attrs->{order_by} eq 'ARRAY' - ? @{$rs_attrs->{order_by}} - : $rs_attrs->{order_by} - )) { + for my $chunk ($self->_order_by_chunks ($rs_attrs->{order_by})) { # order with bind $chunk = $chunk->[0] if (ref $chunk) eq 'ARRAY'; $chunk =~ s/\s+ (?: ASC|DESC ) \s* $//ix; @@ -144,7 +140,7 @@ sub _subqueried_limit_attrs { next if $in_sel_index->{$chunk}; $extra_order_sel{$chunk} ||= $self->_quote ( - '__ORDER_BY_' . scalar keys %extra_order_sel + 'ORDER__BY__' . scalar keys %extra_order_sel ); } } @@ -178,7 +174,10 @@ sub _RowNumberOver { # this is the order supplement magic my $mid_sel = $out_sel; if ($extra_order_sel) { - for my $extra_col (keys %$extra_order_sel) { + for my $extra_col (sort + { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} } + keys %$extra_order_sel + ) { $in_sel .= sprintf (', %s AS %s', $extra_col, $extra_order_sel->{$extra_col}, @@ -320,19 +319,23 @@ sub _Top { my @out_chunks; for my $ch ($self->_order_by_chunks ($inner_order)) { $ch = $ch->[0] if ref $ch eq 'ARRAY'; + $ch =~ s/\s+ ( ASC|DESC ) \s* $//ix; my $dir = uc ($1||'ASC'); push @out_chunks, \join (' ', $ch, $dir eq 'ASC' ? 'DESC' : 'ASC' ); } - $order_by_reversed = $self->_order_by (@out_chunks); + $order_by_reversed = $self->_order_by (\@out_chunks); } # this is the order supplement magic my $mid_sel = $out_sel; if ($extra_order_sel) { - for my $extra_col (keys %$extra_order_sel) { + for my $extra_col (sort + { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} } + keys %$extra_order_sel + ) { $in_sel .= sprintf (', %s AS %s', $extra_col, $extra_order_sel->{$extra_col}, @@ -374,18 +377,11 @@ sub _Top { $sql = sprintf ('SELECT TOP %d %s FROM ( %s ) %s %s', $rows, - $mid_sel, - $sql, - $quoted_rs_alias, - $order_by_requested, - ) if $order_by_requested; - - $sql = sprintf ('SELECT TOP %d %s FROM ( %s ) %s', - $rows, $out_sel, $sql, $quoted_rs_alias, - ) if ($mid_sel ne $out_sel); + $order_by_requested, + ) if ( ($offset && $order_by_requested) || ($mid_sel ne $out_sel) ); return $sql; } @@ -409,8 +405,6 @@ sub select { $table = $self->_quote($table); } - local $self->{rownum_hack_count} = 1 - if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum'); @rest = (-1) unless defined $rest[0]; croak "LIMIT 0 Does Not Compute" if $rest[0] == 0; # and anyway, SQL::Abstract::Limit will cause a barf if we don't first @@ -491,6 +485,8 @@ sub _recurse_fields { croak "Malformed select argument - too many keys in hash: " . join (',', keys %$fields ); } + $func =~ s/^\-+//; # strip leading dash, at some point the dash itself should become mandatory + if (lc ($func) eq 'distinct' && ref $args eq 'ARRAY' && @$args > 1) { croak ( 'The select => { distinct => ... } syntax is not supported for multiple columns.' @@ -536,7 +532,7 @@ sub _parse_rs_attrs { my $sql = ''; - if (my $g = $self->_recurse_fields($arg->{group_by}, { no_rownum_hack => 1 }) ) { + if (my $g = $self->_recurse_fields($arg->{group_by}) ) { $sql .= $self->_sqlcase(' group by ') . $g; } @@ -581,6 +577,23 @@ sub _order_directions { ]); } +sub _order_by_chunks { + my ($self, $arg) = @_; + if ( # non-empty hash with neither an -asc or a -desc + ref $arg eq 'HASH' + && + keys %$arg + && + ! exists $arg->{-desc} + && + ! exists $arg->{-asc} + ) { + return $self->_recurse_fields ($arg); + } + + return $self->SUPER::_order_by_chunks ($arg); +} + sub _table { my ($self, $from) = @_; if (ref $from eq 'ARRAY') {