line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::Pager; |
2
|
|
|
|
|
|
|
# $Id: Pager.pm,v 1.2 2002/08/06 02:06:14 ikechin Exp $ |
3
|
2
|
|
|
2
|
|
47542
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
68
|
|
4
|
2
|
|
|
2
|
|
9
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
174
|
|
5
|
2
|
|
|
2
|
|
8
|
use vars qw($VERSION); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
117
|
|
6
|
2
|
|
|
2
|
|
9
|
use base qw(Class::Accessor); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1721
|
|
7
|
2
|
|
|
2
|
|
5282
|
use POSIX (); |
|
2
|
|
|
|
|
12017
|
|
|
2
|
|
|
|
|
866
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(total limit offset)); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = '0.02'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
15
|
0
|
|
|
|
|
|
my %args = @_; |
16
|
0
|
|
0
|
|
|
|
my $self = bless { |
17
|
|
|
|
|
|
|
dsn => $args{dsn}, |
18
|
|
|
|
|
|
|
user => $args{user}, |
19
|
|
|
|
|
|
|
password => $args{password}, |
20
|
|
|
|
|
|
|
dbh => $args{dbh}, |
21
|
|
|
|
|
|
|
table => $args{table}, |
22
|
|
|
|
|
|
|
offset => $args{offset} || 0, |
23
|
|
|
|
|
|
|
limit => $args{limit}, |
24
|
|
|
|
|
|
|
where_clause => $args{where_clause}, |
25
|
|
|
|
|
|
|
}, $class; |
26
|
0
|
0
|
|
|
|
|
if ($self->{dsn}) { |
27
|
0
|
|
|
|
|
|
eval " require DBI; "; |
28
|
0
|
0
|
|
|
|
|
$self->{dbh} = DBI->connect( |
29
|
|
|
|
|
|
|
$self->{dsn}, $self->{user}, $self->{password}, |
30
|
|
|
|
|
|
|
{ |
31
|
|
|
|
|
|
|
RaiseError => 1, |
32
|
|
|
|
|
|
|
PrintError => 1 |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
) or die $DBI::errstr; |
35
|
|
|
|
|
|
|
} |
36
|
0
|
|
|
|
|
|
$self->_load_page_info; |
37
|
0
|
|
|
|
|
|
$self; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _load_page_info { |
41
|
0
|
|
|
0
|
|
|
my $self = shift; |
42
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
43
|
0
|
|
|
|
|
|
my $table = $self->{table}; |
44
|
0
|
|
|
|
|
|
my $sth; |
45
|
0
|
0
|
|
|
|
|
if (ref $self->{where_clause} eq 'ARRAY') { |
46
|
0
|
|
|
|
|
|
my @args = @{$self->{where_clause}}; |
|
0
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $where = shift @args; |
48
|
0
|
|
|
|
|
|
$sth = $dbh->prepare("SELECT COUNT(*) FROM $table $where"); |
49
|
0
|
|
|
|
|
|
$sth->execute(@args) |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
0
|
|
0
|
|
|
|
my $where = $self->{where_clause} || ""; |
53
|
0
|
|
|
|
|
|
$sth = $dbh->prepare("SELECT COUNT(*) FROM $table $where"); |
54
|
0
|
|
|
|
|
|
$sth->execute; |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
my $count = $sth->fetchrow_arrayref->[0]; |
57
|
0
|
|
|
|
|
|
$sth->finish; |
58
|
0
|
|
|
|
|
|
$dbh->disconnect; |
59
|
0
|
|
|
|
|
|
$self->{total} = $count; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub has_next { |
63
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
64
|
0
|
0
|
|
|
|
|
if ($self->{total} > $self->{offset} + $self->{limit}) { |
65
|
0
|
|
|
|
|
|
return 1; |
66
|
|
|
|
|
|
|
} |
67
|
0
|
|
|
|
|
|
return 0; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub has_prev { |
71
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
72
|
0
|
0
|
|
|
|
|
return $self->{offset} ? 1 : 0; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub next_offset { |
76
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
77
|
0
|
|
|
|
|
|
return $self->{offset} + $self->{limit}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub prev_offset { |
81
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
82
|
0
|
|
|
|
|
|
my $prev = $self->{offset} - $self->{limit}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub page_count { |
86
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
87
|
0
|
|
|
|
|
|
return POSIX::ceil($self->{total} / $self->{limit}); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub current_page { |
91
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
92
|
0
|
|
|
|
|
|
return int($self->{offset} / $self->{limit}) + 1; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
__END__ |