| 1 | package ObjectDB::DBHPool::Connection; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | our $VERSION = '3.26'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | require Carp; | |||||||||||||||||||||||||||
| 9 | 5 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new { | ||||||||||||||||||||||||||||
| 12 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 0 | my (%params) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | my $self = {@_}; | |||||||||||||||||||||||||||
| 16 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | $self->{check_timeout} = $params{check_timeout}; | |||||||||||||||||||||||||||
| 19 | 0 | $self->{dsn} = $params{dsn}; | |||||||||||||||||||||||||||
| 20 | 0 | $self->{username} = $params{username}; | |||||||||||||||||||||||||||
| 21 | 0 | $self->{password} = $params{password}; | |||||||||||||||||||||||||||
| 22 | 0 | $self->{attrs} = $params{attrs}; | |||||||||||||||||||||||||||
| 23 | 0 | $self->{do} = $params{do}; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | $self->{check_timeout} = 5 unless defined $self->{check_timeout}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $self->{attrs} ||= { RaiseError => 1 }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"attrs"} ||= {"RaiseError", 1}
MC/DC0%0/1
$self->{"attrs"} ||= {"RaiseError", 1}
$self->{"attrs"}
Truth table0%0/2
A
A= $self->{"attrs"}
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | return $self; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub dbh { | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
dbh: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if (!$self->{dbh}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | 0 | $self->_connect; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | elsif (!$self->_check_connection) { | ||||||||||||||||||||||||||||
| 38 | 0 | $self->_reconnect; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | return $self->{dbh}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub _connect { | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_connect: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | $self->{dbh} = $self->_get_dbh; | |||||||||||||||||||||||||||
| 48 | 0 | $self->{last_check} = time; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | return $self->{dbh}; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub _reconnect { | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_reconnect: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | eval { $self->{dbh}->disconnect }; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | return $self->_connect; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub _check_connection { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_check_connection: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return 1 unless $self->_is_check_needed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | $self->{last_check} = time; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $dbh = $self->{dbh}; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | return unless $dbh && $dbh->{Active}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dbh and $dbh->{"Active"}
MC/DC0%0/2
$dbh and $dbh->{"Active"}
$dbh
$dbh->{"Active"}
Truth table0%0/3
A and B
A= $dbhB= $dbh->{"Active"}
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return 1 if int $dbh->ping; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return eval { $dbh->do('select 1'); 1; }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub _is_check_needed { | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_is_check_needed: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return 1 if !$self->{dbh}->{Active} || $self->_is_check_timeout_elapsed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->{"dbh"}{"Active"} or $self->_is_check_timeout_elapsed
MC/DC0%0/2
not $self->{"dbh"}{"Active"} or $self->_is_check_timeout_elapsed
not $self->{"dbh"}{"Active"}
$self->_is_check_timeout_elapsed
Truth table0%0/3
not(A) or B
A= not $self->{"dbh"}{"Active"}B= $self->_is_check_timeout_elapsed
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | return 0; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub _is_check_timeout_elapsed { | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_is_check_timeout_elapsed: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($self->{check_timeout} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"check_timeout"} and time - $self->{"last_check"} > $self->{"check_timeout"}
MC/DC0%0/2
$self->{"check_timeout"} and time - $self->{"last_check"} > $self->{"check_timeout"}
$self->{"check_timeout"}
time - $self->{"last_check"} > $self->{"check_timeout"}
Truth table0%0/3
A and B
A= $self->{"check_timeout"}B= time - $self->{"last_check"} > $self->{"check_timeout"}
| |||||||||||||||||||||||||||||
| 89 | && time - $self->{last_check} > $self->{check_timeout}) | ||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 0 | return 1; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | return 0; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub _get_dbh { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_get_dbh: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | my $dbh = DBI->connect($self->{dsn}, $self->{username}, $self->{password}, $self->{attrs}); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | Carp::croak("Can't connect $DBI::errstr") unless $dbh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($self->{do}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | foreach my $do (ref $self->{do} eq 'ARRAY' ? @{ $self->{do} } : ($self->{do})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | $dbh->do($do); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | return $dbh; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 5 | 1; | |||||||||||||||||||||||||||