File Coverage

blib/lib/DBIx/Wrapper/Request.pm
Criterion Covered Total %
statement 6 34 17.6
branch 0 2 0.0
condition 0 5 0.0
subroutine 2 18 11.1
pod 13 16 81.2
total 21 75 28.0


line stmt bran cond sub pod time code
1             # -*-perl-*-
2             # Creation date: 2004-10-29 14:01:59
3             # Authors: Don
4             # $Revision: 1963 $
5              
6 2     2   11 use strict;
  2         4  
  2         107  
7              
8             { package DBIx::Wrapper::Request;
9              
10 2     2   11 use vars qw($VERSION);
  2         3  
  2         1192  
11             $VERSION = do { my @r=(q$Revision: 1963 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
12              
13             sub new {
14 0     0 0   my $proto = shift;
15 0           my $db_obj = shift;
16 0   0       my $self = bless { _db_obj => $db_obj }, ref($proto) || $proto;
17 0           return $self;
18             }
19              
20             sub getDbObj {
21 0     0 1   return shift()->{_db_obj};
22             }
23              
24             sub getQuery {
25 0     0 1   shift()->{_query};
26             }
27              
28             sub setQuery {
29 0     0 1   my $self = shift;
30 0           $self->{_query} = shift;
31             }
32              
33             sub getExecArgs {
34 0   0 0 1   return shift()->{_exec_args} || [];
35             }
36              
37             sub setExecArgs {
38 0     0 1   my $self = shift;
39 0           my $args = shift;
40 0 0         if (ref($args) eq 'ARRAY') {
41 0           $self->{_exec_args} = $args;
42             } else {
43 0           $self->{_exec_args} = [ $args ];
44             }
45             }
46              
47             sub getExecReturnValue {
48 0     0 1   return shift()->{_exec_return_value};
49             }
50              
51             sub setExecReturnValue {
52 0     0 1   my $self = shift;
53 0           $self->{_exec_return_value} = shift;
54             }
55              
56             sub getReturnVal {
57 0     0 1   return shift()->{_return_record};
58             }
59            
60             sub setReturnVal {
61 0     0 1   my $self = shift;
62 0           $self->{_return_record} = shift;
63             }
64              
65             sub getStatementHandle {
66 0     0 1   return shift()->{_statement_handle};
67             }
68              
69             sub setStatementHandle {
70 0     0 1   my $self = shift;
71 0           $self->{_statement_handle} = shift;
72             }
73              
74             sub getErrorStr {
75 0     0 1   return shift()->{_errstr};
76             }
77              
78             sub setErrorStr {
79 0     0 1   my $self = shift;
80 0           $self->{_errstr} = shift;
81             }
82              
83             sub OK {
84 0     0 0   return 1;
85             }
86              
87             sub DECLINED {
88 0     0 0   return 0;
89             }
90              
91             }
92              
93             1;
94              
95             =pod
96              
97             =head1 NAME
98              
99             DBIx::Wrapper::Request - Request object for database operations
100              
101             =head1 SYNOPSIS
102              
103             Objects of the class are created by DBIx::Wrapper objects and
104             passed to hooks. You should never have to create one yourself.
105              
106             my $db = $req->getDbObj;
107              
108             my $query = $req->getQuery;
109             $req->setQuery($query);
110              
111             my $exec_args = $req->getExecArgs;
112             $req->setExecArgs(\@args);
113              
114             my $rv = $req->getExecReturnValue;
115             $req->setExecReturnValue($rv);
116              
117             my $rv = $req->getReturnVal;
118             $req->setReturnVal($rv);
119              
120             my $sth = $req->getStatementHandle;
121             $req->setStatementHandle($sth);
122              
123             my $err_str = $req->getErrorStr;
124             $req->setErrorStr($err_str);
125              
126             =head1 DESCRIPTION
127              
128             DBIx::Wrapper::Request objects are used to encapsulate date
129             passed between DBIx::Wrapper methods at various stages of
130             executing a query.
131              
132             =head1 METHODS
133              
134             =head2 C
135              
136             Returns the DBIx::Wrapper object that created the Request object.
137              
138             =head2 C
139              
140             Returns the current query.
141              
142             =head2 C
143              
144             Sets the current query.
145              
146             =head2 C
147              
148             Returns a reference to the array of execute arguments passed to
149             the DBIx::Wrapper method currently executing.
150              
151             =head2 C
152              
153             Sets the current execute arguments.
154              
155             =head2 C
156              
157             Returns the current execute() return value.
158              
159             =head2 C
160              
161             Sets the current execute() return value.
162              
163             =head2 C
164              
165             Gets the current return value (from a fetch).
166              
167             =head2 C
168              
169             Sets the current return value (from a fetch).
170              
171             =head2 C
172              
173             Get the current statement handle being used.
174              
175             =head2 C
176              
177             Set the current statement handle to use.
178              
179             =head2 C<$req->getErrorStr()>
180              
181             Get the error string.
182              
183             =head2 C
184              
185             Set the error string.
186              
187              
188             =head1 EXAMPLES
189              
190             ##################################################
191             # Pre prepare hook
192              
193             $db_obj->addPrePrepareHook(\&_db_pre_prepare_hook)
194              
195             sub _db_pre_prepare_hook {
196             my $self = shift;
197             my $r = shift;
198             my $query = $r->getQuery;
199            
200             if ($query =~ /^\s*(?:update|delete|insert|replace|create|drop|alter)/i) {
201             my $db = $r->getDbObj;
202             unless ($db->ping) {
203             # db connection has gone away, so try to reconnect
204             my $msg = "UI DataProvider pre-prepare: db ping failed, reconnecting to ";
205             $msg .= $db->_getDataSource;
206             print STDERR $msg . "\n";
207             my $tries_left = 5;
208             my $connected = 0;
209             my $sleep_time = 0;
210             while ($tries_left) {
211             $sleep_time++;
212             sleep $sleep_time;
213             $tries_left--;
214             $connected = $db->reconnect;
215             last if $connected;
216             }
217              
218             unless ($connected) {
219             die "Couldn't reconnect to db after ping failure: dsn=" . $db->_getDataSource;
220             }
221             }
222             }
223            
224             return $r->OK;
225             }
226              
227              
228             ##################################################
229             # Post execute hook
230              
231             sub _db_post_exec_hook {
232             my $self = shift;
233             my $r = shift;
234              
235             my $exec_successful = $r->getExecReturnValue;
236             unless ($exec_successful) {
237             my $query = $r->getQuery;
238             if ($r->getQuery =~ /^\s*(?:select|show)/i) {
239             my $errstr = $r->getErrorStr;
240             if ($errstr =~ /Lost connection to MySQL server during query/i) {
241             my $db = $r->getDbObj;
242             my $msg = "UI DataProvider post exec: lost connection to MySQL server ";
243             $msg .= "during query, reconnecting to " . $db->_getDataSource;
244             print STDERR $msg . "\n";
245             my $tries_left = 5;
246             my $connected = 0;
247             my $sleep_time = 0;
248             while ($tries_left) {
249             $sleep_time++;
250             sleep $sleep_time;
251             $tries_left--;
252             $connected = $db->reconnect;
253             last if $connected;
254             }
255            
256             if ($connected) {
257             my $sth = $db->prepare_no_hooks($r->getQuery);
258             $r->setStatementHandle($sth);
259             my $exec_args = $r->getExecArgs;
260             my $rv = $sth->execute(@$exec_args);
261             $r->setExecReturnValue($rv);
262             } else {
263             die "Couldn't reconnect to db after losing connection: dsn="
264             . $db->_getDataSource;
265             }
266             }
267             }
268             }
269            
270             return $r->OK;
271             }
272              
273              
274             =head1 BUGS
275              
276             =head1 AUTHOR
277              
278             Don Owens
279              
280             =head1 LICENSE AND COPYRIGHT
281              
282             Copyright (c) 2004-2012 Don Owens (don@regexguy.com). All rights reserved.
283              
284             This free software; you can redistribute it and/or modify it
285             under the same terms as Perl itself. See perlartistic.
286              
287             This program is distributed in the hope that it will be
288             useful, but WITHOUT ANY WARRANTY; without even the implied
289             warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
290             PURPOSE.
291              
292              
293             =head1 VERSION
294              
295             $Id: Request.pm 1963 2012-01-17 15:41:53Z don $
296              
297             =cut