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 Jan 3, 2018
1 parent b094888 commit 965353c
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
if ( ! isset($this->data[$this->num])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
if ($this->data === null) {
$this->data = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private function _fetch()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
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 @@ -41,7 +41,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 @@ -74,7 +73,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 @@ -86,22 +87,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 @@ -130,4 +120,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);
}
}
80 changes: 62 additions & 18 deletions lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace Doctrine\DBAL\Driver;

use IteratorAggregate;
use PDO;

/**
Expand All @@ -27,7 +28,7 @@
*
* @since 2.0
*/
class PDOStatement extends \PDOStatement implements Statement
class PDOStatement implements IteratorAggregate, Statement
{
/**
* @var array<int,int>
Expand All @@ -53,10 +54,13 @@ class PDOStatement extends \PDOStatement implements Statement
];

/**
* Protected constructor.
* @var \PDOStatement
*/
protected function __construct()
private $stmt;

public function __construct(\PDOStatement $stmt)
{
$this->stmt = $stmt;
}

/**
Expand All @@ -72,14 +76,14 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
// declaration.
try {
if ($arg2 === null && $arg3 === null) {
return parent::setFetchMode($fetchMode);
return $this->stmt->setFetchMode($fetchMode);
}

if ($arg3 === null) {
return parent::setFetchMode($fetchMode, $arg2);
return $this->stmt->setFetchMode($fetchMode, $arg2);
}

return parent::setFetchMode($fetchMode, $arg2, $arg3);
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -93,7 +97,7 @@ public function bindValue($param, $value, $type = self::PARAM_STR)
$type = $this->convertParamType($type);

try {
return parent::bindValue($param, $value, $type);
return $this->stmt->bindValue($param, $value, $type);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -107,7 +111,7 @@ public function bindParam($column, &$variable, $type = self::PARAM_STR, $length
$type = $this->convertParamType($type);

try {
return parent::bindParam($column, $variable, $type, $length, $driverOptions);
return $this->stmt->bindParam($column, $variable, $type, $length, $driverOptions);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -119,21 +123,45 @@ public function bindParam($column, &$variable, $type = self::PARAM_STR, $length
public function closeCursor()
{
try {
return parent::closeCursor();
return $this->stmt->closeCursor();
} catch (\PDOException $exception) {
// Exceptions not allowed by the interface.
// In case driver implementations do not adhere to the interface, silence exceptions here.
return true;
}
}

/**
* {@inheritdoc}
*/
public function columnCount()
{
return $this->stmt->columnCount();
}

/**
* {@inheritdoc}
*/
public function errorCode()
{
return $this->stmt->errorCode();
}

/**
* {@inheritdoc}
*/
public function errorInfo()
{
return $this->stmt->errorInfo();
}

/**
* {@inheritdoc}
*/
public function execute($params = null)
{
try {
return parent::execute($params);
return $this->stmt->execute($params);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -142,16 +170,24 @@ public function execute($params = null)
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function rowCount()
{
return $this->stmt->rowCount();
}

/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null)
{
$fetchMode = $this->convertFetchMode($fetchMode);

try {
if ($fetchMode === null) {
return parent::fetch();
return $this->stmt->fetch();
}

return parent::fetch($fetchMode);
return $this->stmt->fetch($fetchMode);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -166,18 +202,18 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n

try {
if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) {
return parent::fetchAll();
return $this->stmt->fetchAll();
}

if (null === $fetchArgument && null === $ctorArgs) {
return parent::fetchAll($fetchMode);
return $this->stmt->fetchAll($fetchMode);
}

if (null === $ctorArgs) {
return parent::fetchAll($fetchMode, $fetchArgument);
return $this->stmt->fetchAll($fetchMode, $fetchArgument);
}

return parent::fetchAll($fetchMode, $fetchArgument, $ctorArgs);
return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -189,7 +225,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n
public function fetchColumn($columnIndex = 0)
{
try {
return parent::fetchColumn($columnIndex);
return $this->stmt->fetchColumn($columnIndex);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand Down Expand Up @@ -228,4 +264,12 @@ private function convertFetchMode(?int $fetchMode) : ?int

return self::$fetchModeMap[$fetchMode];
}

/**
* {@inheritdoc}
*/
public function getIterator()
{
yield from $this->stmt;
}
}
4 changes: 1 addition & 3 deletions lib/Doctrine/DBAL/Driver/ResultStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,13 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null);
* @param int|null $fetchMode Controls how the next row will be returned to the caller.
* The value must be one of the self::FETCH_* constants,
* defaulting to self::FETCH_BOTH.
* @param int|null $_ Unused argument for compatibility with PDOStatement
* @param int|null $__ Unused argument for compatibility with PDOStatement
*
* @return mixed The return value of this method on success depends on the fetch mode. In all cases, FALSE is
* returned on failure.
*
* @see self::FETCH_* constants.
*/
public function fetch($fetchMode = null, $_ = null, $__ = null);
public function fetch($fetchMode = null);

/**
* Returns an array containing all of the result set rows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function execute($params = null)
*
* @throws SQLAnywhereException
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
if ( ! is_resource($this->result)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function getIterator()
*
* @throws SQLSrvException
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Portability/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
$fetchMode = $fetchMode ?: $this->defaultFetchMode;

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
return $this->stmt->fetch($fetchMode);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/Mocks/HydratorMockStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function fetchColumn($columnNumber = 0)
*
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $_ = null, $__ = null)
public function fetch($fetchMode = null)
{
$current = current($this->_resultSet);
next($this->_resultSet);
Expand Down Expand Up @@ -93,7 +93,7 @@ public function errorInfo()
{
}

public function execute($params = array())
public function execute($params = null)
{
}

Expand Down

0 comments on commit 965353c

Please sign in to comment.