ParseDSN/Default.pm

filter: Statementstmt 99.2% 127 / 128 Branchbran 81.2% 26 / 32 Conditioncond 62.5% 15 / 24 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 100% 29 / 29 Podpod 91.6% 11 / 12 totaltotal 89.9% 215 / 239 CC 26 SCAR 18.3
CC
26
Cov
91%
parse
15.3
is_local
7.5
__ANON__
6.1
CRAP 6.3
↑ Summary ParseDSN.pm »
1 package DBIx::ParseDSN::Default;
2  
3 8 ▶use v5.8.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use utf8::all;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Carp qw< carp croak confess cluck >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use DBI; # will use parse_dsn from here
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use version; our $VERSION = qv('0.9.3');
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use MooX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 8 has dsn => ( is => "ro", required => 1 );
21  
22 8 has database => ( is => "rw", alias => [qw/db dbname/] );
23 8 has host => ( is => "rw", alias => "server" );
24 8 has port => ( is => "rw" );
25 8 has driver => ( is => "rw" );
26 8 has scheme => ( is => "rw", default => "dbi" );
27  
28 has attr => (
29 handles_via => "Hash",
30 is => "ro",
31 26 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
32 8 handles => {
33 set_attr => "set",
34 get_attr => "get",
35 delete_attr => "delete",
36 attributes => "elements",
37 }
38 );
39  
40 38 ▶around host => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
41  
42 38 my $orig = shift;
43 38 my $self = shift;
44  
45 38 ▶ return $self->$orig unless my ($host) = @_;
Branch: 1 branch
BranchTrueFalseTotal
unless my($host) = 27 11 38
46  
47 11 $host =~ s/^tcp://;
48  
49 11 ▶ if ( $host =~ s/:(\d+)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ s/:(\d+)//u) 1 10 11
50 1 $self->port($1);
51 }
52  
53 11 return $self->$orig($host);
54  
55 8 };
56  
57 25 ▶sub names_for_database {
Subroutine names_for_database: called CC=1 SCAR=0.0
Pod documented
58 return (
59 25 qw/database dbname name db/,
60 "sid", ## Oracle
61 "file name", "initialcatalog", ## from ADO, but generic
62 ## enough to allow in this
63 ## module
64 );
65 }
66 26 ▶sub names_for_host {
Subroutine names_for_host: called CC=1 SCAR=0.0
Pod documented
67 26 return qw/host hostname server/;
68 }
69 26 ▶sub names_for_port {
Subroutine names_for_port: called CC=1 SCAR=0.0
Pod documented
70 26 return qw/port/;
71 }
72 26 ▶sub known_attribute_hash {
Subroutine known_attribute_hash: called CC=1 SCAR=0.0
Pod documented
73  
74 26 my $self = shift;
75 26 my %h;
76  
77 26 my @db_names = map { lc $_ } $self->names_for_database;
78 26 @h{@db_names} = ("database") x @db_names;
79  
80 26 my @h_names = map { lc $_ } $self->names_for_host;
81 26 @h{@h_names} = ("host") x @h_names;
82  
83 26 my @p_names = map { lc $_ } $self->names_for_port;
84 26 @h{@p_names} = ("port") x @p_names;
85  
86 26 return %h;
87  
88 }
89  
90 106 ▶sub dsn_parts {
Subroutine dsn_parts: called CC=1 SCAR=0.0
Pod documented
91 106 my $self = shift;
92 106 return DBI->parse_dsn( $self->dsn );
93 }
94  
95 18 ▶sub dbd_driver {
Subroutine dbd_driver: called CC=1 SCAR=0.0
Pod documented
96 18 my $self = shift;
97 18 my $driver = "DBD::" . $self->driver;
98 18 return $driver;
99 }
100 18 ▶sub driver_attr {
Subroutine driver_attr: called CC=1 SCAR=0.0
Pod documented
101  
102 18 my $self = shift;
103 18 my ( $scheme, $driver, $attr, $attr_hash, $dsn ) = $self->dsn_parts;
104  
105 18 return $attr_hash;
106  
107 }
108 44 ▶sub driver_dsn {
Subroutine driver_dsn: called CC=1 SCAR=0.0
Pod documented
109 44 my $self = shift;
110 44 return ($self->dsn_parts)[4];
111 }
112  
113 4 ▶sub is_remote {
Subroutine is_remote: called CC=1 SCAR=0.0
Pod documented
114 4 my $self = shift;
115 4 return not $self->is_local
116 }
117 6 ▶sub is_local {
Subroutine is_local: called CC=5 SCAR=20.2
Pod documented
118 6 my $self = shift;
119  
120 ## not much the default can do. if database exists as a file we
121 ## guess its a file based database and hence local
122 6 ▶ if ( defined $self->host and
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->host and lc $self->host eq 'localhost' || $self->host eq '127.0.0.1') { } 0 6 6
elsif (-f $self->database) { } 2 4 6
Condition16%1/6
defined $self->host and lc $self->host eq "localhost" || $self->host eq "127.0.0.1"
!ll&&!rl&&r
600
lc $self->host eq "localhost" or $self->host eq "127.0.0.1"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $self->host and lc $self->host eq "localhost" || $self->host eq "127.0.0.1"
defined $self->host lc $self->host eq "localhost" $self->host eq "127.0.0.1"
Truth table25%1/4
A and B or C
A= defined $self->hostB= lc $self->host eq "localhost"C= $self->host eq "127.0.0.1"
ABCresult
0XX0
1000
11X1
1011
123 (
124 lc $self->host eq "localhost" or
125 $self->host eq "127.0.0.1"
126 )
127 ) {
128 0 return 1;
129 }
130 elsif ( -f $self->database ) {
131 2 return 1;
132 }
133  
134 4 confess "Cannot determine if db is local";
135  
136 }
137  
138 26 ▶sub parse {
Subroutine parse: called CC=15 SCAR=27.3
Pod documented
139  
140 ## look for the following in the driver dsn:
141 ## 1: database: database dbname name db
142 ## 2: host: hostname host server
143 ## 3: port: port
144  
145 ## Assumes ";"-separated parameters in driver dsn
146 ## If driver dsn is one argument, its assumed to be the database
147  
148 26 my $self = shift;
149  
150 26 $self->driver( ($self->dsn_parts)[1] );
151  
152 26 my @pairs = split /;/, $self->driver_dsn;
153  
154 26 my %known_attr = $self->known_attribute_hash;
155  
156 26 for (@pairs) {
157  
158 46 my($k,$v) = split /=/, $_, 2;
159  
160 ## An Oracle special case that would otherwise mess things up
161 46 ▶ if ( $self->driver eq "Oracle" and $k eq "SERVER" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->driver eq "Oracle" and $k eq "SERVER") 1 45 46
Condition100%3/3
$self->driver eq "Oracle" and $k eq "SERVER"
!ll&&!rl&&r
34111
MC/DC100%2/2
$self->driver eq "Oracle" and $k eq "SERVER"
$self->driver eq "Oracle" $k eq "SERVER"
Truth table100%3/3
A and B
A= $self->driver eq "Oracle"B= $k eq "SERVER"
ABresult
0X0
100
111
162 ## example: SERVER=POOLED
163 1 next;
164 }
165  
166 ## a //foo:xyz/bar type of uri, like Oracle
167 45 ▶ if ( $k =~ m|^//.+/.+| and not defined $v and @pairs == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k =~ m[^//.+/.+]u and not defined $v and @pairs == 1) { } 2 43 45
elsif (not defined $v and @pairs == 1) { } 3 80 83
Condition66%4/6
$k =~ m[^//.+/.+]u and not defined $v and @pairs == 1
!ll&&!rl&&r
4302
$k =~ m[^//.+/.+]u and not defined $v
!ll&&!rl&&r
4302
MC/DC33%1/3
$k =~ m[^//.+/.+]u and not defined $v and @pairs == 1
$k =~ m[^//.+/.+]u not defined $v @pairs == 1
Truth table50%2/4
A and not(B) and C
A= $k =~ m[^//.+/.+]uB= not defined $vC= @pairs == 1
ABCresult
0XX0
10X0
1100
1111
168  
169 ## For this we offer something that works with oracle
170 2 my $u = URI->new;
171 2 $u->opaque($k);
172  
173 2 my @p = $u->path_segments;
174  
175 ## 2nd part of path is database
176 2 ▶ if ( $p[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p[1]) 2 0 2
177 2 $self->database($p[1]);
178 };
179  
180 ## host should be ok
181 2 ▶ if ( my $host = $u->authority ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $u->authority) 2 0 2
182  
183 ## might contain port
184 2 ▶ if ( $host =~ s/:(\d+)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ s/:(\d+)//u) 2 0 2
185 2 $self->port($1);
186 }
187  
188 2 $self->host( $host );
189  
190 }
191  
192 }
193 ▶ elsif (not defined $v and @pairs == 1) {
Condition66%2/3
not defined $v and @pairs == 1
!ll&&!rl&&r
4003
MC/DC50%1/2
not defined $v and @pairs == 1
not defined $v @pairs == 1
Truth table66%2/3
not(A) and B
A= not defined $vB= @pairs == 1
ABresult
0X0
100
111
194 3 $self->database($k);
195 }
196  
197 45 ▶ if ( my $known_attr = $known_attr{lc $k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $known_attr = $known_attr{lc $k}) 32 13 45
198 32 $self->$known_attr( $v );
199 }
200  
201 45 $self->set_attr($k, $v);
202  
203 }
204  
205 ## Another Oracle speciality, strip ":POOLED" from db
206 26 ▶ if ( $self->driver eq "Oracle" and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->driver eq "Oracle" and (my $new_db = $self->database) =~ s/:POOLED$//u) 1 25 26
Condition100%3/3
$self->driver eq "Oracle" and (my $new_db = $self->database) =~ s/:POOLED$//u
!ll&&!rl&&r
2051
MC/DC100%2/2
$self->driver eq "Oracle" and (my $new_db = $self->database) =~ s/:POOLED$//u
$self->driver eq "Oracle" (my $new_db = $self->database) =~ s/:POOLED$//u
Truth table100%3/3
A and B
A= $self->driver eq "Oracle"B= (my $new_db = $self->database) =~ s/:POOLED$//u
ABresult
0X0
100
111
207 ( my $new_db = $self->database ) =~ s/:POOLED$// ) {
208 1 $self->database($new_db);
209 }
210  
211 }
212  
213 ## intercept constructor to allow 1st arg DSN and 2nd arg user string,
214 ## which may contain db name
215 27 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
216  
217 27 my $orig = shift;
218 27 my $class = shift;
219  
220 27 my @args = @_;
221  
222 ## if first arg is a hash, work with that, otherwise start a new
223 ## empty hash
224 27 ▶ my %h = %{ ref $args[0] eq "HASH" ? $args[0] : {} };
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'HASH' ? : 0 27 27
225  
226 ## 1st arg can be dsn if not a hash
227 27 ▶ if ( defined $args[0] and ref $args[0] ne "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[0] and ref $args[0] ne "HASH") 26 1 27
Condition66%2/3
defined $args[0] and ref $args[0] ne "HASH"
!ll&&!rl&&r
1026
MC/DC50%1/2
defined $args[0] and ref $args[0] ne "HASH"
defined $args[0] ref $args[0] ne "HASH"
Truth table66%2/3
A and B
A= defined $args[0]B= ref $args[0] ne "HASH"
ABresult
0X0
100
111
228 26 $h{dsn} = $args[0];
229 }
230 ## look for db in user string - will not override one found in dsn
231 27 ▶ if ( defined $args[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[1]) 2 25 27
232 2 ▶ if ( $args[1] =~ /@(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[1] =~ /@(.+)$/u) 2 0 2
233 2 (my $db = $1) =~ s|/.*||;
234 2 $h{database} = $db;
235 }
236 }
237  
238 27 return $class->$orig(\%h)
239  
240 8 };
241  
242 26 ▶sub BUILD {};
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
243  
244 ## call parse after build
245 26 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
246 26 my $self = shift;
247 26 $self->parse;
248 8 };
249  
250 8 1; # Magic true value required at end of module
251 __END__
↑ Summary ParseDSN.pm »