| 1 | # -*-perl-*- | ||||||||||||||||||||
| 2 | # Creation date: 2004-10-29 14:01:59 | ||||||||||||||||||||
| 3 | # Authors: Don | ||||||||||||||||||||
| 4 | # $Revision: 1963 $ | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | { package DBIx::Wrapper::Request; | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | $VERSION = do { my @r=(q$Revision: 1963 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r }; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 14 | 0 | my $proto = shift; | |||||||||||||||||||
| 15 | 0 | my $db_obj = shift; | |||||||||||||||||||
| 16 | 0 | ▶ | my $self = bless { _db_obj => $db_obj }, ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 17 | 0 | return $self; | |||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | ▶ | sub getDbObj { | ||||||||||||||||||
|
Subroutine
getDbObj: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 21 | 0 | return shift()->{_db_obj}; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 0 | ▶ | sub getQuery { | ||||||||||||||||||
|
Subroutine
getQuery: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 25 | 0 | shift()->{_query}; | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | ▶ | sub setQuery { | ||||||||||||||||||
|
Subroutine
setQuery: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 29 | 0 | my $self = shift; | |||||||||||||||||||
| 30 | 0 | $self->{_query} = shift; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 0 | ▶ | sub getExecArgs { | ||||||||||||||||||
|
Subroutine
getExecArgs: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 34 | 0 | ▶ | return shift()->{_exec_args} || []; | ||||||||||||||||||
|
Condition0%0/2
+(shift())->{'_exec_args'} || []
MC/DC0%0/1
+(shift())->{'_exec_args'} || []
+(shift())->{'_exec_args'}
Truth table0%0/2
A
A= +(shift())->{'_exec_args'}
| |||||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | ▶ | sub setExecArgs { | ||||||||||||||||||
|
Subroutine
setExecArgs: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 38 | 0 | my $self = shift; | |||||||||||||||||||
| 39 | 0 | my $args = shift; | |||||||||||||||||||
| 40 | 0 | ▶ | if (ref($args) eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | $self->{_exec_args} = $args; | |||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||
| 43 | 0 | $self->{_exec_args} = [ $args ]; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | sub getExecReturnValue { | ||||||||||||||||||
|
Subroutine
getExecReturnValue: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 48 | 0 | return shift()->{_exec_return_value}; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | sub setExecReturnValue { | ||||||||||||||||||
|
Subroutine
setExecReturnValue: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||||||||||
| 53 | 0 | $self->{_exec_return_value} = shift; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | ▶ | sub getReturnVal { | ||||||||||||||||||
|
Subroutine
getReturnVal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 57 | 0 | return shift()->{_return_record}; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | sub setReturnVal { | ||||||||||||||||||
|
Subroutine
setReturnVal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||
| 62 | 0 | $self->{_return_record} = shift; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | sub getStatementHandle { | ||||||||||||||||||
|
Subroutine
getStatementHandle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 66 | 0 | return shift()->{_statement_handle}; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub setStatementHandle { | ||||||||||||||||||
|
Subroutine
setStatementHandle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||
| 71 | 0 | $self->{_statement_handle} = shift; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | sub getErrorStr { | ||||||||||||||||||
|
Subroutine
getErrorStr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 75 | 0 | return shift()->{_errstr}; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | sub setErrorStr { | ||||||||||||||||||
|
Subroutine
setErrorStr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||
| 80 | 0 | $self->{_errstr} = shift; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | ▶ | sub OK { | ||||||||||||||||||
|
Subroutine
OK: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 84 | 0 | return 1; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | sub DECLINED { | ||||||||||||||||||
|
Subroutine
DECLINED: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 88 | 0 | return 0; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 2 | 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<getDbObj()> | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | Returns the DBIx::Wrapper object that created the Request object. | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =head2 C<getQuery()> | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | Returns the current query. | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =head2 C<setQuery($query)> | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | Sets the current query. | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =head2 C<getExecArgs()> | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | Returns a reference to the array of execute arguments passed to | ||||||||||||||||||||
| 149 | the DBIx::Wrapper method currently executing. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =head2 C<setExecArgs(\@args)> | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | Sets the current execute arguments. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head2 C<getExecReturnValue()> | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Returns the current execute() return value. | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =head2 C<setExecReturnValue($rv)> | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | Sets the current execute() return value. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head2 C<getReturnVal()> | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | Gets the current return value (from a fetch). | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =head2 C<setReturnVal($rv)> | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | Sets the current return value (from a fetch). | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head2 C<getStatementHandle()> | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Get the current statement handle being used. | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =head2 C<setStatementHandle($sth)> | ||||||||||||||||||||
| 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<setErrorStr($err_str)> | ||||||||||||||||||||
| 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 <don@regexguy.com> | ||||||||||||||||||||
| 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 | ||||||||||||||||||||