blib/lib/DBIx/AssertIndex.pm

filter: Statementstmt 45.7% 32 / 70 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 56.2% 9 / 16 Podpod n/a totaltotal 36.6% 41 / 112 CC 12 SCAR 26.3
CC
12
Cov
35%
__should_alert
42.0
__expain
20.0
__assert_explain
6.0
CRAP 13.9
↑ Summary
1 package DBIx::AssertIndex;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.008_009;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.02';
8  
9 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $OUTPUT = *STDERR;
12  
13 1 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
14 1 my ($class) = @_;
15  
16 1 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 my $original_st_execute = \&DBI::st::execute;
18 1 *DBI::st::execute = __explain_and_st_execute($original_st_execute);
19  
20 1 foreach my $db_method (qw/do selectall_arrayref selectrow_array selectrow_arrayref /){
21 1 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 my $original = \&{"DBI::db::$db_method"};
23 4 *{"DBI::db::$db_method"} = __explain_and_db_XXX($original, $original_st_execute);
24 }
25 };
26  
27 4 ▶sub __explain_and_db_XXX {
Subroutine __explain_and_db_XXX: called CC=1 SCAR=2.0
28 4 my($original_db_XXX, $original_st_execute) = @_;
29  
30 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 my ($dbh, $statement, @rest ) = @_;
32  
33 0 __expain($original_st_execute, $dbh, $statement, @rest);
34 0 return $original_db_XXX->(@_);
35 4 };
36 }
37  
38 1 ▶sub __explain_and_st_execute {
Subroutine __explain_and_st_execute: called CC=1 SCAR=3.1
39 1 my $original_st_execute = shift;
40 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 my ($sth, @rest ) = @_;
42  
43 0 my $dbh = $sth->{Database};
44 0 my $statement = $sth->{Statement};
45 0 __expain($original_st_execute, $dbh, $statement, @rest);
46 0 return $original_st_execute->(@_);
47 1 };
48 }
49  
50 0 ▶sub __expain {
Subroutine __expain: not called CC=4 SCAR=30.0
51 0 my($original_st_execute, $dbh, $statement, @rest) = @_;
52  
53 0 ▶ return unless($dbh->{Driver}{Name} eq 'mysql');
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"Driver"}{"Name"} eq "mysql" 0 0 0
54 0 ▶ return unless $statement =~ m/^\s*SELECT/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $statement =~ /^\s*SELECT/i 0 0 0
55 0 ▶ return unless $statement =~ m/FROM/mi;
Branch: 1 branch
BranchTrueFalseTotal
unless $statement =~ /FROM/im 0 0 0
56  
57 0 my $explain_sth = $dbh->prepare( 'explain ' . $statement );
58 0 $original_st_execute->($explain_sth, @rest);
59 0 __assert_explain($explain_sth->fetchall_arrayref( +{} ), $statement);
60 }
61  
62 0 ▶sub __assert_explain {
Subroutine __assert_explain: not called CC=2 SCAR=17.9
63 0 my($explains, $statement) = @_;
64 0 my $clean_statement = __clean_statement($statement);
65 0 my @using_no_key = grep {__should_alert($_) } @$explains;
66 0 ▶ return unless @using_no_key;
Branch: 1 branch
BranchTrueFalseTotal
unless @using_no_key 0 0 0
67  
68 0 __warn->('[explain alert] ', $clean_statement);
69 }
70  
71 0 ▶sub __should_alert {
Subroutine __should_alert: not called CC=6 SCAR=37.4
72 0 my ( $explain_by_table ) = @_;
73 0 my $extra = $explain_by_table->{Extra};
74 0 my $type = $explain_by_table->{type};
75 0 my $possible_key = $explain_by_table->{possible_keys};
76 # search uniq/primary key but not found any rows
77 0 ▶ return 0 if $extra and $extra =~ m/^Impossible/;
Branch: 1 branch
BranchTrueFalseTotal
if $extra and $extra =~ /^Impossible/ 0 0 0
Condition0%0/3
$extra and $extra =~ /^Impossible/
!ll&&!rl&&r
000
MC/DC0%0/2
$extra and $extra =~ /^Impossible/
$extra $extra =~ /^Impossible/
Truth table0%0/3
A and B
A= $extraB= $extra =~ /^Impossible/
ABresult
0X0
100
111
78 # not using any index
79 0 ▶ return 0 if defined $possible_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $possible_key 0 0 0
80 0 ▶ return 1 if $type and $type eq 'ALL';
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type eq "ALL" 0 0 0
Condition0%0/3
$type and $type eq "ALL"
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type eq "ALL"
$type $type eq "ALL"
Truth table0%0/3
A and B
A= $typeB= $type eq "ALL"
ABresult
0X0
100
111
81 0 return 0;
82 }
83  
84 0 ▶sub __clean_statement {
Subroutine __clean_statement: not called CC=1 SCAR=6.9
85 0 my $statement = shift;
86 0 $statement =~ s/\n/ /g;
87 0 $statement =~ s/\s+/ /g;
88 0 return $statement;
89 }
90  
91 0 ▶sub __warn {
Subroutine __warn: not called CC=2 SCAR=17.9
92 0 my($message, $statement, $using_no_key) = @_;
93  
94 0 ▶ if (ref $OUTPUT eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $DBIx::AssertIndex::OUTPUT eq 'CODE') { } 0 0 0
95 0 $OUTPUT->(
96 message => $message,
97 statement => $statement,
98 using_no_key => $using_no_key,
99 );
100 } else {
101 0 print {$OUTPUT} $message, ' statement:', $statement;
102 }
103 }
104  
105 1 1;
106 __END__
↑ Summary