Skip to content

Commit

Permalink
[BC] Replaced extension of \PDOStatement with a composition to avoid …
Browse files Browse the repository at this point in the history
…having to replicate the \PDOStatement interface in ResultStatement
  • Loading branch information
morozov committed Mar 19, 2018
1 parent fedca0b commit 2b8c40d
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 175 deletions.
10 changes: 5 additions & 5 deletions lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function columnCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
if ($arg2 !== null || $arg3 !== null) {
if (count($args) > 0) {
throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
}

Expand All @@ -81,7 +81,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
public function fetch($fetchMode = null, ...$args)
{
if (! isset($this->data[$this->num])) {
return false;
Expand Down Expand Up @@ -112,10 +112,10 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$rows = [];
while ($row = $this->fetch($fetchMode)) {
while ($row = $this->fetch($fetchMode, ...$args)) {
$rows[] = $row;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function columnCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->defaultFetchMode = $fetchMode;

Expand All @@ -130,7 +130,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
public function fetch($fetchMode = null, ...$args)
{
if ($this->data === null) {
$this->data = [];
Expand Down Expand Up @@ -170,10 +170,10 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$rows = [];
while ($row = $this->fetch($fetchMode)) {
while ($row = $this->fetch($fetchMode, ...$args)) {
$rows[] = $row;
}

Expand Down
31 changes: 18 additions & 13 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,17 @@ public function execute($params = null)
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->_defaultFetchMode = $fetchMode;
$this->defaultFetchClass = $arg2 ? $arg2 : $this->defaultFetchClass;
$this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs;
$this->_defaultFetchMode = $fetchMode;

if (isset($args[0])) {
$this->defaultFetchClass = $args[0];
}

if (isset($args[1])) {
$this->defaultFetchClassCtorArgs = (array) $args[2];
}

return true;
}
Expand All @@ -191,7 +197,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
public function fetch($fetchMode = null, ...$args)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand All @@ -214,10 +220,9 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
$className = $this->defaultFetchClass;
$ctorArgs = $this->defaultFetchClassCtorArgs;

if (func_num_args() >= 2) {
$args = func_get_args();
$className = $args[1];
$ctorArgs = $args[2] ?? [];
if (count($args) > 0) {
$className = $args[0];
$ctorArgs = $args[1] ?? [];
}

$result = db2_fetch_object($this->_stmt);
Expand All @@ -242,23 +247,23 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$rows = [];

switch ($fetchMode) {
case FetchMode::CUSTOM_OBJECT:
while ($row = call_user_func_array([$this, 'fetch'], func_get_args())) {
while (($row = $this->fetch($fetchMode, ...$args)) !== false) {
$rows[] = $row;
}
break;
case FetchMode::COLUMN:
while ($row = $this->fetchColumn()) {
while (($row = $this->fetchColumn()) !== false) {
$rows[] = $row;
}
break;
default:
while ($row = $this->fetch($fetchMode)) {
while (($row = $this->fetch($fetchMode)) !== false) {
$rows[] = $row;
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private function _fetch()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
public function fetch($fetchMode = null, ...$args)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand Down Expand Up @@ -304,7 +304,7 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$fetchMode = $fetchMode ?: $this->_defaultFetchMode;

Expand Down Expand Up @@ -387,7 +387,7 @@ public function columnCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->_defaultFetchMode = $fetchMode;

Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public function execute($params = null)
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->_defaultFetchMode = $fetchMode;

Expand All @@ -361,7 +361,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
public function fetch($fetchMode = null, ...$args)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand Down Expand Up @@ -392,7 +392,7 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$fetchMode = $fetchMode ?: $this->_defaultFetchMode;

Expand Down
33 changes: 17 additions & 16 deletions lib/Doctrine/DBAL/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function __construct($dsn, $user = null, $password = null, array $options
{
try {
parent::__construct($dsn, $user, $password, $options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['Doctrine\DBAL\Driver\PDOStatement', []]);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $exception) {
throw new PDOException($exception);
Expand Down Expand Up @@ -58,7 +57,9 @@ public function getServerVersion()
public function prepare($prepareString, $driverOptions = [])
{
try {
return parent::prepare($prepareString, $driverOptions);
return $this->createStatement(
parent::prepare($prepareString, $driverOptions)
);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -70,22 +71,11 @@ public function prepare($prepareString, $driverOptions = [])
public function query()
{
$args = func_get_args();
$argsCount = count($args);

try {
if ($argsCount == 4) {
return parent::query($args[0], $args[1], $args[2], $args[3]);
}

if ($argsCount == 3) {
return parent::query($args[0], $args[1], $args[2]);
}

if ($argsCount == 2) {
return parent::query($args[0], $args[1]);
}

return parent::query($args[0]);
return $this->createStatement(
parent::query(...$args)
);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand Down Expand Up @@ -114,4 +104,15 @@ public function requiresQueryForServerVersion()
{
return false;
}

/**
* Creates a wrapped statement
*
* @param \PDOStatement $stmt
* @return PDOStatement
*/
private function createStatement(\PDOStatement $stmt) : PDOStatement
{
return new PDOStatement($stmt);
}
}
Loading

0 comments on commit 2b8c40d

Please sign in to comment.