DBIx::Class::SQLAHacks;
use base qw/SQL::Abstract::Limit/;
+use strict;
+use warnings;
use Carp::Clan qw/^DBIx::Class/;
+use Scalar::Util();
sub new {
my $self = shift->SUPER::new(@_);
my ($self, $sql, $order, $rows, $offset ) = @_;
$offset += 1;
- my $last = $rows + $offset;
+ my $last = $rows + $offset - 1;
my ( $order_by ) = $self->_order_by( $order );
$sql = <<"SQL";
# While we're at it, this should make LIMIT queries more efficient,
# without digging into things too deeply
-use Scalar::Util 'blessed';
sub _find_syntax {
my ($self, $syntax) = @_;
-
- # DB2 is the only remaining DB using this. Even though we are not sure if
- # RowNumberOver is still needed here (should be part of SQLA) leave the
- # code in place
- my $dbhname = blessed($syntax) ? $syntax->{Driver}{Name} : $syntax;
- if(ref($self) && $dbhname && $dbhname eq 'DB2') {
- return 'RowNumberOver';
- }
-
- $self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax);
+ return $self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax);
}
sub select {
if ($func eq 'distinct') {
my $_fields = $fields->{$func};
if (ref $_fields eq 'ARRAY' && @{$_fields} > 1) {
- croak "Unsupported syntax, please use " .
- "{ group_by => [ qw/" . (join ' ', @$_fields) . "/ ] }" .
- " or " .
- "{ select => [ qw/" . (join ' ', @$_fields) . "/ ], distinct => 1 }";
+ croak (
+ 'The select => { distinct => ... } syntax is not supported for multiple columns.'
+ .' Instead please use { group_by => [ qw/' . (join ' ', @$_fields) . '/ ] }'
+ .' or { select => [ qw/' . (join ' ', @$_fields) . '/ ], distinct => 1 }'
+ );
}
else {
$_fields = @{$_fields}[0] if ref $_fields eq 'ARRAY';
- carp "This syntax will be deprecated in 09, please use " .
- "{ group_by => '${_fields}' }" .
- " or " .
- "{ select => '${_fields}', distinct => 1 }";
+ carp (
+ 'The select => { distinct => ... } syntax will be deprecated in DBIC version 0.09,'
+ ." please use { group_by => '${_fields}' } or { select => '${_fields}', distinct => 1 }"
+ );
}
}
-
return $self->_sqlcase($func)
.'( '.$self->_recurse_fields($fields->{$func}).' )';
}
sub _order_directions {
my ($self, $order) = @_;
- $order = $order->{order_by} if ref $order eq 'HASH';
- return $self->SUPER::_order_directions($order);
+ return $self->SUPER::_order_directions( $self->_resolve_order($order) );
+}
+
+sub _resolve_order {
+ my ($self, $order) = @_;
+ $order = $order->{order_by} if (ref $order eq 'HASH' and $order->{order_by});
+
+ if (ref $order eq 'HASH') {
+ $order = [$self->_resolve_order_hash($order)];
+ }
+ elsif (ref $order eq 'ARRAY') {
+ $order = [map {
+ if (ref ($_) eq 'SCALAR') {
+ $$_
+ }
+ elsif (ref ($_) eq 'HASH') {
+ $self->_resolve_order_hash($_)
+ }
+ else {
+ $_
+ }
+ } @$order];
+ }
+
+ return $order;
+}
+
+sub _resolve_order_hash {
+ my ($self, $order) = @_;
+ my @new_order;
+ foreach my $key (keys %{ $order }) {
+ if ($key =~ /^-(desc|asc)/i ) {
+ my $direction = $1;
+ my $type = ref $order->{ $key };
+ if ($type eq 'ARRAY') {
+ push @new_order, map( "$_ $direction", @{ $order->{ $key } } );
+ } elsif (!$type) {
+ push @new_order, "$order->{$key} $direction";
+ } else {
+ croak "hash order_by can only contain Scalar or Array, not $type";
+ }
+ } else {
+ croak "$key is not a valid direction, use -asc or -desc";
+ }
+ }
+ return @new_order;
}
sub _table {
sub _fold_sqlbind {
my ($self, $sqlbind) = @_;
- my $sql = shift @$$sqlbind;
- push @{$self->{from_bind}}, @$$sqlbind;
+
+ my @sqlbind = @$$sqlbind; # copy
+ my $sql = shift @sqlbind;
+ push @{$self->{from_bind}}, @sqlbind;
+
return $sql;
}