Agent/Monitors/DBI/Tracer.pm untested

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