lib/DBI/Log.pm

filter: Statementstmt 82.9% 165 / 199 Branchbran 65.2% 47 / 72 Conditioncond 41.1% 7 / 17 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 3 totaltotal 73.7% 236 / 320 CC 52 SCAR 29.7
CC
52
Cov
76%
pre_query
30.3
to_json
24.6
import
9.3
CRAP 19.4
↑ Summary
1 package DBI::Log;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = "0.12";
11 1 our %opts = (
12 file => $file,
13 trace => 0,
14 timing => 0,
15 replace_placeholders => 1,
16 fh => undef,
17 exclude => undef,
18 format => "sql",
19 );
20  
21 1 my $orig_execute = \&DBI::st::execute;
22 4 ▶*DBI::st::execute = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 4 my ($sth, @args) = @_;
24 4 my $log = pre_query("execute", $sth->{Database}, $sth, $sth->{Statement}, \@args);
25 4 my $retval = $orig_execute->($sth, @args);
26 4 post_query($log);
27 4 return $retval;
28 1 };
29  
30 1 my $orig_selectall_arrayref = \&DBI::db::selectall_arrayref;
31 1 ▶*DBI::db::selectall_arrayref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 my ($dbh, $query, $yup, @args) = @_;
33 1 my $log = pre_query("selectall_arrayref", $dbh, undef, $query, \@args);
34 1 my $retval = $orig_selectall_arrayref->($dbh, $query, $yup, @args);
35 1 post_query($log);
36 1 return $retval;
37 1 };
38  
39 1 my $orig_selectcol_arrayref = \&DBI::db::selectcol_arrayref;
40 1 ▶*DBI::db::selectcol_arrayref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 my ($dbh, $query, $yup, @args) = @_;
42 1 my $log = pre_query("selectcol_arrayref", $dbh, undef, $query, \@args);
43 1 my $retval = $orig_selectcol_arrayref->($dbh, $query, $yup, @args);
44 1 post_query($log);
45 1 return $retval;
46 1 };
47  
48 1 my $orig_selectall_hashref = \&DBI::db::selectall_hashref;
49 0 ▶*DBI::db::selectall_hashref = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my ($dbh, $query, $yup, @args) = @_;
51 0 my $log = pre_query("selectall_hashref", $dbh, undef, $query, \@args);
52 0 my $retval = $orig_selectall_hashref->($dbh, $query, $yup, @args);
53 0 post_query($log);
54 0 return $retval;
55 1 };
56  
57 1 my $orig_selectrow_arrayref = \&DBI::db::selectrow_arrayref;
58 0 ▶*DBI::db::selectrow_arrayref = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 my ($dbh, $query, $yup, @args) = @_;
60 0 my $log = pre_query("selectrow_arrayref", $dbh, $sth, $query, \@args);
61 0 my $retval = $orig_selectrow_arrayref->($dbh, $query, $yup, @args);
62 0 post_query($log);
63 0 return $retval;
64 1 };
65  
66 1 my $orig_selectrow_array = \&DBI::db::selectrow_array;
67 0 ▶*DBI::db::selectrow_array = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 my ($dbh, $query, $yup, @args) = @_;
69 0 my $log = pre_query("selectrow_array", $dbh, undef, $query, \@args);
70 0 my $retval = $orig_selectrow_array->($dbh, $query, $yup, @args);
71 0 post_query($log);
72 0 return $retval;
73 1 };
74  
75 1 my $orig_selectrow_hashref = \&DBI::db::selectrow_hashref;
76 0 ▶*DBI::db::selectrow_hashref = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 my ($dbh, $query, $yup, @args) = @_;
78 0 my $log = pre_query("selectrow_hashref", $dbh, undef, $query, \@args);
79 0 my $retval = $orig_selectrow_hashref->($dbh, $query, $yup, @args);
80 0 post_query($log);
81 0 return $retval;
82 1 };
83  
84 1 my $orig_do = \&DBI::db::do;
85 3 ▶*DBI::db::do = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 3 my ($dbh, $query, $yup, @args) = @_;
87 3 my $log = pre_query("do", $dbh, undef, $query, \@args);
88 3 my $retval = $orig_do->($dbh, $query, $yup, @args);
89 2 post_query($log);
90 2 return $retval;
91 1 };
92  
93  
94 2 ▶sub import {
Subroutine import: called CC=6 SCAR=22.3
95 2 my ($package, %args) = @_;
96 2 for my $key (keys %args) {
97 3 $opts{$key} = $args{$key};
98 }
99 2 ▶ if (!$opts{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'file'}) { } 0 2 2
100 0 $opts{fh} = \*STDERR;
101 }
102 else {
103 2 my $file2 = $opts{file};
104 2 ▶ if ($file2 =~ m{^~/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file2 =~ m[^~/]) 0 2 2
105 0 ▶ my $home = $ENV{HOME} || (getpwuid($<))[7];
Condition0%0/3
$ENV{'HOME'} || (getpwuid $<)[7]
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HOME'} || (getpwuid $<)[7]
$ENV{'HOME'} (getpwuid $<)[7]
Truth table0%0/3
A || B
A= $ENV{'HOME'}B= (getpwuid $<)[7]
ABresult
1X1
011
000
106 0 $file2 =~ s{^~/}{$home/};
107 }
108 2 ▶ open $opts{fh}, ">>", $file2 or die "Can't open $opts{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $opts{"fh"}, ">>", $file2 or die qq[Can't open $opts{"file"}: $!\n] 2 0 2
109 # autoflush so that tailing to watch queries being performed works
110 # as you'd expect
111 2 $opts{fh}->autoflush(1);
112 }
113 }
114  
115 9 ▶sub pre_query {
Subroutine pre_query: called CC=25 SCAR=34.1
Pod undocumented
116 9 my ($name, $dbh, $sth, $query, $args) = @_;
117 9 my $log = {};
118 9 my $mcount = 0;
119  
120 # Some DBI functions are composed of other DBI functions, so make sure we
121 # are only logging the top level one. For example $dbh->do() will call
122 # $dbh->execute() internally, so we need to make sure a DBI::Log function
123 # logs the $dbh->do() and not the internal $dbh->execute(). If multiple
124 # functions were called, we return and flag this log entry to be skipped in
125 # the post_query() part.
126 9 ▶ for (my $i = 0; my @caller = caller($i); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i) 22 6 28
127 22 my ($package, $file, $line, $sub) = @caller;
128 22 ▶ if ($package eq "DBI::Log") {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "DBI::Log") 12 10 22
129 12 $mcount++;
130 12 ▶ if ($mcount > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mcount > 1) 3 9 12
131 3 $log->{skip} = 1;
132 3 return $log;
133 }
134 }
135 }
136 6 my @callers;
137 6 ▶ for (my $i = 0; my @caller = caller($i); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i) 13 6 19
138 13 push @callers, \@caller;
139 }
140  
141 # Order the call stack based on the highest level calls first, then the
142 # lower level calls. Once you reach a package that is excluded, do not show
143 # any more lines in the stack trace. By default, it will exclude anything
144 # past the DBI::Log package, but if user provides an exclude option, it will
145 # stop there.
146 6 my @filtered_callers;
147 6 CALLER: for my $caller (reverse @callers) {
148 13 my ($package, $file, $line, $long_sub) = @$caller;
149 13 ▶ if ($package eq "DBI::Log") {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "DBI::Log") 6 7 13
150 6 last CALLER;
151 }
152 7 ▶ if ($opts{exclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"exclude"}) 0 7 7
153 0 for my $item (@{$opts{exclude}}) {
154 0 ▶ if ($package =~ /^$item(::|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /^$item(::|$)/) 0 0 0
155 0 last CALLER;
156 }
157 }
158 }
159 7 push @filtered_callers, $caller;
160  
161 }
162 6 ▶ if (!$opts{trace}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"trace"}) 6 0 6
163 6 @filtered_callers = ($filtered_callers[-1]);
164 }
165  
166 6 my @stack;
167 6 for my $caller (@filtered_callers) {
168 6 my ($package, $file, $line, $long_sub) = @$caller;
169 6 my $sub = $long_sub;
170 6 $sub =~ s/.*:://;
171 6 ▶ $sub = $name if $long_sub =~ /^DBI::Log::__ANON__/;
Branch: 1 branch
BranchTrueFalseTotal
if $long_sub =~ /^DBI::Log::__ANON__/ 6 0 6
172 6 push @stack, {
173 sub => $sub,
174 file => $file,
175 line => $line,
176 };
177 }
178  
179 6 ▶ if (ref($query) && ref($query) eq "DBI::st") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $query and ref $query eq "DBI::st") 1 5 6
Condition66%2/3
ref $query and ref $query eq "DBI::st"
!ll&&!rl&&r
501
MC/DC50%1/2
ref $query and ref $query eq "DBI::st"
ref $query ref $query eq "DBI::st"
Truth table66%2/3
A and B
A= ref $queryB= ref $query eq "DBI::st"
ABresult
0X0
100
111
180 1 $sth = $query;
181 1 $query = $query->{Statement};
182 }
183  
184 6 ▶ if ($dbh && $opts{replace_placeholders}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh and $opts{"replace_placeholders"}) 6 0 6
Condition33%1/3
$dbh and $opts{"replace_placeholders"}
!ll&&!rl&&r
006
MC/DC0%0/2
$dbh and $opts{"replace_placeholders"}
$dbh $opts{"replace_placeholders"}
Truth table33%1/3
A and B
A= $dbhB= $opts{"replace_placeholders"}
ABresult
0X0
100
111
185 # When you use $sth->bind_param(1, "value") the params can be found in
186 # $sth->{ParamValues} and they override arguments sent in to
187 # $sth->execute()
188  
189 6 my @args_copy = @$args;
190 6 my %values;
191 6 ▶ if ($sth && $sth->{ParamValues}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth and $sth->{"ParamValues"}) 2 4 6
Condition66%2/3
$sth and $sth->{"ParamValues"}
!ll&&!rl&&r
402
MC/DC50%1/2
$sth and $sth->{"ParamValues"}
$sth $sth->{"ParamValues"}
Truth table66%2/3
A and B
A= $sthB= $sth->{"ParamValues"}
ABresult
0X0
100
111
192 2 %values = %{$sth->{ParamValues}};
193 }
194 6 for my $key (keys %values) {
195 0 ▶ if (defined $key && $key =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and $key =~ /^\d+$/) 0 0 0
Condition0%0/3
defined $key and $key =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and $key =~ /^\d+$/
defined $key $key =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $keyB= $key =~ /^\d+$/
ABresult
0X0
100
111
196 0 $args_copy[$key - 1] = $values{$key};
197 }
198 }
199  
200 6 for my $i (0 .. @args_copy - 1) {
201 4 my $value = $args_copy[$i];
202 4 $value = $dbh->quote($value);
203 4 $query =~ s{\?}{$value}e;
204 }
205 }
206  
207 6 $query =~ s/^\s*\n|\s*$//g;
208 6 $log->{time_started} = Time::HiRes::time();
209 6 $log->{query} = $query;
210 6 $log->{stack} = \@stack;
211 6 ▶ if ($opts{format} eq "json") {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'format'} eq 'json') { } 1 5 6
212 # For JSON output we don't want to output anything yet, so post_query()
213 # can emit the whole JSON object, just remember them.
214 }
215 else {
216 5 my $mesg;
217 5 $mesg .= "-- " . scalar(localtime()) . "\n";
218 5 for my $caller (@stack) {
219 5 $mesg .= "-- $caller->{sub} $caller->{file} $caller->{line}\n";
220 }
221 5 $mesg .= "$query\n";
222 5 print {$opts{fh}} $mesg;
223 }
224  
225 6 return $log;
226 }
227  
228 8 ▶sub post_query {
Subroutine post_query: called CC=4 SCAR=14.0
Pod undocumented
229 8 my ($log) = @_;
230 8 ▶ return if $log->{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $log->{"skip"} 3 5 8
231 5 $log->{time_ended} = Time::HiRes::time();
232 5 $log->{time_taken} = sprintf "%.3f", $log->{time_ended} - $log->{time_started};
233  
234 5 ▶ if ($opts{format} eq "json") {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'format'} eq 'json') { } 1 4 5
235 # print all the info as JSON
236 1 print {$opts{fh}} to_json($log) . "\n";
237 }
238 else {
239 # For SQL output format, pre_query already printed most of the info, we
240 # just need to add the time taken - and that only if we're doing
241 # timings...
242 4 ▶ if ($opts{timing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"timing"}) 0 4 4
243 0 print {$opts{fh}} "-- $log->{time_taken}s\n";
244 }
245 4 print {$opts{fh}} "\n";
246 }
247 }
248  
249 10 ▶sub to_json {
Subroutine to_json: called CC=20 SCAR=32.0
Pod undocumented
250 10 my ($val, $depth) = @_;
251 10 ▶ $depth ||= 0;
Condition100%2/2
$depth ||= 0
l!l
91
MC/DC100%1/1
$depth ||= 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
252 10 my $pretty = 0;
253  
254 10 my $out;
255 10 ▶ if (!defined $val) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 10 10
elsif (ref $val eq 'HASH') { } 2 8 10
elsif (ref $val eq 'ARRAY') { } 1 7 8
elsif ($val =~ /^(-?\d+(\.\d*)?(e[+-]?\d+)?)$/i) { } 4 3 7
256 0 $out = "null";
257 }
258 elsif (ref $val eq "HASH") {
259 2 $out = "{";
260 2 ▶ $out .= "\n" if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 2 2
261 2 my $i = 0;
262 2 for my $key (sort keys %$val) {
263 8 my $val2 = $val->{$key};
264 8 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 6 2 8
265 6 ▶ $out .= $pretty ? ",\n" : ", ";
Branch: 1 branch
BranchTrueFalseTotal
$pretty ? : 0 6 6
266 }
267 8 ▶ $out .= " " x ($depth + 1) if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 8 8
268 8 $out .= "\"$key\": " . to_json($val2, $depth + 1);
269 8 $i++;
270 }
271 2 ▶ $out .= "\n" if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 2 2
272 2 ▶ $out .= " " x ($depth) if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 2 2
273 2 $out .= "}";
274 }
275 elsif (ref $val eq "ARRAY") {
276 1 $out = "[";
277 1 ▶ $out .= "\n" if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 1 1
278 1 for my $i (0 .. @$val - 1) {
279 1 my $val2 = $val->[$i];
280 1 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 0 1 1
281 0 ▶ $out .= $pretty ? ",\n" : ", ";
Branch: 1 branch
BranchTrueFalseTotal
$pretty ? : 0 0 0
282 }
283 1 ▶ $out .= " " x ($depth + 1) if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 1 1
284 1 $out .= to_json($val2, $depth + 1);
285 }
286 1 ▶ $out .= "\n" if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 1 1
287 1 ▶ $out .= " " x ($depth) if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 1 1
288 1 $out .= "]";
289 }
290 elsif ($val =~ /^(-?\d+(\.\d*)?(e[+-]?\d+)?)$/i) {
291 4 $out = $val;
292 }
293 else {
294 # Make the value suitable to use in a JSON string - no newlines, escape
295 # control characters and double quotes.
296 3 $val =~ s/"/\\"/g;
297 3 $val =~ s/\n/ /g;
298 0 $val =~ s/([\x00-\x1F])/sprintf("\\u%04x", ord($1))/eg;
299 3 $out = "\"$val\"";
300 }
301  
302 10 return $out;
303 }
304  
305 1 1;
306  
307 __END__
↑ Summary