| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $spec = @args > 1 ? {@args} : $args[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my $spec = shift(@_) || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @_ || {}
MC/DC0%0/1
shift @_ || {}
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | return $CACHE{$name}->{inst} | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if $name && $CACHE{$name} && !$spec->{nocache}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$name and $CACHE{$name} and not $spec->{"nocache"}
$name and $CACHE{$name}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | unless ($spec->{dir}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $spec->{cleanup} = 1 unless defined $spec->{cleanup}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $spec->{bootstrap} = 1 unless defined $spec->{bootstrap}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $spec->{autostart} = 1 unless defined $spec->{autostart}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $spec->{autostop} = $spec->{autostart} unless defined $spec->{autostop}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $driver; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my $drivers = $spec->{driver} ? [$spec->{driver}] : delete $spec->{drivers} || [$class->plugins]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
delete $spec->{"drivers"} or [$class->plugins]
MC/DC0%0/1
delete $spec->{"drivers"} or [$class->plugins]
delete $spec->{"drivers"}
Truth table0%0/2
A
A= delete $spec->{"drivers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $driver = $fqn; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $nope{$d} = $why; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | unless ($driver) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$name and not $spec->{"nocache"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $inst->bootstrap if $spec->{bootstrap}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $inst->start if $spec->{autostart}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (my $sql = $spec->{load_sql}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $sql = $sql->{$driver->name} if ref($sql) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $sql = [$sql] unless ref($sql) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | for (my $i = 0; $i < @$sql; $i += 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$d =~ s/^\+// or $d =~ /^DBIx::QuickDB::Driver::/
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::/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||