blib/lib/DBIO.pm

filter: Statementstmt 96.2% 129 / 134 Branchbran 85.0% 51 / 60 Conditioncond 68.0% 17 / 25 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 90.9% 20 / 22 Podpod 100% 5 / 5 totaltotal 87.7% 229 / 261 CC 43 SCAR 20.1
CC
43
Cov
90%
driver_for
14.1
import
12.0
_apply_shortcut
7.0
CRAP 7.5
« blib/lib/DBIO/Storage/Async.pm ↑ Summary blib/lib/DBIO/PK.pm »
1 package DBIO;
2 # ABSTRACT: Native relational mapping for Perl, built on DBI
3  
4 348 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 348 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 348 our $VERSION = '0.900007';
8  
9 348 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 348 ▶use DBIO::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 348 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 348 ▶use DBIO::Skills ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 376 ▶sub import {
Subroutine import: called CC=12 SCAR=24.9
15 376 my ($class, @args) = @_;
16 376 my $caller = caller;
17  
18 376 my ($role, @opts);
19 376 for my $arg (@args) {
20 25 ▶ if (defined $arg && $arg =~ /^-/) { push @opts, $arg }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $arg and $arg =~ /^-/) { } 11 14 25
elsif (not defined $role) { } 14 0 14
Condition66%2/3
defined $arg and $arg =~ /^-/
!ll&&!rl&&r
01411
MC/DC50%1/2
defined $arg and $arg =~ /^-/
defined $arg $arg =~ /^-/
Truth table66%2/3
A and B
A= defined $argB= $arg =~ /^-/
ABresult
0X0
100
111
21 14 elsif (!defined $role) { $role = $arg }
22 0 else { push @opts, $arg }
23 }
24  
25 376 ▶ unless (defined $role) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $role) 362 14 376
26 362 ▶ if ($caller =~ /::Result::[^:]+$/) { $role = 'Core' }
Branch: 2 branches
BranchTrueFalseTotal
if ($caller =~ /::Result::[^:]+$/) { } 4 358 362
elsif ($caller =~ /::ResultSet::[^:]+$/) { } 1 357 358
27 1 elsif ($caller =~ /::ResultSet::[^:]+$/) { $role = 'ResultSet' }
28 357 else { $role = 'Core' }
29 }
30  
31 376 my $base = "DBIO::$role";
32 376 DBIO::Util::assert_class_name($base);
33 376 ▶ eval "require $base; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $base; 1" or die "use DBIO '${role}': cannot load ${base}: $@" 375 1 376
34 or die "use DBIO '$role': cannot load $base: $@";
35  
36 {
37 348 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 375 ▶ push @{"${caller}::ISA"}, $base unless $caller->isa($base);
Branch: 1 branch
BranchTrueFalseTotal
unless $caller->isa($base) 373 2 375
39 }
40  
41 375 strict->import;
42 375 warnings->import;
43  
44 # Schema classes get the skills-override declaration sugar (see DBIO::Skills).
45 375 ▶ _install_skill_sugar($caller) if $caller->isa('DBIO::Schema');
Branch: 1 branch
BranchTrueFalseTotal
if $caller->isa("DBIO::Schema") 11 364 375
46  
47 375 _apply_shortcut($caller, $_) for @opts;
48 }
49  
50 # Install the per-schema skills-override sugar into a schema class body:
51 #
52 # skills { 'mysql-database' => $md, ... }; # set/replace the whole map
53 # skill 'mysql-database' => $md; # merge a single entry
54 #
55 # Both write the schema's skills() classdata (DBIO::Schema). namespace::clean
56 # removes the helpers after compilation, so at runtime $schema->skill(...) and
57 # $schema->skills resolve to the inherited DBIO::Schema accessor/method rather
58 # than these declaration helpers. The classdata accessor is reached via its
59 # fully-qualified name to bypass the (still-installed) sugar during the body.
60 11 ▶sub _install_skill_sugar {
Subroutine _install_skill_sugar: called CC=1 SCAR=0.0
61 11 my ($caller) = @_;
62 348 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 348 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 11 ▶ *{"${caller}::skills"} = sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
65 1 ▶ my $map = (@_ == 1 && ref $_[0] eq 'HASH') ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 1 0 1
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
66 1 $caller->DBIO::Schema::skills($map);
67 11 };
68 11 ▶ *{"${caller}::skill"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
69 1 my ($name, $markdown) = @_;
70 1 ▶ my $cur = $caller->DBIO::Schema::skills || {};
Condition50%1/2
$caller->DBIO::Schema::skills || {}
l!l
10
MC/DC0%0/1
$caller->DBIO::Schema::skills || {}
$caller->DBIO::Schema::skills
Truth table50%1/2
A
A= $caller->DBIO::Schema::skills
Aresult
11
00
71 1 $caller->DBIO::Schema::skills({ %$cur, $name => $markdown });
72 11 };
73 11 require namespace::clean;
74 11 namespace::clean->import(-cleanee => $caller, qw(skills skill));
75 }
76  
77  
78 0 ▶sub skill { shift; DBIO::Skills->skill(@_) }
Subroutine skill: not called CC=1 SCAR=6.9
Pod documented
79  
80  
81 0 ▶sub skills { shift; DBIO::Skills->skills(@_) }
Subroutine skills: not called CC=1 SCAR=6.9
Pod documented
82  
83 # Shortcut resolution is two-tier and core never names a single driver:
84 #
85 # 1. Explicit stub DBIO::Shortcut::<key> -- a driver ships one for each
86 # curated alias (e.g. -pg, -mysql). It is lazy-loaded here (so it works even
87 # "cold") and its apply($caller) decides what to do, usually delegating to
88 # DBIO->apply_driver:
89 # package DBIO::Shortcut::pg;
90 # sub apply { DBIO->apply_driver($_[1], 'PostgreSQL') }
91 # 1;
92 #
93 # 2. No stub? Fall back to an ALREADY-LOADED driver whose name matches the key
94 # case-insensitively (-postgresql -> DBIO::PostgreSQL). The correct casing
95 # comes from the loaded symbol table, not from guessing. This is why the
96 # canonical name "just works" once the driver is loaded, with no stub.
97 11 ▶sub _apply_shortcut {
Subroutine _apply_shortcut: called CC=7 SCAR=19.5
98 11 my ($caller, $opt) = @_;
99 11 (my $key = lc $opt) =~ s/^-//;
100  
101 11 ▶ $key =~ /\A[a-z0-9_]+\z/
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /\A[a-z0-9_]+\z/ or die "use DBIO: invalid shortcut '${opt}'" 10 1 11
102 or die "use DBIO: invalid shortcut '$opt'";
103  
104 10 my $stub = "DBIO::Shortcut::$key";
105 10 ▶ if (eval { require "DBIO/Shortcut/$key.pm"; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require "DBIO/Shortcut/$key.pm"; 1 } }) 7 3 10
106 7 ▶ $stub->can('apply')
Branch: 1 branch
BranchTrueFalseTotal
$stub->can("apply") or die "use DBIO ${opt}: $stub provides no apply() method" 7 0 7
107 or die "use DBIO $opt: $stub provides no apply() method";
108 7 ▶ eval { $stub->apply($caller); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $stub->apply($caller); 1 } } or die "use DBIO ${opt}: $@" 7 0 7
109 or die "use DBIO $opt: $@";
110 7 return;
111 }
112 3 my $stub_err = $@;
113 3 ▶ die "use DBIO $opt: loading $stub failed: $stub_err"
Branch: 1 branch
BranchTrueFalseTotal
unless $stub_err =~ /\ACan't locate/ 0 3 3
114 unless $stub_err =~ /\ACan't locate/;
115  
116 3 ▶ if (my $name = _loaded_driver_for($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = _loaded_driver_for($key)) 1 2 3
117 1 DBIO->apply_driver($caller, $name);
118 1 return;
119 }
120  
121 2 die "use DBIO $opt: unknown shortcut '$opt' (no $stub, and no loaded driver "
122 . "named '$key' -- is the driver installed and loaded?)\n";
123 }
124  
125 # Find an already-loaded driver whose namespace lower-cases to $key. A driver
126 # is a DBIO::<Name> main module whose DBIO::<Name>::Storage is loadable -- this
127 # guard keeps core classes (Core, Schema, ...) from ever matching.
128 3 ▶sub _loaded_driver_for {
Subroutine _loaded_driver_for: called CC=6 SCAR=18.0
129 3 my ($key) = @_;
130 348 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
131 3 for my $ns (keys %{'DBIO::'}) {
132 91 ▶ $ns =~ s/::$// or next;
Branch: 1 branch
BranchTrueFalseTotal
$ns =~ s/::$// or next 64 27 91
133 64 ▶ next unless lc $ns eq $key;
Branch: 1 branch
BranchTrueFalseTotal
unless lc $ns eq $key 63 1 64
134 1 ▶ next unless $INC{"DBIO/$ns.pm"};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"DBIO/$ns.pm"} 0 1 1
135 1 ▶ return $ns if eval { require "DBIO/$ns/Storage.pm"; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require "DBIO/$ns/Storage.pm"; 1 } } 1 0 1
136 }
137 2 return undef;
138 }
139  
140 # Apply a driver to a class by the DBIO::<Name>::{Storage,Result} convention:
141 # pin the storage driver on a Schema, load the Result component on a Result
142 # class (only if the driver ships one). Driver shortcut stubs delegate here.
143  
144 10 ▶sub apply_driver {
Subroutine apply_driver: called CC=4 SCAR=14.0
Pod documented
145 10 my ($class, $caller, $name) = @_;
146 10 ▶ if ($caller->isa('DBIO::Schema')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($caller->isa('DBIO::Schema')) { } 9 1 10
elsif ($caller->isa('DBIO::Core')) { } 1 0 1
147 9 $caller->storage_type("+DBIO::${name}::Storage");
148 }
149 elsif ($caller->isa('DBIO::Core')) {
150 $caller->load_components("${name}::Result")
151 1 ▶ if eval { require "DBIO/${name}/Result.pm"; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require "DBIO/$name/Result.pm"; 1 } } 1 0 1
152 }
153 }
154  
155  
156 15 ▶sub driver_for {
Subroutine driver_for: called CC=14 SCAR=26.5
Pod documented
157 15 my ($class, @connect_info) = @_;
158  
159 15 my $info = $connect_info[0];
160 $info = $info->{dbh_maker} // $info->{dsn}
161 15 ▶ if ref $info eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $info eq "HASH" 3 12 15
Condition100%3/3
$info->{"dbh_maker"} // $info->{"dsn"}
l!l&&r!l&&!r
111
MC/DC100%2/2
$info->{"dbh_maker"} // $info->{"dsn"}
$info->{"dbh_maker"} $info->{"dsn"}
Truth table100%3/3
A // B
A= $info->{"dbh_maker"}B= $info->{"dsn"}
ABresult
1X1
011
000
162  
163 15 my $dbd;
164 15 ▶ if ((Scalar::Util::reftype($info) // '') eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ((builtin::reftype($info) // '') eq 'CODE') { } 4 11 15
elsif (defined $info and not ref $info) { } 9 2 11
Condition100%2/2
builtin::reftype($info) // ""
l!l
411
MC/DC100%1/1
builtin::reftype($info) // ""
builtin::reftype($info)
Truth table100%2/2
A
A= builtin::reftype($info)
Aresult
11
00
165 4 my $dbh = $info->();
166 4 ▶ $dbd = Scalar::Util::blessed($dbh) && $dbh->{Driver} && $dbh->{Driver}{Name};
Condition66%4/6
builtin::blessed($dbh) && $dbh->{'Driver'} && $dbh->{'Driver'}{'Name'}
!ll&&!rl&&r
103
builtin::blessed($dbh) and $dbh->{"Driver"}
!ll&&!rl&&r
103
MC/DC33%1/3
builtin::blessed($dbh) && $dbh->{'Driver'} && $dbh->{'Driver'}{'Name'}
builtin::blessed($dbh) $dbh->{"Driver"} $dbh->{'Driver'}{'Name'}
Truth table50%2/4
A and B && C
A= builtin::blessed($dbh)B= $dbh->{"Driver"}C= $dbh->{'Driver'}{'Name'}
ABCresult
0XX0
10X0
1100
1111
167 4 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $dbd 1 3 4
168 'DBIO->driver_for: the connection code reference returned no database '
169 . 'handle, so there is no DBD to name the driver.'
170 ) unless $dbd;
171 }
172 ▶ elsif (defined $info && !ref $info) {
Condition66%2/3
defined $info and not ref $info
!ll&&!rl&&r
209
MC/DC50%1/2
defined $info and not ref $info
defined $info not ref $info
Truth table66%2/3
A and not(B)
A= defined $infoB= not ref $info
ABresult
0X0
100
111
173 9 ($dbd) = $info =~ /^dbi:(\w*)/i;
174 9 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbd and length $dbd 1 8 9
Condition66%2/3
defined $dbd and length $dbd
!ll&&!rl&&r
018
MC/DC50%1/2
defined $dbd and length $dbd
defined $dbd length $dbd
Truth table66%2/3
A and B
A= defined $dbdB= length $dbd
ABresult
0X0
100
111
175 'DBIO->driver_for: the DSN names no DBD (dbi:<driver>:...), so there is '
176 . 'no driver to find.'
177 ) unless defined $dbd && length $dbd;
178 }
179 else {
180 2 DBIO::Exception->throw(
181 'DBIO->driver_for takes connect info with a DSN or a code reference '
182 . 'returning a database handle.'
183 );
184 }
185  
186 # The driver registry lives on the storage class. Loaded here, not by
187 # use DBIO: the pragma every result class uses does not load the storage
188 # layer.
189 11 require DBIO::Storage::DBI;
190 11 my $storage_class = DBIO::Storage::DBI->driver_storage_class($dbd);
191 11 ▶ my ($name) = defined $storage_class
Branch: 1 branch
BranchTrueFalseTotal
defined $storage_class ? : 8 3 11
192 ? $storage_class =~ /\ADBIO::(\w+)::Storage(?:::|\z)/
193 : ();
194  
195 11 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 4 7 11
196 "DBIO->driver_for: DBD::$dbd names no DBIO driver, so the connection does "
197 . 'not say which driver to use (ADR 0067). Declare the driver: '
198 . "use DBIO 'Schema', -<driver>, its component, or DBIO->apply_driver."
199 ) unless defined $name;
200  
201 7 return $name;
202 }
203  
204  
205 3 ▶sub schema_for {
Subroutine schema_for: called CC=2 SCAR=6.9
Pod documented
206 3 my ($class, @connect_info) = @_;
207  
208 3 my $driver = $class->driver_for(@connect_info);
209  
210 2 my $schema_class = "DBIO::SchemaFor::$driver";
211 2 ▶ unless ($schema_class->isa('DBIO::Schema')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_class->isa("DBIO::Schema")) 1 1 2
212 1 require DBIO::Schema;
213 {
214 348 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
215 1 @{"${schema_class}::ISA"} = ('DBIO::Schema');
216 }
217 1 $class->apply_driver($schema_class, $driver);
218 }
219  
220 2 return $schema_class->connect(@connect_info);
221 }
222  
223 348 1;
224  
225 __END__
« blib/lib/DBIO/Storage/Async.pm ↑ Summary blib/lib/DBIO/PK.pm »