line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package |
3
|
|
|
|
|
|
|
Class::DBI::Lite::Dataset; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
7
|
1
|
|
|
1
|
|
3
|
use Carp 'confess'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
8
|
1
|
|
|
1
|
|
13
|
use POSIX 'ceil'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
9
|
1
|
|
|
1
|
|
53
|
use SQL::Abstract; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
548
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new |
13
|
|
|
|
|
|
|
{ |
14
|
1
|
|
|
1
|
0
|
6
|
my ($class, %args) = @_; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
|
|
2
|
foreach(qw( sort_field sort_dir page_number page_size filters )) |
17
|
|
|
|
|
|
|
{ |
18
|
|
|
|
|
|
|
confess "Required param '$_' was not provided" |
19
|
5
|
50
|
|
|
|
12
|
unless $args{$_}; |
20
|
|
|
|
|
|
|
}# end foreach() |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# One of the following must be present: |
23
|
1
|
50
|
33
|
|
|
9
|
unless( $args{type} || ( $args{data_sql} && $args{count_sql} ) ) |
|
|
|
33
|
|
|
|
|
24
|
|
|
|
|
|
|
{ |
25
|
0
|
|
|
|
|
0
|
confess "Either type OR data_sql AND count_sql must be provided"; |
26
|
|
|
|
|
|
|
}# end unless() |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
|
|
2
|
my $s = bless \%args, $class; |
29
|
1
|
|
|
|
|
3
|
$s->init; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
2
|
return $s; |
32
|
|
|
|
|
|
|
}# end new() |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
1
|
0
|
|
sub init { } |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub execute |
39
|
|
|
|
|
|
|
{ |
40
|
1
|
|
|
1
|
0
|
446
|
my ($s, $dbh) = @_; |
41
|
|
|
|
|
|
|
|
42
|
1
|
50
|
|
|
|
4
|
confess "Usage: \$ds->execute(\$dbh)" unless $dbh; |
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
2
|
my ($filters, @vals) = $s->where; |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
179
|
my $count_sql = $s->count_sql( $filters ); |
47
|
1
|
50
|
|
|
|
3
|
warn "CountSQL($count_sql)" if $s->{debug}; |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
3
|
my $data_sql = $s->data_sql( $filters ); |
50
|
1
|
50
|
|
|
|
2
|
warn "DataSQL($data_sql)" if $s->{debug}; |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
5
|
my $count_sth = $dbh->prepare( $count_sql ); |
53
|
1
|
|
|
|
|
182
|
$count_sth->execute( @vals ); |
54
|
1
|
|
|
|
|
21
|
my ($total_count) = $count_sth->fetchrow; |
55
|
1
|
|
|
|
|
4
|
$count_sth->finish(); |
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
9
|
my $page_count = $s->_get_page_count( $s->{page_size}, $total_count ); |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
4
|
my $data_sth = $dbh->prepare( $data_sql ); |
60
|
1
|
|
|
|
|
839
|
$data_sth->execute( @vals ); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
return { |
63
|
|
|
|
|
|
|
sth => $data_sth, |
64
|
|
|
|
|
|
|
count_sql => $count_sql, |
65
|
|
|
|
|
|
|
data_sql => $data_sql, |
66
|
|
|
|
|
|
|
total_items => $total_count, |
67
|
|
|
|
|
|
|
page_number => $s->{page_number}, |
68
|
|
|
|
|
|
|
page_size => $s->{page_size}, |
69
|
|
|
|
|
|
|
sort_field => $s->{sort_field}, |
70
|
|
|
|
|
|
|
sort_dir => $s->{sort_dir}, |
71
|
|
|
|
|
|
|
sql_args => \@vals, |
72
|
|
|
|
|
|
|
page_count => $page_count, |
73
|
|
|
|
|
|
|
show_prev => $s->{page_number} > 1, |
74
|
1
|
|
|
|
|
24
|
show_next => $s->{page_number} < $page_count, |
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
}# end execute() |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub where |
80
|
|
|
|
|
|
|
{ |
81
|
1
|
|
|
1
|
0
|
2
|
my $s = shift; |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
6
|
return SQL::Abstract->new->where( $s->{filters} ); |
84
|
|
|
|
|
|
|
}# end where() |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub count_sql |
88
|
|
|
|
|
|
|
{ |
89
|
1
|
|
|
1
|
0
|
7
|
my ($s, $filters) = @_; |
90
|
|
|
|
|
|
|
|
91
|
1
|
50
|
|
|
|
12
|
if( $s->{count_sql} ) |
92
|
|
|
|
|
|
|
{ |
93
|
1
|
50
|
|
|
|
4
|
return $filters ? join " ", ( $s->{count_sql}, $filters ) : $s->{count_sql}; |
94
|
|
|
|
|
|
|
}# end if() |
95
|
|
|
|
|
|
|
}# end count_sql() |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub data_sql |
99
|
|
|
|
|
|
|
{ |
100
|
1
|
|
|
1
|
0
|
2
|
my ($s, $filters) = @_; |
101
|
|
|
|
|
|
|
|
102
|
1
|
|
|
|
|
1
|
my $sql; |
103
|
1
|
50
|
|
|
|
4
|
if( $s->{data_sql} ) |
104
|
|
|
|
|
|
|
{ |
105
|
1
|
50
|
|
|
|
3
|
$sql = $filters ? join " ", ( $s->{data_sql}, $filters ) : $s->{data_sql}; |
106
|
|
|
|
|
|
|
}# end if() |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
2
|
$sql .= $s->_order_clause(); |
109
|
1
|
|
|
|
|
3
|
$sql .= $s->_limit_clause(); |
110
|
|
|
|
|
|
|
|
111
|
1
|
|
|
|
|
2
|
return $sql; |
112
|
|
|
|
|
|
|
}# end data_sql() |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _order_clause |
116
|
|
|
|
|
|
|
{ |
117
|
1
|
|
|
1
|
|
2
|
my $s = shift; |
118
|
|
|
|
|
|
|
|
119
|
1
|
|
|
|
|
2
|
return " ORDER BY @{[ $s->{sort_field} ]} @{[ $s->{sort_dir} ]}"; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4
|
|
120
|
|
|
|
|
|
|
}# end _order_clause() |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
#============================================================================== |
124
|
|
|
|
|
|
|
sub _limit_clause |
125
|
|
|
|
|
|
|
{ |
126
|
1
|
|
|
1
|
|
1
|
my $s = shift; |
127
|
|
|
|
|
|
|
|
128
|
1
|
50
|
|
|
|
4
|
my $offset = $s->{page_number} == 1 ? 0 : ($s->{page_number} - 1) * $s->{page_size}; |
129
|
1
|
50
|
|
|
|
3
|
my $limit = " LIMIT $offset, @{[ $s->{page_size} ]} " if $s->{page_size}; |
|
1
|
|
|
|
|
3
|
|
130
|
|
|
|
|
|
|
|
131
|
1
|
|
|
|
|
3
|
return $limit; |
132
|
|
|
|
|
|
|
}# end _limit_clause() |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub _get_page_count |
136
|
|
|
|
|
|
|
{ |
137
|
1
|
|
|
1
|
|
3
|
my ($s, $page_size, $total) = @_; |
138
|
1
|
50
|
33
|
|
|
7
|
if( ! $page_size || $total <= $page_size ) |
139
|
|
|
|
|
|
|
{ |
140
|
0
|
|
|
|
|
0
|
return 1; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
else |
143
|
|
|
|
|
|
|
{ |
144
|
1
|
|
|
|
|
10
|
return POSIX::ceil($total / $page_size); |
145
|
|
|
|
|
|
|
}# end if() |
146
|
|
|
|
|
|
|
}# end _get_page_count |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1;# return true: |
150
|
|
|
|
|
|
|
|