Teng/Plugin/Pager.pm untested

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