[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;
use Doctrine\DBAL\FetchMode;
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
use function array_merge;
use function array_values;
use function count;
......@@ -59,9 +58,9 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/**
* {@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()');
}
......@@ -83,7 +82,7 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/**
* {@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;
......@@ -114,10 +113,10 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
/**
* {@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;
}
......
......@@ -9,7 +9,6 @@ use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\FetchMode;
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
use function array_merge;
use function array_values;
use function reset;
......@@ -104,7 +103,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->defaultFetchMode = $fetchMode;
......@@ -124,7 +123,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/**
* {@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 = [];
......@@ -164,9 +163,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/**
* {@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;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use PDO;
use ReflectionClass;
use ReflectionObject;
use ReflectionProperty;
......@@ -19,6 +18,7 @@ use const DB2_LONG;
use const DB2_PARAM_FILE;
use const DB2_PARAM_IN;
use function array_change_key_case;
use function count;
use function db2_bind_param;
use function db2_execute;
use function db2_fetch_array;
......@@ -32,8 +32,6 @@ use function db2_stmt_error;
use function db2_stmt_errormsg;
use function error_get_last;
use function fclose;
use function func_get_args;
use function func_num_args;
use function fwrite;
use function gettype;
use function is_object;
......@@ -241,11 +239,17 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->defaultFetchMode = $fetchMode;
$this->defaultFetchClass = $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;
}
......@@ -261,7 +265,7 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@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
......@@ -284,10 +288,9 @@ class DB2Statement implements IteratorAggregate, Statement
$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);
......@@ -312,13 +315,13 @@ class DB2Statement implements IteratorAggregate, Statement
/**
* {@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 = $this->fetch(...func_get_args())) !== false) {
while (($row = $this->fetch($fetchMode, ...$args)) !== false) {
$rows[] = $row;
}
break;
......
......@@ -10,7 +10,6 @@ use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use mysqli;
use mysqli_stmt;
use PDO;
use stdClass;
use function array_combine;
use function array_fill;
......@@ -304,7 +303,7 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@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
......@@ -358,7 +357,7 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$fetchMode = $fetchMode ?: $this->_defaultFetchMode;
......@@ -441,7 +440,7 @@ class MysqliStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->_defaultFetchMode = $fetchMode;
......
......@@ -8,7 +8,6 @@ use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
use const OCI_ASSOC;
use const OCI_B_BIN;
use const OCI_B_BLOB;
......@@ -380,7 +379,7 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->_defaultFetchMode = $fetchMode;
......@@ -398,7 +397,7 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@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
......@@ -429,7 +428,7 @@ class OCI8Statement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$fetchMode = $fetchMode ?: $this->_defaultFetchMode;
......
......@@ -4,7 +4,6 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\ParameterType;
use PDO;
use function count;
use function func_get_args;
/**
......@@ -25,7 +24,6 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
{
try {
parent::__construct($dsn, $user, $password, $options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $exception) {
throw new PDOException($exception);
......@@ -58,7 +56,9 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
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);
}
......@@ -69,23 +69,12 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
*/
public function query()
{
$args = func_get_args();
$argsCount = count($args);
$args = func_get_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);
}
......@@ -114,4 +103,12 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
{
return false;
}
/**
* Creates a wrapped statement
*/
private function createStatement(\PDOStatement $stmt) : PDOStatement
{
return new PDOStatement($stmt);
}
}
......@@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use PDO;
use const E_USER_DEPRECATED;
use function sprintf;
......@@ -13,7 +14,7 @@ use function trigger_error;
* The PDO implementation of the Statement interface.
* Used by all PDO-based drivers.
*/
class PDOStatement extends \PDOStatement implements Statement
class PDOStatement implements IteratorAggregate, Statement
{
private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL,
......@@ -33,34 +34,23 @@ class PDOStatement extends \PDOStatement implements Statement
FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS,
];
/**
* Protected constructor.
*/
protected function __construct()
/** @var \PDOStatement */
private $stmt;
public function __construct(\PDOStatement $stmt)
{
$this->stmt = $stmt;
}
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$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 {
if ($arg2 === null && $arg3 === null) {
return parent::setFetchMode($fetchMode);
}
if ($arg3 === null) {
return parent::setFetchMode($fetchMode, $arg2);
}
return parent::setFetchMode($fetchMode, $arg2, $arg3);
return $this->stmt->setFetchMode($fetchMode, ...$args);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
......@@ -74,7 +64,7 @@ class PDOStatement extends \PDOStatement implements Statement
$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);
}
......@@ -88,7 +78,7 @@ class PDOStatement extends \PDOStatement implements Statement
$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);
}
......@@ -100,7 +90,7 @@ class PDOStatement extends \PDOStatement implements Statement
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.
......@@ -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}
*/
public function execute($params = null)
{
try {
return parent::execute($params);
return $this->stmt->execute($params);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
......@@ -123,24 +137,24 @@ class PDOStatement extends \PDOStatement implements Statement
/**
* {@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);
try {
if ($fetchMode === null && $cursorOrientation === PDO::FETCH_ORI_NEXT && $cursorOffset === 0) {
return parent::fetch();
}
if ($cursorOrientation === PDO::FETCH_ORI_NEXT && $cursorOffset === 0) {
return parent::fetch($fetchMode);
}
if ($cursorOffset === 0) {
return parent::fetch($fetchMode, $cursorOrientation);
if ($fetchMode === null) {
return $this->stmt->fetch();
}
return parent::fetch($fetchMode, $cursorOrientation, $cursorOffset);
return $this->stmt->fetch($fetchMode, ...$args);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
......@@ -149,24 +163,16 @@ class PDOStatement extends \PDOStatement implements Statement
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$fetchMode = $this->convertFetchMode($fetchMode);
try {
if ($fetchMode === null && $fetchArgument === null && $ctorArgs === null) {
return parent::fetchAll();
}
if ($fetchArgument === null && $ctorArgs === null) {
return parent::fetchAll($fetchMode);
if ($fetchMode === null) {
return $this->stmt->fetchAll();
}
if ($ctorArgs === null) {
return parent::fetchAll($fetchMode, $fetchArgument);
}
return parent::fetchAll($fetchMode, $fetchArgument, $ctorArgs);
return $this->stmt->fetchAll($fetchMode, ...$args);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
......@@ -178,7 +184,7 @@ class PDOStatement extends \PDOStatement implements Statement
public function fetchColumn($columnIndex = 0)
{
try {
return parent::fetchColumn($columnIndex);
return $this->stmt->fetchColumn($columnIndex);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
......@@ -228,4 +234,12 @@ class PDOStatement extends \PDOStatement implements Statement
return self::FETCH_MODE_MAP[$fetchMode];
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
yield from $this->stmt;
}
}
......@@ -2,7 +2,6 @@
namespace Doctrine\DBAL\Driver;
use PDO;
use Traversable;
/**
......@@ -29,62 +28,43 @@ interface ResultStatement extends Traversable
/**
* 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 mixed $arg2
* @param mixed $arg3
* @param int $fetchMode Controls how the next row will be returned to the caller.
* The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants.
* @param array $args Optional mode-specific arguments (see {@link self::fetchAll()}).
*
* @return bool
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null);
public function setFetchMode($fetchMode, ...$args);
/**
* Returns the next row of a result set.
*
* @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,
* defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}.
* @param int $cursorOrientation For a ResultStatement object representing a scrollable cursor,
* 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.
* @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,
* defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}.
* @param array $args Optional mode-specific arguments (see {@link self::fetchAll()}).
*
* @return mixed The return value of this method on success depends on the fetch mode. In all cases, FALSE is
* 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.
*
* @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,
* 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:
* * {@link \Doctrine\DBAL\FetchMode::COLUMN}:
* Returns the indicated 0-indexed column.
* * {@link \Doctrine\DBAL\FetchMode::CUSTOM_OBJECT}:
* Returns instances of the specified class, mapping the columns of each row
* to named properties in the class.
* * \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}.
* @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,
* defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}.
* @param array $args Optional mode-specific arguments. Supported modes:
* * {@link \Doctrine\DBAL\FetchMode::COLUMN}
* 1. The 0-indexed column to be returned.
* * {@link \Doctrine\DBAL\FetchMode::CUSTOM_OBJECT}
* 1. The classname of the object to be created,
* 2. Array of constructor arguments
*
* @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.
......
......@@ -7,14 +7,13 @@ use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use PDO;
use ReflectionClass;
use ReflectionObject;
use stdClass;
use const SASQL_BOTH;
use function array_key_exists;
use function count;
use function func_get_args;
use function func_num_args;
use function gettype;
use function is_array;
use function is_numeric;
......@@ -192,7 +191,7 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
*
* @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)) {
return false;
......@@ -214,10 +213,9 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
$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 = sasql_fetch_object($this->result);
......@@ -242,25 +240,25 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/**
* {@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 = $this->fetch(...func_get_args())) {
while (($row = $this->fetch(...func_get_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;
}
}
......@@ -301,11 +299,19 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->defaultFetchMode = $fetchMode;
$this->defaultFetchClass = $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])) {
return;
}
$this->defaultFetchClassCtorArgs = (array) $args[1];
}
/**
......
......@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use PDO;
use const SQLSRV_ENC_BINARY;
use const SQLSRV_ERR_ERRORS;
use const SQLSRV_FETCH_ASSOC;
......@@ -16,7 +15,6 @@ use const SQLSRV_FETCH_NUMERIC;
use const SQLSRV_PARAM_IN;
use function array_key_exists;
use function count;
use function func_get_args;
use function in_array;
use function is_numeric;
use function sqlsrv_errors;
......@@ -302,11 +300,17 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->defaultFetchMode = $fetchMode;
$this->defaultFetchClass = $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;
}
......@@ -324,7 +328,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement
*
* @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
// in order to prevent exceptional situation
......@@ -332,7 +336,6 @@ class SQLSrvStatement implements IteratorAggregate, Statement
return false;
}
$args = func_get_args();
$fetchMode = $fetchMode ?: $this->defaultFetchMode;
if ($fetchMode === FetchMode::COLUMN) {
......@@ -347,9 +350,9 @@ class SQLSrvStatement implements IteratorAggregate, Statement
$className = $this->defaultFetchClass;
$ctorArgs = $this->defaultFetchClassCtorArgs;
if (count($args) >= 2) {
$className = $args[1];
$ctorArgs = $args[2] ?? [];
if (count($args) > 0) {
$className = $args[0];
$ctorArgs = $args[1] ?? [];
}
return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs) ?: false;
......@@ -361,13 +364,13 @@ class SQLSrvStatement implements IteratorAggregate, Statement
/**
* {@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 = $this->fetch(...func_get_args())) !== false) {
while (($row = $this->fetch($fetchMode, $args)) !== false) {
$rows[] = $row;
}
break;
......
......@@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use IteratorAggregate;
use PDO;
use function array_change_key_case;
use function is_string;
use function rtrim;
......@@ -100,11 +99,11 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null)
public function setFetchMode($fetchMode, ...$args)
{
$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
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
public function fetch($fetchMode = null, ...$args)
{
$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);
$fixCase = $this->case !== null
......@@ -137,15 +136,11 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$fetchMode = $fetchMode ?: $this->defaultFetchMode;
if ($fetchArgument) {
$rows = $this->stmt->fetchAll($fetchMode, $fetchArgument);
} else {
$rows = $this->stmt->fetchAll($fetchMode);
}
$rows = $this->stmt->fetchAll($fetchMode, ...$args);
$iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM);
$fixCase = $this->case !== null
......
......@@ -6,7 +6,6 @@ use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use IteratorAggregate;
use PDO;
use Throwable;
use function is_array;
use function is_string;
......@@ -213,15 +212,9 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
if ($arg2 === null) {
return $this->stmt->setFetchMode($fetchMode);
} elseif ($arg3 === null) {
return $this->stmt->setFetchMode($fetchMode, $arg2);
}
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
return $this->stmt->setFetchMode($fetchMode, ...$args);
}
/**
......@@ -237,21 +230,17 @@ class Statement implements IteratorAggregate, DriverStatement
/**
* {@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}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
if ($fetchArgument) {
return $this->stmt->fetchAll($fetchMode, $fetchArgument);
}
return $this->stmt->fetchAll($fetchMode);
return $this->stmt->fetchAll($fetchMode, ...$args);
}
/**
......
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