From: Dan Thomas Date: Fri, 12 Feb 2010 17:05:11 +0000 (+0000) Subject: Add is_ordered to DBIC::ResultSet X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d1f542dbc28d54536e6544b996ace7454f681f0e;p=dbsrgits%2FDBIx-Class-Historic.git Add is_ordered to DBIC::ResultSet --- diff --git a/Changes b/Changes index 7514185..98f3ecf 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for DBIx::Class + - Add $rs->is_ordered to test for existing order_by on a resultset - Add as_subselect_rs to DBIC::ResultSet from DBIC::Helper::ResultSet::VirtualView::as_virtual_view - Depend on newest bugfixed Moose diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 60a4f75..8d64df4 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -245,6 +245,8 @@ da5id: David Jack Olrik debolaz: Anders Nor Berle +dew: Dan Thomas + dkubb: Dan Kubb dnm: Justin Wheeler diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 173857c..26ee0f7 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2470,6 +2470,23 @@ sub is_paged { return !!$self->{attrs}{page}; } +=head2 is_ordered + +=over 4 + +=item Arguments: none + +=item Return Value: true, if the resultset has been ordered with C. + +=back + +=cut + +sub is_ordered { + my ($self) = @_; + return scalar $self->result_source->storage->_parse_order_by($self->{attrs}{order_by}); +} + =head2 related_resultset =over 4 diff --git a/t/resultset/is_ordered.t b/t/resultset/is_ordered.t new file mode 100644 index 0000000..bab58d0 --- /dev/null +++ b/t/resultset/is_ordered.t @@ -0,0 +1,90 @@ +use strict; +use warnings; + +use lib qw(t/lib); +use Test::More; +use Test::Exception; +use DBICTest; + +my $schema = DBICTest->init_schema(); +my $rs = $schema->resultset('Artist'); + +ok !$rs->is_ordered, 'vanilla resultset is not ordered'; + +# Simple ordering with a single column +{ + my $ordered = $rs->search(undef, { order_by => 'artistid' }); + ok $ordered->is_ordered, 'Simple column ordering detected by is_ordered'; +} + +# Hashref order direction +{ + my $ordered = $rs->search(undef, { order_by => { -desc => 'artistid' } }); + ok $ordered->is_ordered, 'resultset with order direction is_ordered'; +} + +# Column ordering with literal SQL +{ + my $ordered = $rs->search(undef, { order_by => \'artistid DESC' }); + ok $ordered->is_ordered, 'resultset with literal SQL is_ordered'; +} + +# Multiple column ordering +{ + my $ordered = $rs->search(undef, { order_by => ['artistid', 'name'] }); + ok $ordered->is_ordered, 'ordering with multiple columns as arrayref is ordered'; +} + +# More complicated ordering +{ + my $ordered = $rs->search(undef, { + order_by => [ + { -asc => 'artistid' }, + { -desc => 'name' }, + ] + }); + ok $ordered->is_ordered, 'more complicated resultset ordering is_ordered'; +} + +# Empty multi-column ordering arrayref +{ + my $ordered = $rs->search(undef, { order_by => [] }); + ok !$ordered->is_ordered, 'ordering with empty arrayref is not ordered'; +} + +# Multi-column ordering syntax with empty hashref +{ + my $ordered = $rs->search(undef, { order_by => [{}] }); + ok !$ordered->is_ordered, 'ordering with [{}] is not ordered'; +} + +# Remove ordering after being set +{ + my $ordered = $rs->search(undef, { order_by => 'artistid' }); + ok $ordered->is_ordered, 'resultset with ordering applied works..'; + my $unordered = $ordered->search(undef, { order_by => undef }); + ok !$unordered->is_ordered, '..and is not ordered with ordering removed'; +} + +# Search without ordering +{ + my $ordered = $rs->search({ name => 'We Are Goth' }, { join => 'cds' }); + ok !$ordered->is_ordered, 'WHERE clause but no order_by is not ordered'; +} + +# Other functions without ordering +{ + # Join + my $joined = $rs->search(undef, { join => 'cds' }); + ok !$joined->is_ordered, 'join but no order_by is not ordered'; + + # Group By + my $grouped = $rs->search(undef, { group_by => 'rank' }); + ok !$grouped->is_ordered, 'group_by but no order_by is not ordered'; + + # Paging + my $paged = $rs->search(undef, { page=> 5 }); + ok !$paged->is_ordered, 'paging but no order_by is not ordered'; +} + +done_testing;