blib/lib/DBIx/Handler.pm

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