| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$config->{'connection_check_threshold'} || 30
$config->{'connection_check_threshold'}
Truth table50%1/2
A
A= $config->{'connection_check_threshold'}
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | ▶ | $self->{'_dsn_extra'} = $config->{'dsn_extra'} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'dsn_extra'} || {}
MC/DC0%0/1
$config->{'dsn_extra'} || {}
$config->{'dsn_extra'}
Truth table50%1/2
A
A= $config->{'dsn_extra'}
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | ▶ | $self->{'_on_connect'} = $config->{'on_connect'} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'on_connect'} || []
MC/DC0%0/1
$config->{'on_connect'} || []
$config->{'on_connect'}
Truth table50%1/2
A
A= $config->{'on_connect'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$self->{'_handles'}{$pid_tid} || undef
$self->{'_handles'}{$pid_tid}
Truth table100%2/2
A
A= $self->{'_handles'}{$pid_tid}
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 12 | ▶ | if ($handle->{'dbh'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 5 | ▶ | if ($handle->{'dbh'}{'Active'} && $self->{'_check_threshold'} && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$handle->{"dbh"}{"Active"} and $self->{"_check_threshold"} and time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
$handle->{"dbh"}{"Active"} and $self->{"_check_threshold"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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")
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'};})
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($dbh->{Active} && (my $result = $dbh->ping())) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dbh->{"Active"} and my $result = $dbh->ping
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
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (int($result)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 7 | ▶ | return $self->_merge($left, $self->_merge(@right)) if @right > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$hr and $hl
MC/DC0%0/2
$hr and $hl
$hr
$hl
Truth table0%0/3
A and B
A= $hrB= $hl
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||