blib/lib/DBIx/Squirrel.pm

filter: Statementstmt 100% 80 / 80 Branchbran 92.8% 26 / 28 Conditioncond 72.4% 21 / 29 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 100% 19 / 19 Podpod 100% 2 / 2 totaltotal 89.6% 156 / 174 CC 30 SCAR 14.6
CC
30
Cov
93%
connect
9.1
_split_attrs
5.2
_dialect
5.0
CRAP 4.3
« blib/lib/DBIx/Squirrel/st.pm ↑ Summary blib/lib/DBIx/Squirrel/Util.pm »
1 28 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package DBIx::Squirrel;
4  
5 28 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use DBI qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use DBI::Const::GetInfoType qw(%GetInfoType);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Ref::Util qw(is_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 ▶use DBIx::Squirrel::Stop qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use DBIx::Squirrel::Util qw(canonical split_attrs);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use DBIx::Squirrel::db qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use DBIx::Squirrel::st qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 28 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 28 our @ISA = 'DBI';
18 28 our $VERSION = 'v3.4.1';
19  
20 # Export what the DBI exports, such as :sql_types, to the package using
21 # DBIx::Squirrel. The names are the DBI's, so the DBI's import exports
22 # them: goto leaves no frame of this sub's, so it exports them to the
23 # package that used DBIx::Squirrel, and reports a mistake at its line.
24 # It's a sub without a signature, as goto passes @_.
25 31 ▶sub import { ## no critic (Subroutines::RequireArgUnpacking)
Subroutine import: called CC=2 SCAR=6.9
26 31 ▶ return if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 27 4 31
27 4 @_ = ('DBI', @_[1 .. $#_]);
28 4 goto &{ DBI->can('import') };
29 }
30  
31 # Return the handle of the connection to the requested data source.
32 127 ▶sub connect ($class, $source, @args) {
Subroutine connect: called CC=9 SCAR=22.1
Pod documented
33 127 ▶ return _connect_dbh($class, $source, @args)
Branch: 1 branch
BranchTrueFalseTotal
if $source isa "DBI::db" 8 119 127
34 if $source isa 'DBI::db';
35 119 ▶ my $username = is_hashref($args[0]) ? undef : shift @args;
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_hashref($args[0]) ? : 2 117 119
36 119 ▶ my $password = is_hashref($args[0]) ? undef : shift @args;
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_hashref($args[0]) ? : 2 117 119
37 119 ▶ my $attrs = is_hashref($args[0]) ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_hashref($args[0]) ? : 119 0 119
38 119 my($dbi_attrs, $options) = _split_attrs($class, $attrs);
39  
40 # The options go to the DBI as a string, so connect_cached, which keys
41 # its cache on every attribute's value, finds a handle made with the
42 # same ones. A handle it finds has them already.
43 119 ▶ $dbi_attrs->{private_ekorn_options} = canonical($options) if %$options;
Branch: 1 branch
BranchTrueFalseTotal
if %$options 10 109 119
44 119 my $dbh = $class->SUPER::connect($source, $username, $password, $dbi_attrs);
45 118 ▶ croak _connect_failed($class, $source, $attrs->{Username} // $username)
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 4 114 118
Condition33%1/3
$attrs->{"Username"} // $username
l!l&&r!l&&!r
040
MC/DC0%0/2
$attrs->{"Username"} // $username
$attrs->{"Username"} $username
Truth table33%1/3
A // B
A= $attrs->{"Username"}B= $username
ABresult
1X1
011
000
46 unless $dbh;
47 114 ▶ $dbh->{private_ekorn} //= {Options => $options};
Condition100%2/2
$dbh->{"private_ekorn"} //= {"Options", $options}
l!l
13101
MC/DC100%1/1
$dbh->{"private_ekorn"} //= {"Options", $options}
$dbh->{"private_ekorn"}
Truth table100%2/2
A
A= $dbh->{"private_ekorn"}
Aresult
11
00
48 114 return _discern_dialect($dbh);
49 }
50  
51 # Return the handle of a cached connection to the requested data source,
52 # as the DBI's connect_cached does, given its arguments as connect takes
53 # them. Given the attributes straight after a data source, the username
54 # and password go in front of them: the DBI's connect_cached would take
55 # the attributes for the username, and give connect attributes of its
56 # own, which connect, finding the shortcut, wouldn't read, so it would
57 # connect afresh each time. A handle is given as it is.
58 33 ▶sub connect_cached ($class, $source, @args) {
Subroutine connect_cached: called CC=3 SCAR=11.0
Pod documented
59 33 ▶ unshift @args, undef, undef if !ref $source && is_hashref($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
if not ref $source and Ref::Util::XS::is_hashref($args[0]) 6 27 33
Condition100%3/3
not ref $source and Ref::Util::XS::is_hashref($args[0])
!ll&&!rl&&r
1266
MC/DC100%2/2
not ref $source and Ref::Util::XS::is_hashref($args[0])
not ref $source Ref::Util::XS::is_hashref($args[0])
Truth table100%3/3
not(A) and B
A= not ref $sourceB= Ref::Util::XS::is_hashref($args[0])
ABresult
0X0
100
111
60 33 return $class->SUPER::connect_cached($source, @args);
61 }
62  
63  
64 # Return the handle of a new connection using the same parameters as
65 # another connection.
66 8 ▶sub _connect_dbh ($class, $dbh, @args) {
Subroutine _connect_dbh: called CC=4 SCAR=14.2
67 8 ▶ my $attrs = @args && is_hashref($args[0]) ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
@args && Ref::Util::XS::is_hashref($args[0]) ? : 2 6 8
Condition66%2/3
@args and Ref::Util::XS::is_hashref($args[0])
!ll&&!rl&&r
602
MC/DC50%1/2
@args and Ref::Util::XS::is_hashref($args[0])
@args Ref::Util::XS::is_hashref($args[0])
Truth table66%2/3
A and B
A= @argsB= Ref::Util::XS::is_hashref($args[0])
ABresult
0X0
100
111
68 my $clone = $dbh->clone(_connect_attrs($class, $attrs, $dbh->{private_ekorn}))
69 8 ▶ or croak _connect_failed($class, $dbh->{Name}, $dbh->{Username});
Branch: 1 branch
BranchTrueFalseTotal
my $clone = $dbh->clone(_connect_attrs($class, $attrs, $dbh->{"private_ekorn"})) or croak(_connect_failed($class, $dbh->{"Name"}, $dbh->{"Username"})) 8 0 8
70 8 return _discern_dialect($clone);
71 }
72  
73 # Return the message the DBI raises under RaiseError when it can't
74 # connect, without the dbi:Driver: prefix of the data source, as the
75 # DBI's message has none.
76 4 ▶sub _connect_failed ($class, $source, $username) {
Subroutine _connect_failed: called CC=3 SCAR=11.7
77 4 my $dsn = $source =~ s( \A dbi: \w*? (?: \( .*? \) )? : )()xir;
78 4 ▶ return "$class connect('$dsn','@{[ $username // '' ]}',...) failed: "
Condition50%2/4
$username // ""
l!l
40
$DBI::errstr // "(no error string)"
l!l
40
MC/DC0%0/1
$username // ""
$username
MC/DC0%0/1
$DBI::errstr // "(no error string)"
$DBI::errstr
Truth table50%1/2
A
A= $username
Aresult
11
00
Truth table50%1/2
A
A= $DBI::errstr
Aresult
11
00
79 . ($DBI::errstr // '(no error string)');
80 }
81  
82 # Return the attributes to connect with, for the DBI, and DBIx::Squirrel's
83 # own, those starting "ekorn_", the defaults for the statements the handle
84 # prepares, over any a cloned handle already had.
85 127 ▶sub _split_attrs ($class, $attrs, $state = undef) {
Subroutine _split_attrs: called CC=5 SCAR=16.6
86 127 my($dbi_attrs, $ekorn) = split_attrs($attrs);
87 127 ▶ $dbi_attrs->{RootClass} = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
01270
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
88 127 ▶ my %options = (%{ $state && $state->{Options} // {} }, %$ekorn);
Condition80%4/5
$state && $state->{"Options"} // {}
l!l
7120
$state and $state->{"Options"}
!ll&&!rl&&r
12007
MC/DC50%1/2
$state && $state->{"Options"} // {}
$state $state->{"Options"}
Truth table66%2/3
A and B
A= $stateB= $state->{"Options"}
ABresult
111
0X0
100
89 127 return $dbi_attrs, \%options;
90 }
91  
92 # Return the attributes to clone a handle with, DBIx::Squirrel's own kept
93 # in the private_ekorn attribute.
94 8 ▶sub _connect_attrs ($class, $attrs, $state = undef) {
Subroutine _connect_attrs: called CC=3 SCAR=11.0
95 8 my($dbi_attrs, $options) = _split_attrs($class, $attrs, $state);
96 8 ▶ $dbi_attrs->{private_ekorn} = {Options => $options} if %$options;
Branch: 1 branch
BranchTrueFalseTotal
if %$options 7 1 8
97 8 return $dbi_attrs;
98 }
99  
100 # Give a handle the dialect its statements are read by, unless it was
101 # given one, and return it.
102 122 ▶sub _discern_dialect ($dbh) {
Subroutine _discern_dialect: called CC=4 SCAR=13.9
103 122 ▶ my $state = $dbh->{private_ekorn} // {};
Condition100%2/2
$dbh->{"private_ekorn"} // {}
l!l
1211
MC/DC100%1/1
$dbh->{"private_ekorn"} // {}
$dbh->{"private_ekorn"}
Truth table100%2/2
A
A= $dbh->{"private_ekorn"}
Aresult
11
00
104 122 ▶ my $options = $state->{Options} // {};
Condition100%2/2
$state->{"Options"} // {}
l!l
1211
MC/DC100%1/1
$state->{"Options"} // {}
$state->{"Options"}
Truth table100%2/2
A
A= $state->{"Options"}
Aresult
11
00
105 return $dbh
106 122 ▶ if defined $options->{ekorn_dialect};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"ekorn_dialect"} 21 101 122
107 $dbh->{private_ekorn} = {
108 101 %$state,
109 Options => {%$options, ekorn_dialect => _dialect($dbh)},
110 };
111 101 return $dbh;
112 }
113  
114 # Return the dialect a handle's statements are read by: its driver's name,
115 # or, for a driver that passes the connection on to another, such as
116 # DBD::ODBC or DBD::Proxy, the name of the driver for the database behind
117 # it, if the database says what it is and it's one with rules of its own.
118 107 ▶sub _dialect ($dbh) {
Subroutine _dialect: called CC=5 SCAR=16.1
119 107 state %PASSES_ON = map { ($_ => 1) } qw(ADO Gofer JDBC ODBC Proxy);
120 107 state @DIALECTS = (
121 [qr( mariadb )xi => 'MariaDB'],
122 [qr( mysql )xi => 'mysql'],
123 [qr( postgres )xi => 'Pg'],
124 [qr( sqlite )xi => 'SQLite'],
125 );
126 107 my $driver = $dbh->{Driver}{Name};
127 return $driver
128 107 ▶ unless $PASSES_ON{$driver};
Branch: 1 branch
BranchTrueFalseTotal
unless $PASSES_ON{$driver} 101 6 107
129 6 local $dbh->{RaiseError} = 0;
130 6 local $dbh->{PrintError} = 0;
131 6 ▶ my $dbms = $dbh->get_info($GetInfoType{SQL_DBMS_NAME}) // '';
Condition100%2/2
$dbh->get_info($DBIx::Squirrel::GetInfoType{"SQL_DBMS_NAME"}) // ""
l!l
51
MC/DC100%1/1
$dbh->get_info($DBIx::Squirrel::GetInfoType{"SQL_DBMS_NAME"}) // ""
$dbh->get_info($DBIx::Squirrel::GetInfoType{"SQL_DBMS_NAME"})
Truth table100%2/2
A
A= $dbh->get_info($DBIx::Squirrel::GetInfoType{"SQL_DBMS_NAME"})
Aresult
11
00
132 6 for my $dialect (@DIALECTS) {
133 18 ▶ return $dialect->[1] if $dbms =~ $dialect->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $dbms =~ /$dialect->[0]/u 4 14 18
134 }
135 2 return $driver;
136 }
137  
138 28 1;
139  
140 __END__
« blib/lib/DBIx/Squirrel/st.pm ↑ Summary blib/lib/DBIx/Squirrel/Util.pm »