blib/lib/DBIx/Tracer.pm

filter: Statementstmt 30.9% 43 / 139 Branchbran 3.8% 2 / 52 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 45.0% 9 / 20 Podpod 100% 1 / 1 totaltotal 24.5% 55 / 224 SCAR 61.7
CC
31
Cov
23%
new
74.9
__ANON__
56.0
__ANON__
31.3
CRAP 476.4
↑ Summary Build »
1 package DBIx::Tracer;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.02';
6  
7 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $org_execute = \&DBI::st::execute;
12 1 my $org_bind_param = \&DBI::st::bind_param;
13 1 my $org_db_do = \&DBI::db::do;
14 1 my $org_db_selectall_arrayref = \&DBI::db::selectall_arrayref;
15 1 my $org_db_selectrow_arrayref = \&DBI::db::selectrow_arrayref;
16 1 my $org_db_selectrow_array = \&DBI::db::selectrow_array;
17  
18 1 ▶my $has_mysql = eval { require DBD::mysql; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require DBD::mysql; 1 } } ? : 0 1 1
19 1 ▶my $pp_mode = $INC{'DBI/PurePerl.pm'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$INC{'DBI/PurePerl.pm'} ? : 0 1 1
20  
21 1 my $st_execute;
22 my $st_bind_param;
23 1 my $db_do;
24 1 my $selectall_arrayref;
25 1 my $selectrow_arrayref;
26 1 my $selectrow_array;
27  
28 1 our $OUTPUT;
29  
30 sub new {
31 0 ▶ my $class = shift;
Subroutine new: not called CC=9 SCAR=43.2
Pod documented
32  
33 # argument processing
34 0 my %args;
35 0 ▶ if (@_==1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
36 0 ▶ if (ref $_[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 0 0 0
37 0 $args{code} = $_[0];
38 } else {
39 0 %args = %{$_[0]};
40 }
41 } else {
42 0 %args = @_;
43 }
44 0 for (qw(code)) {
45 0 ▶ unless ($args{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{$_}) 0 0 0
46 0 croak "Missing mandatory parameter $_ for DBIx::Tracer->new";
47 }
48 }
49  
50 0 my $logger = $args{code};
51  
52 # create object
53 0 my $self = bless \%args, $class;
54  
55 # wrap methods
56 0 my $st_execute = $class->_st_execute($org_execute, $logger);
57 0 $st_bind_param = $class->_st_bind_param($org_bind_param, $logger);
58 0 ▶ $db_do = $class->_db_do($org_db_do, $logger) if $has_mysql;
Branch: 1 branch
BranchTrueFalseTotal
if $has_mysql 0 0 0
59 0 ▶ unless ($pp_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pp_mode) 0 0 0
60 0 $selectall_arrayref = $class->_select_array($org_db_selectall_arrayref, 0, $logger);
61 0 $selectrow_arrayref = $class->_select_array($org_db_selectrow_arrayref, 0, $logger);
62 0 $selectrow_array = $class->_select_array($org_db_selectrow_array, 1, $logger);
63 }
64  
65 1 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 0 *DBI::st::execute = $st_execute;
67 0 *DBI::st::bind_param = $st_bind_param;
68 0 ▶ *DBI::db::do = $db_do if $has_mysql;
Branch: 1 branch
BranchTrueFalseTotal
if $has_mysql 0 0 0
69 0 ▶ unless ($pp_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pp_mode) 0 0 0
70 0 *DBI::db::selectall_arrayref = $selectall_arrayref;
71 0 *DBI::db::selectrow_arrayref = $selectrow_arrayref;
72 0 *DBI::db::selectrow_array = $selectrow_array;
73 }
74  
75 0 return $self;
76 }
77  
78 sub DESTROY {
79 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=3 SCAR=20.3
80  
81 1 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
82 0 *DBI::st::execute = $org_execute;
83 0 *DBI::st::bind_param = $org_bind_param;
84 0 ▶ *DBI::db::do = $org_db_do if $has_mysql;
Branch: 1 branch
BranchTrueFalseTotal
if $has_mysql 0 0 0
85 0 ▶ unless ($pp_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pp_mode) 0 0 0
86 0 *DBI::db::selectall_arrayref = $org_db_selectall_arrayref;
87 0 *DBI::db::selectrow_arrayref = $org_db_selectrow_arrayref;
88 0 *DBI::db::selectrow_array = $org_db_selectrow_array;
89 }
90 }
91  
92 # -------------------------------------------------------------------------
93 # wrapper methods.
94  
95 sub _st_execute {
96 0 ▶ my ($class, $org, $logger) = @_;
Subroutine _st_execute: not called CC=1 SCAR=6.9
97  
98 return sub {
99 0 ▶ my $sth = shift;
Subroutine __ANON__: not called CC=7 SCAR=40.3
100 0 my @params = @_;
101 0 my @types;
102  
103 0 my $dbh = $sth->{Database};
104 0 my $ret = $sth->{Statement};
105 0 ▶ if (my $attrs = $sth->{private_DBIx_Tracer_attrs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attrs = $sth->{"private_DBIx_Tracer_attrs"}) 0 0 0
106 0 my $bind_params = $sth->{private_DBIx_Tracer_params};
107 0 for my $i (1..@$attrs) {
108 0 push @types, $attrs->[$i - 1]{TYPE};
109 0 ▶ push @params, $bind_params->[$i - 1] if $bind_params;
Branch: 1 branch
BranchTrueFalseTotal
if $bind_params 0 0 0
110 }
111 }
112 0 $sth->{private_DBIx_Tracer_params} = undef;
113  
114 0 my $begin = [gettimeofday];
115 0 ▶ my $wantarray = wantarray ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
116 0 ▶ my $res = $wantarray ? [$org->($sth, @_)] : scalar $org->($sth, @_);
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
117 0 my $time = tv_interval($begin, [gettimeofday]);
118  
119 0 $class->_logging($logger, $dbh, $ret, $time, \@params);
120  
121 0 ▶ return $wantarray ? @$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
122 0 };
123 }
124  
125 sub _st_bind_param {
126 0 ▶ my ($class, $org) = @_;
Subroutine _st_bind_param: not called CC=1 SCAR=6.9
127  
128 return sub {
129 0 ▶ my ($sth, $p_num, $value, $attr) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
130 0 ▶ $sth->{private_DBIx_Tracer_params} ||= [];
Condition0%0/2
$sth->{"private_DBIx_Tracer_params"} ||= []
l!l
00
MC/DC0%0/1
$sth->{"private_DBIx_Tracer_params"} ||= []
$sth->{"private_DBIx_Tracer_params"}
Truth table0%0/2
A
A= $sth->{"private_DBIx_Tracer_params"}
Aresult
11
00
131 0 ▶ $sth->{private_DBIx_Tracer_attrs } ||= [];
Condition0%0/2
$sth->{"private_DBIx_Tracer_attrs"} ||= []
l!l
00
MC/DC0%0/1
$sth->{"private_DBIx_Tracer_attrs"} ||= []
$sth->{"private_DBIx_Tracer_attrs"}
Truth table0%0/2
A
A= $sth->{"private_DBIx_Tracer_attrs"}
Aresult
11
00
132 0 ▶ $attr = +{ TYPE => $attr || 0 } unless ref $attr eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attr eq "HASH" 0 0 0
Condition0%0/2
$attr || 0
l!l
00
MC/DC0%0/1
$attr || 0
$attr
Truth table0%0/2
A
A= $attr
Aresult
11
00
133 0 $sth->{private_DBIx_Tracer_params}[$p_num - 1] = $value;
134 0 $sth->{private_DBIx_Tracer_attrs }[$p_num - 1] = $attr;
135 0 $org->(@_);
136 0 };
137 }
138  
139 sub _select_array {
140 0 ▶ my ($class, $org, $is_selectrow_array, $logger) = @_;
Subroutine _select_array: not called CC=1 SCAR=5.4
141  
142 return sub {
143 0 ▶ my $wantarray = wantarray;
Subroutine __ANON__: not called CC=6 SCAR=34.4
144 0 my ($dbh, $stmt, $attr, @bind) = @_;
145  
146 1 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
147 0 local *DBI::st::execute = $org_execute; # suppress duplicate logging
148  
149 0 ▶ my $ret = ref $stmt ? $stmt->{Statement} : $stmt;
Branch: 1 branch
BranchTrueFalseTotal
ref $stmt ? : 0 0 0
150  
151 0 my $begin = [gettimeofday];
152 0 my $res;
153 0 ▶ if ($is_selectrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_selectrow_array) { } 0 0 0
154 0 ▶ $res = $wantarray ? [$org->($dbh, $stmt, $attr, @bind)] : $org->($dbh, $stmt, $attr, @bind);
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
155 }
156 else {
157 0 $res = $org->($dbh, $stmt, $attr, @bind);
158 }
159 0 my $time = tv_interval($begin, [gettimeofday]);
160  
161 0 $class->_logging($logger, $dbh, $ret, $time, \@bind);
162  
163 0 ▶ if ($is_selectrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_selectrow_array) 0 0 0
164 0 ▶ return $wantarray ? @$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
165 }
166 0 return $res;
167 0 };
168 }
169  
170 sub _db_do {
171 0 ▶ my ($class, $org, $logger) = @_;
Subroutine _db_do: not called CC=1 SCAR=6.9
172  
173 return sub {
174 0 ▶ my $wantarray = wantarray ? 1 : 0;
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
175 0 my ($dbh, $stmt, $attr, @bind) = @_;
176  
177 0 ▶ if ($dbh->{Driver}{Name} ne 'mysql') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"Driver"}{"Name"} ne "mysql") 0 0 0
178 0 return $org->($dbh, $stmt, $attr, @bind);
179 }
180  
181 0 my $ret = $stmt;
182  
183 0 my $begin = [gettimeofday];
184 0 ▶ my $res = $wantarray ? [$org->($dbh, $stmt, $attr, @bind)] : scalar $org->($dbh, $stmt, $attr, @bind);
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
185 0 my $time = tv_interval($begin, [gettimeofday]);
186  
187 0 $class->_logging($logger, $dbh, $ret, $time, \@bind);
188  
189 0 ▶ return $wantarray ? @$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
190 0 };
191 }
192  
193 sub _logging {
194 0 ▶ my ($class, $logger, $dbh, $sql, $time, $bind_params) = @_;
Subroutine _logging: not called CC=2 SCAR=17.9
195 0 ▶ $bind_params ||= [];
Condition0%0/2
$bind_params ||= []
l!l
00
MC/DC0%0/1
$bind_params ||= []
$bind_params
Truth table0%0/2
A
A= $bind_params
Aresult
11
00
196  
197 0 $logger->(
198 dbh => $dbh,
199 time => $time,
200 sql => $sql,
201 bind_params => $bind_params,
202 );
203 }
204  
205 1 1;
206 __END__
↑ Summary Build »