blib/lib/Scope/Container/DBI.pm

filter: Statementstmt 89.9% 116 / 129 Branchbran 76.6% 46 / 60 Conditioncond 47.5% 19 / 40 MC/DCmcdc 18.1% 4 / 22 Subroutinesub 100% 18 / 18 Podpod 50.0% 1 / 2 totaltotal 75.2% 204 / 271 CC 49 SCAR 30.1
CC
49
Cov
79%
is_class_loaded
41.7
connect
23.5
_lookup_cache
9.5
CRAP 20.3
↑ Summary
1 package Scope::Container::DBI;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Scope::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Log::Minimal;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use List::Util qw/shuffle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Time::HiRes qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Module::Load qw/load/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use DBI 1.615;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 our $VERSION = '0.09';
16 6 our $DBI_CLASS = 'DBI';
17  
18 104 ▶sub connect {
Subroutine connect: called CC=22 SCAR=31.6
Pod documented
19 104 my $class = shift;
20  
21 104 ▶ if ( @_ && (ref $_[0] || '' eq 'ARRAY') ) {
Branch: 1 branch
BranchTrueFalseTotal
if () 36 68 104
Condition80%4/5
@_ and
!ll&&!rl&&r
06836
ref $_[0] or
l!l
3668
MC/DC50%1/2
@_ and
@_ ref $_[0]
Truth table66%2/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
22 36 my @dsn = @_;
23 36 my $dbi;
24 36 my $dsn_key = _build_dsn_key(@dsn);
25 36 my $dbh = _lookup_cache($dsn_key);
26 36 ▶ return $dbh if $dbh;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh 3 33 36
27  
28 33 for my $s_dsn ( shuffle(@dsn) ) {
29 45 eval {
30 45 ($dbh, $dbi) = $class->connect(@$s_dsn);
31 };
32 45 ▶ infof("Connection failed: " . $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 12 33 45
33 45 ▶ last if ( $dbh );
Branch: 1 branch
BranchTrueFalseTotal
if $dbh 33 12 45
34 }
35  
36 33 ▶ if ( $dbh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) 33 0 33
37 33 _save_cache($dsn_key, $dbi);
38 33 ▶ return wantarray ? ( $dbh, $dbi) : $dbh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 33 33
39 }
40
41 croak("couldn't connect all DB, " .
42 0 join(",", map { $_->[0] } @dsn));
43 }
44  
45 68 my @dsn = @_;
46 68 my $dsn_key = _build_dsn_key(\@dsn);
47 68 my $cached_dbh = _lookup_cache($dsn_key);
48 68 ▶ return $cached_dbh if $cached_dbh;
Branch: 1 branch
BranchTrueFalseTotal
if $cached_dbh 6 62 68
49  
50 62 my ($dsn, $user, $pass, $attr) = @dsn;
51 62 ▶ $attr ||= {};
Condition100%2/2
$attr ||= {}
l!l
2141
MC/DC100%1/1
$attr ||= {}
$attr
Truth table100%2/2
A
A= $attr
Aresult
11
00
52 62 $attr->{AutoInactiveDestroy} = 1;
53 62 ▶ my $retry = exists $attr->{ScopeContainerConnectRetry} ? delete $attr->{ScopeContainerConnectRetry} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{'ScopeContainerConnectRetry'} ? : 4 58 62
54 62 my $sleep = delete $attr->{ScopeContainerConnectRetrySleep};
55 62 ▶ $sleep = $sleep / 1000 if $sleep;
Branch: 1 branch
BranchTrueFalseTotal
if $sleep 1 61 62
56  
57 62 ▶ if ( ! is_class_loaded($DBI_CLASS) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_class_loaded($Scope::Container::DBI::DBI_CLASS)) 0 62 62
58 0 load $DBI_CLASS;
59 }
60 62 my $lasterrstr;
61 62 my $dbh = do {
62 62 my $connect;
63 62 for ( 1..$retry ) {
64 64 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=16.4
65 64 ▶ if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Apache/DBI.pm'} and $ENV{'MOD_PERL'}) { } 0 64 64
Condition33%1/3
$INC{"Apache/DBI.pm"} and $ENV{"MOD_PERL"}
!ll&&!rl&&r
6400
MC/DC0%0/2
$INC{"Apache/DBI.pm"} and $ENV{"MOD_PERL"}
$INC{"Apache/DBI.pm"} $ENV{"MOD_PERL"}
Truth table33%1/3
A and B
A= $INC{"Apache/DBI.pm"}B= $ENV{"MOD_PERL"}
ABresult
0X0
100
111
66 0 local $DBI::connect_via = 'connect'; # Disable Apache::DBI.
67 0 $connect = $DBI_CLASS->connect( $dsn, $user, $pass, $attr );
68 } else {
69 64 $connect = $DBI_CLASS->connect( $dsn, $user, $pass, $attr );
70 }
71 60 ▶ die $DBI::errstr."\n" unless $connect;
Branch: 1 branch
BranchTrueFalseTotal
unless $connect 12 48 60
72 }
73 16 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 16 $lasterrstr = $_;
75 64 };
76 64 ▶ last if $connect;
Branch: 1 branch
BranchTrueFalseTotal
if $connect 48 16 64
77 16 ▶ Time::HiRes::sleep($sleep) if $sleep && $retry != $_;
Branch: 1 branch
BranchTrueFalseTotal
if $sleep and $retry != $_ 2 14 16
Condition66%2/3
$sleep and $retry != $_
!ll&&!rl&&r
1402
MC/DC50%1/2
$sleep and $retry != $_
$sleep $retry != $_
Truth table66%2/3
A and B
A= $sleepB= $retry != $_
ABresult
0X0
100
111
78 }
79 62 $connect;
80 };
81 62 ▶ croak($lasterrstr) if !$dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 14 48 62
82  
83 48 my $dbi = {
84 dbh => $dbh,
85 pid => $$,
86 };
87 48 ▶ $dbi->{tid} = threads->tid if $INC{'threads.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 48 48
88  
89 48 _save_cache($dsn_key, $dbi);
90 48 ▶ return wantarray ? ( $dbh, $dbi ) : $dbh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 33 15 48
91 }
92  
93  
94 104 ▶sub _build_dsn_key {
Subroutine _build_dsn_key: called CC=1 SCAR=0.0
95 104 my @dsn = @_;
96 104 local $Data::Dumper::Terse = 1;
97 104 local $Data::Dumper::Indent = 0;
98 104 local $Data::Dumper::Sortkeys = 1;
99 104 my $key = Data::Dumper::Dumper(\@dsn);
100 104 "sc:dbix:".$key;
101 }
102  
103 104 ▶sub _lookup_cache {
Subroutine _lookup_cache: called CC=8 SCAR=22.5
104 104 my $key = shift;
105 104 ▶ return unless in_scope_container();
Branch: 1 branch
BranchTrueFalseTotal
unless in_scope_container() 73 31 104
106 31 my $dbi = scope_container($key);
107 31 ▶ return if !$dbi;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbi 21 10 31
108  
109 10 my $dbh = $dbi->{dbh};
110 10 ▶ if ( defined $dbi->{tid} && $dbi->{tid} != threads->tid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbi->{"tid"} and $dbi->{"tid"} != "threads"->tid) 0 10 10
Condition33%1/3
defined $dbi->{"tid"} and $dbi->{"tid"} != "threads"->tid
!ll&&!rl&&r
1000
MC/DC0%0/2
defined $dbi->{"tid"} and $dbi->{"tid"} != "threads"->tid
defined $dbi->{"tid"} $dbi->{"tid"} != "threads"->tid
Truth table33%1/3
A and B
A= defined $dbi->{"tid"}B= $dbi->{"tid"} != "threads"->tid
ABresult
0X0
100
111
111 0 return;
112 }
113 10 ▶ if ( $dbi->{pid} != $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi->{"pid"} != $$) 1 9 10
114 1 $dbh->STORE(InactiveDestroy => 1);
115 1 return;
116 }
117 9 ▶ return $dbh if $dbh->FETCH('Active') && $dbh->ping;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->FETCH("Active") and $dbh->ping 9 0 9
Condition33%1/3
$dbh->FETCH("Active") and $dbh->ping
!ll&&!rl&&r
009
MC/DC0%0/2
$dbh->FETCH("Active") and $dbh->ping
$dbh->FETCH("Active") $dbh->ping
Truth table33%1/3
A and B
A= $dbh->FETCH("Active")B= $dbh->ping
ABresult
0X0
100
111
118 0 return;
119 }
120  
121 81 ▶sub _save_cache {
Subroutine _save_cache: called CC=2 SCAR=6.9
122 81 my $key = shift;
123 81 ▶ return unless in_scope_container();
Branch: 1 branch
BranchTrueFalseTotal
unless in_scope_container() 61 20 81
124 20 scope_container($key, shift);
125 }
126  
127 # stolen from Mouse::PurePerl
128 62 ▶sub is_class_loaded {
Subroutine is_class_loaded: called CC=17 SCAR=37.3
Pod undocumented
129 62 my $class = shift;
130  
131 62 ▶ return 0 if ref($class) || !defined($class) || !length($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class or not defined $class or not length $class 0 62 62
Condition33%2/6
ref $class or not defined $class or not length $class
l!l&&r!l&&!r
0062
ref $class or not defined $class
l!l&&r!l&&!r
0062
MC/DC0%0/3
ref $class or not defined $class or not length $class
ref $class not defined $class not length $class
Truth table25%1/4
A or not(B) or not(C)
A= ref $classB= not defined $classC= not length $class
ABCresult
1XX1
01X1
0011
0000
132  
133 # walk the symbol table tree to avoid autovififying
134 # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
135  
136 62 my $pack = \%::;
137  
138 62 foreach my $part (split('::', $class)) {
139 62 $part .= '::';
140 62 ▶ return 0 if !exists $pack->{$part};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pack->{$part} 0 62 62
141  
142 62 my $entry = \$pack->{$part};
143 62 ▶ return 0 if ref($entry) ne 'GLOB';
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry ne "GLOB" 0 62 62
144 62 $pack = *{$entry}{HASH};
145 }
146  
147 62 ▶ return 0 if !%{$pack};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$pack;} 0 62 62
148  
149 # check for $VERSION or @ISA
150 return 1 if exists $pack->{VERSION}
151 62 ▶ && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};} 61 1 62
Condition66%4/6
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};}
!ll&&!rl&&r
1061
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"}
!ll&&!rl&&r
1061
MC/DC33%1/3
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};}
exists $pack->{"VERSION"} defined *{$pack->{"VERSION"};}{"SCALAR"} defined ${$$pack{"VERSION"};}
Truth table50%2/4
A and B and C
A= exists $pack->{"VERSION"}B= defined *{$pack->{"VERSION"};}{"SCALAR"}C= defined ${$$pack{"VERSION"};}
ABCresult
0XX0
10X0
1100
1111
152 return 1 if exists $pack->{ISA}
153 1 ▶ && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0 1 0 1
Condition33%2/6
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0
!ll&&!rl&&r
001
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"}
!ll&&!rl&&r
001
MC/DC0%0/3
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0
exists $pack->{"ISA"} defined *{$pack->{"ISA"};}{"ARRAY"} @{$pack->{"ISA"};} != 0
Truth table25%1/4
A and B and C
A= exists $pack->{"ISA"}B= defined *{$pack->{"ISA"};}{"ARRAY"}C= @{$pack->{"ISA"};} != 0
ABCresult
0XX0
10X0
1100
1111
154  
155 # check for any method
156 0 foreach my $name( keys %{$pack} ) {
157 0 my $entry = \$pack->{$name};
158 0 ▶ return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry ne "GLOB" or defined *{$entry;}{"CODE"} 0 0 0
Condition0%0/3
ref $entry ne "GLOB" or defined *{$entry;}{"CODE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $entry ne "GLOB" or defined *{$entry;}{"CODE"}
ref $entry ne "GLOB" defined *{$entry;}{"CODE"}
Truth table0%0/3
A or B
A= ref $entry ne "GLOB"B= defined *{$entry;}{"CODE"}
ABresult
1X1
011
000
159 }
160  
161 # fail
162 0 return 0;
163 }
164  
165  
166 6 1;
167 __END__
↑ Summary