line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package DBI::ResultPager; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
736
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
6
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
7
|
1
|
|
|
1
|
|
6406
|
use CGI qw/:standard/; |
|
1
|
|
|
|
|
15449
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
6404
|
use DBI; |
|
1
|
|
|
|
|
19529
|
|
|
1
|
|
|
|
|
80
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
BEGIN { |
11
|
1
|
|
|
1
|
|
14
|
use Exporter (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
49
|
|
12
|
1
|
|
|
1
|
|
3
|
our ($VERSION, @ISA); |
13
|
1
|
|
|
|
|
2
|
$VERSION = '0.9.2'; |
14
|
1
|
|
|
|
|
1518
|
@ISA = qw(Exporter); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
0
|
|
|
0
|
0
|
|
my $self = {}; |
20
|
0
|
|
|
|
|
|
$self->{DBH} = ''; |
21
|
0
|
|
|
|
|
|
$self->{QUERY} = ''; |
22
|
0
|
|
|
|
|
|
$self->{PERPAGE} = 30; |
23
|
0
|
|
|
|
|
|
$self->{DEFAULTORDER} = ''; |
24
|
0
|
|
|
|
|
|
$self->{ALIGN} = ''; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
my %fmt = (); |
27
|
0
|
|
|
|
|
|
my %hidden = (); |
28
|
0
|
|
|
|
|
|
my @cc; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
$self->{FORMATTERS} = \%fmt; |
31
|
0
|
|
|
|
|
|
$self->{CUSTOMCOLUMNS} = \@cc; |
32
|
0
|
|
|
|
|
|
$self->{HIDDENCOLUMNS} = \%hidden; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
bless($self); |
35
|
0
|
|
|
|
|
|
return $self; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub display { |
39
|
0
|
|
|
0
|
0
|
|
my ($self, @params) = (@_); |
40
|
0
|
|
|
|
|
|
my $rv = getOutput($self, @params); |
41
|
0
|
|
|
|
|
|
print $rv; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub getOutput { |
45
|
0
|
|
|
0
|
0
|
|
my ($self, @params) = (@_); |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $output = ''; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my @columns; |
50
|
0
|
|
|
|
|
|
my $page = 1; |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
if(defined(url_param('page'))) { |
53
|
0
|
|
|
|
|
|
$page = url_param('page'); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $hidden = $self->{HIDDENCOLUMNS}; |
57
|
0
|
|
|
|
|
|
my $query = $self->{QUERY}; |
58
|
0
|
|
|
|
|
|
my $perPage = $self->{PERPAGE}; |
59
|
0
|
|
|
|
|
|
my $offset = (($page - 1) * $perPage); |
60
|
0
|
|
|
|
|
|
my $dbh = $self->{DBH}; |
61
|
0
|
|
|
|
|
|
my $order = $self->{DEFAULTORDER}; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $ccref = $self->{CUSTOMCOLUMNS}; |
64
|
0
|
|
|
|
|
|
my @customColumns = @$ccref; |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
|
if($order ne '') { |
67
|
0
|
|
|
|
|
|
$query = $query . ' order by ' . $order; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
$query = $query . ' limit ' . ($perPage + 1); |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
if($page ne 1) { |
73
|
0
|
|
|
|
|
|
$query = $query . " offset $offset"; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($query); |
77
|
0
|
0
|
|
|
|
|
$sth->execute(@params) or die "Error in $query: $!\n"; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
$output .= '';
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
my $nref = $sth->{NAME}; |
82
|
0
|
|
|
|
|
|
my @names = @$nref; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Print the header. |
85
|
0
|
|
|
|
|
|
$output .= ' | ';
86
|
0
|
|
|
|
|
|
foreach(@names) { |
87
|
0
|
0
|
|
|
|
|
if(!defined($hidden->{$_})) { |
88
|
0
|
|
|
|
|
|
$output .= ' | ' . $_ . ' | ';
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
push(@columns, $_); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
foreach(@customColumns) { |
95
|
0
|
|
|
|
|
|
$output .= ' | ' . $_->{'columnName'} . ' | ';
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Add headers for any custom columns |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
$output .= " | \n";
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my $formatters = $self->{FORMATTERS}; |
103
|
0
|
|
|
|
|
|
my $count = 0; |
104
|
0
|
|
|
|
|
|
while(my @row = $sth->fetchrow_array()) { |
105
|
0
|
|
|
|
|
|
$count++; |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
if($count > $perPage) { next; } |
|
0
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my $color = ""; |
110
|
0
|
0
|
|
|
|
|
if( ($count %2) eq 1) { |
111
|
0
|
|
|
|
|
|
$color = ' bgcolor="#EEEEEE"'; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
$output .= " | ";
115
|
0
|
|
|
|
|
|
my $colcount = 0; |
116
|
0
|
|
|
|
|
|
my $al = ''; |
117
|
0
|
|
|
|
|
|
my $align = $self->{ALIGN}; |
118
|
0
|
0
|
|
|
|
|
if($align ne '') { |
119
|
0
|
|
|
|
|
|
$al = ' valign="' . $align . '"'; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
foreach(@row) { |
123
|
0
|
|
|
|
|
|
$colcount++; |
124
|
0
|
|
|
|
|
|
my $colname = $columns[$colcount - 1]; |
125
|
|
|
|
|
|
|
|
126
|
0
|
0
|
|
|
|
|
if(defined($hidden->{$colname})) { next; } |
|
0
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
$output .= ' | '; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Check if this column has a custom formatter |
131
|
0
|
0
|
|
|
|
|
if(defined($formatters->{$colname})) { |
132
|
0
|
|
|
|
|
|
my $subref = $formatters->{$colname}; |
133
|
0
|
|
|
|
|
|
$output .= &$subref($_, \@row); |
134
|
|
|
|
|
|
|
} else { |
135
|
0
|
|
|
|
|
|
$output .= $_; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
$output .= ' | ';
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
foreach(@customColumns) { |
142
|
0
|
|
|
|
|
|
$output .= ' | '; |
143
|
0
|
|
|
|
|
|
my $cref = $_->{'codeRef'}; |
144
|
0
|
|
|
|
|
|
$output .= &$cref(@row) . ' | ';
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
$output .= " | \n";
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
$output .= ' | '; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
my $prev = 0; |
153
|
0
|
0
|
|
|
|
|
if($offset ne 0) { |
154
|
0
|
|
|
|
|
|
my $u = manglePage($page - 1); |
155
|
0
|
|
|
|
|
|
$output .= 'Previous Page'; |
156
|
0
|
|
|
|
|
|
$prev = 1; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
if($count > $perPage) { |
160
|
|
|
|
|
|
|
|
161
|
0
|
0
|
|
|
|
|
if($prev ne 0) { |
162
|
0
|
|
|
|
|
|
$output .= ' | '; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
my $u = manglePage($page + 1); |
166
|
0
|
|
|
|
|
|
$output .= 'Next Page'; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
$output .= ''; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub alignRows { |
173
|
0
|
|
|
0
|
0
|
|
my ($self, $align) = (@_); |
174
|
0
|
|
|
|
|
|
$self->{ALIGN} = $align; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# Returns the current URL with the given page number. |
178
|
|
|
|
|
|
|
# FIXME: URL params only |
179
|
|
|
|
|
|
|
sub manglePage { |
180
|
0
|
|
|
0
|
0
|
|
my ($page) = (@_); |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
my $u = url(-relative=>1, -query=>1); |
183
|
0
|
0
|
|
|
|
|
if($u =~ /[\?&;]page=/) { |
184
|
0
|
|
|
|
|
|
$u =~ s/[\?&;]page=[0-9]*//; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
0
|
0
|
|
|
|
|
if($u =~ /\?/) { |
188
|
0
|
|
|
|
|
|
$u = $u . '&page=' . $page; |
189
|
|
|
|
|
|
|
} else { |
190
|
0
|
|
|
|
|
|
$u = $u . '?page=' . $page; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
return $u; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub hideColumn { |
197
|
0
|
|
|
0
|
0
|
|
my ($self, $columnName) = (@_); |
198
|
0
|
|
|
|
|
|
my $ar = $self->{HIDDENCOLUMNS}; |
199
|
0
|
|
|
|
|
|
$ar->{$columnName} = 1; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub addCustomColumn { |
203
|
0
|
|
|
0
|
0
|
|
my ($self, $columnName, $codeRef, $identityColumn) = (@_); |
204
|
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
my %cc = (); |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
$cc{'columnName'} = $columnName; |
208
|
0
|
|
|
|
|
|
$cc{'codeRef'} = $codeRef; |
209
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
my $ar = $self->{CUSTOMCOLUMNS}; |
211
|
0
|
|
|
|
|
|
push(@$ar, \%cc); # Push the hashref onto the arrayref. |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub addColumnFormatter { |
215
|
0
|
|
|
0
|
1
|
|
my ($self, $column, $formatref) = (@_); |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
my $formatters = $self->{FORMATTERS}; |
218
|
0
|
|
|
|
|
|
$formatters->{$column} = $formatref; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub defaultOrder { |
222
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
223
|
0
|
0
|
|
|
|
|
if(@_) { $self->{DEFAULTORDER} = shift; } |
|
0
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
return $self->{DEFAULTORDER}; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
sub dbh { |
228
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
229
|
0
|
0
|
|
|
|
|
if(@_) { $self->{DBH} = shift; } |
|
0
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
return $self->{DBH}; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
sub perPage { |
234
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
235
|
0
|
0
|
|
|
|
|
if(@_) { $self->{PERPAGE} = shift; } |
|
0
|
|
|
|
|
|
|
236
|
0
|
|
|
|
|
|
return $self->{PERPAGE}; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
sub query { |
240
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
241
|
0
|
0
|
|
|
|
|
if(@_) { $self->{QUERY} = shift; } |
|
0
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
return $self->{QUERY}; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
1; |
246
|
|
|
|
|
|
|
__END__ |