lib/Class/DBI/Lite/Dataset.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 50.0% 12 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 6 totaltotal 74.5% 91 / 122 CC 17 SCAR 13.2
CC
17
Cov
79%
new
8.1
execute
4.0
_get_page_count
3.8
CRAP 3.7
« lib/Class/DBI/Lite.pm ↑ Summary lib/Class/DBI/Lite/RootMeta.pm »
1  
2 package
3 Class::DBI::Lite::Dataset;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX 'ceil';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 sub new
13 1 ▶{
Subroutine new: called CC=6 SCAR=20.9
Pod undocumented
14 1 my ($class, %args) = @_;
15
16 1 foreach(qw( sort_field sort_dir page_number page_size filters ))
17 {
18 confess "Required param '$_' was not provided"
19 5 ▶ unless $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$_} 0 5 5
20 }# end foreach()
21
22 # One of the following must be present:
23 1 ▶ unless( $args{type} || ( $args{data_sql} && $args{count_sql} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"type"} or $args{"data_sql"} and $args{"count_sql"}) 0 1 1
Condition33%2/6
$args{"type"} or $args{"data_sql"} and $args{"count_sql"}
l!l&&r!l&&!r
010
$args{"data_sql"} and $args{"count_sql"}
!ll&&!rl&&r
001
MC/DC0%0/3
$args{"type"} or $args{"data_sql"} and $args{"count_sql"}
$args{"type"} $args{"data_sql"} $args{"count_sql"}
Truth table25%1/4
A or B and C
A= $args{"type"}B= $args{"data_sql"}C= $args{"count_sql"}
ABCresult
1XX1
0111
00X0
0100
24 {
25 0 confess "Either type OR data_sql AND count_sql must be provided";
26 }# end unless()
27
28 1 my $s = bless \%args, $class;
29 1 $s->init;
30
31 1 return $s;
32 }# end new()
33  
34  
35 1 ▶sub init { }
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
36  
37  
38 sub execute
39 1 ▶{
Subroutine execute: called CC=4 SCAR=14.0
Pod undocumented
40 1 my ($s, $dbh) = @_;
41
42 1 ▶ confess "Usage: \$ds->execute(\$dbh)" unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 1 1
43
44 1 my ($filters, @vals) = $s->where;
45
46 1 my $count_sql = $s->count_sql( $filters );
47 1 ▶ warn "CountSQL($count_sql)" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 1 1
48
49 1 my $data_sql = $s->data_sql( $filters );
50 1 ▶ warn "DataSQL($data_sql)" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 1 1
51
52 1 my $count_sth = $dbh->prepare( $count_sql );
53 1 $count_sth->execute( @vals );
54 1 my ($total_count) = $count_sth->fetchrow;
55 1 $count_sth->finish();
56
57 1 my $page_count = $s->_get_page_count( $s->{page_size}, $total_count );
58
59 1 my $data_sth = $dbh->prepare( $data_sql );
60 1 $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 show_next => $s->{page_number} < $page_count,
75 };
76 }# end execute()
77  
78  
79 sub where
80 1 ▶{
Subroutine where: called CC=1 SCAR=0.0
Pod undocumented
81 1 my $s = shift;
82
83 1 return SQL::Abstract->new->where( $s->{filters} );
84 }# end where()
85  
86  
87 sub count_sql
88 1 ▶{
Subroutine count_sql: called CC=3 SCAR=11.7
Pod undocumented
89 1 my ($s, $filters) = @_;
90
91 1 ▶ if( $s->{count_sql} )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"count_sql"}) 1 0 1
92 {
93 1 ▶ return $filters ? join " ", ( $s->{count_sql}, $filters ) : $s->{count_sql};
Branch: 1 branch
BranchTrueFalseTotal
$filters ? : 0 1 1
94 }# end if()
95 }# end count_sql()
96  
97  
98 sub data_sql
99 1 ▶{
Subroutine data_sql: called CC=3 SCAR=11.2
Pod undocumented
100 1 my ($s, $filters) = @_;
101
102 1 my $sql;
103 1 ▶ if( $s->{data_sql} )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"data_sql"}) 1 0 1
104 {
105 1 ▶ $sql = $filters ? join " ", ( $s->{data_sql}, $filters ) : $s->{data_sql};
Branch: 1 branch
BranchTrueFalseTotal
$filters ? : 0 1 1
106 }# end if()
107
108 1 $sql .= $s->_order_clause();
109 1 $sql .= $s->_limit_clause();
110
111 1 return $sql;
112 }# end data_sql()
113  
114  
115 sub _order_clause
116 1 ▶{
Subroutine _order_clause: called CC=1 SCAR=0.0
117 1 my $s = shift;
118
119 1 return " ORDER BY @{[ $s->{sort_field} ]} @{[ $s->{sort_dir} ]}";
120 }# end _order_clause()
121  
122  
123 #==============================================================================
124 sub _limit_clause
125 1 ▶{
Subroutine _limit_clause: called CC=3 SCAR=11.3
126 1 my $s = shift;
127
128 1 ▶ my $offset = $s->{page_number} == 1 ? 0 : ($s->{page_number} - 1) * $s->{page_size};
Branch: 1 branch
BranchTrueFalseTotal
$s->{'page_number'} == 1 ? : 1 0 1
129 1 ▶ my $limit = " LIMIT $offset, @{[ $s->{page_size} ]} " if $s->{page_size};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"page_size"} 1 0 1
130
131 1 return $limit;
132 }# end _limit_clause()
133  
134  
135 sub _get_page_count
136 1 ▶{
Subroutine _get_page_count: called CC=3 SCAR=13.3
137 1 my ($s, $page_size, $total) = @_;
138 1 ▶ if( ! $page_size || $total <= $page_size )
Branch: 1 branch
BranchTrueFalseTotal
if (not $page_size or $total <= $page_size) { } 0 1 1
Condition33%1/3
not $page_size or $total <= $page_size
l!l&&r!l&&!r
001
MC/DC0%0/2
not $page_size or $total <= $page_size
not $page_size $total <= $page_size
Truth table33%1/3
not(A) or B
A= not $page_sizeB= $total <= $page_size
ABresult
1X1
011
000
139 {
140 0 return 1;
141 }
142 else
143 {
144 1 return POSIX::ceil($total / $page_size);
145 }# end if()
146 }# end _get_page_count
147  
148  
149 1 1;# return true:
150  
« lib/Class/DBI/Lite.pm ↑ Summary lib/Class/DBI/Lite/RootMeta.pm »