blib/lib/HealthCheck/Diagnostic/DBHCheck.pm

filter: Statementstmt 95.7% 67 / 70 Branchbran 90.0% 27 / 30 Conditioncond 57.8% 22 / 38 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 79.5% 136 / 171 SCAR 35.2
CC
30
Cov
84%
check
14.4
run
9.4
new
7.6
CRAP 33.6
↑ Summary
1 package HealthCheck::Diagnostic::DBHCheck;
2  
3 # ABSTRACT: Check a database handle to make sure you have read/write access
4 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = 'v0.500.3'; # VERSION
6  
7 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use parent 'HealthCheck::Diagnostic';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new {
16 6 ▶ my ($class, @params) = @_;
Subroutine new: called CC=6 SCAR=20.3
Pod documented
17  
18 # Allow either a hashref or even-sized list of params
19 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
20 6 ▶ ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && (ref $params[0] || '') eq 'HASH' ? : 0 6 6
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
!ll&&!rl&&r
600
ref $params[0] || ''
l!l
00
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1 ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
ABresult
0X0
100
111
21  
22 croak("The 'dbh' parameter should be a coderef!")
23 6 ▶ if ($params{dbh} && (ref $params{dbh} ne "CODE"));
Branch: 1 branch
BranchTrueFalseTotal
if $params{"dbh"} and ref $params{"dbh"} ne "CODE" 1 5 6
Condition100%3/3
$params{"dbh"} and ref $params{"dbh"} ne "CODE"
!ll&&!rl&&r
141
MC/DC100%2/2
$params{"dbh"} and ref $params{"dbh"} ne "CODE"
$params{"dbh"} ref $params{"dbh"} ne "CODE"
Truth table100%3/3
A and B
A= $params{"dbh"}B= ref $params{"dbh"} ne "CODE"
ABresult
0X0
100
111
24  
25 5 return $class->SUPER::new(
26 tags => [ 'dbh_check' ],
27 label => 'dbh_check',
28 %params
29 );
30 }
31  
32 sub check {
33 14 ▶ my ( $self, %params ) = @_;
Subroutine check: called CC=14 SCAR=26.7
Pod documented
34  
35 # 1st, try to get dbh from provided parameters
36 14 my $dbh = $params{dbh};
37 # 2nd, if invoked with an object (not the class), then get dbh from object
38 14 ▶ $dbh ||= $self->{dbh} if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 5 9 14
Condition66%2/3
$dbh ||= $self->{"dbh"}
l!l&&r!l&&!r
041
MC/DC50%1/2
$dbh ||= $self->{"dbh"}
$dbh $self->{"dbh"}
Truth table66%2/3
A ||= B
A= $dbhB= $self->{"dbh"}
ABresult
1X1
011
000
39  
40 14 ▶ croak "Valid 'dbh' is required" unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 2 12 14
41  
42 12 ▶ croak "The 'dbh' parameter should be a coderef!"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dbh eq "CODE" 1 11 12
43 unless ref $dbh eq "CODE";
44  
45 my $db_access = $params{db_access} # Provided call to check()
46 // ((ref $self) && $self->{db_access}) # Value from new()
47 11 ▶ || "rw"; # default value
Condition87%7/8
($params{'db_access'} // ref $self && $self->{'db_access'}) || 'rw'
l!l
29
$params{"db_access"} // ref $self && $self->{"db_access"}
l!l&&r!l&&!r
029
ref $self and $self->{"db_access"}
!ll&&!rl&&r
722
MC/DC66%2/3
($params{'db_access'} // ref $self && $self->{'db_access'}) || 'rw'
$params{"db_access"} ref $self $self->{"db_access"}
Truth table75%3/4
A // B and C
A= $params{"db_access"}B= ref $selfC= $self->{"db_access"}
ABCresult
1XX1
0111
00X0
0100
48  
49 11 ▶ croak "The value '$db_access' is not valid for the 'db_access' parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless $db_access =~ /^r[ow]$/ 1 10 11
50 unless $db_access =~ /^r[ow]$/;
51  
52 10 $dbh = $dbh->(%params);
53  
54 8 ▶ croak "The 'dbh' coderef should return an object!"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dbh) 2 6 8
55 unless blessed $dbh;
56  
57 my $db_class = $params{db_class} # Provided in call to check()
58 // ((ref $self) && $self->{db_class}) # Value from new
59 6 ▶ || "DBI::db"; # default value
Condition50%4/8
($params{'db_class'} // ref $self && $self->{'db_class'}) || 'DBI::db'
l!l
06
$params{"db_class"} // ref $self && $self->{"db_class"}
l!l&&r!l&&!r
006
ref $self and $self->{"db_class"}
!ll&&!rl&&r
330
MC/DC0%0/3
($params{'db_class'} // ref $self && $self->{'db_class'}) || 'DBI::db'
$params{"db_class"} ref $self $self->{"db_class"}
Truth table50%2/4
A // B and C
A= $params{"db_class"}B= ref $selfC= $self->{"db_class"}
ABCresult
1XX1
0111
00X0
0100
60  
61 6 my $isa = ref $dbh;
62  
63 6 ▶ croak "The 'dbh' coderef should return a '$db_class', not a '$isa'"
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->isa($db_class) 1 5 6
64 unless $dbh->isa($db_class);
65  
66 5 my $res = $self->SUPER::check(
67 %params,
68 dbh => $dbh,
69 db_access => $db_access,
70 db_class => $db_class
71 );
72 5 delete $res->{dbh}; # don't include the object in the result
73  
74 5 return $res;
75 }
76  
77  
78 sub _read_write_temp_table {
79 2 ▶ my (%params) = @_;
Subroutine _read_write_temp_table: called CC=4 SCAR=14.2
80 2 my $dbh = $params{dbh};
81 2 ▶ my $table = $params{table_name} // "__DBH_CHECK__";
Condition50%1/2
$params{"table_name"} // "__DBH_CHECK__"
l!l
02
MC/DC0%0/1
$params{"table_name"} // "__DBH_CHECK__"
$params{"table_name"}
Truth table50%1/2
A
A= $params{"table_name"}
Aresult
11
00
82 2 my $status = "CRITICAL";
83  
84 2 my $qtable = $dbh->quote_identifier($table);
85  
86 # Drop it like it's hot
87 2 $dbh->do("DROP TEMPORARY TABLE IF EXISTS $qtable");
88  
89 2 $dbh->do(
90 join(
91 "",
92 "CREATE TEMPORARY TABLE IF NOT EXISTS $qtable (",
93 "check_id INTEGER PRIMARY KEY,",
94 "check_string VARCHAR(64) NOT NULL",
95 ")"
96 )
97 );
98  
99 2 $dbh->do(
100 join(
101 "",
102 "INSERT INTO $qtable ",
103 " (check_id, check_string) ",
104 "VALUES (1, 'Hello world')",
105 )
106 );
107 2 my @row = $dbh->selectrow_array(
108 "SELECT check_string FROM $qtable WHERE check_id = 1"
109 );
110  
111 2 ▶ $status = "OK" if ($row[0] && ($row[0] eq "Hello world"));
Branch: 1 branch
BranchTrueFalseTotal
if $row[0] and $row[0] eq "Hello world" 2 0 2
Condition33%1/3
$row[0] and $row[0] eq "Hello world"
!ll&&!rl&&r
002
MC/DC0%0/2
$row[0] and $row[0] eq "Hello world"
$row[0] $row[0] eq "Hello world"
Truth table33%1/3
A and B
A= $row[0]B= $row[0] eq "Hello world"
ABresult
0X0
100
111
112  
113 2 $dbh->do("DROP TEMPORARY TABLE $qtable");
114  
115 2 return $status;
116 }
117  
118 sub run {
119 5 ▶ my ( $self, %params ) = @_;
Subroutine run: called CC=9 SCAR=22.5
Pod documented
120 5 my $dbh = $params{dbh};
121  
122 # Get db_access from parameters
123 5 my $read_write = ($params{db_access} =~ /^rw$/i);
124  
125 5 my $status = "OK";
126  
127 RUN_TESTS: {
128  
129 # See if we can ping the DB connection
130 5 ▶ if ($dbh->can("ping") && !$dbh->ping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->can("ping") and not $dbh->ping) 2 3 5
Condition66%2/3
$dbh->can("ping") and not $dbh->ping
!ll&&!rl&&r
032
MC/DC50%1/2
$dbh->can("ping") and not $dbh->ping
$dbh->can("ping") not $dbh->ping
Truth table66%2/3
A and not(B)
A= $dbh->can("ping")B= not $dbh->ping
ABresult
0X0
100
111
131 2 $status = "CRITICAL";
132 2 last RUN_TESTS;
133 }
134  
135 # See if a simple SELECT works
136 3 my $value = eval { $dbh->selectrow_array("SELECT 1"); };
137 3 ▶ unless (defined $value && $value == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and $value == 1) 0 3 3
Condition33%1/3
defined $value and $value == 1
!ll&&!rl&&r
003
MC/DC0%0/2
defined $value and $value == 1
defined $value $value == 1
Truth table33%1/3
A and B
A= defined $valueB= $value == 1
ABresult
0X0
100
111
138 0 $status = "CRITICAL";
139 0 last RUN_TESTS;
140 }
141  
142 3 ▶ $status = _read_write_temp_table(%params) if $read_write;
Branch: 1 branch
BranchTrueFalseTotal
if $read_write 2 1 3
143 }
144  
145 # Generate the human readable info string
146 my $info = sprintf(
147 "%s %s %s check of %s%s",
148 $status eq "OK" ? "Successful" : "Unsuccessful",
149 $dbh->{Driver}->{Name},
150 $read_write ? "read write" : "read only",
151 $dbh->{Name},
152 5 ▶ $dbh->{Username} ? " as $dbh->{Username}" : "",
Branch: 3 branches
BranchTrueFalseTotal
$status eq 'OK' ? : 3 2 5
$read_write ? : 4 1 5
$dbh->{'Username'} ? : 2 3 5
153 );
154  
155 5 return { status => $status, info => $info };
156 }
157  
158 2 1;
159  
160 __END__
↑ Summary