Created
December 4, 2011 16:50
-
-
Save ktat/1430656 to your computer and use it in GitHub Desktop.
Teng::Plugin::Pager::MySQLFoundRows::WithSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Teng::Plugin::Pager::MySQLFoundRows::WithSQL; | |
use strict; | |
use warnings; | |
use utf8; | |
use Carp (); | |
use Teng::Iterator; | |
use Data::Page; | |
our @EXPORT = qw/search_by_sql_with_pager/; | |
sub search_by_sql_with_pager { | |
my ($self, $table_name, $sql, $binds, $opt) = @_; | |
my $table = $self->schema->get_table($table_name) or Carp::croak("'$table_name' is unknown table"); | |
my $page = $opt->{page}; | |
my $rows = $opt->{rows}; | |
for (qw/page rows/) { | |
Carp::croak("missing mandatory parameter: $_") unless exists $opt->{$_}; | |
} | |
$sql .= " LIMIT $rows OFFSET " . $rows * ($page - 1); | |
$sql =~s{^\s*SELECT }{SELECT SQL_CALC_FOUND_ROWS }i; | |
my $sth = $self->dbh->prepare($sql) or Carp::croak $self->dbh->errstr; | |
$sth->execute(@$binds) or Carp::croak $self->dbh->errstr; | |
my $total_entries = $self->dbh->selectrow_array(q{SELECT FOUND_ROWS()}); | |
my $itr = Teng::Iterator->new( | |
teng => $self, | |
sth => $sth, | |
sql => $sql, | |
row_class => $self->schema->get_row_class($table_name), | |
table_name => $table_name, | |
suppress_object_creation => $self->suppress_row_objects, | |
); | |
my $pager = Data::Page->new(); | |
$pager->entries_per_page($rows); | |
$pager->current_page($page); | |
$pager->total_entries($total_entries); | |
return ([$itr->all], $pager); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment