[BC] Replaced extension of \PDOStatement with a composition to avoid having to...

[BC] Replaced extension of \PDOStatement with a composition to avoid having to replicate the \PDOStatement interface in ResultStatement
parent 77d9b2f1
...@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\ResultStatement; ...@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate; use IteratorAggregate;
use PDO;
use function array_merge; use function array_merge;
use function array_values; use function array_values;
use function count; use function count;
...@@ -59,9 +58,9 @@ class ArrayStatement implements IteratorAggregate, ResultStatement ...@@ -59,9 +58,9 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/** /**
* {@inheritdoc} * {@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()'); throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
} }
...@@ -83,7 +82,7 @@ class ArrayStatement implements IteratorAggregate, ResultStatement ...@@ -83,7 +82,7 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/** /**
* {@inheritdoc} * {@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])) { if (! isset($this->data[$this->num])) {
return false; return false;
...@@ -114,10 +113,10 @@ class ArrayStatement implements IteratorAggregate, ResultStatement ...@@ -114,10 +113,10 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$rows = []; $rows = [];
while ($row = $this->fetch($fetchMode)) { while ($row = $this->fetch($fetchMode, ...$args)) {
$rows[] = $row; $rows[] = $row;
} }
......
...@@ -9,7 +9,6 @@ use Doctrine\DBAL\Driver\Statement; ...@@ -9,7 +9,6 @@ use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate; use IteratorAggregate;
use PDO;
use function array_merge; use function array_merge;
use function array_values; use function array_values;
use function reset; use function reset;
...@@ -104,7 +103,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement ...@@ -104,7 +103,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, ...$args)
{ {
$this->defaultFetchMode = $fetchMode; $this->defaultFetchMode = $fetchMode;
...@@ -124,7 +123,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement ...@@ -124,7 +123,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) public function fetch($fetchMode = null, ...$args)
{ {
if ($this->data === null) { if ($this->data === null) {
$this->data = []; $this->data = [];
...@@ -164,9 +163,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement ...@@ -164,9 +163,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
return $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs); return $this->statement->fetchAll($fetchMode, ...$args);
} }
/** /**
......
...@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\StatementIterator; ...@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
use PDO;
use ReflectionClass; use ReflectionClass;
use ReflectionObject; use ReflectionObject;
use ReflectionProperty; use ReflectionProperty;
...@@ -19,6 +18,7 @@ use const DB2_LONG; ...@@ -19,6 +18,7 @@ use const DB2_LONG;
use const DB2_PARAM_FILE; use const DB2_PARAM_FILE;
use const DB2_PARAM_IN; use const DB2_PARAM_IN;
use function array_change_key_case; use function array_change_key_case;
use function count;
use function db2_bind_param; use function db2_bind_param;
use function db2_execute; use function db2_execute;
use function db2_fetch_array; use function db2_fetch_array;
...@@ -32,8 +32,6 @@ use function db2_stmt_error; ...@@ -32,8 +32,6 @@ use function db2_stmt_error;
use function db2_stmt_errormsg; use function db2_stmt_errormsg;
use function error_get_last; use function error_get_last;
use function fclose; use function fclose;
use function func_get_args;
use function func_num_args;
use function fwrite; use function fwrite;
use function gettype; use function gettype;
use function is_object; use function is_object;
...@@ -241,11 +239,17 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -241,11 +239,17 @@ class DB2Statement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, ...$args)
{ {
$this->defaultFetchMode = $fetchMode; $this->defaultFetchMode = $fetchMode;
$this->defaultFetchClass = $arg2 ?: $this->defaultFetchClass;
$this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs; if (isset($args[0])) {
$this->defaultFetchClass = $args[0];
}
if (isset($args[1])) {
$this->defaultFetchClassCtorArgs = (array) $args[2];
}
return true; return true;
} }
...@@ -261,7 +265,7 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -261,7 +265,7 @@ class DB2Statement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@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 // do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation // in order to prevent exceptional situation
...@@ -284,10 +288,9 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -284,10 +288,9 @@ class DB2Statement implements IteratorAggregate, Statement
$className = $this->defaultFetchClass; $className = $this->defaultFetchClass;
$ctorArgs = $this->defaultFetchClassCtorArgs; $ctorArgs = $this->defaultFetchClassCtorArgs;
if (func_num_args() >= 2) { if (count($args) > 0) {
$args = func_get_args(); $className = $args[0];
$className = $args[1]; $ctorArgs = $args[1] ?? [];
$ctorArgs = $args[2] ?? [];
} }
$result = db2_fetch_object($this->stmt); $result = db2_fetch_object($this->stmt);
...@@ -312,13 +315,13 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -312,13 +315,13 @@ class DB2Statement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$rows = []; $rows = [];
switch ($fetchMode) { switch ($fetchMode) {
case FetchMode::CUSTOM_OBJECT: case FetchMode::CUSTOM_OBJECT:
while (($row = $this->fetch(...func_get_args())) !== false) { while (($row = $this->fetch($fetchMode, ...$args)) !== false) {
$rows[] = $row; $rows[] = $row;
} }
break; break;
......
...@@ -10,7 +10,6 @@ use Doctrine\DBAL\ParameterType; ...@@ -10,7 +10,6 @@ use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
use mysqli; use mysqli;
use mysqli_stmt; use mysqli_stmt;
use PDO;
use stdClass; use stdClass;
use function array_combine; use function array_combine;
use function array_fill; use function array_fill;
...@@ -304,7 +303,7 @@ class MysqliStatement implements IteratorAggregate, Statement ...@@ -304,7 +303,7 @@ class MysqliStatement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@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 // do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation // in order to prevent exceptional situation
...@@ -358,7 +357,7 @@ class MysqliStatement implements IteratorAggregate, Statement ...@@ -358,7 +357,7 @@ class MysqliStatement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$fetchMode = $fetchMode ?: $this->_defaultFetchMode; $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
...@@ -441,7 +440,7 @@ class MysqliStatement implements IteratorAggregate, Statement ...@@ -441,7 +440,7 @@ class MysqliStatement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, ...$args)
{ {
$this->_defaultFetchMode = $fetchMode; $this->_defaultFetchMode = $fetchMode;
......
...@@ -8,7 +8,6 @@ use Doctrine\DBAL\FetchMode; ...@@ -8,7 +8,6 @@ use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate; use IteratorAggregate;
use PDO;
use const OCI_ASSOC; use const OCI_ASSOC;
use const OCI_B_BIN; use const OCI_B_BIN;
use const OCI_B_BLOB; use const OCI_B_BLOB;
...@@ -380,7 +379,7 @@ class OCI8Statement implements IteratorAggregate, Statement ...@@ -380,7 +379,7 @@ class OCI8Statement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, ...$args)
{ {
$this->_defaultFetchMode = $fetchMode; $this->_defaultFetchMode = $fetchMode;
...@@ -398,7 +397,7 @@ class OCI8Statement implements IteratorAggregate, Statement ...@@ -398,7 +397,7 @@ class OCI8Statement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@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 // do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation // in order to prevent exceptional situation
...@@ -429,7 +428,7 @@ class OCI8Statement implements IteratorAggregate, Statement ...@@ -429,7 +428,7 @@ class OCI8Statement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$fetchMode = $fetchMode ?: $this->_defaultFetchMode; $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
......
...@@ -4,7 +4,6 @@ namespace Doctrine\DBAL\Driver; ...@@ -4,7 +4,6 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use PDO; use PDO;
use function count;
use function func_get_args; use function func_get_args;
/** /**
...@@ -25,7 +24,6 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection ...@@ -25,7 +24,6 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
{ {
try { try {
parent::__construct($dsn, $user, $password, $options); parent::__construct($dsn, $user, $password, $options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
...@@ -58,7 +56,9 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection ...@@ -58,7 +56,9 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
public function prepare($prepareString, $driverOptions = []) public function prepare($prepareString, $driverOptions = [])
{ {
try { try {
return parent::prepare($prepareString, $driverOptions); return $this->createStatement(
parent::prepare($prepareString, $driverOptions)
);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -69,23 +69,12 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection ...@@ -69,23 +69,12 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
*/ */
public function query() public function query()
{ {
$args = func_get_args(); $args = func_get_args();
$argsCount = count($args);
try { try {
if ($argsCount === 4) { return $this->createStatement(
return parent::query($args[0], $args[1], $args[2], $args[3]); parent::query(...$args)
} );
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]);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -114,4 +103,12 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection ...@@ -114,4 +103,12 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
{ {
return false; return false;
} }
/**
* Creates a wrapped statement
*/
private function createStatement(\PDOStatement $stmt) : PDOStatement
{
return new PDOStatement($stmt);
}
} }
...@@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Driver; ...@@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use PDO; use PDO;
use const E_USER_DEPRECATED; use const E_USER_DEPRECATED;
use function sprintf; use function sprintf;
...@@ -13,7 +14,7 @@ use function trigger_error; ...@@ -13,7 +14,7 @@ use function trigger_error;
* The PDO implementation of the Statement interface. * The PDO implementation of the Statement interface.
* Used by all PDO-based drivers. * Used by all PDO-based drivers.
*/ */
class PDOStatement extends \PDOStatement implements Statement class PDOStatement implements IteratorAggregate, Statement
{ {
private const PARAM_TYPE_MAP = [ private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL, ParameterType::NULL => PDO::PARAM_NULL,
...@@ -33,34 +34,23 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -33,34 +34,23 @@ class PDOStatement extends \PDOStatement implements Statement
FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS, FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS,
]; ];
/** /** @var \PDOStatement */
* Protected constructor. private $stmt;
*/
protected function __construct() public function __construct(\PDOStatement $stmt)
{ {
$this->stmt = $stmt;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, ...$args)
{ {
$fetchMode = $this->convertFetchMode($fetchMode); $fetchMode = $this->convertFetchMode($fetchMode);
// This thin wrapper is necessary to shield against the weird signature
// of PDOStatement::setFetchMode(): even if the second and third
// parameters are optional, PHP will not let us remove it from this
// declaration.
try { try {
if ($arg2 === null && $arg3 === null) { return $this->stmt->setFetchMode($fetchMode, ...$args);
return parent::setFetchMode($fetchMode);
}
if ($arg3 === null) {
return parent::setFetchMode($fetchMode, $arg2);
}
return parent::setFetchMode($fetchMode, $arg2, $arg3);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -74,7 +64,7 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -74,7 +64,7 @@ class PDOStatement extends \PDOStatement implements Statement
$type = $this->convertParamType($type); $type = $this->convertParamType($type);
try { try {
return parent::bindValue($param, $value, $type); return $this->stmt->bindValue($param, $value, $type);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -88,7 +78,7 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -88,7 +78,7 @@ class PDOStatement extends \PDOStatement implements Statement
$type = $this->convertParamType($type); $type = $this->convertParamType($type);
try { try {
return parent::bindParam($column, $variable, $type, $length, $driverOptions); return $this->stmt->bindParam($column, $variable, $type, $length, $driverOptions);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -100,7 +90,7 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -100,7 +90,7 @@ class PDOStatement extends \PDOStatement implements Statement
public function closeCursor() public function closeCursor()
{ {
try { try {
return parent::closeCursor(); return $this->stmt->closeCursor();
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
// Exceptions not allowed by the interface. // Exceptions not allowed by the interface.
// In case driver implementations do not adhere to the interface, silence exceptions here. // In case driver implementations do not adhere to the interface, silence exceptions here.
...@@ -108,13 +98,37 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -108,13 +98,37 @@ class PDOStatement extends \PDOStatement implements Statement
} }
} }
/**
* {@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} * {@inheritdoc}
*/ */
public function execute($params = null) public function execute($params = null)
{ {
try { try {
return parent::execute($params); return $this->stmt->execute($params);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -123,24 +137,24 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -123,24 +137,24 @@ class PDOStatement extends \PDOStatement implements Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) public function rowCount()
{
return $this->stmt->rowCount();
}
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, ...$args)
{ {
$fetchMode = $this->convertFetchMode($fetchMode); $fetchMode = $this->convertFetchMode($fetchMode);
try { try {
if ($fetchMode === null && $cursorOrientation === PDO::FETCH_ORI_NEXT && $cursorOffset === 0) { if ($fetchMode === null) {
return parent::fetch(); return $this->stmt->fetch();
}
if ($cursorOrientation === PDO::FETCH_ORI_NEXT && $cursorOffset === 0) {
return parent::fetch($fetchMode);
}
if ($cursorOffset === 0) {
return parent::fetch($fetchMode, $cursorOrientation);
} }
return parent::fetch($fetchMode, $cursorOrientation, $cursorOffset); return $this->stmt->fetch($fetchMode, ...$args);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -149,24 +163,16 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -149,24 +163,16 @@ class PDOStatement extends \PDOStatement implements Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$fetchMode = $this->convertFetchMode($fetchMode); $fetchMode = $this->convertFetchMode($fetchMode);
try { try {
if ($fetchMode === null && $fetchArgument === null && $ctorArgs === null) { if ($fetchMode === null) {
return parent::fetchAll(); return $this->stmt->fetchAll();
}
if ($fetchArgument === null && $ctorArgs === null) {
return parent::fetchAll($fetchMode);
} }
if ($ctorArgs === null) { return $this->stmt->fetchAll($fetchMode, ...$args);
return parent::fetchAll($fetchMode, $fetchArgument);
}
return parent::fetchAll($fetchMode, $fetchArgument, $ctorArgs);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -178,7 +184,7 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -178,7 +184,7 @@ class PDOStatement extends \PDOStatement implements Statement
public function fetchColumn($columnIndex = 0) public function fetchColumn($columnIndex = 0)
{ {
try { try {
return parent::fetchColumn($columnIndex); return $this->stmt->fetchColumn($columnIndex);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -228,4 +234,12 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -228,4 +234,12 @@ class PDOStatement extends \PDOStatement implements Statement
return self::FETCH_MODE_MAP[$fetchMode]; return self::FETCH_MODE_MAP[$fetchMode];
} }
/**
* {@inheritdoc}
*/
public function getIterator()
{
yield from $this->stmt;
}
} }
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace Doctrine\DBAL\Driver; namespace Doctrine\DBAL\Driver;
use PDO;
use Traversable; use Traversable;
/** /**
...@@ -29,62 +28,43 @@ interface ResultStatement extends Traversable ...@@ -29,62 +28,43 @@ interface ResultStatement extends Traversable
/** /**
* Sets the fetch mode to use while iterating this statement. * Sets the fetch mode to use while iterating this statement.
* *
* @param int $fetchMode The fetch mode must be one of the {@link \Doctrine\DBAL\FetchMode} constants. * @param int $fetchMode Controls how the next row will be returned to the caller.
* @param mixed $arg2 * The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants.
* @param mixed $arg3 * @param array $args Optional mode-specific arguments (see {@link self::fetchAll()}).
* *
* @return bool * @return bool
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null); public function setFetchMode($fetchMode, ...$args);
/** /**
* Returns the next row of a result set. * Returns the next row of a result set.
* *
* @param int|null $fetchMode Controls how the next row will be returned to the caller. * @param int|null $fetchMode Controls how the next row will be returned to the caller.
* The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants, * The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants,
* defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}. * defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}.
* @param int $cursorOrientation For a ResultStatement object representing a scrollable cursor, * @param array $args Optional mode-specific arguments (see {@link self::fetchAll()}).
* this value determines which row will be returned to the caller.
* This value must be one of the \PDO::FETCH_ORI_* constants,
* defaulting to \PDO::FETCH_ORI_NEXT. To request a scrollable
* cursor for your ResultStatement object, you must set the \PDO::ATTR_CURSOR
* attribute to \PDO::CURSOR_SCROLL when you prepare the SQL statement with
* \PDO::prepare().
* @param int $cursorOffset For a ResultStatement object representing a scrollable cursor for which the
* cursorOrientation parameter is set to \PDO::FETCH_ORI_ABS, this value
* specifies the absolute number of the row in the result set that shall be
* fetched.
* For a ResultStatement object representing a scrollable cursor for which the
* cursorOrientation parameter is set to \PDO::FETCH_ORI_REL, this value
* specifies the row to fetch relative to the cursor position before
* ResultStatement::fetch() was called.
* *
* @return mixed The return value of this method on success depends on the fetch mode. In all cases, FALSE is * @return mixed The return value of this method on success depends on the fetch mode. In all cases, FALSE is
* returned on failure. * returned on failure.
*/ */
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0); public function fetch($fetchMode = null, ...$args);
/** /**
* Returns an array containing all of the result set rows. * Returns an array containing all of the result set rows.
* *
* @param int|null $fetchMode Controls how the next row will be returned to the caller. * @param int|null $fetchMode Controls how the next row will be returned to the caller.
* The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants, * The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants,
* defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}. * defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}.
* @param int|null $fetchArgument This argument has a different meaning depending on the value of the $fetchMode parameter: * @param array $args Optional mode-specific arguments. Supported modes:
* * {@link \Doctrine\DBAL\FetchMode::COLUMN}: * * {@link \Doctrine\DBAL\FetchMode::COLUMN}
* Returns the indicated 0-indexed column. * 1. The 0-indexed column to be returned.
* * {@link \Doctrine\DBAL\FetchMode::CUSTOM_OBJECT}: * * {@link \Doctrine\DBAL\FetchMode::CUSTOM_OBJECT}
* Returns instances of the specified class, mapping the columns of each row * 1. The classname of the object to be created,
* to named properties in the class. * 2. Array of constructor arguments
* * \PDO::FETCH_FUNC: Returns the results of calling the specified function, using each row's
* columns as parameters in the call.
* @param mixed[]|null $ctorArgs Controls how the next row will be returned to the caller.
* The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants,
* defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}.
* *
* @return mixed[] * @return mixed[]
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null); public function fetchAll($fetchMode = null, ...$args);
/** /**
* Returns a single column from the next row of a result set or FALSE if there are no more rows. * Returns a single column from the next row of a result set or FALSE if there are no more rows.
......
...@@ -7,14 +7,13 @@ use Doctrine\DBAL\Driver\StatementIterator; ...@@ -7,14 +7,13 @@ use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
use PDO;
use ReflectionClass; use ReflectionClass;
use ReflectionObject; use ReflectionObject;
use stdClass; use stdClass;
use const SASQL_BOTH; use const SASQL_BOTH;
use function array_key_exists; use function array_key_exists;
use function count;
use function func_get_args; use function func_get_args;
use function func_num_args;
use function gettype; use function gettype;
use function is_array; use function is_array;
use function is_numeric; use function is_numeric;
...@@ -192,7 +191,7 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -192,7 +191,7 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
* *
* @throws SQLAnywhereException * @throws SQLAnywhereException
*/ */
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) public function fetch($fetchMode = null, ...$args)
{ {
if (! is_resource($this->result)) { if (! is_resource($this->result)) {
return false; return false;
...@@ -214,10 +213,9 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -214,10 +213,9 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
$className = $this->defaultFetchClass; $className = $this->defaultFetchClass;
$ctorArgs = $this->defaultFetchClassCtorArgs; $ctorArgs = $this->defaultFetchClassCtorArgs;
if (func_num_args() >= 2) { if (count($args) > 0) {
$args = func_get_args(); $className = $args[0];
$className = $args[1]; $ctorArgs = $args[1] ?? [];
$ctorArgs = $args[2] ?? [];
} }
$result = sasql_fetch_object($this->result); $result = sasql_fetch_object($this->result);
...@@ -242,25 +240,25 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -242,25 +240,25 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$rows = []; $rows = [];
switch ($fetchMode) { switch ($fetchMode) {
case FetchMode::CUSTOM_OBJECT: case FetchMode::CUSTOM_OBJECT:
while ($row = $this->fetch(...func_get_args())) { while (($row = $this->fetch(...func_get_args())) !== false) {
$rows[] = $row; $rows[] = $row;
} }
break; break;
case FetchMode::COLUMN: case FetchMode::COLUMN:
while ($row = $this->fetchColumn()) { while (($row = $this->fetchColumn()) !== false) {
$rows[] = $row; $rows[] = $row;
} }
break; break;
default: default:
while ($row = $this->fetch($fetchMode)) { while (($row = $this->fetch($fetchMode)) !== false) {
$rows[] = $row; $rows[] = $row;
} }
} }
...@@ -301,11 +299,19 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -301,11 +299,19 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, ...$args)
{ {
$this->defaultFetchMode = $fetchMode; $this->defaultFetchMode = $fetchMode;
$this->defaultFetchClass = $arg2 ?: $this->defaultFetchClass;
$this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs; if (isset($args[0])) {
$this->defaultFetchClass = $args[0];
}
if (! isset($args[1])) {
return;
}
$this->defaultFetchClassCtorArgs = (array) $args[1];
} }
/** /**
......
...@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\StatementIterator; ...@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
use PDO;
use const SQLSRV_ENC_BINARY; use const SQLSRV_ENC_BINARY;
use const SQLSRV_ERR_ERRORS; use const SQLSRV_ERR_ERRORS;
use const SQLSRV_FETCH_ASSOC; use const SQLSRV_FETCH_ASSOC;
...@@ -16,7 +15,6 @@ use const SQLSRV_FETCH_NUMERIC; ...@@ -16,7 +15,6 @@ use const SQLSRV_FETCH_NUMERIC;
use const SQLSRV_PARAM_IN; use const SQLSRV_PARAM_IN;
use function array_key_exists; use function array_key_exists;
use function count; use function count;
use function func_get_args;
use function in_array; use function in_array;
use function is_numeric; use function is_numeric;
use function sqlsrv_errors; use function sqlsrv_errors;
...@@ -302,11 +300,17 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -302,11 +300,17 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, ...$args)
{ {
$this->defaultFetchMode = $fetchMode; $this->defaultFetchMode = $fetchMode;
$this->defaultFetchClass = $arg2 ?: $this->defaultFetchClass;
$this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs; if (isset($args[0])) {
$this->defaultFetchClass = $args[0];
}
if (isset($args[1])) {
$this->defaultFetchClassCtorArgs = (array) $args[2];
}
return true; return true;
} }
...@@ -324,7 +328,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -324,7 +328,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement
* *
* @throws SQLSrvException * @throws SQLSrvException
*/ */
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 // do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation // in order to prevent exceptional situation
...@@ -332,7 +336,6 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -332,7 +336,6 @@ class SQLSrvStatement implements IteratorAggregate, Statement
return false; return false;
} }
$args = func_get_args();
$fetchMode = $fetchMode ?: $this->defaultFetchMode; $fetchMode = $fetchMode ?: $this->defaultFetchMode;
if ($fetchMode === FetchMode::COLUMN) { if ($fetchMode === FetchMode::COLUMN) {
...@@ -347,9 +350,9 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -347,9 +350,9 @@ class SQLSrvStatement implements IteratorAggregate, Statement
$className = $this->defaultFetchClass; $className = $this->defaultFetchClass;
$ctorArgs = $this->defaultFetchClassCtorArgs; $ctorArgs = $this->defaultFetchClassCtorArgs;
if (count($args) >= 2) { if (count($args) > 0) {
$className = $args[1]; $className = $args[0];
$ctorArgs = $args[2] ?? []; $ctorArgs = $args[1] ?? [];
} }
return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs) ?: false; return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs) ?: false;
...@@ -361,13 +364,13 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -361,13 +364,13 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$rows = []; $rows = [];
switch ($fetchMode) { switch ($fetchMode) {
case FetchMode::CUSTOM_OBJECT: case FetchMode::CUSTOM_OBJECT:
while (($row = $this->fetch(...func_get_args())) !== false) { while (($row = $this->fetch($fetchMode, $args)) !== false) {
$rows[] = $row; $rows[] = $row;
} }
break; break;
......
...@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\StatementIterator; ...@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
use PDO;
use function array_change_key_case; use function array_change_key_case;
use function is_string; use function is_string;
use function rtrim; use function rtrim;
...@@ -100,11 +99,11 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -100,11 +99,11 @@ class Statement implements IteratorAggregate, DriverStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null) public function setFetchMode($fetchMode, ...$args)
{ {
$this->defaultFetchMode = $fetchMode; $this->defaultFetchMode = $fetchMode;
return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2); return $this->stmt->setFetchMode($fetchMode, ...$args);
} }
/** /**
...@@ -118,11 +117,11 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -118,11 +117,11 @@ class Statement implements IteratorAggregate, DriverStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) public function fetch($fetchMode = null, ...$args)
{ {
$fetchMode = $fetchMode ?: $this->defaultFetchMode; $fetchMode = $fetchMode ?: $this->defaultFetchMode;
$row = $this->stmt->fetch($fetchMode); $row = $this->stmt->fetch($fetchMode, ...$args);
$iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
$fixCase = $this->case !== null $fixCase = $this->case !== null
...@@ -137,15 +136,11 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -137,15 +136,11 @@ class Statement implements IteratorAggregate, DriverStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$fetchMode = $fetchMode ?: $this->defaultFetchMode; $fetchMode = $fetchMode ?: $this->defaultFetchMode;
if ($fetchArgument) { $rows = $this->stmt->fetchAll($fetchMode, ...$args);
$rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
} else {
$rows = $this->stmt->fetchAll($fetchMode);
}
$iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
$fixCase = $this->case !== null $fixCase = $this->case !== null
......
...@@ -6,7 +6,6 @@ use Doctrine\DBAL\Driver\Statement as DriverStatement; ...@@ -6,7 +6,6 @@ use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use IteratorAggregate; use IteratorAggregate;
use PDO;
use Throwable; use Throwable;
use function is_array; use function is_array;
use function is_string; use function is_string;
...@@ -213,15 +212,9 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -213,15 +212,9 @@ class Statement implements IteratorAggregate, DriverStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, ...$args)
{ {
if ($arg2 === null) { return $this->stmt->setFetchMode($fetchMode, ...$args);
return $this->stmt->setFetchMode($fetchMode);
} elseif ($arg3 === null) {
return $this->stmt->setFetchMode($fetchMode, $arg2);
}
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
} }
/** /**
...@@ -237,21 +230,17 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -237,21 +230,17 @@ class Statement implements IteratorAggregate, DriverStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) public function fetch($fetchMode = null, ...$args)
{ {
return $this->stmt->fetch($fetchMode); return $this->stmt->fetch($fetchMode, ...$args);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
if ($fetchArgument) { return $this->stmt->fetchAll($fetchMode, ...$args);
return $this->stmt->fetchAll($fetchMode, $fetchArgument);
}
return $this->stmt->fetchAll($fetchMode);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment