blib/lib/Class/DBI/Plugin/AbstractCount.pm

filter: Statementstmt 95.9% 47 / 49 Branchbran 79.1% 19 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 50.0% 1 / 2 totaltotal 86.0% 74 / 86 CC 17 SCAR 26.5
CC
17
Cov
88%
count_search_where
18.0
BEGIN
1.0
BEGIN
1.0
CRAP 14.1
↑ Summary
1 package Class::DBI::Plugin::AbstractCount;
2 # vim:set tabstop=2 shiftwidth=2 expandtab:
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base 'Class::DBI::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.08';
9  
10 sub init
11 3 ▶{
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
12 3 my $class = shift;
13 3 $class->set_sql( count_search_where => qq{
14 SELECT COUNT(*)
15 FROM __TABLE__
16 %s
17 } );
18 }
19  
20 10 ▶sub count_search_where : Plugged
Subroutine count_search_where: called CC=17 SCAR=28.9
Pod documented
21 {
22 10 my $class = shift;
23 10 my %where = ();
24 10 my $rh_attr = {};
25 10 ▶ if ( ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 9 1 10
26 9 ▶ $class->_croak( "where-clause must be a hashref it it's a reference" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 9 9
27 unless ref( $_[0] ) eq 'HASH';
28 9 %where = %{ $_[0] };
29 9 $rh_attr = $_[1];
30 }
31 else {
32 1 ▶ $rh_attr = pop if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
33 1 %where = @_;
34 }
35 10 delete $rh_attr->{order_by};
36  
37 10 ▶ $class->can( 'retrieve_from_sql' )
Branch: 1 branch
BranchTrueFalseTotal
$class->can("retrieve_from_sql") or $class->_croak("$class should inherit from Class::DBI >= 0.95") 10 0 10
38 or $class->_croak( "$class should inherit from Class::DBI >= 0.95" );
39
40 10 my ( %columns, %accessors ) = ();
41 10 for my $column ( $class->columns ) {
42 40 ++$columns{ $column };
43 40 $accessors{ $column->accessor } = $column;
44 }
45  
46 10 COLUMN: for my $column ( keys %where ) {
47 # Column names are (of course) OK
48 20 ▶ next COLUMN if exists $columns{ $column };
Branch: 1 branch
BranchTrueFalseTotal
if exists $columns{$column} 8 12 20
49  
50 # Accessor names are OK, but replace them with corresponding column name
51 $where{ $accessors{ $column }} = delete $where{ $column }, next COLUMN
52 12 ▶ if exists $accessors{ $column };
Branch: 1 branch
BranchTrueFalseTotal
if exists $accessors{$column} 5 7 12
53  
54 # SQL::Abstract keywords are OK
55 next COLUMN
56 7 ▶ if $column =~ /^-(?:and|or|nest|(?:(not_)?(?:like|between)))$/;
Branch: 1 branch
BranchTrueFalseTotal
if $column =~ /^-(?:and|or|nest|(?:(not_)?(?:like|between)))$/ 1 6 7
57  
58 # Check for functions
59 6 ▶ if ( index( $column, '(' ) > 0
Branch: 1 branch
BranchTrueFalseTotal
if (index($column, "(") > 0 and index($column, ")") > 1) 6 0 6
Condition33%1/3
index($column, "(") > 0 and index($column, ")") > 1
!ll&&!rl&&r
006
MC/DC0%0/2
index($column, "(") > 0 and index($column, ")") > 1
index($column, "(") > 0 index($column, ")") > 1
Truth table33%1/3
A and B
A= index($column, "(") > 0B= index($column, ")") > 1
ABresult
0X0
100
111
60 && index( $column, ')' ) > 1 )
61 {
62 6 my @tokens = ( $column =~ /(-?\w+(?:\s*\(\s*)?|\W+)/g );
63 6 TOKEN: for my $token ( @tokens ) {
64 30 ▶ if ( $token !~ /\W/ ) { # must be column or accessor name
Branch: 1 branch
BranchTrueFalseTotal
unless ($token =~ /\W/) 8 22 30
65 8 ▶ next TOKEN if exists $columns{ $token };
Branch: 1 branch
BranchTrueFalseTotal
if exists $columns{$token} 5 3 8
66 $token = $accessors{ $token }, next TOKEN
67 3 ▶ if exists $accessors{ $token };
Branch: 1 branch
BranchTrueFalseTotal
if exists $accessors{$token} 3 0 3
68 0 $class->_croak(
69 qq{"$token" is not a column/accessor of class "$class"} );
70 }
71 }
72  
73 6 my $normalized = join "", @tokens;
74 6 ▶ $where{ $normalized } = delete $where{ $column }
Branch: 1 branch
BranchTrueFalseTotal
if $normalized ne $column 3 3 6
75 if $normalized ne $column;
76 6 next COLUMN;
77 }
78  
79 0 $class->_croak( qq{"$column" is not a column/accessor of class "$class"} );
80 }
81  
82 10 my ( $phrase, @bind ) = SQL::Abstract
83 -> new( %$rh_attr )
84 -> where( \%where );
85 10 $class
86 -> sql_count_search_where( $phrase )
87 -> select_val( @bind );
88 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 3 1;
91 __END__
↑ Summary