sub _order_directions {
my ($self, $order) = @_;
$order = $order->{order_by} if ref $order eq 'HASH';
+ if (ref $order eq 'HASH') {
+ $order = [$self->_order_directions_hash($order)];
+ } elsif (ref $order eq 'ARRAY') {
+ $order = [map {
+ if (ref $_ eq 'HASH') {
+ $self->_order_directions_hash($_);
+ } else {
+ $_;
+ }
+ } @{ $order }];
+ }
return $self->SUPER::_order_directions($order);
}
+sub _order_directions_hash {
+ my ($self, $order) = @_;
+ if (grep { $_ =~ /^-(desc|asc)/i } keys %{$order}) {
+ return map {
+ my $key = $_;
+ my @tmp;
+ s/^-(desc|asc)/\1/i;
+ my $dir = $_;
+ if (ref $order->{ $key } eq 'ARRAY') {
+ @tmp = map "$_ $dir", @{ $order->{ $key } };
+ } else { # should be scalar
+ @tmp = ( "$order->{$key} $dir" );
+ }
+ @tmp;
+ } keys %{$order};
+ }
+}
+
sub _table {
my ($self, $from) = @_;
if (ref $from eq 'ARRAY') {
use DBIx::Class::Storage::DBI;\r
use lib qw(t/lib);\r
use DBICTest; # do not remove even though it is not used\r
+use DBIC::SqlMakerTest;\r
\r
-plan tests => 1;\r
+plan tests => 8;\r
\r
my $sa = new DBIx::Class::SQLAHacks;\r
-\r
$sa->limit_dialect( 'Top' );\r
\r
-is(\r
- $sa->select( 'rubbish', [ 'foo.id', 'bar.id' ], undef, { order_by => 'artistid' }, 1, 3 ),\r
- 'SELECT * FROM\r
-(\r
- SELECT TOP 1 * FROM\r
- (\r
- SELECT TOP 4 foo.id, bar.id FROM rubbish ORDER BY artistid ASC\r
- ) AS foo\r
- ORDER BY artistid DESC\r
-) AS bar\r
-ORDER BY artistid ASC\r
-',\r
- "make sure limit_dialect( 'Top' ) is working okay"\r
-);\r
+sub test_order {\r
+ my $args = shift;\r
+ my $order_by = $args->{order_by};\r
+ my $expected_sql_order = $args->{expected_sql_order};\r
+\r
+ my $query = $sa->select( 'foo', [qw{bar baz}], undef, {\r
+ order_by => $order_by,\r
+ }, 1, 3\r
+ );\r
+ is_same_sql(\r
+ $query,\r
+ "SELECT * FROM ( SELECT TOP 1 * FROM ( SELECT TOP 4 bar,baz FROM foo ORDER BY $expected_sql_order->[0] ) AS foo ORDER BY $expected_sql_order->[1] ) AS bar ORDER BY $expected_sql_order->[0]",\r
+ );\r
+}\r
+\r
+ test_order({ order_by => \'foo DESC' , expected_sql_order => [ 'foo DESC', 'foo ASC' ] });\r
+ test_order({ order_by => 'foo' , expected_sql_order => [ 'foo ASC', 'foo DESC'] });\r
+ test_order({ order_by => [ qw{ foo bar} ], expected_sql_order => [ 'foo ASC,bar ASC', 'foo DESC, bar DESC']});\r
+ test_order({ order_by => { -asc => 'foo' }, expected_sql_order => [ 'foo ASC', 'foo DESC' ] });\r
+ test_order({ order_by => { -desc => 'foo' }, expected_sql_order => [ 'foo DESC', 'foo ASC' ] });\r
+\r
+ test_order({ order_by => ['foo', { -desc => 'bar' } ], expected_sql_order => [ 'foo ASC, bar DESC', 'foo DESC, bar ASC'] });\r
+ test_order({ order_by => {-asc => [qw{ foo bar }] }, expected_sql_order => ['foo ASC, bar ASC', 'foo DESC, bar DESC' ] });\r
+ test_order({ order_by =>\r
+ [\r
+ { -asc => 'foo' },\r
+ { -desc => [qw{bar}] },\r
+ { -asc => [qw{baz frew}]},\r
+ ],\r
+ expected_sql_order => ['foo ASC, bar DESC, baz ASC, frew ASC', 'foo DESC, bar ASC, baz DESC, frew DESC']\r
+ });\r