Teng/Plugin/Pager/MySQLFoundRows.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 63 CC 2 SCAR 11.0
CC
2
Cov
0%
search_with_pager
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.0
1 package Teng::Plugin::Pager::MySQLFoundRows;
2 use strict;
3 use warnings;
4 use utf8;
5 use Data::Page;
6 use Teng::Iterator;
7 use Carp ();
8  
9 our @EXPORT = qw/search_with_pager/;
10  
11 sub search_with_pager {
12 my ($self, $table_name, $where, $opt) = @_;
13  
14 my $table = $self->schema->get_table($table_name) or Carp::croak("'$table_name' is unknown table");
15  
16 my $page = $opt->{page};
17 my $rows = $opt->{rows};
18  
19 my $columns = $opt->{'+columns'}
20 ? [@{$table->{columns}}, @{$opt->{'+columns'}}]
21 : ($opt->{columns} || $table->{columns})
22 ;
23  
24 my ($sql, @binds) = $self->sql_builder->select(
25 $table_name,
26 $columns,
27 $where,
28 +{
29 %$opt,
30 limit => $rows,
31 offset => $rows*($page-1),
32 prefix => 'SELECT SQL_CALC_FOUND_ROWS ',
33 }
34 );
35 my $sth = $self->dbh->prepare($sql) or Carp::croak $self->dbh->errstr;
36 $sth->execute(@binds) or Carp::croak $self->dbh->errstr;
37 my $total_entries = $self->dbh->selectrow_array(q{SELECT FOUND_ROWS()});
38  
39 my $itr = Teng::Iterator->new(
40 teng => $self,
41 sth => $sth,
42 sql => $sql,
43 row_class => $self->schema->get_row_class($table_name),
44 table => $table,
45 table_name => $table_name,
46 suppress_object_creation => $self->suppress_row_objects,
47 );
48  
49 my $pager = Data::Page->new();
50 $pager->entries_per_page($rows);
51 $pager->current_page($page);
52 $pager->total_entries($total_entries);
53  
54 return ([$itr->all], $pager);
55 }
56  
57 1;
58 __END__
59  
60 =for test_synopsis
61 my ($c, $dbh);
62  
63 =head1 NAME
64  
65 Teng::Plugin::Pager::MySQLFoundRows - Paginate with SQL_CALC_FOUND_ROWS
66  
67 =head1 SYNOPSIS
68  
69 package MyApp::DB;
70 use parent qw/Teng/;
71 __PACKAGE__->load_plugin('Pager::MySQLFoundRows');
72  
73 package main;
74 my $db = MyApp::DB->new(dbh => $dbh);
75 my $page = $c->req->param('page') || 1;
76 my ($rows, $pager) = $db->search_with_pager('user' => {type => 3}, {page => $page, rows => 5});
77  
78 =head1 DESCRIPTION
79  
80 This is a helper class for pagination. This helper only supports B<MySQL>.
81 Since this plugin uses SQL_CALC_FOUND_ROWS for calculate total entries.
82  
83 =head1 METHODS
84  
85 =over 4
86  
87 =item my (\@rows, $pager) = $db->search_with_pager($table, \%where, \%opts);
88  
89 Select from database with pagination.
90  
91 The arguments are mostly same as C<$db->search()>. But two additional options are available.
92  
93 =over 4
94  
95 =item $opts->{page}
96  
97 Current page number.
98  
99 =item $opts->{rows}
100  
101 The number of entries per page.
102  
103 =back
104  
105 This method returns ArrayRef[Teng::Row] and instance of L<Teng::Plugin::Pager::Page>.
106  
107 =back
108  
109 =head1 AUTHOR
110  
111 Tokuhiro Matsuno
112