DBIx/QuickDB.pm

filter: Statementstmt 45.7% 38 / 83 Branchbran 9.0% 4 / 44 Conditioncond 12.5% 2 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 75.0% 9 / 12 Podpod 100% 2 / 2 totaltotal 33.7% 56 / 166 CC 31 SCAR 60.8
CC
31
Cov
31%
build_db
702.0
import
7.8
check_driver
4.0
CRAP 435.7
↑ Summary DBIx/QuickDB/Driver/MySQLCom.pm »
1 package DBIx::QuickDB;
2 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 23 our $VERSION = '0.000047';
6  
7 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Module::Pluggable search_path => 'DBIx::QuickDB::Driver', max_depth => 4, require => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 23 my %CACHE;
13  
14 23 ▶END { local $?; %CACHE = () }
Subroutine END: called CC=1 SCAR=0.0
15  
16 sub import {
17 0 ▶ my $class = shift;
Subroutine import: not called CC=3 SCAR=20.6
18 0 my ($name, @args) = @_;
19  
20 0 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
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 23 ▶ 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 sub build_db {
32 0 ▶ my $class = shift;
Subroutine build_db: not called CC=26 SCAR=65.5
Pod documented
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 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
41 0 ▶ $spec->{cleanup} = 1 unless defined $spec->{cleanup};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"cleanup"} 0 0 0
42 0 ▶ $spec->{bootstrap} = 1 unless defined $spec->{bootstrap};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"bootstrap"} 0 0 0
43 }
44  
45 0 ▶ $spec->{autostart} = 1 unless defined $spec->{autostart};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"autostart"} 0 0 0
46 0 ▶ $spec->{autostop} = $spec->{autostart} unless defined $spec->{autostop};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"autostop"} 0 0 0
47  
48 0 my $driver;
49 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
50 0 my %nope;
51 0 for my $d (@$drivers) {
52 0 my ($v, $fqn, $why) = $class->check_driver($d, $spec);
53 0 ▶ if ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 0 0
54 0 $driver = $fqn;
55 0 last;
56 }
57 0 $nope{$d} = $why;
58 }
59  
60 0 ▶ unless ($driver) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver) 0 0 0
61 0 my @err = "== Could not find a viable driver from the following ==";
62 0 for my $d (keys %nope) {
63 0 push @err => "\n=== $d ===", $nope{$d};
64 }
65  
66 0 confess join "\n" => @err, "", "====================", "", "Aborting";
67 }
68  
69 0 my $inst = $driver->new(%$spec);
70  
71 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
72  
73 0 ▶ $inst->bootstrap if $spec->{bootstrap};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"bootstrap"} 0 0 0
74 0 ▶ $inst->start if $spec->{autostart};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"autostart"} 0 0 0
75  
76 0 ▶ if (my $sql = $spec->{load_sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $spec->{"load_sql"}) 0 0 0
77 0 ▶ $sql = $sql->{$driver->name} if ref($sql) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $sql eq "HASH" 0 0 0
78 0 ▶ $sql = [$sql] unless ref($sql) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sql eq "ARRAY" 0 0 0
79  
80 0 ▶ for (my $i = 0; $i < @$sql; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$sql) 0 0 0
81 0 my ($db, $file) = @{$sql}[$i, $i + 1];
82 0 $inst->load_sql($db => $file);
83 }
84 }
85  
86 0 return $inst;
87 }
88  
89 sub check_driver {
90 51 ▶ my $class = shift;
Subroutine check_driver: called CC=4 SCAR=13.9
Pod documented
91 51 my ($d, $spec) = @_;
92  
93 51 ▶ $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::/ 30 21 51
Condition66%2/3
$d =~ s/^\+// or $d =~ /^DBIx::QuickDB::Driver::/
l!l&&r!l&&!r
02130
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
94  
95 51 my $f = $d;
96 51 $f =~ s{::}{/}g;
97 51 $f .= ".pm";
98  
99 51 my ($v, $why);
100 51 ▶ if (eval { require $f }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require $f } }) { } 46 5 51
101 46 ($v, $why) = $d->viable($spec);
102 }
103 else {
104 5 ($v, $why) = (0, "Could not load $d: $@");
105 }
106  
107 51 return ($v, $d, $why);
108 }
109  
110 23 1;
111  
112 __END__
↑ Summary DBIx/QuickDB/Driver/MySQLCom.pm »