NAME

DBIx::Wrapper::Request - Request object for database operations

SYNOPSIS

Objects of the class are created by DBIx::Wrapper objects and passed to hooks. You should never have to create one yourself.

my $db = $req->getDbObj;

my $query = $req->getQuery;
$req->setQuery($query);

my $exec_args = $req->getExecArgs;
$req->setExecArgs(\@args);

my $rv = $req->getExecReturnValue;
$req->setExecReturnValue($rv);

my $rv = $req->getReturnVal;
$req->setReturnVal($rv);

my $sth = $req->getStatementHandle;
$req->setStatementHandle($sth);

my $err_str = $req->getErrorStr;
$req->setErrorStr($err_str);

DESCRIPTION

DBIx::Wrapper::Request objects are used to encapsulate date passed between DBIx::Wrapper methods at various stages of executing a query.

METHODS

getDbObj()

Returns the DBIx::Wrapper object that created the Request object.

getQuery()

Returns the current query.

setQuery($query)

Sets the current query.

getExecArgs()

Returns a reference to the array of execute arguments passed to the DBIx::Wrapper method currently executing.

setExecArgs(\@args)

Sets the current execute arguments.

getExecReturnValue()

Returns the current execute() return value.

setExecReturnValue($rv)

Sets the current execute() return value.

getReturnVal()

Gets the current return value (from a fetch).

setReturnVal($rv)

Sets the current return value (from a fetch).

getStatementHandle()

Get the current statement handle being used.

setStatementHandle($sth)

Set the current statement handle to use.

$req-getErrorStr()>

Get the error string.

setErrorStr($err_str)

Set the error string.

EXAMPLES

##################################################
# Pre prepare hook

$db_obj->addPrePrepareHook(\&_db_pre_prepare_hook)

sub _db_pre_prepare_hook {
    my $self = shift;
    my $r = shift;
    my $query = $r->getQuery;
    
    if ($query =~ /^\s*(?:update|delete|insert|replace|create|drop|alter)/i) {
        my $db = $r->getDbObj;
        unless ($db->ping) {
            # db connection has gone away, so try to reconnect
            my $msg = "UI DataProvider pre-prepare: db ping failed, reconnecting to ";
            $msg .= $db->_getDataSource;
            print STDERR $msg . "\n";
            my $tries_left = 5;
            my $connected = 0;
            my $sleep_time = 0;
            while ($tries_left) {
                $sleep_time++;
                sleep $sleep_time;
                $tries_left--;
                $connected = $db->reconnect;
                last if $connected;
            }

            unless ($connected) {
                die "Couldn't reconnect to db after ping failure: dsn=" . $db->_getDataSource;
            }
        }
    }
                        
    return $r->OK;
}


##################################################
# Post execute hook

sub _db_post_exec_hook {
    my $self = shift;
    my $r = shift;

    my $exec_successful = $r->getExecReturnValue;
    unless ($exec_successful) {
        my $query = $r->getQuery;
        if ($r->getQuery =~ /^\s*(?:select|show)/i) {
            my $errstr = $r->getErrorStr;
            if ($errstr =~ /Lost connection to MySQL server during query/i) {
                my $db = $r->getDbObj;
                my $msg = "UI DataProvider post exec: lost connection to MySQL server ";
                $msg .= "during query, reconnecting to " . $db->_getDataSource;
                print STDERR $msg . "\n";
                my $tries_left = 5;
                my $connected = 0;
                my $sleep_time = 0;
                while ($tries_left) {
                    $sleep_time++;
                    sleep $sleep_time;
                    $tries_left--;
                    $connected = $db->reconnect;
                    last if $connected;
                }
                                  
                if ($connected) {
                    my $sth = $db->prepare_no_hooks($r->getQuery);
                    $r->setStatementHandle($sth);
                    my $exec_args = $r->getExecArgs;
                    my $rv = $sth->execute(@$exec_args);
                    $r->setExecReturnValue($rv);
                } else {
                    die "Couldn't reconnect to db after losing connection: dsn="
                        . $db->_getDataSource;
                }
            }
        }
    }
                      
    return $r->OK;
}

BUGS

AUTHOR

Don Owens <don@regexguy.com>

LICENSE AND COPYRIGHT

Copyright (c) 2004-2012 Don Owens (don@regexguy.com). All rights reserved.

This free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

VERSION

$Id: Request.pm 1963 2012-01-17 15:41:53Z don $