| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$args{"type"} or $args{"data_sql"} and $args{"count_sql"}
$args{"data_sql"} and $args{"count_sql"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | my $data_sql = $s->data_sql( $filters ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | warn "DataSQL($data_sql)" if $s->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | return $filters ? join " ", ( $s->{count_sql}, $filters ) : $s->{count_sql}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | $sql = $filters ? join " ", ( $s->{data_sql}, $filters ) : $s->{data_sql}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | my $limit = " LIMIT $offset, @{[ $s->{page_size} ]} " if $s->{page_size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
not $page_size or $total <= $page_size
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||