File Coverage

blib/lib/DBIx/LogAny/st.pm
Criterion Covered Total %
statement 36 118 30.5
branch 11 70 15.7
condition 11 51 21.5
subroutine 8 15 53.3
pod 9 10 90.0
total 75 264 28.4


line stmt bran cond sub pod time code
1             # $Id$
2 2     2   12 use strict;
  2         4  
  2         48  
3 2     2   8 use warnings;
  2         4  
  2         49  
4 2     2   8 use DBI;
  2         8  
  2         94  
5              
6             package DBIx::LogAny::st;
7             @DBIx::LogAny::st::ISA = qw(DBI::st DBIx::LogAny);
8 2     2   9 use DBIx::LogAny::Constants qw (:masks $LogMask);
  2         4  
  2         2767  
9              
10             sub finish {
11 6     6 1 49 my ($sth) = shift;
12              
13 6         14 my $h = _unseen_sth($sth);
14              
15             $sth->_dbix_la_debug($h, 2,
16             "finish($h->{dbh_no}.$sth->{private_DBIx_st_no})")
17 6 50       39 if ($h->{logmask} & DBIX_LA_LOG_INPUT);
18 6         33 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 5     5 1 427 my $sth = shift;
32 5         23 my $h = $sth->{private_DBIx_LogAny};
33              
34 5 100 66     44 if (($h->{logmask} & (DBIX_LA_LOG_INPUT|DBIX_LA_LOG_SQL)) &&
      66        
35             (caller !~ /^DBD::/o) &&
36             (!$h->{dbd_specific})) {
37 1         3 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 1 50       3 if (scalar(@_)) {
46             $sth->_dbix_la_debug(
47             $h, 2,
48             "execute($h->{dbh_no}.$sth->{private_DBIx_st_no}) (" .
49 1 50       15 ($sth->{Statement} ? $sth->{Statement} : '') . ')', @_);
50             } else {
51 0         0 my @param_info;
52             push @param_info, $sth->{ParamValues}, $sth->{ParamTypes}
53 0 0       0 if ($h->{logmask} & DBIX_LA_LOG_DELAYBINDPARAM);
54 0         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 5         31 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 5 0 33     2762 if (($h->{logger}->is_debug()) &&
      33        
      33        
82             ($h->{logmask} & DBIX_LA_LOG_DBDSPECIFIC) &&
83             ($h->{driver} eq 'Oracle') && (!$h->{dbd_specific})) {
84              
85 0         0 my ($errstr, $err, $state) = (
86             $sth->errstr, $sth->err, $sth->state);
87 0         0 $h->{dbd_specific} = 1;
88 0         0 my $dbh = $sth->FETCH('Database');
89              
90 0         0 my @lines = $dbh->func('dbms_output_get');
91 0 0       0 $sth->_dbix_la_debug($h, 2, 'dbms', @lines) if (scalar(@lines) > 0);
92 0         0 $h->{dbd_specific} = 0;
93             {
94 0         0 local $sth->{HandleError} = undef;
  0         0  
95 0         0 local $sth->{HandleSetErr} = undef;
96 0         0 $sth->set_err($err, $errstr, $state);
97             }
98             }
99              
100 5 100 33     104 if (!$ret) { # error
    50          
101 1 50 33     11 if (($h->{logmask} & DBIX_LA_LOG_ERRCAPTURE) && # logging errors
102             (caller !~ /^DBD::/o)) { # ! called from DBD e.g. execute_array
103 0 0 0     0 if ((exists($h->{err_regexp}) && ($sth->err !~ $h->{err_regexp})) ||
      0        
104             (!exists($h->{err_regexp}))) {
105 0         0 $sth->_dbix_la_error(
106             2, "\tfailed with " . DBI::neat($sth->errstr));
107             }
108             }
109             } elsif (defined($ret) && (!$h->{dbd_specific})) {
110             $sth->_dbix_la_debug(
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 4 0 33     28 ($h->{logmask} & DBIX_LA_LOG_INPUT) && # logging input
      33        
114             (caller !~ /^DBD::/o));
115             }
116 5         22 return $ret;
117             }
118              
119             sub execute_array {
120 0     0 1 0 my ($sth, @args) = @_;
121 0         0 my $h = $sth->{private_DBIx_LogAny};
122              
123             $sth->_dbix_la_debug($h, 2,
124             "execute_array($h->{dbh_no}.$sth->{private_DBIx_st_no})", @args)
125 0 0       0 if ($h->{logmask} & DBIX_LA_LOG_INPUT);
126              
127 0 0 0     0 if (($#args >= 0) && ($args[0]) &&
    0 0        
      0        
128             (ref($args[0]) eq 'HASH') &&
129             (!exists($args[0]->{ArrayTupleStatus}))) {
130 0         0 $args[0]->{ArrayTupleStatus} = \my @tuple_status;
131             } elsif (!$args[0]) {
132 0         0 $args[0] = {ArrayTupleStatus => \my @tuple_status};
133             }
134 0         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         0 my ($executed, $affected) = $sth->SUPER::execute_array(@args);
148 0 0       0 if (!$executed) {
    0          
149             #print Data::Dumper->Dump([$sth->{ParamArrays}], ['ParamArrays']), "\n";
150 0 0       0 if (!$h->{logmask} & DBIX_LA_LOG_ERRORS) {
151 0 0       0 return $executed unless wantarray;
152 0         0 return ($executed, $affected);
153             }
154 0         0 my $pa = $sth->{ParamArrays};
155 0         0 $sth->_dbix_la_error(2, "execute_array error:");
156 0         0 for my $n (0..@{$array_tuple_status}-1) {
  0         0  
157 0 0       0 next if (!ref($array_tuple_status->[$n]));
158 0         0 $sth->_dbix_la_error('Error', $array_tuple_status->[$n]);
159 0         0 my @plist;
160 0         0 foreach my $p (keys %{$pa}) {
  0         0  
161 0 0       0 if (ref($pa->{$p})) {
162 0         0 push @plist, $pa->{$p}->[$n];
163             } else {
164 0         0 push @plist, $pa->{$p};
165             }
166             }
167             $sth->_dbix_la_error(
168 0         0 2, "\t for " . join(',', map(DBI::neat($_), @plist)));
169             }
170             } elsif ($executed) {
171 0 0 0     0 if ((defined($sth->{NUM_OF_FIELDS})) || # result-set
172             !($h->{logmask} & DBIX_LA_LOG_INPUT)) { # logging input
173 0 0       0 return $executed unless wantarray;
174 0         0 return ($executed, $affected);
175             }
176 0         0 $sth->_dbix_la_debug($h, 2, "executed $executed, affected " .
177             DBI::neat($affected));
178             }
179             $sth->_dbix_la_debug($h, 2, Data::Dumper->Dump(
180             [$array_tuple_status], ['ArrayTupleStatus']))
181 0 0       0 if ($h->{logmask} & DBIX_LA_LOG_INPUT);
182 0 0       0 return $executed unless wantarray;
183 0         0 return ($executed, $affected);
184             }
185              
186             sub bind_param {
187 0     0 1 0 my $sth = shift;
188              
189 0         0 my $h = $sth->{private_DBIx_LogAny};
190              
191             $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 0 0 0     0 (($h->{logmask} & DBIX_LA_LOG_DELAYBINDPARAM) == 0));
195              
196 0         0 return $sth->SUPER::bind_param(@_);
197             }
198              
199             sub bind_param_inout {
200 0     0 1 0 my $sth = shift;
201 0         0 my $h = $sth->{private_DBIx_LogAny};
202              
203             $sth->_dbix_la_debug(
204             $h, 2,
205             "bind_param_inout($h->{dbh_no}.$sth->{private_DBIx_st_no})", @_)
206 0 0 0     0 if (($h->{logmask} & DBIX_LA_LOG_INPUT) && (caller !~ /^DBD::/o));
207 0         0 return $sth->SUPER::bind_param_inout(@_);
208             }
209              
210             sub bind_param_array {
211 0     0 1 0 my($sth, @args) = @_;
212 0         0 my $h = $sth->{private_DBIx_LogAny};
213              
214             $sth->_dbix_la_debug($h, 2,
215             "bind_param_array($h->{dbh_no}.$sth->{private_DBIx_st_no})",
216 0 0       0 @args) if ($h->{logmask} & DBIX_LA_LOG_INPUT);
217 0         0 return $sth->SUPER::bind_param_array(@args);
218             }
219              
220             sub fetch { # alias for fetchrow_arrayref
221 2     2 0 42 my($sth, @args) = @_;
222              
223 2         6 my $h = _unseen_sth($sth);
224              
225 2         14 my $res = $sth->SUPER::fetch(@args);
226             $sth->_dbix_la_debug(
227             $h, 2,
228             Data::Dumper->Dump([$res], ["fetch($h->{dbh_no}.$sth->{private_DBIx_st_no})"]))
229 2 50       68 if ($h->{logmask} & DBIX_LA_LOG_OUTPUT);
230 2         6 return $res;
231             }
232              
233             sub fetchrow_arrayref { # alias for fetchrow_arrayref
234 0     0 1 0 my($sth, @args) = @_;
235              
236 0         0 my $h =_unseen_sth($sth);
237              
238 0         0 my $res = $sth->SUPER::fetchrow_arrayref(@args);
239             $sth->_dbix_la_debug(
240             $h, 2,
241             Data::Dumper->Dump([$res], ["fetchrow_arrayref($h->{dbh_no}.$sth->{private_DBIx_st_no})"]))
242 0 0       0 if ($h->{logmask} & DBIX_LA_LOG_OUTPUT);
243 0         0 return $res;
244             }
245              
246             sub fetchrow_array {
247 0     0 1 0 my ($sth, @args) = @_;
248              
249 0         0 my $h = _unseen_sth($sth);
250              
251 0 0       0 if (wantarray) {
252 0         0 my @row = $sth->SUPER::fetchrow_array(@args);
253             $sth->_dbix_la_debug(
254             $h, 2,
255             Data::Dumper->Dump(
256             [\@row], ["fetchrow_array($h->{dbh_no}.$sth->{private_DBIx_st_no})"]))
257 0 0       0 if ($h->{logmask} & DBIX_LA_LOG_OUTPUT);
258 0         0 return @row;
259             } else {
260 0         0 my $row = $sth->SUPER::fetchrow_array(@args);
261             $sth->_dbix_la_debug(
262             $h, 2,
263             Data::Dumper->Dump(
264             [$row], ["fetchrow_array($h->{dbh_no}.$sth->{private_DBIx_st_no})"]))
265 0 0       0 if ($h->{logmask} & DBIX_LA_LOG_OUTPUT);
266 0         0 return $row;
267             }
268             }
269              
270             sub fetchrow_hashref {
271 0     0 1 0 my($sth, @args) = @_;
272              
273 0         0 my $h = _unseen_sth($sth);
274              
275 0         0 my $res = $sth->SUPER::fetchrow_hashref(@args);
276             $sth->_dbix_la_debug(
277             $h, 2,
278             Data::Dumper->Dump(
279             [$res], ["fetchrow_hashref($h->{dbh_no}.$sth->{private_DBIx_st_no})"]))
280 0 0       0 if ($h->{logmask} & DBIX_LA_LOG_OUTPUT);
281 0         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 8     8   11 my $sth = shift;
293              
294 8 50       21 if (!exists($sth->{private_DBIx_LogAny})) {
295 0         0 my $p = $sth->FETCH('Database')->{private_DBIx_LogAny};
296 0         0 $sth->{private_DBIx_LogAny} = $p;
297 0         0 $sth->{private_DBIx_st_no} = $p->{new_stmt_no}();
298 0         0 return $p;
299             } else {
300 8         21 return $sth->{private_DBIx_LogAny};
301             }
302             }
303              
304             1;