[DBAL-2958] Replaced extension of \PDOStatement with a composition to avoid...

[DBAL-2958] Replaced extension of \PDOStatement with a composition to avoid having to replicate the \PDOStatement interface in ResultStatement
parent 11106683
...@@ -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 assert; use function assert;
...@@ -105,7 +104,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement ...@@ -105,7 +104,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;
...@@ -125,7 +124,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement ...@@ -125,7 +124,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 = [];
...@@ -165,9 +164,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement ...@@ -165,9 +164,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs); $data = $this->statement->fetchAll($fetchMode, ...$args);
if ($fetchMode === FetchMode::COLUMN) { if ($fetchMode === FetchMode::COLUMN) {
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
......
...@@ -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;
...@@ -229,11 +227,17 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -229,11 +227,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[1];
}
return true; return true;
} }
...@@ -249,7 +253,7 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -249,7 +253,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
...@@ -272,10 +276,9 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -272,10 +276,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);
...@@ -300,13 +303,13 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -300,13 +303,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 function array_combine; use function array_combine;
use function array_fill; use function array_fill;
use function assert; use function assert;
...@@ -303,7 +302,7 @@ class MysqliStatement implements IteratorAggregate, Statement ...@@ -303,7 +302,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
...@@ -353,7 +352,7 @@ class MysqliStatement implements IteratorAggregate, Statement ...@@ -353,7 +352,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;
...@@ -436,7 +435,7 @@ class MysqliStatement implements IteratorAggregate, Statement ...@@ -436,7 +435,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;
...@@ -407,7 +406,7 @@ class OCI8Statement implements IteratorAggregate, Statement ...@@ -407,7 +406,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;
...@@ -425,7 +424,7 @@ class OCI8Statement implements IteratorAggregate, Statement ...@@ -425,7 +424,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
...@@ -456,7 +455,7 @@ class OCI8Statement implements IteratorAggregate, Statement ...@@ -456,7 +455,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;
......
...@@ -25,7 +25,6 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection ...@@ -25,7 +25,6 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
{ {
try { try {
parent::__construct($dsn, (string) $user, (string) $password, (array) $options); parent::__construct($dsn, (string) $user, (string) $password, (array) $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 +57,9 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection ...@@ -58,7 +57,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);
} }
...@@ -75,7 +76,7 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection ...@@ -75,7 +76,7 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
$stmt = parent::query(...$args); $stmt = parent::query(...$args);
assert($stmt instanceof \PDOStatement); assert($stmt instanceof \PDOStatement);
return $stmt; return $this->createStatement($stmt);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -112,4 +113,12 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection ...@@ -112,4 +113,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);
}
} }
...@@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Driver; ...@@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate;
use PDO; use PDO;
use function array_slice; use function array_slice;
use function assert; use function assert;
...@@ -15,7 +16,7 @@ use function is_array; ...@@ -15,7 +16,7 @@ use function is_array;
* 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,
...@@ -35,34 +36,23 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -35,34 +36,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);
} }
...@@ -76,7 +66,7 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -76,7 +66,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);
} }
...@@ -90,7 +80,7 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -90,7 +80,7 @@ class PDOStatement extends \PDOStatement implements Statement
$type = $this->convertParamType($type); $type = $this->convertParamType($type);
try { try {
return parent::bindParam($column, $variable, $type, ...array_slice(func_get_args(), 3)); return $this->stmt->bindParam($column, $variable, $type, ...array_slice(func_get_args(), 3));
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -102,7 +92,7 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -102,7 +92,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.
...@@ -110,31 +100,52 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -110,31 +100,52 @@ class PDOStatement extends \PDOStatement implements Statement
} }
} }
public function columnCount()
{
return $this->stmt->columnCount();
}
public function errorCode()
{
return $this->stmt->errorCode();
}
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);
} }
} }
public function rowCount()
{
return $this->stmt->rowCount();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) public function fetch($fetchMode = null, ...$args)
{ {
$args = func_get_args();
if (isset($args[0])) {
$args[0] = $this->convertFetchMode($args[0]);
}
try { try {
return parent::fetch(...$args); if ($fetchMode === null) {
return $this->stmt->fetch();
}
return $this->stmt->fetch(
$this->convertFetchMode($fetchMode),
...$args
);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
...@@ -143,32 +154,24 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -143,32 +154,24 @@ class PDOStatement extends \PDOStatement implements Statement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, ...$args)
{ {
$args = func_get_args();
if (isset($args[0])) {
$args[0] = $this->convertFetchMode($args[0]);
}
if ($fetchMode === null && $fetchArgument === null && $ctorArgs === null) {
$args = [];
} elseif ($fetchArgument === null && $ctorArgs === null) {
$args = [$fetchMode];
} elseif ($ctorArgs === null) {
$args = [$fetchMode, $fetchArgument];
} else {
$args = [$fetchMode, $fetchArgument, $ctorArgs];
}
try { try {
$data = parent::fetchAll(...$args); if ($fetchMode === null) {
assert(is_array($data)); $data = $this->stmt->fetchAll();
} else {
return $data; $data = $this->stmt->fetchAll(
$this->convertFetchMode($fetchMode),
...$args
);
}
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
throw new PDOException($exception); throw new PDOException($exception);
} }
assert(is_array($data));
return $data;
} }
/** /**
...@@ -177,7 +180,7 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -177,7 +180,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);
} }
...@@ -210,4 +213,12 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -210,4 +213,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 mixed ...$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 mixed ...$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 mixed ...$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 class name 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,12 @@ use Doctrine\DBAL\Driver\StatementIterator; ...@@ -7,14 +7,12 @@ 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 func_get_args; use function count;
use function func_num_args;
use function gettype; use function gettype;
use function is_array; use function is_array;
use function is_int; use function is_int;
...@@ -199,7 +197,7 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -199,7 +197,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;
...@@ -221,10 +219,9 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -221,10 +219,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);
...@@ -249,13 +246,13 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -249,13 +246,13 @@ 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())) !== false) { while (($row = $this->fetch($fetchMode, ...$args)) !== false) {
$rows[] = $row; $rows[] = $row;
} }
break; break;
...@@ -308,11 +305,19 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -308,11 +305,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])) {
$this->defaultFetchClassCtorArgs = (array) $args[1];
}
return true;
} }
/** /**
......
...@@ -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_int; use function is_int;
use function is_numeric; use function is_numeric;
...@@ -313,11 +311,17 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -313,11 +311,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[1];
}
return true; return true;
} }
...@@ -335,7 +339,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -335,7 +339,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
...@@ -343,7 +347,6 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -343,7 +347,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) {
...@@ -358,9 +361,9 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -358,9 +361,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;
...@@ -372,13 +375,13 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -372,13 +375,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;
......
...@@ -8,7 +8,6 @@ use Doctrine\DBAL\Driver\StatementIterator; ...@@ -8,7 +8,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 assert; use function assert;
use function is_string; use function is_string;
...@@ -112,11 +111,11 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -112,11 +111,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);
} }
/** /**
...@@ -130,11 +129,11 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -130,11 +129,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
...@@ -149,15 +148,11 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -149,15 +148,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,17 +212,9 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -213,17 +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);
}
if ($arg3 === null) {
return $this->stmt->setFetchMode($fetchMode, $arg2);
}
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
} }
/** /**
...@@ -239,21 +230,17 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -239,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