Skip to content

Commit

Permalink
Fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
adamturcsan committed Jan 17, 2017
1 parent 059716b commit 6735463
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/ReconnectingPDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ protected function call($method, &$arguments)
$this->trackCursor();
break;
case 'execute':
$this->executed = true;
$parameters = isset($arguments[0]) ? $arguments[0] : null;
return $this->execute($parameters);
break;
default:
break;
Expand Down Expand Up @@ -193,12 +194,14 @@ protected function recreatePreparedStatement()
if (!empty($this->seedData)) {
/* @var $method string bindParam, bindColumn or bindValue */
foreach ($this->seedData as $method => $arguments) {
if ($method === 'execute') {
continue;
}
/* @var $key string Parameter name */
foreach ($arguments as $key => $params) {
list($name,, $paramType) = $params;
// Value comes from the seedData array, because bindParam only takes it by reference
$statement->$method($name,
$this->seedData[$method][$key][1], $paramType);
$statement->$method($name, $this->seedData[$method][$key][1], $paramType);
}
}
}
Expand Down Expand Up @@ -252,6 +255,19 @@ public function bindColumn($column, &$param, $type = null, $maxlen = null,
return $this->statement->bindColumn($column, $param, $type, $maxlen,
$driverdata);
}
/**
* Executes a prepared statement
* @param array $parameters [optional]
*/
protected function execute(array $parameters = null)
{
if($parameters != null && count($parameters)) {
$this->seedData['execute'] = $parameters;
}
$success = call_user_func([$this->statement, 'execute'], $parameters);
$this->executed = true;
return $success;
}

/**
* Fetches the next row from a result set
Expand Down
26 changes: 26 additions & 0 deletions test/ReconnectingPDOStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,5 +421,31 @@ public function testSetFetchMode()
$stm->setFetchMode(\PDO::FETCH_NUM);
$this->assertEquals(array_values($this->testDBData[1]), $stm->fetch());
}

public function testExecuteWithParameters()
{
// Create failing statement while execute
$mockStm = $this->getMockBuilder(PDOStatement::class)->getMock();
$mockStm->method('execute')->will($this->throwException(new \PDOException('Mysql server has gone away')));

// Create reconnecting PDO
$rPDO = new ReconnectingPDO($this->testDSN, '', '');

$rstm = new ReconnectingPDOStatement($mockStm, $rPDO);

// Set query string to have working querystring while recreation
$reflection = new \ReflectionClass($rstm);
$queryStringProperty = $reflection->getProperty('queryString');
$queryStringProperty->setAccessible(true);
$queryStringProperty->setValue($rstm, 'SELECT :value, :param;');

$value = $param = 'value';
$rstm->execute([
'value' => $value,
'param' => $param
]);

$this->assertEquals([$value, $param], $rstm->fetch(PDO::FETCH_NUM));
}

}

0 comments on commit 6735463

Please sign in to comment.