blib/lib/DBIx/Handler.pm

filter: Statementstmt 11.2% 18 / 160 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 16.1% 5 / 31 Podpod 94.4% 17 / 18 totaltotal 11.7% 40 / 340 CC 55 SCAR 34.4
CC
55
Cov
7%
new
90.0
_connect
72.0
_run_on
42.0
CRAP 31.2
↑ Summary Build »
1 package DBIx::Handler;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 our $VERSION = '0.13';
5  
6 6 ▶use DBI 1.605;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use DBIx::TransactionManager 1.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 ▶sub _noop {}
Subroutine _noop: not called CC=1 SCAR=0.0
11  
12 6 *connect = \&new;
13 sub new {
14 0 ▶ my $class = shift;
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
15  
16 0 ▶ my $opts = scalar(@_) == 5 ? pop @_ : +{};
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 5 ? : 0 0 0
17 bless {
18 _connect_info => [@_],
19 _pid => undef,
20 _dbh => undef,
21 trace_query => $opts->{trace_query} || 0,
22 trace_ignore_if => $opts->{trace_ignore_if} || \&_noop,
23 result_class => $opts->{result_class} || undef,
24 on_connect_do => $opts->{on_connect_do} || undef,
25 on_disconnect_do => $opts->{on_disconnect_do} || undef,
26 no_ping => $opts->{no_ping} || 0,
27 0 ▶ dbi_class => $opts->{dbi_class} || "DBI",
Condition0%0/14
$opts->{'trace_query'} || 0
l!l
00
$opts->{'trace_ignore_if'} || \&DBIx::Handler::_noop
l!l
00
$opts->{'result_class'} || undef
l!l
00
$opts->{'on_connect_do'} || undef
l!l
00
$opts->{'on_disconnect_do'} || undef
l!l
00
$opts->{'no_ping'} || 0
l!l
00
$opts->{'dbi_class'} || 'DBI'
l!l
00
MC/DC0%0/1
$opts->{'trace_query'} || 0
$opts->{'trace_query'}
MC/DC0%0/1
$opts->{'trace_ignore_if'} || \&DBIx::Handler::_noop
$opts->{'trace_ignore_if'}
MC/DC0%0/1
$opts->{'result_class'} || undef
$opts->{'result_class'}
MC/DC0%0/1
$opts->{'on_connect_do'} || undef
$opts->{'on_connect_do'}
MC/DC0%0/1
$opts->{'on_disconnect_do'} || undef
$opts->{'on_disconnect_do'}
MC/DC0%0/1
$opts->{'no_ping'} || 0
$opts->{'no_ping'}
MC/DC0%0/1
$opts->{'dbi_class'} || 'DBI'
$opts->{'dbi_class'}
Truth table0%0/2
A
A= $opts->{'trace_query'}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{'trace_ignore_if'}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{'result_class'}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{'on_connect_do'}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{'on_disconnect_do'}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{'no_ping'}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{'dbi_class'}
Aresult
11
00
28 }, $class;
29 }
30  
31 sub _connect {
32 0 ▶ my $self = shift;
Subroutine _connect: not called CC=8 SCAR=42.8
33  
34 0 my $dbh = $self->{_dbh} = $self->{dbi_class}->connect(@{$self->{_connect_info}});
35 0 ▶ my $attr = @{$self->{_connect_info}} > 3 ? $self->{_connect_info}->[3] : {};
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'_connect_info'};} > 3 ? : 0 0 0
36  
37 0 ▶ if (DBI->VERSION > 1.613 && !exists $attr->{AutoInactiveDestroy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("DBI"->VERSION > "1.613" and not exists $attr->{"AutoInactiveDestroy"}) 0 0 0
Condition0%0/3
"DBI"->VERSION > "1.613" and not exists $attr->{"AutoInactiveDestroy"}
!ll&&!rl&&r
000
MC/DC0%0/2
"DBI"->VERSION > "1.613" and not exists $attr->{"AutoInactiveDestroy"}
"DBI"->VERSION > "1.613" not exists $attr->{"AutoInactiveDestroy"}
Truth table0%0/3
A and not(B)
A= "DBI"->VERSION > "1.613"B= not exists $attr->{"AutoInactiveDestroy"}
ABresult
0X0
100
111
38 0 $dbh->STORE(AutoInactiveDestroy => 1);
39 }
40  
41 0 ▶ if (!exists $attr->{RaiseError} && !exists $attr->{HandleError}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $attr->{"RaiseError"} or exists $attr->{"HandleError"}) 0 0 0
Condition0%0/3
exists $attr->{"RaiseError"} or exists $attr->{"HandleError"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $attr->{"RaiseError"} or exists $attr->{"HandleError"}
exists $attr->{"RaiseError"} exists $attr->{"HandleError"}
Truth table0%0/3
A or B
A= exists $attr->{"RaiseError"}B= exists $attr->{"HandleError"}
ABresult
1X1
011
000
42 0 $dbh->STORE(RaiseError => 1);
43 }
44  
45 0 ▶ if ($dbh->FETCH('RaiseError') && !exists $attr->{PrintError}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->FETCH("RaiseError") and not exists $attr->{"PrintError"}) 0 0 0
Condition0%0/3
$dbh->FETCH("RaiseError") and not exists $attr->{"PrintError"}
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh->FETCH("RaiseError") and not exists $attr->{"PrintError"}
$dbh->FETCH("RaiseError") not exists $attr->{"PrintError"}
Truth table0%0/3
A and not(B)
A= $dbh->FETCH("RaiseError")B= not exists $attr->{"PrintError"}
ABresult
0X0
100
111
46 0 $dbh->STORE(PrintError => 0);
47 }
48  
49 0 $self->{_pid} = $$;
50  
51 0 $self->_run_on('on_connect_do', $dbh);
52  
53 0 $dbh;
54 }
55  
56 sub dbh {
57 0 ▶ my $self = shift;
Subroutine dbh: not called CC=2 SCAR=17.9
Pod documented
58 0 ▶ $self->_seems_connected or $self->_connect;
Branch: 1 branch
BranchTrueFalseTotal
$self->_seems_connected or $self->_connect 0 0 0
59 }
60  
61 sub _ping {
62 0 ▶ my ($self, $dbh) = @_;
Subroutine _ping: not called CC=2 SCAR=17.9
63 0 ▶ $self->{no_ping} || $dbh->ping;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"no_ping"} or $dbh->ping 0 0 0
64 }
65  
66 sub _seems_connected {
67 0 ▶ my $self = shift;
Subroutine _seems_connected: not called CC=5 SCAR=34.0
68  
69 0 ▶ my $dbh = $self->{_dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->{"_dbh"} or return 0 0 0
70  
71 0 ▶ if ( $self->{_pid} != $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pid"} != $$) 0 0 0
72 0 $dbh->STORE(InactiveDestroy => 1);
73 0 $self->_in_txn_check;
74 0 delete $self->{txn_manager};
75 0 return;
76 }
77  
78 0 ▶ unless ($dbh->FETCH('Active') && $self->_ping($dbh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->FETCH("Active") and $self->_ping($dbh)) 0 0 0
Condition0%0/3
$dbh->FETCH("Active") and $self->_ping($dbh)
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh->FETCH("Active") and $self->_ping($dbh)
$dbh->FETCH("Active") $self->_ping($dbh)
Truth table0%0/3
A and B
A= $dbh->FETCH("Active")B= $self->_ping($dbh)
ABresult
0X0
100
111
79 0 $self->_in_txn_check;
80 0 $self->_disconnect;
81 0 return;
82 }
83  
84 0 $dbh;
85 }
86  
87 sub disconnect {
88 0 ▶ my $self = shift;
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
89  
90 0 ▶ $self->_seems_connected or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_seems_connected or return 0 0 0
91 0 $self->_disconnect;
92 }
93  
94 sub _disconnect {
95 0 ▶ my $self = shift;
Subroutine _disconnect: not called CC=2 SCAR=17.9
96 0 ▶ my $dbh = delete $self->{_dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = delete $self->{"_dbh"} or return 0 0 0
97 0 delete $self->{txn_manager};
98 0 $self->_run_on('on_disconnect_do', $dbh);
99 0 $dbh->STORE(CachedKids => {});
100 0 $dbh->disconnect;
101 }
102  
103 sub _run_on {
104 0 ▶ my ($self, $mode, $dbh) = @_;
Subroutine _run_on: not called CC=6 SCAR=37.4
105 0 ▶ if ( my $on_connect_do = $self->{$mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_connect_do = $self->{$mode}) 0 0 0
106 0 ▶ if (not ref($on_connect_do)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $on_connect_do) { } 0 0 0
elsif (ref $on_connect_do eq 'CODE') { } 0 0 0
elsif (ref $on_connect_do eq 'ARRAY') { } 0 0 0
107 0 $dbh->do($on_connect_do);
108 } elsif (ref($on_connect_do) eq 'CODE') {
109 0 $on_connect_do->($dbh);
110 } elsif (ref($on_connect_do) eq 'ARRAY') {
111 0 $dbh->do($_) for @$on_connect_do;
112 } else {
113 0 Carp::croak("Invalid $mode: ".ref($on_connect_do));
114 }
115 }
116 }
117  
118 0 ▶sub DESTROY { $_[0]->disconnect }
Subroutine DESTROY: not called CC=1 SCAR=6.9
119  
120 sub result_class {
121 0 ▶ my ($self, $result_class) = @_;
Subroutine result_class: not called CC=2 SCAR=17.9
Pod documented
122 0 ▶ $self->{result_class} = $result_class if $result_class;
Branch: 1 branch
BranchTrueFalseTotal
if $result_class 0 0 0
123 0 $self->{result_class};
124 }
125  
126 sub trace_query {
127 0 ▶ my ($self, $flag) = @_;
Subroutine trace_query: not called CC=2 SCAR=17.9
Pod documented
128 0 ▶ $self->{trace_query} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 0 0 0
129 0 $self->{trace_query};
130 }
131  
132 sub trace_ignore_if {
133 0 ▶ my ($self, $callback) = @_;
Subroutine trace_ignore_if: not called CC=2 SCAR=17.9
Pod documented
134 0 ▶ $self->{trace_ignore_if} = $callback if defined $callback;
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback 0 0 0
135 0 $self->{trace_ignore_if};
136 }
137  
138 sub no_ping {
139 0 ▶ my ($self, $enable) = @_;
Subroutine no_ping: not called CC=2 SCAR=17.9
Pod documented
140 0 ▶ $self->{no_ping} = $enable if defined $enable;
Branch: 1 branch
BranchTrueFalseTotal
if defined $enable 0 0 0
141 0 $self->{no_ping};
142 }
143  
144 sub query {
145 0 ▶ my ($self, $sql, @args) = @_;
Subroutine query: not called CC=6 SCAR=37.4
Pod documented
146  
147 0 my $bind;
148 0 ▶ if (ref($args[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq 'HASH') { } 0 0 0
149 0 ($sql, $bind) = $self->replace_named_placeholder($sql, $args[0]);
150 }
151 else {
152 0 ▶ $bind = ref($args[0]) eq 'ARRAY' ? $args[0] : \@args;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'ARRAY' ? : 0 0 0
153 }
154  
155 0 $sql = $self->trace_query_set_comment($sql);
156  
157 0 my $sth;
158 0 eval {
159 0 $sth = $self->dbh->prepare($sql);
160 0 ▶ $sth->execute(@{$bind || []});
Branch: 1 branch
BranchTrueFalseTotal
$bind or [] 0 0 0
161 };
162 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
163 0 Carp::croak($error);
164 }
165  
166 0 my $result_class = $self->result_class;
167 0 ▶ $result_class ? $result_class->new($self, $sth) : $sth;
Branch: 1 branch
BranchTrueFalseTotal
$result_class ? : 0 0 0
168 }
169  
170 sub replace_named_placeholder {
171 0 ▶ my ($self, $sql, $args) = @_;
Subroutine replace_named_placeholder: not called CC=4 SCAR=30.0
Pod undocumented
172  
173 0 my %named_bind = %{$args};
174 0 my @bind;
175 0 $sql =~ s{:(\w+)}{
176 0 ▶ Carp::croak("$1 does not exists in hash") if !exists $named_bind{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $named_bind{$1} 0 0 0
177 0 ▶ if ( ref $named_bind{$1} && ref $named_bind{$1} eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $named_bind{$1} and ref $named_bind{$1} eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $named_bind{$1} and ref $named_bind{$1} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $named_bind{$1} and ref $named_bind{$1} eq "ARRAY"
ref $named_bind{$1} ref $named_bind{$1} eq "ARRAY"
Truth table0%0/3
A and B
A= ref $named_bind{$1}B= ref $named_bind{$1} eq "ARRAY"
ABresult
0X0
100
111
178 0 push @bind, @{ $named_bind{$1} };
179 0 my $tmp = join ',', map { '?' } @{ $named_bind{$1} };
180 0 "($tmp)";
181 } else {
182 0 push @bind, $named_bind{$1};
183 0 '?'
184 }
185 }ge;
186  
187 0 return ($sql, \@bind);
188 }
189  
190 sub trace_query_set_comment {
191 0 ▶ my ($self, $sql) = @_;
Subroutine trace_query_set_comment: not called CC=5 SCAR=34.0
Pod documented
192 0 ▶ return $sql unless $self->trace_query;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->trace_query 0 0 0
193  
194 0 my $i = 1;
195 0 ▶ while ( my (@caller) = caller($i++) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i++) 0 0 0
196 0 ▶ next if ( $caller[0]->isa( __PACKAGE__ ) );
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0]->isa(__PACKAGE__) 0 0 0
197 0 ▶ next if $self->trace_ignore_if->(@caller);
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace_ignore_if->(@caller) 0 0 0
198 0 my $comment = "$caller[1] at line $caller[2]";
199 0 $comment =~ s/\*\// /g;
200 0 $sql = "/* $comment */ $sql";
201 0 last;
202 }
203  
204 0 $sql;
205 }
206  
207 sub run {
208 0 ▶ my ($self, $coderef) = @_;
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
209 0 my $wantarray = wantarray;
210  
211 0 my @ret = eval {
212 0 my $dbh = $self->dbh;
213 0 ▶ $wantarray ? $coderef->($dbh) : scalar $coderef->($dbh);
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
214 };
215 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
216 0 Carp::croak($error);
217 }
218  
219 0 ▶ $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
220 }
221  
222 # --------------------------------------------------------------------------------
223 # for transaction
224 sub txn_manager {
225 0 ▶ my $self = shift;
Subroutine txn_manager: not called CC=2 SCAR=17.9
Pod documented
226  
227 0 my $dbh = $self->dbh;
228 0 ▶ $self->{txn_manager} ||= DBIx::TransactionManager->new($dbh);
Condition0%0/3
$self->{"txn_manager"} ||= "DBIx::TransactionManager"->new($dbh)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"txn_manager"} ||= "DBIx::TransactionManager"->new($dbh)
$self->{"txn_manager"} "DBIx::TransactionManager"->new($dbh)
Truth table0%0/3
A ||= B
A= $self->{"txn_manager"}B= "DBIx::TransactionManager"->new($dbh)
ABresult
1X1
011
000
229 }
230  
231 sub in_txn {
232 0 ▶ my $self = shift;
Subroutine in_txn: not called CC=2 SCAR=17.9
Pod documented
233 0 ▶ return unless $self->{txn_manager};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"txn_manager"} 0 0 0
234 0 return $self->{txn_manager}->in_transaction;
235 }
236  
237 sub _in_txn_check {
238 0 ▶ my $self = shift;
Subroutine _in_txn_check: not called CC=2 SCAR=17.9
239  
240 0 my $info = $self->in_txn;
241 0 ▶ return unless $info;
Branch: 1 branch
BranchTrueFalseTotal
unless $info 0 0 0
242  
243 0 my $caller = $info->{caller};
244 0 my $pid = $info->{pid};
245 0 Carp::confess("Detected transaction during a connect operation (last known transaction at $caller->[1] line $caller->[2], pid $pid). Refusing to proceed at");
246 }
247  
248 sub txn_scope {
249 0 ▶ my @caller = caller();
Subroutine txn_scope: not called CC=1 SCAR=6.9
Pod documented
250 0 $_[0]->txn_manager->txn_scope(caller => \@caller);
251 }
252  
253 sub txn {
254 0 ▶ my ($self, $coderef) = @_;
Subroutine txn: not called CC=5 SCAR=34.0
Pod documented
255  
256 0 my $wantarray = wantarray;
257 0 my $txn = $self->txn_scope;
258  
259 0 my @ret = eval {
260 0 my $dbh = $self->dbh;
261 0 ▶ $wantarray ? $coderef->($dbh) : scalar $coderef->($dbh);
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
262 };
263  
264 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) { } 0 0 0
265 0 $txn->rollback;
266 0 Carp::croak($error);
267 } else {
268 0 eval { $txn->commit };
269 0 ▶ Carp::croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
270 }
271  
272 0 ▶ $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
273 }
274  
275 0 ▶sub txn_begin { $_[0]->txn_manager->txn_begin }
Subroutine txn_begin: not called CC=1 SCAR=6.9
Pod documented
276 0 ▶sub txn_rollback { $_[0]->txn_manager->txn_rollback }
Subroutine txn_rollback: not called CC=1 SCAR=6.9
Pod documented
277 0 ▶sub txn_commit { $_[0]->txn_manager->txn_commit }
Subroutine txn_commit: not called CC=1 SCAR=6.9
Pod documented
278  
279 6 1;
280  
281 __END__
↑ Summary Build »