File Coverage

blib/lib/DBIx/LogAny/st.pm
Criterion Covered Total %
statement 12 118 10.1
branch 0 70 0.0
condition 0 51 0.0
subroutine 4 15 26.6
pod 9 10 90.0
total 25 264 9.4


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