blib/lib/DBIx/QuickDB.pm

filter: Statementstmt 49.4% 41 / 83 Branchbran 11.3% 5 / 44 Conditioncond 12.5% 2 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 83.3% 10 / 12 Podpod 100% 2 / 2 totaltotal 36.7% 61 / 166 CC 31 SCAR 60.8
CC
31
Cov
34%
build_db
702.0
import
4.6
check_driver
4.0
CRAP 435.5
↑ Summary blib/lib/DBIx/QuickDB/Driver/MySQLCom.pm »
1 package DBIx::QuickDB;
2 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 34 our $VERSION = '0.000066';
6  
7 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Module::Pluggable search_path => 'DBIx::QuickDB::Driver', max_depth => 4, require => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 34 my %CACHE;
13  
14 34 ▶END { local $?; %CACHE = () }
Subroutine END: called CC=1 SCAR=0.0
15  
16 3 ▶sub import {
Subroutine import: called CC=3 SCAR=15.3
17 3 my $class = shift;
18 3 my ($name, @args) = @_;
19  
20 3 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 3 0 3
21  
22 0 ▶ my $spec = @args > 1 ? {@args} : $args[0];
Branch: 1 branch
BranchTrueFalseTotal
@args > 1 ? : 0 0 0
23  
24 0 my $db = $class->build_db($name, $spec);
25  
26 0 my $caller = caller;
27 34 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 0 ▶ *{"$caller\::$name"} = sub() { $db };
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 }
30  
31 0 ▶sub build_db {
Subroutine build_db: not called CC=26 SCAR=65.5
Pod documented
32 0 my $class = shift;
33 0 ▶ my $name = ref($_[0]) ? undef : shift(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
34 0 ▶ my $spec = shift(@_) || {};
Condition0%0/2
shift @_ || {}
l!l
00
MC/DC0%0/1
shift @_ || {}
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
35  
36 return $CACHE{$name}->{inst}
37 0 ▶ if $name && $CACHE{$name} && !$spec->{nocache};
Branch: 1 branch
BranchTrueFalseTotal
if $name and $CACHE{$name} and not $spec->{"nocache"} 0 0 0
Condition0%0/6
$name and $CACHE{$name} and not $spec->{"nocache"}
!ll&&!rl&&r
000
$name and $CACHE{$name}
!ll&&!rl&&r
000
MC/DC0%0/3
$name and $CACHE{$name} and not $spec->{"nocache"}
$name $CACHE{$name} not $spec->{"nocache"}
Truth table0%0/4
A and B and not(C)
A= $nameB= $CACHE{$name}C= not $spec->{"nocache"}
ABCresult
0XX0
10X0
1100
1111
38  
39 0 ▶ unless ($spec->{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec->{"dir"}) 0 0 0
40 # $$ in the template: File::Temp draws names from rand(), and forked
41 # processes (or processes given the same srand seed, e.g. by
42 # Test2::Plugin::SRand) draw identical name sequences. Names must
43 # differ by pid or two processes can converge on one dir -- the
44 # EEXIST retry does not help when the first dir has already been
45 # cleaned up and the second process recreates it while a stale
46 # watcher deletes it by path.
47 0 ▶ $spec->{dir} = tempdir("DB-QUICK-$$-XXXXXXXX", CLEANUP => 0, $ENV{QDB_TMPDIR} ? (DIR => $ENV{QDB_TMPDIR}) : (TMPDIR => 1));
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'QDB_TMPDIR'} ? : 0 0 0
48 0 ▶ $spec->{cleanup} = 1 unless defined $spec->{cleanup};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"cleanup"} 0 0 0
49 0 ▶ $spec->{bootstrap} = 1 unless defined $spec->{bootstrap};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"bootstrap"} 0 0 0
50 }
51  
52 0 ▶ $spec->{autostart} = 1 unless defined $spec->{autostart};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"autostart"} 0 0 0
53 0 ▶ $spec->{autostop} = $spec->{autostart} unless defined $spec->{autostop};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"autostop"} 0 0 0
54  
55 0 my $driver;
56 0 ▶ my $drivers = $spec->{driver} ? [$spec->{driver}] : delete $spec->{drivers} || [$class->plugins];
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'driver'} ? : 0 0 0
Condition0%0/2
delete $spec->{"drivers"} or [$class->plugins]
l!l
00
MC/DC0%0/1
delete $spec->{"drivers"} or [$class->plugins]
delete $spec->{"drivers"}
Truth table0%0/2
A
A= delete $spec->{"drivers"}
Aresult
11
00
57 0 my %nope;
58 0 for my $d (@$drivers) {
59 0 my ($v, $fqn, $why) = $class->check_driver($d, $spec);
60 0 ▶ if ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 0 0
61 0 $driver = $fqn;
62 0 last;
63 }
64 0 $nope{$d} = $why;
65 }
66  
67 0 ▶ unless ($driver) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver) 0 0 0
68 0 my @err = "== Could not find a viable driver from the following ==";
69 0 for my $d (keys %nope) {
70 0 push @err => "\n=== $d ===", $nope{$d};
71 }
72  
73 0 confess join "\n" => @err, "", "====================", "", "Aborting";
74 }
75  
76 0 my $inst = $driver->new(%$spec);
77  
78 0 ▶ $CACHE{$name} = {spec => $spec, inst => $inst} if $name && !$spec->{nocache};
Branch: 1 branch
BranchTrueFalseTotal
if $name and not $spec->{"nocache"} 0 0 0
Condition0%0/3
$name and not $spec->{"nocache"}
!ll&&!rl&&r
000
MC/DC0%0/2
$name and not $spec->{"nocache"}
$name not $spec->{"nocache"}
Truth table0%0/3
A and not(B)
A= $nameB= not $spec->{"nocache"}
ABresult
0X0
100
111
79  
80 0 ▶ $inst->bootstrap if $spec->{bootstrap};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"bootstrap"} 0 0 0
81 0 ▶ $inst->start if $spec->{autostart};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"autostart"} 0 0 0
82  
83 0 ▶ if (my $sql = $spec->{load_sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $spec->{"load_sql"}) 0 0 0
84 0 ▶ $sql = $sql->{$driver->name} if ref($sql) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $sql eq "HASH" 0 0 0
85 0 ▶ $sql = [$sql] unless ref($sql) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sql eq "ARRAY" 0 0 0
86  
87 0 ▶ for (my $i = 0; $i < @$sql; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$sql) 0 0 0
88 0 my ($db, $file) = @{$sql}[$i, $i + 1];
89 0 $inst->load_sql($db => $file);
90 }
91 }
92  
93 0 return $inst;
94 }
95  
96 59 ▶sub check_driver {
Subroutine check_driver: called CC=4 SCAR=13.9
Pod documented
97 59 my $class = shift;
98 59 my ($d, $spec) = @_;
99  
100 59 ▶ $d = "DBIx::QuickDB::Driver::$d" unless $d =~ s/^\+// || $d =~ m/^DBIx::QuickDB::Driver::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $d =~ s/^\+// or $d =~ /^DBIx::QuickDB::Driver::/ 38 21 59
Condition66%2/3
$d =~ s/^\+// or $d =~ /^DBIx::QuickDB::Driver::/
l!l&&r!l&&!r
02138
MC/DC50%1/2
$d =~ s/^\+// or $d =~ /^DBIx::QuickDB::Driver::/
$d =~ s/^\+// $d =~ /^DBIx::QuickDB::Driver::/
Truth table66%2/3
A or B
A= $d =~ s/^\+//B= $d =~ /^DBIx::QuickDB::Driver::/
ABresult
1X1
011
000
101  
102 59 my $f = $d;
103 59 $f =~ s{::}{/}g;
104 59 $f .= ".pm";
105  
106 59 my ($v, $why);
107 59 ▶ if (eval { require $f }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require $f } }) { } 54 5 59
108 54 ($v, $why) = $d->viable($spec);
109 }
110 else {
111 5 ($v, $why) = (0, "Could not load $d: $@");
112 }
113  
114 59 return ($v, $d, $why);
115 }
116  
117 34 1;
118  
119 __END__
↑ Summary blib/lib/DBIx/QuickDB/Driver/MySQLCom.pm »