Paged queries for cdbi
Adds 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.
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
Loads the \*(C`pager\*(C' method into the \s-1CDBI\s0 app.
Also accepts named arguments: where => $where, abstract_attr => $attr, order_by => $order_by, per_page => $per_page, page => $page, syntax => $syntax Returns a pager object. This subclasses Data::Page. Note that for positional arguments, $abstract_attr can only be passed if preceded by a $where argument. $abstract_attr can contain the $order_by setting (just as in SQL::Abstract).
The named arguments all exist as get/set methods.
A hashref specifying the query. See SQL::Abstract.
A hashref specifying extra options to be passed through to the SQL::Abstract constructor.
Single column name or arrayref of column names for the \s-1ORDER\s0 \s-1BY\s0 clause. Defaults to the primary key(s) if not set.
Number of results per page.
The pager will retrieve results just for this page. Defaults to 1.
Change the way the 'limit' clause is constructed. See \*(C`set_syntax\*(C'. Default is \*(C`LimitOffset\*(C'.
Retrieves results from the pager. Accepts the same arguments as the \*(C`pager\*(C' method.
Convenience method, generates a \s-1WHERE\s0 clause that matches all rows from the table. Accepts the same arguments as the \*(C`pager\*(C' or \*(C`search_where\*(C' methods, except that no \s-1WHERE\s0 clause should be specified. Note that the argument parsing routine called by the \*(C`pager\*(C' method cannot cope with positional arguments that lack a \s-1WHERE\s0 clause, so either use named arguments, or the 'bit by bit' approach, or pass the arguments directly to \*(C`retrieve_all\*(C'. Changes the syntax used to generate the \*(C`limit\*(C' or other phrase that restricts the results set to the required page. The syntax is implemented as a method called on the pager, which can be queried to provide the $rows and $offset parameters (see the subclasses included in this distribution).
A class with a \*(C`make_limit\*(C' method. Name of a class in the \*(C`Class::DBI::Plugin::Pager::\*(C' namespace, which has a \*(C`make_limit\*(C' method. Will be called as a method on the pager object, so receives the pager as its argument.
Called without args, will default to \*(C`LimitOffset\*(C', which causes Class::DBI::Plugin::Pager::LimitOffset to be used.
This is called automatically when you call \*(C`pager\*(C', and attempts to set the syntax automatically. If you are using a subclass of the pager, this method will not be called. Will \*(C`die\*(C' if using Oracle or \s-1DB2\s0, since there is no simple syntax for limiting the results set. \s-1DB2\s0 has a \*(C`FETCH\*(C' keyword, but that seems to apply to a cursor and I don't know if there is a cursor available to the pager. There should probably be others to add to the unsupported list. Supports the following drivers: DRIVER CDBI::P::Pager subclass my %supported = ( pg => 'LimitOffset', mysql => 'LimitOffset', # older versions need LimitXY sqlite => 'LimitOffset', # or LimitYX sqlite2 => 'LimitOffset', # or LimitYX interbase => 'RowsTo', firebird => 'RowsTo', ); Older versions of MySQL should use the LimitXY syntax. You'll need to set it manually, either by \*(C`use CDBI::P::Pager::LimitXY\*(C', or by passing \*(C`syntax => 'LimitXY'\*(C' to a method call, or call \*(C`set_syntax\*(C' directly. Any driver not in the supported or unsupported lists defaults to LimitOffset. Any additions to the supported and unsupported lists gratefully received.
The '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 \*(C`use base\*(C' and switch syntax by calling \*(C`$pager->set_syntax( 'RowsTo' )\*(C'. Or you can leave in the \*(C`use base\*(C' and still say \*(C`$pager->set_syntax( 'RowsTo' )\*(C', because in this case the class is \*(C`require\*(C'd and the \*(C`import\*(C' in the base class doesn't get called. Or something. At any rate, It Works.
The subclasses implement the following \s-1LIMIT\s0 syntaxes:
LIMIT $rows OFFSET $offset This is the default if your driver is not in the list of known drivers. This should work for PostgreSQL, more recent MySQL, SQLite, and maybe some others.
LIMIT $offset, $rows Older versions of MySQL.
LIMIT $rows, $offset SQLite.
ROWS $offset TO $offset + $rows InterBase, also FireBird, maybe others?
I'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 \*(C`set_sql\*(C' to build the complex queries required by some databases to emulate \s-1LIMIT\s0 (see notes in source).
This class can't implement the subselect mechanism required by some databases to emulate the \s-1LIMIT\s0 phrase, because it only has access to the \s-1WHERE\s0 clause, not the whole \s-1SQL\s0 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 \*(C`order_by\*(C' 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.
The source code for this module is hosted on GitHub https://github.com/majesticcpan/class-dbi-plugin-pager <https://github.com/majesticcpan/class-dbi-plugin-pager>. Feel free to fork the repository and submit pull requests!
SQL::Abstract, Data::Page, Class::DBI::Plugin::AbstractCount, Class::Accessor, Class::Data::Inheritable, Carp.
Class::DBI::Pager does a similar job, but retrieves the entire results set into memory before chopping out the page you want.
Please report all bugs via the \s-1CPAN\s0 Request Tracker at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-DBI-Plugin-Pager <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-DBI-Plugin-Pager>.
Copyright 2004-2012 by David Baird.
Copyright 2012 Nikolay S. \*(C`[email protected]\*(C'
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
David Baird