|
NAMEClass::DBI::Plugin::Pager - paged queries for CDBI DESCRIPTIONAdds a pager method to your class that can query using SQL::Abstract where clauses, and limit the number of rows returned to a specific subset. SYNOPSIS package CD;
use base 'Class::DBI';
use Class::DBI::Plugin::AbstractCount; # pager needs this
use Class::DBI::Plugin::Pager;
# or to use a different syntax
# use Class::DBI::Plugin::Pager::RowsTo;
__PACKAGE__->set_db(...);
# in a nearby piece of code...
use CD;
# see SQL::Abstract for how to specify the query
my $where = { ... };
my $order_by => [ qw( foo bar ) ];
# bit by bit:
my $pager = CD->pager;
$pager->per_page( 10 );
$pager->page( 3 );
$pager->where( $where );
$pager->order_by( $order_by );
$pager->set_syntax( 'RowsTo' );
my @cds = $pager->search_where;
# or all at once
my $pager = CD->pager( $where, $order_by, 10, 3 );
my @cds = $pager->search_where;
# or
my $pager = CD->pager;
my @cds = $pager->search_where( $where, $order_by, 10, 3 );
# $pager isa Data::Page
# @cds contains the CDs just for the current page
METHODS
SUBCLASSINGThe 'limit' syntax can be set by using a subclass, e.g. use Class::DBI::Plugin::Pager::RowsTo; instead of setting at runtime. A subclass looks like this: package Class::DBI::Plugin::Pager::RowsTo;
use base 'Class::DBI::Plugin::Pager';
sub make_limit {
my ( $self ) = @_;
my $offset = $self->skipped;
my $rows = $self->entries_per_page;
my $last = $rows + $offset;
return "ROWS $offset TO $last";
}
1;
You can omit the "use base" and switch syntax by calling "$pager->set_syntax( 'RowsTo' )". Or you can leave in the "use base" and still say "$pager->set_syntax( 'RowsTo' )", because in this case the class is "require"d and the "import" in the base class doesn't get called. Or something. At any rate, It Works. The subclasses implement the following LIMIT syntaxes:
TODOI've only used this on an older version of MySQL. Reports of this thing working (or not) elsewhere would be useful. It should be possible to use "set_sql" to build the complex queries required by some databases to emulate LIMIT (see notes in source). CAVEATSThis class can't implement the subselect mechanism required by some databases to emulate the LIMIT phrase, because it only has access to the WHERE clause, not the whole SQL statement. At the moment. Each query issues two requests to the database - the first to count the entire result set, the second to retrieve the required subset of results. If your tables are small it may be quicker to use Class::DBI::Pager. The "order_by" clause means the database has to retrieve (internally) and sort the entire results set, before chopping out the requested subset. It's probably a good idea to have an index on the column(s) used to order the results. For huge tables, this approach to paging may be too inefficient. SOURCE CODEThe source code for this module is hosted on GitHub <https://github.com/majesticcpan/class-dbi-plugin-pager>. Feel free to fork the repository and submit pull requests! DEPENDENCIESSQL::Abstract, Data::Page, Class::DBI::Plugin::AbstractCount, Class::Accessor, Class::Data::Inheritable, Carp. SEE ALSOClass::DBI::Pager does a similar job, but retrieves the entire results set into memory before chopping out the page you want. BUGSPlease report all bugs via the CPAN Request Tracker at <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-DBI-Plugin-Pager>. COPYRIGHT AND LICENSECopyright 2004-2012 by David Baird. Copyright 2012 Nikolay S. "majestic@cpan.org" This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. AUTHORDavid Baird
|