Database/Driver.pm

filter: Statementstmt 66.3% 65 / 98 Branchbran 26.6% 8 / 30 Conditioncond 36.3% 8 / 22 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 61.1% 11 / 18 Podpod 0.0% 0 / 2 totaltotal 51.1% 93 / 182 CC 27 SCAR 24.8
CC
27
Cov
54%
_check_connection
30.0
handle
23.4
_merge
11.1
CRAP 12.0
« Database/Driver/Pg.pm ↑ Summary Database/Driver/SQLite.pm »
1 package Prancer::Plugin::Database::Driver;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $VERSION = '1.04';
8  
9 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # even though this *should* work automatically, it was not
13 2 our @CARP_NOT = qw(Prancer Try::Tiny);
14  
15 7 ▶sub new {
Subroutine new: called CC=5 SCAR=17.2
Pod undocumented
16 7 my ($class, $config, $connection) = @_;
17  
18 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 7 require DBI;
20 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
21 0 ▶ my $error = (defined($_) ? $_ : "unknown");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
22 0 croak "could not initialize database connection '${connection}': could not load DBI: ${error}";
23 7 };
24  
25 # this is the only required field
26 7 ▶ unless ($config->{'database'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"database"}) 0 7 7
27 0 croak "could not initialize database connection '${connection}': no database name configured";
28 }
29  
30 7 my $self = bless({}, $class);
31 7 $self->{'_connection'} = $connection;
32 7 $self->{'_database'} = $config->{'database'};
33 7 $self->{'_username'} = $config->{'username'};
34 7 $self->{'_password'} = $config->{'password'};
35 7 $self->{'_hostname'} = $config->{'hostname'};
36 7 $self->{'_port'} = $config->{'port'};
37 7 $self->{'_autocommit'} = $config->{'autocommit'};
38 7 $self->{'_charset'} = $config->{'charset'};
39 7 ▶ $self->{'_check_threshold'} = $config->{'connection_check_threshold'} || 30;
Condition50%1/2
$config->{'connection_check_threshold'} || 30
l!l
70
MC/DC0%0/1
$config->{'connection_check_threshold'} || 30
$config->{'connection_check_threshold'}
Truth table50%1/2
A
A= $config->{'connection_check_threshold'}
Aresult
11
00
40 7 ▶ $self->{'_dsn_extra'} = $config->{'dsn_extra'} || {};
Condition50%1/2
$config->{'dsn_extra'} || {}
l!l
07
MC/DC0%0/1
$config->{'dsn_extra'} || {}
$config->{'dsn_extra'}
Truth table50%1/2
A
A= $config->{'dsn_extra'}
Aresult
11
00
41 7 ▶ $self->{'_on_connect'} = $config->{'on_connect'} || [];
Condition50%1/2
$config->{'on_connect'} || []
l!l
07
MC/DC0%0/1
$config->{'on_connect'} || []
$config->{'on_connect'}
Truth table50%1/2
A
A= $config->{'on_connect'}
Aresult
11
00
42  
43 # store a pool of database connection handles
44 7 $self->{'_handles'} = {};
45  
46 7 return $self;
47 }
48  
49 12 ▶sub handle {
Subroutine handle: called CC=10 SCAR=31.5
Pod undocumented
50 12 my $self = shift;
51  
52 # to be fork safe and thread safe, use a combination of the PID and TID (if
53 # running with use threads) to make sure no two processes/threads share a
54 # handle. implementation based on DBIx::Connector by David E. Wheeler
55 12 my $pid_tid = $$;
56 12 ▶ $pid_tid .= "_" . threads->tid if $INC{'threads.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 12 12
57  
58 # see if we have a matching handle
59 12 ▶ my $handle = $self->{'_handles'}->{$pid_tid} || undef;
Condition100%2/2
$self->{'_handles'}{$pid_tid} || undef
l!l
57
MC/DC100%1/1
$self->{'_handles'}{$pid_tid} || undef
$self->{'_handles'}{$pid_tid}
Truth table100%2/2
A
A= $self->{'_handles'}{$pid_tid}
Aresult
11
00
60  
61 12 ▶ if ($handle->{'dbh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{'dbh'}) { } 5 7 12
62 5 ▶ if ($handle->{'dbh'}{'Active'} && $self->{'_check_threshold'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{'dbh'}{'Active'} and $self->{'_check_threshold'} and time - $handle->{'last_connection_check'} < $self->{'_check_threshold'}) { } 5 0 5
Condition33%2/6
$handle->{"dbh"}{"Active"} and $self->{"_check_threshold"} and time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
!ll&&!rl&&r
005
$handle->{"dbh"}{"Active"} and $self->{"_check_threshold"}
!ll&&!rl&&r
005
MC/DC0%0/3
$handle->{"dbh"}{"Active"} and $self->{"_check_threshold"} and time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
$handle->{"dbh"}{"Active"} $self->{"_check_threshold"} time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
Truth table25%1/4
A and B and C
A= $handle->{"dbh"}{"Active"}B= $self->{"_check_threshold"}C= time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
ABCresult
0XX0
10X0
1100
1111
63 (time - $handle->{'last_connection_check'} < $self->{'_check_threshold'})) {
64  
65 # the handle has been checked recently so just return it
66 5 return $handle->{'dbh'};
67 } else {
68 0 ▶ if ($self->_check_connection($handle->{'dbh'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_connection($handle->{'dbh'})) { } 0 0 0
69 0 $handle->{'last_connection_check'} = time;
70 0 return $handle->{'dbh'};
71 } else {
72 # er need to reconnect
73 0 carp "database connection to '${\$self->{'_connection'}}' went away -- reconnecting";
74  
75 # try to disconnect but don't care if it fails
76 0 ▶ if ($handle->{'dbh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{"dbh"}) 0 0 0
77 0 ▶ try { $handle->{'dbh'}->disconnect(); } catch {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 }
79  
80 # try to connect again and save the new handle
81 0 $handle->{'dbh'} = $self->_get_connection();
82 0 return $handle->{'dbh'};
83 }
84 }
85 } else {
86 7 $handle->{'dbh'} = $self->_get_connection();
87 7 ▶ if ($handle->{'dbh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{"dbh"}) 7 0 7
88 7 $handle->{'last_connection_check'} = time;
89 7 $self->{'_handles'}->{$pid_tid} = $handle;
90 7 return $handle->{'dbh'};
91 }
92 }
93  
94 0 return;
95 }
96  
97 7 ▶sub _get_connection {
Subroutine _get_connection: called CC=1 SCAR=0.8
98 7 my $self = shift;
99  
100 7 my $dbh = undef;
101 7 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.7
102 0 ▶ $dbh = DBI->connect(@{$self->{'_dsn'}}) || croak "${\$DBI::errstr}\n";
Condition50%1/2
'DBI'->connect(@{$$self{'_dsn'};}) || croak("${\$DBI::errstr;}\n")
l!l
70
MC/DC0%0/1
'DBI'->connect(@{$$self{'_dsn'};}) || croak("${\$DBI::errstr;}\n")
'DBI'->connect(@{$$self{'_dsn'};})
Truth table50%1/2
A
A= 'DBI'->connect(@{$$self{'_dsn'};})
Aresult
11
00
103  
104 # run any on_connect sql
105 7 $dbh->do($_) for (@{$self->{'_on_connect'}});
106 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
107 0 ▶ my $error = (defined($_) ? $_ : "unknown");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
108 0 croak "could not initialize database connection '${\$self->{'_connection'}}': ${error}";
109 7 };
110  
111 7 return $dbh;
112 }
113  
114 # Check the connection is alive
115 0 ▶sub _check_connection {
Subroutine _check_connection: not called CC=5 SCAR=34.0
116 0 my $self = shift;
117 0 my $dbh = shift;
118 0 ▶ return 0 unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 0 0
119  
120 0 ▶ if ($dbh->{Active} && (my $result = $dbh->ping())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"Active"} and my $result = $dbh->ping) 0 0 0
Condition0%0/3
$dbh->{"Active"} and my $result = $dbh->ping
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh->{"Active"} and my $result = $dbh->ping
$dbh->{"Active"} my $result = $dbh->ping
Truth table0%0/3
A and B
A= $dbh->{"Active"}B= my $result = $dbh->ping
ABresult
0X0
100
111
121 0 ▶ if (int($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $result) { } 0 0 0
122 # DB driver itself claims all is OK, trust it:
123 0 return 1;
124 } else {
125 # it was "0 but true" meaning the DBD doesn't implement ping and
126 # instead we got the default DBI ping implementation. implement
127 # our own basic check by performing a real simple query.
128 0 ▶ return try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 return $dbh->do("SELECT 1");
130 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 return 0;
132 0 };
133 }
134 }
135  
136 0 return 0;
137 }
138  
139 # stolen from Hash::Merge::Simple
140 ## no critic (ProhibitUnusedPrivateSubroutines)
141 7 ▶sub _merge {
Subroutine _merge: called CC=6 SCAR=24.1
142 7 my ($self, $left, @right) = @_;
143  
144 7 ▶ return $left unless @right;
Branch: 1 branch
BranchTrueFalseTotal
unless @right 0 7 7
145 7 ▶ return $self->_merge($left, $self->_merge(@right)) if @right > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @right > 1 0 7 7
146  
147 7 my ($right) = @right;
148 7 my %merged = %{$left};
149  
150 7 for my $key (keys %{$right}) {
151 0 my ($hr, $hl) = map { ref($_->{$key}) eq "HASH" } $right, $left;
152  
153 0 ▶ if ($hr and $hl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr and $hl) { } 0 0 0
Condition0%0/3
$hr and $hl
!ll&&!rl&&r
000
MC/DC0%0/2
$hr and $hl
$hr $hl
Truth table0%0/3
A and B
A= $hrB= $hl
ABresult
0X0
100
111
154 0 $merged{$key} = $self->_merge($left->{$key}, $right->{$key});
155 } else {
156 0 $merged{$key} = $right->{$key};
157 }
158 }
159  
160 7 return \%merged;
161 }
162  
163 2 1;
« Database/Driver/Pg.pm ↑ Summary Database/Driver/SQLite.pm »