blib/lib/DBI/Log.pm untested

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