From c9fc68be9eade8a1ff4c07c4867803e23473bf18 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sat, 1 Oct 2016 11:18:58 -0300 Subject: [PATCH 1/4] Normalize method signatures for `ResultStatement::fetch()` and `ResultStatement::fetchAll()` --- lib/Doctrine/DBAL/Cache/ArrayStatement.php | 4 +- .../DBAL/Cache/ResultCacheStatement.php | 4 +- .../DBAL/Driver/OCI8/OCI8Statement.php | 4 +- lib/Doctrine/DBAL/Driver/PDOStatement.php | 14 +++---- lib/Doctrine/DBAL/Driver/ResultStatement.php | 42 +++++++++++++++---- lib/Doctrine/DBAL/Portability/Statement.php | 8 ++-- lib/Doctrine/DBAL/Statement.php | 20 +++------ 7 files changed, 55 insertions(+), 41 deletions(-) diff --git a/lib/Doctrine/DBAL/Cache/ArrayStatement.php b/lib/Doctrine/DBAL/Cache/ArrayStatement.php index 0752dda31bd..7417e9af3bf 100644 --- a/lib/Doctrine/DBAL/Cache/ArrayStatement.php +++ b/lib/Doctrine/DBAL/Cache/ArrayStatement.php @@ -98,7 +98,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { if (isset($this->data[$this->num])) { $row = $this->data[$this->num++]; @@ -122,7 +122,7 @@ public function fetch($fetchMode = null) /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { $rows = array(); while ($row = $this->fetch($fetchMode)) { diff --git a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php index 0062255e5d2..7a5921d052a 100644 --- a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php +++ b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php @@ -147,7 +147,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { if ($this->data === null) { $this->data = array(); @@ -179,7 +179,7 @@ public function fetch($fetchMode = null) /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { $rows = array(); while ($row = $this->fetch($fetchMode)) { diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php index abf5595d88d..3b0365908cb 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php @@ -378,7 +378,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { // do not try fetching from the statement if it's not expected to contain result // in order to prevent exceptional situation @@ -405,7 +405,7 @@ public function fetch($fetchMode = null) /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { $fetchMode = $fetchMode ?: $this->_defaultFetchMode; diff --git a/lib/Doctrine/DBAL/Driver/PDOStatement.php b/lib/Doctrine/DBAL/Driver/PDOStatement.php index 07b6365db9d..deb35de0162 100644 --- a/lib/Doctrine/DBAL/Driver/PDOStatement.php +++ b/lib/Doctrine/DBAL/Driver/PDOStatement.php @@ -111,18 +111,18 @@ public function execute($params = null) /** * {@inheritdoc} */ - public function fetch($fetchMode = null, $cursorOrientation = null, $cursorOffset = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { try { - if ($fetchMode === null && $cursorOrientation === null && $cursorOffset === null) { + if ($fetchMode === null && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) { return parent::fetch(); } - if ($cursorOrientation === null && $cursorOffset === null) { + if (\PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) { return parent::fetch($fetchMode); } - if ($cursorOffset === null) { + if (0 === $cursorOffset) { return parent::fetch($fetchMode, $cursorOrientation); } @@ -138,15 +138,15 @@ public function fetch($fetchMode = null, $cursorOrientation = null, $cursorOffse public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { try { - if ($fetchMode === null && $fetchArgument === null && $ctorArgs === null) { + if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) { return parent::fetchAll(); } - if ($fetchArgument === null && $ctorArgs === null) { + if (null === $fetchArgument && null === $ctorArgs) { return parent::fetchAll($fetchMode); } - if ($ctorArgs === null) { + if (null === $ctorArgs) { return parent::fetchAll($fetchMode, $fetchArgument); } diff --git a/lib/Doctrine/DBAL/Driver/ResultStatement.php b/lib/Doctrine/DBAL/Driver/ResultStatement.php index ad76ddb1164..3578d41d36f 100644 --- a/lib/Doctrine/DBAL/Driver/ResultStatement.php +++ b/lib/Doctrine/DBAL/Driver/ResultStatement.php @@ -58,29 +58,53 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null); /** * Returns the next row of a result set. * - * @param integer|null $fetchMode Controls how the next row will be returned to the caller. - * The value must be one of the PDO::FETCH_* constants, - * defaulting to PDO::FETCH_BOTH. + * * @param int|null $fetchMode Controls how the next row will be returned to the caller. + * The value must be one of the \PDO::FETCH_* constants, + * defaulting to \PDO::FETCH_BOTH. + * @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. * * @return mixed The return value of this method on success depends on the fetch mode. In all cases, FALSE is * returned on failure. * * @see PDO::FETCH_* constants. */ - public function fetch($fetchMode = null); + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0); /** * Returns an array containing all of the result set rows. * - * @param integer|null $fetchMode Controls how the next row will be returned to the caller. - * The value must be one of the PDO::FETCH_* constants, - * defaulting to PDO::FETCH_BOTH. + * @param int|null $fetchMode Controls how the next row will be returned to the caller. + * The value must be one of the \PDO::FETCH_* constants, + * defaulting to \PDO::FETCH_BOTH. + * @param int|null $fetchArgument This argument has a different meaning depending on the value of the $fetchMode parameter: + * * \PDO::FETCH_COLUMN: Returns the indicated 0-indexed column. + * * \PDO::FETCH_CLASS: 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 \PDO::FETCH_* constants, + * defaulting to \PDO::FETCH_BOTH. * * @return array * - * @see PDO::FETCH_* constants. + * @see \PDO::FETCH_* constants. */ - public function fetchAll($fetchMode = null); + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null); /** * 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/Portability/Statement.php b/lib/Doctrine/DBAL/Portability/Statement.php index 41470bb52a4..15741e37234 100644 --- a/lib/Doctrine/DBAL/Portability/Statement.php +++ b/lib/Doctrine/DBAL/Portability/Statement.php @@ -142,7 +142,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { $fetchMode = $fetchMode ?: $this->defaultFetchMode; @@ -159,12 +159,12 @@ public function fetch($fetchMode = null) /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $columnIndex = 0) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { $fetchMode = $fetchMode ?: $this->defaultFetchMode; - if ($columnIndex != 0) { - $rows = $this->stmt->fetchAll($fetchMode, $columnIndex); + if ($fetchArgument) { + $rows = $this->stmt->fetchAll($fetchMode, $fetchArgument); } else { $rows = $this->stmt->fetchAll($fetchMode); } diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index 77d0584ef94..88038dc6184 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -252,29 +252,19 @@ public function getIterator() } /** - * Fetches the next row from a result set. - * - * @param integer|null $fetchMode - * - * @return mixed The return value of this function on success depends on the fetch type. - * In all cases, FALSE is returned on failure. + * {@inheritdoc} */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { return $this->stmt->fetch($fetchMode); } /** - * Returns an array containing all of the result set rows. - * - * @param integer|null $fetchMode - * @param mixed $fetchArgument - * - * @return array An array containing all of the remaining rows in the result set. + * {@inheritdoc} */ - public function fetchAll($fetchMode = null, $fetchArgument = 0) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { - if ($fetchArgument !== 0) { + if ($fetchArgument) { return $this->stmt->fetchAll($fetchMode, $fetchArgument); } From 003f1e235668b6eda0eacbf5c984c43f88b71a51 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sat, 1 Oct 2016 11:36:21 -0300 Subject: [PATCH 2/4] Update `MysqliStatement` --- lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php index efd1cb2cce1..6e4520b73d6 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php @@ -263,7 +263,7 @@ private function _fetch() /** * {@inheritdoc} */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { // do not try fetching from the statement if it's not expected to contain result // in order to prevent exceptional situation @@ -313,7 +313,7 @@ public function fetch($fetchMode = null) /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { $fetchMode = $fetchMode ?: $this->_defaultFetchMode; From 5c80649f5477d4337113d517dcd256c7b5bcee80 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sat, 1 Oct 2016 11:42:44 -0300 Subject: [PATCH 3/4] Update statement for some drivers --- lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php | 12 ++++++------ lib/Doctrine/DBAL/Driver/ResultStatement.php | 2 +- .../Driver/SQLAnywhere/SQLAnywhereStatement.php | 4 ++-- .../DBAL/Driver/SQLSrv/SQLSrvStatement.php | 14 ++++++++------ 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php index 892db0ae520..c8025f60e2e 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php @@ -26,7 +26,7 @@ class DB2Statement implements \IteratorAggregate, Statement /** * @var resource */ - private $_stmt = null; + private $_stmt; /** * @var array @@ -147,8 +147,8 @@ public function errorCode() public function errorInfo() { return array( - 0 => db2_stmt_errormsg(), - 1 => db2_stmt_error(), + db2_stmt_errormsg(), + db2_stmt_error(), ); } @@ -207,7 +207,7 @@ public function getIterator() /** * {@inheritdoc} */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { // do not try fetching from the statement if it's not expected to contain result // in order to prevent exceptional situation @@ -243,14 +243,14 @@ public function fetch($fetchMode = null) case \PDO::FETCH_OBJ: return db2_fetch_object($this->_stmt); default: - throw new DB2Exception("Given Fetch-Style " . $fetchMode . " is not supported."); + throw new DB2Exception('Given Fetch-Style ' . $fetchMode . ' is not supported.'); } } /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { $rows = array(); diff --git a/lib/Doctrine/DBAL/Driver/ResultStatement.php b/lib/Doctrine/DBAL/Driver/ResultStatement.php index 3578d41d36f..b73c2cdae3c 100644 --- a/lib/Doctrine/DBAL/Driver/ResultStatement.php +++ b/lib/Doctrine/DBAL/Driver/ResultStatement.php @@ -58,7 +58,7 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null); /** * 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 \PDO::FETCH_* constants, * defaulting to \PDO::FETCH_BOTH. * @param int $cursorOrientation For a ResultStatement object representing a scrollable cursor, diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php index 160cc1021d2..392ed85cd8b 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php @@ -193,7 +193,7 @@ public function execute($params = null) * * @throws SQLAnywhereException */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { if ( ! is_resource($this->result)) { return false; @@ -235,7 +235,7 @@ public function fetch($fetchMode = null) /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { $rows = array(); diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php index eee341796d8..cc26331c783 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php @@ -257,7 +257,7 @@ private function prepare() $params = array(); foreach ($this->variables as $column => &$variable) { - if ($this->types[$column] === \PDO::PARAM_LOB) { + if (PDO::PARAM_LOB === $this->types[$column]) { $params[$column - 1] = array( &$variable, SQLSRV_PARAM_IN, @@ -302,8 +302,10 @@ public function getIterator() /** * {@inheritdoc} + * + * @throws SQLSrvException */ - public function fetch($fetchMode = null) + public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { // do not try fetching from the statement if it's not expected to contain result // in order to prevent exceptional situation @@ -318,25 +320,25 @@ public function fetch($fetchMode = null) return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]) ?: false; } - if ($fetchMode == PDO::FETCH_OBJ || $fetchMode == PDO::FETCH_CLASS) { + if (in_array($fetchMode, array(PDO::FETCH_OBJ, PDO::FETCH_CLASS), true)) { $className = $this->defaultFetchClass; $ctorArgs = $this->defaultFetchClassCtorArgs; if (count($args) >= 2) { $className = $args[1]; - $ctorArgs = (isset($args[2])) ? $args[2] : array(); + $ctorArgs = isset($args[2]) ? $args[2] : array(); } return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs) ?: false; } - throw new SQLSrvException("Fetch mode is not supported!"); + throw new SQLSrvException('Fetch mode is not supported!'); } /** * {@inheritdoc} */ - public function fetchAll($fetchMode = null) + public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { $rows = array(); From cd1450a8c0468f75491e360967de329be35c9130 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 6 Jun 2017 12:46:49 -0300 Subject: [PATCH 4/4] Add BC break declaration at upgrade path for 2.6 --- UPGRADE.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/UPGRADE.md b/UPGRADE.md index 39d0849011a..2847c786413 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,30 @@ # Upgrade to 2.6 +## MINOR BC BREAK: `fetch()` and `fetchAll()` method signatures in `Doctrine\DBAL\Driver\ResultStatement` + +1. ``Doctrine\DBAL\Driver\ResultStatement::fetch()`` now has 3 arguments instead of 1, respecting +``PDO::fetch()`` signature. + +Before: + + Doctrine\DBAL\Driver\ResultStatement::fetch($fetchMode); + +After: + + Doctrine\DBAL\Driver\ResultStatement::fetch($fetchMode, $cursorOrientation, $cursorOffset); + +2. ``Doctrine\DBAL\Driver\ResultStatement::fetchAll()`` now has 3 arguments instead of 1, respecting +``PDO::fetchAll()`` signature. + +Before: + + Doctrine\DBAL\Driver\ResultStatement::fetchAll($fetchMode); + +After: + + Doctrine\DBAL\Driver\ResultStatement::fetch($fetchMode, $fetchArgument, $ctorArgs); + + ## MINOR BC BREAK: URL-style DSN with percentage sign in password URL-style DSNs (e.g. ``mysql://foo@bar:localhost/db``) are now assumed to be percent-encoded