File Coverage

blib/lib/Teng/Plugin/SQLPager.pm
Criterion Covered Total %
statement 29 29 100.0
branch 4 6 66.6
condition 1 3 33.3
subroutine 5 5 100.0
pod 1 1 100.0
total 40 44 90.9


line stmt bran cond sub pod time code
1             use strict;
2 1     1   841 use warnings;
  1         2  
  1         29  
3 1     1   5 use utf8;
  1         1  
  1         31  
4 1     1   5  
  1         2  
  1         12  
5             our @EXPORT = qw/search_by_sql_with_pager/;
6              
7             use Data::Page::NoTotalEntries;
8 1     1   416  
  1         521  
  1         223  
9             my ($self, $sql, $binds, $opt, $table_name) = @_;
10             $table_name ||= $self->_guess_table_name( $sql );
11 2     2 1 3810  
12 2   33     17 my $page = 0+$opt->{page};
13             my $entries_per_page = 0+$opt->{rows};
14 2         3 my $offset = ( $page - 1 ) * $entries_per_page;
15 2         4  
16 2         3 $sql .= " LIMIT @{[ $entries_per_page + 1 ]} OFFSET $offset";
17              
18 2         4 my $sth = $self->dbh->prepare($sql) or Carp::croak $self->dbh->errstr;
  2         8  
19             $sth->execute(@$binds) or Carp::croak $self->dbh->errstr;
20 2 50       8  
21 2 50       445 my $itr = Teng::Iterator->new(
22             teng => $self,
23 2         12 sth => $sth,
24             sql => $sql,
25             row_class => $self->schema->get_row_class($table_name),
26             table_name => $table_name,
27             suppress_object_creation => $self->suppress_row_objects,
28             );
29             my $rows = [$itr->all];
30             my $has_next = 0;
31 2         8 if (@$rows == $entries_per_page + 1) {
32 2         3 pop @$rows;
33 2 100       5 $has_next++;
34 1         2 }
35 1         5  
36             my $pager = Data::Page::NoTotalEntries->new(
37             entries_per_page => $entries_per_page,
38 2         7 current_page => $page,
39             has_next => $has_next,
40             );
41              
42             return ($rows, $pager);
43             }
44 2         41  
45              
46             1;
47              
48             =head1 NAME
49              
50             Teng::Plugin::SQLPager - Paginate with SQL
51              
52             =head1 SYNOPSIS
53              
54             package My::DB;
55             use parent qw/Teng/;
56             __PACKAGE__->load_plugin(qw/SQLPager/);
57              
58             # in your application
59             $db->search_by_sql_with_pager(
60             q{SELECT * FROM member ORDER BY id DESC},
61             [],
62             {page => 1, rows => 20}
63             );
64              
65             =head1 DESCRIPTION
66              
67             This module searches database by SQL with paginate.
68              
69             search_by_sql_with_pager method adds LIMIT clause automatically.
70              
71             =head1 ARGUMENTS FOR search_by_sql_with_pager
72              
73             You can pass arguments as following.
74              
75             =head2 C<< $db->search_by_sql_with_pager($sql, $binds, $opt[, $table_name]); >>
76              
77             =over 4
78              
79             =item $sql: Str
80              
81             This is a SQL statement in string.
82              
83             =item $binds: ArrayRef[Str]
84              
85             This is a bind values in arrayref.
86              
87             =item $opt: HashRef
88              
89             Options for search_by_sql_with_pager. Important options are 'page' and 'rows'.
90              
91             B<page> is a current page number. B<rows> is a entries per page.
92              
93             =item $table_name: Str
94              
95             You can pass a table name.
96              
97             This argument is optional. If you don't pass a table name, Teng guess table name automatically.
98              
99             =back
100              
101             =head1 LIMITATIONS
102              
103             This module does not work with Oracle since Oracle does not support limit clause.
104