From: Peter Rabbitson Date: Fri, 14 May 2010 09:57:40 +0000 (+0000) Subject: cleanup test by wreis X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ba42e08cae27496c32d44d3987ac566099140aaf;p=dbsrgits%2FDBIx-Class-Historic.git cleanup test by wreis --- diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index 40bc7a3..c362e91 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -485,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.' @@ -575,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') { diff --git a/t/bind/order_by.t b/t/bind/order_by.t index 7a8bce6..7f8df43 100644 --- a/t/bind/order_by.t +++ b/t/bind/order_by.t @@ -103,4 +103,3 @@ my @tests = ( plan( tests => scalar @tests * 2 ); test_order($_) for @tests; - diff --git a/t/sqlahacks/order_by_func.t b/t/sqlahacks/order_by_func.t index 51968ed..1caac14 100644 --- a/t/sqlahacks/order_by_func.t +++ b/t/sqlahacks/order_by_func.t @@ -7,29 +7,45 @@ use DBICTest; use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); +$schema->storage->sql_maker->quote_char ('"'); +$schema->storage->sql_maker->name_sep ('.'); my $rs = $schema->resultset('CD')->search({}, { 'join' => 'tracks', - order_by => { + order_by => [ + { -length => 'me.title' }, + { -desc => { - count => 'tracks.track_id', + count => 'tracks.trackid', }, - }, + }, + ], distinct => 1, rows => 2, - page => 1, + page => 2, }); -my $match = q{ - SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me - GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track - ORDER BY COUNT(tracks.trackid) DESC -}; -TODO: { - todo_skip 'order_by using function', 2; - is_same_sql($rs->as_query, $match, 'order by with func query'); +is_same_sql_bind( + $rs->as_query, + '( + SELECT "me"."cdid", "me"."artist", "me"."title", "me"."year", "me"."genreid", "me"."single_track" + FROM cd "me" + LEFT JOIN "track" "tracks" ON "tracks"."cd" = "me"."cdid" + GROUP BY "me"."cdid", "me"."artist", "me"."title", "me"."year", "me"."genreid", "me"."single_track" + ORDER BY + LENGTH( "me"."title" ), + COUNT( "tracks"."trackid" ) DESC + LIMIT 2 OFFSET 2 + )', + [], + 'order by with func query', +); - ok($rs->count == 2, 'amount of rows return in order by func query'); -} +ok($rs->count_rs->next == 2, 'amount of rows return in order by func query'); +is_deeply ( + [ $rs->get_column ('me.title')->all ], + [ "Caterwaulin' Blues", "Come Be Depressed With Us" ], + 'Correctly ordered stuff by title-length', +); done_testing;