From 00f5c2220075009460ec73d899f38113a9c6b25b Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Tue, 2 Jan 2018 15:07:37 -0800 Subject: [PATCH] [BC] Replaced extension of \PDOStatement with a composition to avoid having to replicate the \PDOStatement interface in ResultStatement --- lib/Doctrine/DBAL/Cache/ArrayStatement.php | 10 +- .../DBAL/Cache/ResultCacheStatement.php | 8 +- .../DBAL/Driver/IBMDB2/DB2Statement.php | 27 +++-- .../DBAL/Driver/Mysqli/MysqliStatement.php | 6 +- .../DBAL/Driver/OCI8/OCI8Statement.php | 6 +- lib/Doctrine/DBAL/Driver/PDOConnection.php | 33 +++--- lib/Doctrine/DBAL/Driver/PDOStatement.php | 108 ++++++++++-------- lib/Doctrine/DBAL/Driver/ResultStatement.php | 57 +++------ .../SQLAnywhere/SQLAnywhereStatement.php | 19 ++- .../DBAL/Driver/SQLSrv/SQLSrvStatement.php | 27 +++-- lib/Doctrine/DBAL/Portability/Statement.php | 16 +-- lib/Doctrine/DBAL/Statement.php | 22 +--- 12 files changed, 166 insertions(+), 173 deletions(-) diff --git a/lib/Doctrine/DBAL/Cache/ArrayStatement.php b/lib/Doctrine/DBAL/Cache/ArrayStatement.php index e9247280e09..c7bc6e71d5a 100644 --- a/lib/Doctrine/DBAL/Cache/ArrayStatement.php +++ b/lib/Doctrine/DBAL/Cache/ArrayStatement.php @@ -61,9 +61,9 @@ public function columnCount() /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) + public function setFetchMode($fetchMode, ...$args) { - if ($arg2 !== null || $arg3 !== null) { + if (count($args) > 0) { throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()"); } @@ -85,7 +85,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) + public function fetch($fetchMode = null, ...$args) { if (! isset($this->data[$this->num])) { return false; @@ -116,10 +116,10 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $rows = []; - while ($row = $this->fetch($fetchMode)) { + while ($row = $this->fetch($fetchMode, ...$args)) { $rows[] = $row; } diff --git a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php index 3951c52e815..f1de248d460 100644 --- a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php +++ b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php @@ -113,7 +113,7 @@ public function columnCount() /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) + public function setFetchMode($fetchMode, ...$args) { $this->defaultFetchMode = $fetchMode; @@ -133,7 +133,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) + public function fetch($fetchMode = null, ...$args) { if ($this->data === null) { $this->data = []; @@ -173,10 +173,10 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $rows = []; - while ($row = $this->fetch($fetchMode)) { + while ($row = $this->fetch($fetchMode, ...$args)) { $rows[] = $row; } diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php index 242dc2381e6..54b71d622b9 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php @@ -195,11 +195,17 @@ public function execute($params = null) /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) + public function setFetchMode($fetchMode, ...$args) { - $this->_defaultFetchMode = $fetchMode; - $this->defaultFetchClass = $arg2 ? $arg2 : $this->defaultFetchClass; - $this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs; + $this->_defaultFetchMode = $fetchMode; + + if (isset($args[0])) { + $this->defaultFetchClass = $args[0]; + } + + if (isset($args[1])) { + $this->defaultFetchClassCtorArgs = (array) $args[2]; + } return true; } @@ -215,7 +221,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) + public function fetch($fetchMode = null, ...$args) { // do not try fetching from the statement if it's not expected to contain result // in order to prevent exceptional situation @@ -238,10 +244,9 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $className = $this->defaultFetchClass; $ctorArgs = $this->defaultFetchClassCtorArgs; - if (func_num_args() >= 2) { - $args = func_get_args(); - $className = $args[1]; - $ctorArgs = $args[2] ?? []; + if (count($args) > 0) { + $className = $args[0]; + $ctorArgs = $args[1] ?? []; } $result = db2_fetch_object($this->_stmt); @@ -266,13 +271,13 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $rows = []; switch ($fetchMode) { case FetchMode::CUSTOM_OBJECT: - while (($row = $this->fetch(...func_get_args())) !== false) { + while (($row = $this->fetch($fetchMode, ...$args)) !== false) { $rows[] = $row; } break; diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php index 2b9be599e5d..7924548e769 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php @@ -256,7 +256,7 @@ private function _fetch() /** * {@inheritdoc} */ - public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) + public function fetch($fetchMode = null, ...$args) { // do not try fetching from the statement if it's not expected to contain result // in order to prevent exceptional situation @@ -310,7 +310,7 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $fetchMode = $fetchMode ?: $this->_defaultFetchMode; @@ -393,7 +393,7 @@ public function columnCount() /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) + public function setFetchMode($fetchMode, ...$args) { $this->_defaultFetchMode = $fetchMode; diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php index ae0604805fd..6a3dd8dc451 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php @@ -392,7 +392,7 @@ public function execute($params = null) /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) + public function setFetchMode($fetchMode, ...$args) { $this->_defaultFetchMode = $fetchMode; @@ -410,7 +410,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) + public function fetch($fetchMode = null, ...$args) { // do not try fetching from the statement if it's not expected to contain result // in order to prevent exceptional situation @@ -441,7 +441,7 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $fetchMode = $fetchMode ?: $this->_defaultFetchMode; diff --git a/lib/Doctrine/DBAL/Driver/PDOConnection.php b/lib/Doctrine/DBAL/Driver/PDOConnection.php index 18c3efc7346..9eadea4619b 100644 --- a/lib/Doctrine/DBAL/Driver/PDOConnection.php +++ b/lib/Doctrine/DBAL/Driver/PDOConnection.php @@ -27,7 +27,6 @@ public function __construct($dsn, $user = null, $password = null, array $options { try { parent::__construct($dsn, $user, $password, $options); - $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['Doctrine\DBAL\Driver\PDOStatement', []]); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (\PDOException $exception) { throw new PDOException($exception); @@ -60,7 +59,9 @@ public function getServerVersion() public function prepare($prepareString, $driverOptions = []) { try { - return parent::prepare($prepareString, $driverOptions); + return $this->createStatement( + parent::prepare($prepareString, $driverOptions) + ); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -72,22 +73,11 @@ public function prepare($prepareString, $driverOptions = []) public function query() { $args = func_get_args(); - $argsCount = count($args); try { - if ($argsCount == 4) { - return parent::query($args[0], $args[1], $args[2], $args[3]); - } - - if ($argsCount == 3) { - return parent::query($args[0], $args[1], $args[2]); - } - - if ($argsCount == 2) { - return parent::query($args[0], $args[1]); - } - - return parent::query($args[0]); + return $this->createStatement( + parent::query(...$args) + ); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -116,4 +106,15 @@ public function requiresQueryForServerVersion() { return false; } + + /** + * Creates a wrapped statement + * + * @param \PDOStatement $stmt + * @return PDOStatement + */ + private function createStatement(\PDOStatement $stmt) : PDOStatement + { + return new PDOStatement($stmt); + } } diff --git a/lib/Doctrine/DBAL/Driver/PDOStatement.php b/lib/Doctrine/DBAL/Driver/PDOStatement.php index ab383c2fb51..67ae0e55377 100644 --- a/lib/Doctrine/DBAL/Driver/PDOStatement.php +++ b/lib/Doctrine/DBAL/Driver/PDOStatement.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\ParameterType; +use IteratorAggregate; use PDO; use const E_USER_DEPRECATED; use function sprintf; @@ -15,7 +16,7 @@ * * @since 2.0 */ -class PDOStatement extends \PDOStatement implements Statement +class PDOStatement implements IteratorAggregate, Statement { /** * @var int[] @@ -42,33 +43,24 @@ class PDOStatement extends \PDOStatement implements Statement ]; /** - * Protected constructor. + * @var \PDOStatement */ - protected function __construct() + 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); } @@ -82,7 +74,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING) $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); } @@ -96,7 +88,7 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l $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); } @@ -108,7 +100,7 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l 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. @@ -116,13 +108,37 @@ public function closeCursor() } } + /** + * {@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); } @@ -131,24 +147,24 @@ public function execute($params = null) /** * {@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 && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) { - return parent::fetch(); - } - - if (\PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) { - return parent::fetch($fetchMode); - } - - if (0 === $cursorOffset) { - 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); } @@ -157,24 +173,16 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $fetchMode = $this->convertFetchMode($fetchMode); try { - if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) { - return parent::fetchAll(); + if ($fetchMode === null) { + return $this->stmt->fetchAll(); } - if (null === $fetchArgument && null === $ctorArgs) { - return parent::fetchAll($fetchMode); - } - - if (null === $ctorArgs) { - return parent::fetchAll($fetchMode, $fetchArgument); - } - - return parent::fetchAll($fetchMode, $fetchArgument, $ctorArgs); + return $this->stmt->fetchAll($fetchMode, ...$args); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -186,7 +194,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n public function fetchColumn($columnIndex = 0) { try { - return parent::fetchColumn($columnIndex); + return $this->stmt->fetchColumn($columnIndex); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -236,4 +244,12 @@ private function convertFetchMode(?int $fetchMode) : ?int return self::FETCH_MODE_MAP[$fetchMode]; } + + /** + * {@inheritdoc} + */ + public function getIterator() + { + yield from $this->stmt; + } } diff --git a/lib/Doctrine/DBAL/Driver/ResultStatement.php b/lib/Doctrine/DBAL/Driver/ResultStatement.php index 1dea8bf5959..34f40600fbb 100644 --- a/lib/Doctrine/DBAL/Driver/ResultStatement.php +++ b/lib/Doctrine/DBAL/Driver/ResultStatement.php @@ -28,62 +28,43 @@ public function columnCount(); /** * 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 array|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 array */ - 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. diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php index bd493e83aa4..64b95c19858 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php @@ -207,7 +207,7 @@ public function execute($params = null) * * @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; @@ -229,10 +229,9 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $className = $this->defaultFetchClass; $ctorArgs = $this->defaultFetchClassCtorArgs; - if (func_num_args() >= 2) { - $args = func_get_args(); - $className = $args[1]; - $ctorArgs = $args[2] ?? []; + if (count($args) > 0) { + $className = $args[0]; + $ctorArgs = $args[1] ?? []; } $result = sasql_fetch_object($this->result); @@ -257,25 +256,25 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $rows = []; switch ($fetchMode) { case FetchMode::CUSTOM_OBJECT: - while ($row = call_user_func_array([$this, 'fetch'], func_get_args())) { + while (($row = $this->fetch($fetchMode, ...$args)) !== false) { $rows[] = $row; } break; case FetchMode::COLUMN: - while ($row = $this->fetchColumn()) { + while (($row = $this->fetchColumn()) !== false) { $rows[] = $row; } break; default: - while ($row = $this->fetch($fetchMode)) { + while (($row = $this->fetch($fetchMode)) !== false) { $rows[] = $row; } } @@ -316,7 +315,7 @@ public function rowCount() /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) + public function setFetchMode($fetchMode, ...$args) { $this->defaultFetchMode = $fetchMode; $this->defaultFetchClass = $arg2 ? $arg2 : $this->defaultFetchClass; diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php index 1330a01551f..04a44c2fb98 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php @@ -303,11 +303,17 @@ private function prepare() /** * {@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; } @@ -325,7 +331,7 @@ public function getIterator() * * @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 @@ -333,7 +339,6 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE return false; } - $args = func_get_args(); $fetchMode = $fetchMode ?: $this->defaultFetchMode; if ($fetchMode === FetchMode::COLUMN) { @@ -348,9 +353,9 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $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; @@ -362,13 +367,13 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $rows = []; switch ($fetchMode) { case FetchMode::CUSTOM_OBJECT: - while (($row = $this->fetch(...func_get_args())) !== false) { + while (($row = $this->fetch($fetchMode, $args)) !== false) { $rows[] = $row; } break; diff --git a/lib/Doctrine/DBAL/Portability/Statement.php b/lib/Doctrine/DBAL/Portability/Statement.php index 06af420d07e..8f5211f33cc 100644 --- a/lib/Doctrine/DBAL/Portability/Statement.php +++ b/lib/Doctrine/DBAL/Portability/Statement.php @@ -111,11 +111,11 @@ public function execute($params = null) /** * {@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); } /** @@ -129,11 +129,11 @@ public function getIterator() /** * {@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 = ! is_null($this->case) @@ -148,15 +148,11 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) + public function fetchAll($fetchMode = null, ...$args) { $fetchMode = $fetchMode ?: $this->defaultFetchMode; - 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 = ! is_null($this->case) diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index 6ceffb3f711..9cb64436488 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -214,15 +214,9 @@ public function errorInfo() /** * {@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); } /** @@ -238,21 +232,17 @@ public function getIterator() /** * {@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); } /**