Skip to content

Commit

Permalink
Statement from PDO::query is handled;
Browse files Browse the repository at this point in the history
Hand bindColumn in case of fetchAll
  • Loading branch information
adamturcsan committed Nov 14, 2016
1 parent 89df08d commit d3a4098
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 50 deletions.
10 changes: 5 additions & 5 deletions src/ReconnectingPDO.php
Expand Up @@ -10,6 +10,7 @@
namespace LegoW\ReconnectingPDO;

use PDO;
use LegoW\ReconnectingPDO\ReconnectingPDOStatement;

/**
* It covers the PDO database handler to prevent connection loss caused by non-critical
Expand Down Expand Up @@ -127,8 +128,8 @@ protected function call($method, $arguments)
$ex->getCode(), $ex);
}
}
if($returnValue instanceof \PDOStatement) {
return new ReconnectingPDOStatement($returnValue, $this);
if ($returnValue instanceof \PDOStatement) {
return new ReconnectingPDOStatement($returnValue, $this, $method === 'query');
}
return $returnValue;
}
Expand Down Expand Up @@ -163,7 +164,7 @@ public function setMaxReconnection($max)
{
$this->maxReconnection = $max;
}

/**
*
* Parameters can be <b>(string)dsn</b>, <b>(string)username</b>,
Expand All @@ -179,7 +180,7 @@ public function setConnectionParameters($parameters, $autoconnect = false)
$this->$key = $param;
}
}
if($autoconnect) {
if ($autoconnect) {
$this->connectDb();
}
}
Expand All @@ -199,5 +200,4 @@ protected function connectionParametersAreSet()
}
return false;
}

}
62 changes: 59 additions & 3 deletions src/ReconnectingPDOStatement.php
Expand Up @@ -15,7 +15,9 @@
use PDO;

/**
* Description of ReconnectingPDOStatement
* Represents a prepared statement and, after the statement is executed, an
* associated result set.
* If server has gone away it can recreate itself. Use it with great caution in cases of update or insert statements
*
* @author Turcsán Ádám <turcsan.adam@legow.hu>
* @method bool execute(array $parameters = null [optional]) Executes a prepared statement
Expand Down Expand Up @@ -56,6 +58,11 @@ class ReconnectingPDOStatement
*/
protected $executed = false;

/**
* @var bool
*/
protected $isQuery = false;

/**
* @return \PDOStatement
*/
Expand All @@ -78,11 +85,13 @@ public function isExecuted()
* @param ReconnectingPDO $connection
*/
public function __construct(PDOStatement $statement,
ReconnectingPDO $connection, StatementCursor $cursor = null)
ReconnectingPDO $connection, $isQuery = false,
StatementCursor $cursor = null)
{
$this->statement = $statement;
$this->queryString = $statement->queryString;
$this->connection = $connection;
$this->isQuery = $isQuery;
if ($cursor == null) {
$cursor = new StatementCursor();
}
Expand Down Expand Up @@ -143,6 +152,25 @@ protected function call($method, &$arguments)
}

protected function recreateStatement()
{
if ($this->isQuery) {
return $this->recreateQuery();
}
return $this->recreatePreparedStatement();
}

protected function recreateQuery()
{
//Recreate only if it is a fetchable result set and the error occured during longrun fetching
if ($this->cursor->getPosition()) {
/* @var $reconnectingStatement ReconnectingPDOStatement */
$reconnectingStatement = $this->connection->query($this->queryString);
$this->statement = $reconnectingStatement->getPDOStatement();
$this->searchPosition();
}
}

protected function recreatePreparedStatement()
{
$shouldBeExecuted = $this->executed;
$reconnectingstatement = $this->connection->prepare($this->queryString);
Expand Down Expand Up @@ -196,10 +224,38 @@ public function fetch($fetchType = null,
$result = $this->call('fetch', $args);
if (isset($this->seedData['bindColumn']) && count($this->seedData['bindColumn'])) {
foreach ($this->seedData['bindColumn'] as $name => $column) {
$this->seedData['bindColumn'][$name] = $result[$name];
if (is_int($name)) {
$keys = array_keys($result);
$this->seedData['bindColumn'][$name] = $result[$keys[$name - 1]];
} else {
$this->seedData['bindColumn'][$name] = $result[$name];
}
}
}
return $result;
}

/**
*
* @param int $fetch_style
* @param mixed $fetch_argumnet
* @param array $ctor_args
*/
public function fetchAll($fetch_style = \PDO::FETCH_BOTH,
$fetch_argumnet = null, $ctor_args = [])
{
$args = func_get_args();
$result = $this->call('fetchAll', $args);
if (isset($this->seedData['bindColumn']) && count($this->seedData['bindColumn'])) {
foreach ($this->seedData['bindColumn'] as $name => $column) {
if (is_int($name)) {
$keys = array_keys($result);
$this->seedData['bindColumn'][$name] = $result[count($result) - 1][$keys[$name - 1]];
} else {
$this->seedData['bindColumn'][$name] = $result[count($result) - 1][$name];
}
}
}
}

}

0 comments on commit d3a4098

Please sign in to comment.