lib/DBIx/Class/CDBICompat/LiveObjectIndex.pm
lib/DBIx/Class/CDBICompat/MightHave.pm
lib/DBIx/Class/CDBICompat/ObjIndexStubs.pm
+lib/DBIx/Class/CDBICompat/Pager.pm
lib/DBIx/Class/CDBICompat/ReadOnly.pm
lib/DBIx/Class/CDBICompat/Retrieve.pm
lib/DBIx/Class/CDBICompat/Stringify.pm
t/04db.t
t/05multipk.t
t/06relationship.t
+t/07pager.t
t/08inflate.t
t/08inflate_has_a.t
t/09update.t
t/cdbi-t/18-has_a.t
t/cdbi-t/19-set_sql.t
t/cdbi-t/21-iterator.t
+t/cdbi-t/30-pager.t
t/cdbi-t/98-failure.t
t/lib/DBICTest.pm
t/lib/DBICTest/Schema.pm
AutoUpdate
TempColumns
Retrieve
+ Pager
ColumnGroups
ImaDBI/);
--- /dev/null
+package DBIx::Class::CDBICompat::Pager;\r
+\r
+use strict;\r
+use warnings FATAL => 'all';\r
+\r
+*pager = \&page;\r
+\r
+sub page {\r
+ my $class = shift;\r
+\r
+ my $it = $class->search(@_);\r
+ my $pager = $class->_page_object;\r
+ return ( $pager, $it );\r
+}\r
+\r
+1;\r
__PACKAGE__->load_components(qw/
InflateColumn
Relationship
- Pager
PK
Row
Table
+++ /dev/null
-package DBIx::Class::Pager;
-
-use strict;
-use warnings;
-
-use NEXT;
-use Data::Page;
-
-=head1 NAME
-
-DBIx::Class::Pager - Pagination of resultsets
-
-=head1 SYNOPSIS
-
-=head1 DESCRIPTION
-
-This class lets you page through a resultset.
-
-=head1 METHODS
-
-=over 4
-
-=item page
-
-=item pager
-
-=cut
-
-*pager = \&page;
-
-sub page {
- my $self = shift;
- my ($criteria, $attr) = @_;
-
- my $rows = $attr->{rows} || 10;
- my $current = $attr->{page} || 1;
-
- # count must not use LIMIT, so strip out rows/offset
- delete $attr->{$_} for qw/rows offset/;
-
- my $total = $self->count( $criteria, $attr );
- my $page = Data::Page->new( $total, $rows, $current );
-
- $attr->{rows} = $page->entries_per_page;
- $attr->{offset} = $page->skipped;
-
- my $iterator = $self->search( $criteria, $attr );
-
- return ( $page, $iterator );
-}
-
-1;
-
-=back
-
-=head1 AUTHORS
-
-Andy Grundman <andy@hybridized.org>
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
-
-=cut
-
my ($self) = @_;
my $db_class = $self->{class};
- # offset breaks COUNT(*), so remove it
+ # offset and order by are not needed to count
my $attrs = { %{ $self->{attrs} } };
- delete $attrs->{offset};
+ delete $attrs->{$_} for qw/offset order_by/;
my @cols = 'COUNT(*)';
my ($c) = $db_class->storage->select_single($db_class->_table_name, \@cols,
use warnings;
use DBIx::Class::ResultSet;
+use Data::Page;
use base qw/Class::Data::Inheritable/;
__PACKAGE__->mk_classdata('_resultset_class' => 'DBIx::Class::ResultSet');
+__PACKAGE__->mk_classdata('_page_object');
+
sub iterator_class { shift->_resultset_class(@_) }
=head1 NAME
$attrs = { %{ pop(@_) } };
}
$attrs->{where} = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
+
+ # for pagination, we create the resultset with no limit and slice it later
+ my $page = {};
+ if ( $attrs->{page} ) {
+ map { $page->{$_} = $attrs->{$_} } qw/rows page/;
+ delete $attrs->{$_} for qw/rows offset page/;
+ }
my $rs = $class->resultset($attrs);
-
+
+ if ( $page->{page} ) {
+ my $pager = Data::Page->new(
+ $rs->count,
+ $page->{rows} || 10,
+ $page->{page} || 1 );
+ $class->_page_object( $pager );
+ return $rs->slice( $pager->skipped,
+ $pager->skipped + $pager->entries_per_page - 1 );
+ }
+
return (wantarray ? $rs->all : $rs);
}
sub columns { return keys %{shift->_columns}; }
+=item page
+
+ $pager = $class->page;
+
+Returns a Data::Page object for the most recent search that was performed
+using the page parameter.
+
+=cut
+
+sub page { shift->_page_object }
+
1;
=back
use_ok('DBICTest');
# first page
-my ( $pager, $it ) = DBICTest::CD->page(
+my $it = DBICTest::CD->search(
{},
{ order_by => 'title',
rows => 3,
page => 1 }
);
-
+my $pager = DBICTest::CD->page;
+
is( $pager->entries_on_this_page, 3, "entries_on_this_page ok" );
is( $pager->next_page, 2, "next_page ok" );
is( $it->next, undef, "next past end of page ok" );
-# second page
-( $pager, $it ) = DBICTest::CD->page(
+# second page, testing with array
+my @page2 = DBICTest::CD->search(
{},
{ order_by => 'title',
rows => 3,
page => 2 }
);
+$pager = DBICTest::CD->page;
is( $pager->entries_on_this_page, 2, "entries on second page ok" );
-is( $it->next->title, "Generic Manufactured Singles", "second page first title ok" );
-
-# XXX: Should we support disable_sql_paging?
-#( $pager, $it ) = DBICTest::CD->page(
-# {},
-# { rows => 2,
-# page => 2,
-# disable_sql_paging => 1 } );
-#
-#cmp_ok( $pager->total_entries, '==', 5, "disable_sql_paging total_entries ok" );
-#
-#cmp_ok( $pager->previous_page, '==', 1, "disable_sql_paging previous_page ok" );
-#
-#is( $it->next->title, "Caterwaulin' Blues", "disable_sql_paging iterator->next ok" );
-#
-#$it->next;
-#
-#is( $it->next, undef, "disable_sql_paging next past end of page ok" );
+is( $page2[0]->title, "Generic Manufactured Singles", "second page first title ok" );
# based on a failing criteria submitted by waswas
# requires SQL::Abstract >= 1.20
-( $pager, $it ) = DBICTest::CD->page(
+$it = DBICTest::CD->search(
{ title => [
-and =>
{
--- /dev/null
+use strict;\r
+use Test::More;\r
+\r
+BEGIN {\r
+ eval "use DBD::SQLite";\r
+ plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 6);\r
+}\r
+\r
+use lib 't/testlib';\r
+use Film;\r
+\r
+my @film = (\r
+ Film->create({ Title => 'Film 1' }),\r
+ Film->create({ Title => 'Film 2' }),\r
+ Film->create({ Title => 'Film 3' }),\r
+ Film->create({ Title => 'Film 4' }),\r
+ Film->create({ Title => 'Film 5' }),\r
+);\r
+\r
+# first page\r
+my ( $pager, $it ) = Film->page(\r
+ {},\r
+ { rows => 3,\r
+ page => 1 }\r
+);\r
+\r
+is( $pager->entries_on_this_page, 3, "entries_on_this_page ok" );\r
+\r
+is( $pager->next_page, 2, "next_page ok" );\r
+\r
+is( $it->next->title, "Film 1", "iterator->next ok" );\r
+\r
+$it->next;\r
+$it->next;\r
+\r
+is( $it->next, undef, "next past end of page ok" );\r
+\r
+# second page\r
+( $pager, $it ) = Film->page( \r
+ {},\r
+ { rows => 3,\r
+ page => 2 }\r
+);\r
+\r
+is( $pager->entries_on_this_page, 2, "entries on second page ok" );\r
+\r
+is( $it->next->title, "Film 4", "second page first title ok" );\r