File Coverage

blib/lib/DBIx/Log4perl/st.pm
Criterion Covered Total %
statement 15 128 11.7
branch 0 70 0.0
condition 0 51 0.0
subroutine 5 23 21.7
pod 9 10 90.0
total 29 282 10.2


line stmt bran cond sub pod time code
1             # $Id: st.pm 284 2006-09-07 13:50:57Z martin $
2 2     2   10 use strict;
  2         4  
  2         83  
3 2     2   11 use warnings;
  2         4  
  2         64  
4 2     2   10 use DBI;
  2         4  
  2         74  
5 2     2   9 use Log::Log4perl;
  2         11  
  2         12  
6              
7             package DBIx::Log4perl::st;
8             @DBIx::Log4perl::st::ISA = qw(DBI::st DBIx::Log4perl);
9 2     2   92 use DBIx::Log4perl::Constants qw (:masks $LogMask);
  2         4  
  2         5080  
10              
11             sub finish {
12 0     0 1   my ($sth) = shift;
13              
14 0           my $h = _unseen_sth($sth);
15              
16 0 0         $sth->_dbix_l4p_debug($h, 2,
17             "finish($h->{dbh_no}.$sth->{private_DBIx_st_no})")
18             if ($h->{logmask} & DBIX_L4P_LOG_INPUT);
19 0           return $sth->SUPER::finish;
20             }
21              
22             #
23             # NOTE: execute can be called from the DBD. Now we support retrieving
24             # dbms_output from DBD::Oracle we need a flag to say we are in the
25             # 'dbms_output_get' or when we call dbms_output_get we will log a second
26             # execute and potentially recurse until we run out of stack.
27             # We use the "dbd_specific" flag since we may need it for other
28             # drivers in the future and that is the logging flag we implement dbms_output
29             # fetching under
30             #
31             sub execute {
32 0     0 1   my $sth = shift;
33 0           my $h = $sth->{private_DBIx_Log4perl};
34              
35 0 0 0       if (($h->{logmask} & (DBIX_L4P_LOG_INPUT|DBIX_L4P_LOG_SQL)) &&
      0        
36             (caller !~ /^DBD::/o) &&
37             (!$h->{dbd_specific})) {
38 0           my $params;
39             #if (defined($sth->{ParamValues})) {
40             # $params = '{params => ';
41             # foreach (sort keys %{$sth->{ParamValues}}) {
42             # $params .= "$_=" . DBI::neat($h->{ParamValues}->{$_}) . ",";
43             # }
44             # $params .= '}'; # TO_DO not used yet
45             #}
46 0 0         if (scalar(@_)) {
47 0 0         $sth->_dbix_l4p_debug(
48             $h, 2,
49             "execute($h->{dbh_no}.$sth->{private_DBIx_st_no}) (" .
50             ($sth->{Statement} ? $sth->{Statement} : '') . ')', @_);
51             } else {
52 0           my @param_info;
53 0 0         push @param_info, $sth->{ParamValues}, $sth->{ParamTypes}
54             if ($h->{logmask} & DBIX_L4P_LOG_DELAYBINDPARAM);
55 0           $sth->_dbix_l4p_debug(
56             $h, 2,
57             "execute($h->{dbh_no}.$sth->{private_DBIx_st_no})",
58             @param_info);
59             }
60             }
61              
62 0           my $ret = $sth->SUPER::execute(@_);
63              
64             #
65             # If DBDSPECIFIC is enabled and this is DBD::Oracle we will attempt to
66             # to retrieve any dbms_output. However, 'dbms_output_get' actually
67             # creates a new statement, prepares it, executes it, binds parameters
68             # and then fetches the dbms_output. This will cause this execute method
69             # to be called again and we could recurse forever. To prevent that
70             # happening we set {dbd_specific} flag before calling dbms_output_get
71             # and clear it afterwards.
72             #
73             # Also in DBI (at least up to 1.54) and most DBDs, the same memory is
74             # used for a dbh errstr/err/state and each statement under it. As a
75             # result, if you sth1->execute (it fails) then $sth2->execute which
76             # succeeds, sth1->errstr/err are undeffed :-(
77             # see http://www.nntp.perl.org/group/perl.dbi.users/2007/02/msg30971.html
78             # To sort this out, we save the errstr/err/state on the first sth
79             # and put them back after using the second sth (ensuring we temporarily
80             # turn off any error handler to avoid set_err calling them again).
81             #
82 0 0 0       if (($h->{logger}->is_debug()) &&
      0        
      0        
83             ($h->{logmask} & DBIX_L4P_LOG_DBDSPECIFIC) &&
84             ($h->{driver} eq 'Oracle') && (!$h->{dbd_specific})) {
85              
86 0           my ($errstr, $err, $state) = (
87             $sth->errstr, $sth->err, $sth->state);
88 0           $h->{dbd_specific} = 1;
89 0           my $dbh = $sth->FETCH('Database');
90              
91 0           my @lines = $dbh->func('dbms_output_get');
92 0 0         $sth->_dbix_l4p_debug($h, 2, 'dbms', @lines) if (scalar(@lines) > 0);
93 0           $h->{dbd_specific} = 0;
94             {
95 0           local $sth->{HandleError} = undef;
  0            
96 0           local $sth->{HandleSetErr} = undef;
97 0           $sth->set_err($err, $errstr, $state);
98             }
99             }
100              
101 0 0 0       if (!$ret) { # error
    0          
102 0 0 0       if (($h->{logmask} & DBIX_L4P_LOG_ERRCAPTURE) && # logging errors
103             (caller !~ /^DBD::/o)) { # ! called from DBD e.g. execute_array
104 0 0 0       if ((exists($h->{err_regexp}) && ($sth->err !~ $h->{err_regexp})) ||
      0        
105             (!exists($h->{err_regexp}))) {
106 0           $sth->_dbix_l4p_error(
107             2, "\tfailed with " . DBI::neat($sth->errstr));
108             }
109             }
110             } elsif (defined($ret) && (!$h->{dbd_specific})) {
111 0 0 0       $sth->_dbix_l4p_debug(
      0        
112             $h, 2, "affected($h->{dbh_no}.$sth->{private_DBIx_st_no})", $ret)
113             if ((!defined($sth->{NUM_OF_FIELDS})) && # not a result-set
114             ($h->{logmask} & DBIX_L4P_LOG_INPUT) && # logging input
115             (caller !~ /^DBD::/o));
116             }
117 0           return $ret;
118             }
119              
120             sub execute_array {
121 0     0 1   my ($sth, @args) = @_;
122 0           my $h = $sth->{private_DBIx_Log4perl};
123              
124 0 0         $sth->_dbix_l4p_debug($h, 2,
125             "execute_array($h->{dbh_no}.$sth->{private_DBIx_st_no})", @args)
126             if ($h->{logmask} & DBIX_L4P_LOG_INPUT);
127              
128 0 0 0       if (($#args >= 0) && ($args[0]) &&
    0 0        
      0        
129             (ref($args[0]) eq 'HASH') &&
130             (!exists($args[0]->{ArrayTupleStatus}))) {
131 0           $args[0]->{ArrayTupleStatus} = \my @tuple_status;
132             } elsif (!$args[0]) {
133 0           $args[0] = {ArrayTupleStatus => \my @tuple_status};
134             }
135 0           my $array_tuple_status = $args[0]->{ArrayTupleStatus};
136              
137             #
138             # NOTE: We have a problem here. The DBI pod stipulates that
139             # execute_array returns undef (for error) or the number of tuples
140             # executed. If we want to access the number of rows updated or
141             # inserted then we need to add up the values in the ArrayTupleStatus.
142             # Unfortunately, the drivers which implement execute_array themselves
143             # (e.g. DBD::Oracle) don't do this properly (e.g. DBD::Oracle 1.18a).
144             # As a result, until this is sorted out, our logging of execute_array
145             # may be less than accurate.
146             # NOTE: DBD::Oracle 1.19 is working now from my supplied patch
147             #
148 0           my ($executed, $affected) = $sth->SUPER::execute_array(@args);
149 0 0         if (!$executed) {
    0          
150             #print Data::Dumper->Dump([$sth->{ParamArrays}], ['ParamArrays']), "\n";
151 0 0         if (!$h->{logmask} & DBIX_L4P_LOG_ERRORS) {
152 0 0         return $executed unless wantarray;
153 0           return ($executed, $affected);
154             }
155 0           my $pa = $sth->{ParamArrays};
156 0           $sth->_dbix_l4p_error(2, "execute_array error:");
157 0           for my $n (0..@{$array_tuple_status}-1) {
  0            
158 0 0         next if (!ref($array_tuple_status->[$n]));
159 0           $sth->_dbix_l4p_error('Error', $array_tuple_status->[$n]);
160 0           my @plist;
161 0           foreach my $p (keys %{$pa}) {
  0            
162 0 0         if (ref($pa->{$p})) {
163 0           push @plist, $pa->{$p}->[$n];
164             } else {
165 0           push @plist, $pa->{$p};
166             }
167             }
168             $sth->_dbix_l4p_error(
169 0     0     2, sub {"\t for " . join(',', map(DBI::neat($_), @plist))});
  0            
170             }
171             } elsif ($executed) {
172 0 0 0       if ((defined($sth->{NUM_OF_FIELDS})) || # result-set
173             !($h->{logmask} & DBIX_L4P_LOG_INPUT)) { # logging input
174 0 0         return $executed unless wantarray;
175 0           return ($executed, $affected);
176             }
177 0           $sth->_dbix_l4p_debug($h, 2, "executed $executed, affected " .
178             DBI::neat($affected));
179             }
180 0     0     $sth->_dbix_l4p_debug($h, 2, sub {Data::Dumper->Dump(
181             [$array_tuple_status], ['ArrayTupleStatus'])})
182 0 0         if ($h->{logmask} & DBIX_L4P_LOG_INPUT);
183 0 0         return $executed unless wantarray;
184 0           return ($executed, $affected);
185             }
186              
187             sub bind_param {
188 0     0 1   my $sth = shift;
189              
190 0           my $h = $sth->{private_DBIx_Log4perl};
191              
192 0 0 0       $sth->_dbix_l4p_debug(
193             $h, 2, "bind_param($h->{dbh_no}.$sth->{private_DBIx_st_no})", @_)
194             if (($h->{logmask} & DBIX_L4P_LOG_INPUT) &&
195             (($h->{logmask} & DBIX_L4P_LOG_DELAYBINDPARAM) == 0));
196              
197 0           return $sth->SUPER::bind_param(@_);
198             }
199              
200             sub bind_param_inout {
201 0     0 1   my $sth = shift;
202 0           my $h = $sth->{private_DBIx_Log4perl};
203              
204 0 0 0       $sth->_dbix_l4p_debug(
205             $h, 2,
206             "bind_param_inout($h->{dbh_no}.$sth->{private_DBIx_st_no})", @_)
207             if (($h->{logmask} & DBIX_L4P_LOG_INPUT) && (caller !~ /^DBD::/o));
208 0           return $sth->SUPER::bind_param_inout(@_);
209             }
210              
211             sub bind_param_array {
212 0     0 1   my($sth, @args) = @_;
213 0           my $h = $sth->{private_DBIx_Log4perl};
214              
215 0 0         $sth->_dbix_l4p_debug($h, 2,
216             "bind_param_array($h->{dbh_no}.$sth->{private_DBIx_st_no})",
217             @args) if ($h->{logmask} & DBIX_L4P_LOG_INPUT);
218 0           return $sth->SUPER::bind_param_array(@args);
219             }
220              
221             sub fetch { # alias for fetchrow_arrayref
222 0     0 0   my($sth, @args) = @_;
223              
224 0           my $h = _unseen_sth($sth);
225              
226 0           my $res = $sth->SUPER::fetch(@args);
227 0     0     $sth->_dbix_l4p_debug($h, 2,
228             sub {Data::Dumper->Dump(
229             [$res], ["fetch($h->{dbh_no}.$sth->{private_DBIx_st_no})"])})
230 0 0         if ($h->{logmask} & DBIX_L4P_LOG_OUTPUT);
231 0           return $res;
232             }
233              
234             sub fetchrow_arrayref { # alias for fetchrow_arrayref
235 0     0 1   my($sth, @args) = @_;
236              
237 0           my $h =_unseen_sth($sth);
238              
239 0           my $res = $sth->SUPER::fetchrow_arrayref(@args);
240 0     0     $sth->_dbix_l4p_debug($h, 2,
241             sub {Data::Dumper->Dump(
242             [$res],
243             ["fetchrow_arrayref($h->{dbh_no}.$sth->{private_DBIx_st_no})"])})
244 0 0         if ($h->{logmask} & DBIX_L4P_LOG_OUTPUT);
245 0           return $res;
246             }
247              
248             sub fetchrow_array {
249 0     0 1   my ($sth, @args) = @_;
250              
251 0           my $h = _unseen_sth($sth);
252              
253 0 0         if (wantarray) {
254 0           my @row = $sth->SUPER::fetchrow_array(@args);
255 0     0     $sth->_dbix_l4p_debug($h, 2,
256             sub {Data::Dumper->Dump(
257             [\@row],
258             ["fetchrow_array($h->{dbh_no}.$sth->{private_DBIx_st_no})"])})
259 0 0         if ($h->{logmask} & DBIX_L4P_LOG_OUTPUT);
260 0           return @row;
261             } else {
262 0           my $row = $sth->SUPER::fetchrow_array(@args);
263 0     0     $sth->_dbix_l4p_debug($h, 2,
264             sub {Data::Dumper->Dump(
265             [$row],
266             ["fetchrow_array($h->{dbh_no}.$sth->{private_DBIx_st_no})"])})
267 0 0         if ($h->{logmask} & DBIX_L4P_LOG_OUTPUT);
268 0           return $row;
269             }
270             }
271              
272             sub fetchrow_hashref {
273 0     0 1   my($sth, @args) = @_;
274              
275 0           my $h = _unseen_sth($sth);
276              
277 0           my $res = $sth->SUPER::fetchrow_hashref(@args);
278 0     0     $sth->_dbix_l4p_debug($h, 2,
279             sub {Data::Dumper->Dump(
280             [$res],
281             ["fetchrow_hashref($h->{dbh_no}.$sth->{private_DBIx_st_no})"])})
282 0 0         if ($h->{logmask} & DBIX_L4P_LOG_OUTPUT);
283 0           return $res;
284             }
285              
286             #
287             # _unseen_sth is called when we might come across a statement handle which was
288             # not created via the prepare method e.g., a statement handle DBD::Oracle
289             # magicked into existence when a function or procedure returns a cursor.
290             # We need to save the private log handle and set the statement number.
291             #
292             sub _unseen_sth
293             {
294 0     0     my $sth = shift;
295              
296 0 0         if (!exists($sth->{private_DBIx_Log4perl})) {
297 0           my $p = $sth->FETCH('Database')->{private_DBIx_Log4perl};
298 0           $sth->{private_DBIx_Log4perl} = $p;
299 0           $sth->{private_DBIx_st_no} = $p->{new_stmt_no}();
300 0           return $p;
301             } else {
302 0           return $sth->{private_DBIx_Log4perl};
303             }
304             }
305              
306             1;