blib/lib/DBIx/Handler.pm

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