diff --git a/UPGRADE.md b/UPGRADE.md index 596095e2e55..9c55f692ddf 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,9 @@ # Upgrade to 3.0 +## BC BREAK `Statement` and `Connection` methods return `void`. + +`Connection::connect()`, `Statement::bindParam()`, `::bindValue()`, `::execute()`, `ResultStatement::setFetchMode()` and `::closeCursor()` no longer return a boolean value. They will throw an exception in case of failure. + ## BC BREAK `Statement::rowCount()` is moved. `Statement::rowCount()` has been moved to the `ResultStatement` interface where it belongs by definition. diff --git a/lib/Doctrine/DBAL/Cache/ArrayStatement.php b/lib/Doctrine/DBAL/Cache/ArrayStatement.php index 18d660be1ca..ef9bf7bff25 100644 --- a/lib/Doctrine/DBAL/Cache/ArrayStatement.php +++ b/lib/Doctrine/DBAL/Cache/ArrayStatement.php @@ -44,7 +44,7 @@ public function __construct(array $data) /** * {@inheritdoc} */ - public function closeCursor() + public function closeCursor() : void { unset($this->data); } @@ -72,15 +72,13 @@ public function rowCount() : int /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { if (count($args) > 0) { throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()'); } $this->defaultFetchMode = $fetchMode; - - return true; } /** diff --git a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php index c0b28bb8740..527c730a2fb 100644 --- a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php +++ b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php @@ -75,11 +75,12 @@ public function __construct(ResultStatement $stmt, Cache $resultCache, $cacheKey /** * {@inheritdoc} */ - public function closeCursor() + public function closeCursor() : void { $this->statement->closeCursor(); + if (! $this->emptied || $this->data === null) { - return true; + return; } $data = $this->resultCache->fetch($this->cacheKey); @@ -90,8 +91,6 @@ public function closeCursor() $this->resultCache->save($this->cacheKey, $data, $this->lifetime); unset($this->data); - - return true; } /** @@ -105,11 +104,9 @@ public function columnCount() /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { $this->defaultFetchMode = $fetchMode; - - return true; } /** diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 5cf35373a4d..9d43535f1c1 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -338,13 +338,12 @@ public function getExpressionBuilder() /** * Establishes the connection with the database. * - * @return bool TRUE if the connection was successfully established, FALSE if - * the connection is already open. + * @throws DriverException */ - public function connect() + public function connect() : void { if ($this->isConnected) { - return false; + return; } $driverOptions = $this->params['driverOptions'] ?? []; @@ -358,12 +357,12 @@ public function connect() $this->beginTransaction(); } - if ($this->_eventManager->hasListeners(Events::postConnect)) { - $eventArgs = new Event\ConnectionEventArgs($this); - $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); + if (! $this->_eventManager->hasListeners(Events::postConnect)) { + return; } - return true; + $eventArgs = new Event\ConnectionEventArgs($this); + $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); } /** @@ -517,10 +516,8 @@ public function setAutoCommit(bool $autoCommit) : void * Sets the fetch mode. * * @param int $fetchMode - * - * @return void */ - public function setFetchMode($fetchMode) + public function setFetchMode($fetchMode) : void { $this->defaultFetchMode = $fetchMode; } diff --git a/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php b/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php index 66a895d81f3..6ef3d034438 100644 --- a/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php +++ b/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php @@ -123,7 +123,7 @@ public function isConnectedToMaster() /** * {@inheritDoc} */ - public function connect($connectionName = null) + public function connect($connectionName = null) : void { $requestedConnectionChange = ($connectionName !== null); $connectionName = $connectionName ?: 'slave'; @@ -136,7 +136,7 @@ public function connect($connectionName = null) // change request, then abort right here, because we are already done. // This prevents writes to the slave in case of "keepSlave" option enabled. if ($this->_conn !== null && ! $requestedConnectionChange) { - return false; + return; } $forceMasterAsSlave = false; @@ -153,7 +153,7 @@ public function connect($connectionName = null) $this->connections['slave'] = $this->_conn; } - return false; + return; } if ($connectionName === 'master') { @@ -167,12 +167,12 @@ public function connect($connectionName = null) $this->connections['slave'] = $this->_conn = $this->connectTo($connectionName); } - if ($this->_eventManager->hasListeners(Events::postConnect)) { - $eventArgs = new ConnectionEventArgs($this); - $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); + if (! $this->_eventManager->hasListeners(Events::postConnect)) { + return; } - return true; + $eventArgs = new ConnectionEventArgs($this); + $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); } /** diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php index d3d0a454dad..d829c7c79ee 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php @@ -89,15 +89,15 @@ public function __construct($stmt) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = ParameterType::STRING) + public function bindValue($param, $value, $type = ParameterType::STRING) : void { - return $this->bindParam($param, $value, $type); + $this->bindParam($param, $value, $type); } /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void { switch ($type) { case ParameterType::INTEGER: @@ -122,8 +122,6 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l $this->bind($column, $variable, DB2_PARAM_IN, DB2_CHAR); break; } - - return true; } /** @@ -144,17 +142,17 @@ private function bind($position, &$variable, int $parameterType, int $dataType) /** * {@inheritdoc} */ - public function closeCursor() + public function closeCursor() : void { $this->bindParam = []; - if (! db2_free_result($this->stmt)) { - return false; + if (! $this->result) { + return; } - $this->result = false; + db2_free_result($this->stmt); - return true; + $this->result = false; } /** @@ -187,7 +185,7 @@ public function errorInfo() /** * {@inheritdoc} */ - public function execute($params = null) + public function execute($params = null) : void { if ($params === null) { ksort($this->bindParam); @@ -222,14 +220,12 @@ public function execute($params = null) } $this->result = true; - - return $retval; } /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { $this->defaultFetchMode = $fetchMode; @@ -237,11 +233,11 @@ public function setFetchMode($fetchMode, ...$args) $this->defaultFetchClass = $args[0]; } - if (isset($args[1])) { - $this->defaultFetchClassCtorArgs = (array) $args[2]; + if (! isset($args[1])) { + return; } - return true; + $this->defaultFetchClassCtorArgs = (array) $args[2]; } /** diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php index 9be2c3f167c..2faed0afb3c 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php @@ -3,6 +3,7 @@ namespace Doctrine\DBAL\Driver\Mysqli; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Driver\DriverException; use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\StatementIterator; use Doctrine\DBAL\Exception\InvalidArgumentException; @@ -101,7 +102,7 @@ public function __construct(mysqli $conn, $prepareString) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void { assert(is_int($column)); @@ -111,14 +112,12 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l $this->_bindedValues[$column] =& $variable; $this->types[$column - 1] = self::$_paramTypeMap[$type]; - - return true; } /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = ParameterType::STRING) + public function bindValue($param, $value, $type = ParameterType::STRING) : void { assert(is_int($param)); @@ -129,14 +128,12 @@ public function bindValue($param, $value, $type = ParameterType::STRING) $this->_values[$param] = $value; $this->_bindedValues[$param] =& $this->_values[$param]; $this->types[$param - 1] = self::$_paramTypeMap[$type]; - - return true; } /** * {@inheritdoc} */ - public function execute($params = null) + public function execute($params = null) : void { if ($params !== null && count($params) > 0) { if (! $this->bindUntypedValues($params)) { @@ -199,12 +196,12 @@ public function execute($params = null) } $this->result = true; - - return true; } /** * Binds parameters with known types previously bound to the statement + * + * @throws DriverException */ private function bindTypedParameters() { @@ -408,12 +405,10 @@ public function errorInfo() /** * {@inheritdoc} */ - public function closeCursor() + public function closeCursor() : void { $this->_stmt->free_result(); $this->result = false; - - return true; } /** @@ -439,11 +434,9 @@ public function columnCount() /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { $this->_defaultFetchMode = $fetchMode; - - return true; } /** diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php index 4db8b1e11e8..3fe3b61d136 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php @@ -268,15 +268,15 @@ private static function findToken($statement, &$offset, $regex) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = ParameterType::STRING) + public function bindValue($param, $value, $type = ParameterType::STRING) : void { - return $this->bindParam($param, $value, $type, null); + $this->bindParam($param, $value, $type, null); } /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void { $column = $this->_paramMap[$column]; @@ -293,13 +293,15 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l $this->boundValues[$column] =& $variable; - return oci_bind_by_name( + if (! oci_bind_by_name( $this->_sth, $column, $variable, $length ?? -1, $this->convertParameterType($type) - ); + )) { + throw OCI8Exception::fromErrorInfo($this->errorInfo()); + } } /** @@ -322,18 +324,16 @@ private function convertParameterType(int $type) : int /** * {@inheritdoc} */ - public function closeCursor() + public function closeCursor() : void { // not having the result means there's nothing to close if (! $this->result) { - return true; + return; } oci_cancel($this->_sth); $this->result = false; - - return true; } /** @@ -374,7 +374,7 @@ public function errorInfo() /** * {@inheritdoc} */ - public function execute($params = null) + public function execute($params = null) : void { if ($params) { $hasZeroIndex = array_key_exists(0, $params); @@ -386,9 +386,7 @@ public function execute($params = null) $param = $key; } - if (! $this->bindValue($param, $val)) { - throw OCI8Exception::fromErrorInfo($this->errorInfo()); - } + $this->bindValue($param, $val); } } @@ -398,18 +396,14 @@ public function execute($params = null) } $this->result = true; - - return $ret; } /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { $this->_defaultFetchMode = $fetchMode; - - return true; } /** diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php index 6803bb14ed7..2844a682697 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php @@ -14,7 +14,7 @@ class Statement extends PDOStatement /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) : void { if (($type === ParameterType::LARGE_OBJECT || $type === ParameterType::BINARY) && $driverOptions === null @@ -22,14 +22,14 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l $driverOptions = PDO::SQLSRV_ENCODING_BINARY; } - return parent::bindParam($column, $variable, $type, $length, $driverOptions); + parent::bindParam($column, $variable, $type, $length, $driverOptions); } /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = ParameterType::STRING) + public function bindValue($param, $value, $type = ParameterType::STRING) : void { - return $this->bindParam($param, $value, $type); + $this->bindParam($param, $value, $type); } } diff --git a/lib/Doctrine/DBAL/Driver/PDOStatement.php b/lib/Doctrine/DBAL/Driver/PDOStatement.php index e8f0a33e890..c89507d0e79 100644 --- a/lib/Doctrine/DBAL/Driver/PDOStatement.php +++ b/lib/Doctrine/DBAL/Driver/PDOStatement.php @@ -50,12 +50,12 @@ public function __construct(\PDOStatement $stmt) /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { $fetchMode = $this->convertFetchMode($fetchMode); try { - return $this->stmt->setFetchMode($fetchMode, ...$args); + $this->stmt->setFetchMode($fetchMode, ...$args); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -64,12 +64,12 @@ public function setFetchMode($fetchMode, ...$args) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = ParameterType::STRING) + public function bindValue($param, $value, $type = ParameterType::STRING) : void { $type = $this->convertParamType($type); try { - return $this->stmt->bindValue($param, $value, $type); + $this->stmt->bindValue($param, $value, $type); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -78,12 +78,12 @@ public function bindValue($param, $value, $type = ParameterType::STRING) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) : void { $type = $this->convertParamType($type); try { - return $this->stmt->bindParam($column, $variable, $type, ...array_slice(func_get_args(), 3)); + $this->stmt->bindParam($column, $variable, $type, ...array_slice(func_get_args(), 3)); } catch (\PDOException $exception) { throw new PDOException($exception); } @@ -92,15 +92,9 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l /** * {@inheritdoc} */ - public function closeCursor() + public function closeCursor() : void { - try { - 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. - return true; - } + $this->stmt->closeCursor(); } /** @@ -130,10 +124,10 @@ public function errorInfo() /** * {@inheritdoc} */ - public function execute($params = null) + public function execute($params = null) : void { try { - return $this->stmt->execute($params); + $this->stmt->execute($params); } catch (\PDOException $exception) { throw new PDOException($exception); } diff --git a/lib/Doctrine/DBAL/Driver/ResultStatement.php b/lib/Doctrine/DBAL/Driver/ResultStatement.php index 321778e1c60..02422c50177 100644 --- a/lib/Doctrine/DBAL/Driver/ResultStatement.php +++ b/lib/Doctrine/DBAL/Driver/ResultStatement.php @@ -11,10 +11,8 @@ interface ResultStatement extends Traversable { /** * Closes the cursor, enabling the statement to be executed again. - * - * @return bool TRUE on success or FALSE on failure. */ - public function closeCursor(); + public function closeCursor() : void; /** * Returns the number of columns in the result set @@ -42,10 +40,8 @@ public function rowCount() : int; * @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 mixed ...$args Optional mode-specific arguments (see {@link self::fetchAll()}). - * - * @return bool */ - public function setFetchMode($fetchMode, ...$args); + public function setFetchMode($fetchMode, ...$args) : void; /** * Returns the next row of a result set. diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php index 03139989be0..8b02b9456bb 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php @@ -89,7 +89,7 @@ public function __construct($conn, $sql) * * @throws SQLAnywhereException */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void { switch ($type) { case ParameterType::INTEGER: @@ -116,30 +116,22 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l if (! sasql_stmt_bind_param_ex($this->stmt, $column - 1, $variable, $type, $variable === null)) { throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); } - - return true; } /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = ParameterType::STRING) + public function bindValue($param, $value, $type = ParameterType::STRING) : void { - return $this->bindParam($param, $value, $type); + $this->bindParam($param, $value, $type); } /** * {@inheritdoc} - * - * @throws SQLAnywhereException */ - public function closeCursor() + public function closeCursor() : void { - if (! sasql_stmt_reset($this->stmt)) { - throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); - } - - return true; + sasql_stmt_reset($this->stmt); } /** @@ -171,7 +163,7 @@ public function errorInfo() * * @throws SQLAnywhereException */ - public function execute($params = null) + public function execute($params = null) : void { if (is_array($params)) { $hasZeroIndex = array_key_exists(0, $params); @@ -190,8 +182,6 @@ public function execute($params = null) } $this->result = sasql_stmt_result_metadata($this->stmt); - - return true; } /** @@ -309,7 +299,7 @@ public function rowCount() : int /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { $this->defaultFetchMode = $fetchMode; @@ -317,11 +307,11 @@ public function setFetchMode($fetchMode, ...$args) $this->defaultFetchClass = $args[0]; } - if (isset($args[1])) { - $this->defaultFetchClassCtorArgs = (array) $args[1]; + if (! isset($args[1])) { + return; } - return true; + $this->defaultFetchClassCtorArgs = (array) $args[1]; } /** diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php index 86f50d25b3d..f16e9b95ed0 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php @@ -147,7 +147,7 @@ public function __construct($conn, $sql, ?LastInsertId $lastInsertId = null) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = ParameterType::STRING) + public function bindValue($param, $value, $type = ParameterType::STRING) : void { if (! is_numeric($param)) { throw new SQLSrvException( @@ -162,7 +162,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void { if (! is_numeric($column)) { throw new SQLSrvException('sqlsrv does not support named parameters to queries, use question mark (?) placeholders instead.'); @@ -178,11 +178,11 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l /** * {@inheritdoc} */ - public function closeCursor() + public function closeCursor() : void { // not having the result means there's nothing to close if ($this->stmt === null || ! $this->result) { - return true; + return; } // emulate it by fetching and discarding rows, similarly to what PDO does in this case @@ -193,8 +193,6 @@ public function closeCursor() } $this->result = false; - - return true; } /** @@ -233,7 +231,7 @@ public function errorInfo() /** * {@inheritdoc} */ - public function execute($params = null) + public function execute($params = null) : void { if ($params) { $hasZeroIndex = array_key_exists(0, $params); @@ -312,7 +310,7 @@ private function prepare() /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { $this->defaultFetchMode = $fetchMode; @@ -320,11 +318,11 @@ public function setFetchMode($fetchMode, ...$args) $this->defaultFetchClass = $args[0]; } - if (isset($args[1])) { - $this->defaultFetchClassCtorArgs = (array) $args[1]; + if (! isset($args[1])) { + return; } - return true; + $this->defaultFetchClassCtorArgs = (array) $args[1]; } /** diff --git a/lib/Doctrine/DBAL/Driver/Statement.php b/lib/Doctrine/DBAL/Driver/Statement.php index c5f2e3e2546..dca1ecda43e 100644 --- a/lib/Doctrine/DBAL/Driver/Statement.php +++ b/lib/Doctrine/DBAL/Driver/Statement.php @@ -26,9 +26,9 @@ interface Statement extends ResultStatement * @param int $type Explicit data type for the parameter using the {@link \Doctrine\DBAL\ParameterType} * constants. * - * @return bool TRUE on success or FALSE on failure. + * @throws DriverException */ - public function bindValue($param, $value, $type = ParameterType::STRING); + public function bindValue($param, $value, $type = ParameterType::STRING) : void; /** * Binds a PHP variable to a corresponding named (not supported by mysqli driver, see comment below) or question @@ -53,9 +53,9 @@ public function bindValue($param, $value, $type = ParameterType::STRING); * @param int|null $length You must specify maxlength when using an OUT bind * so that PHP allocates enough memory to hold the returned value. * - * @return bool TRUE on success or FALSE on failure. + * @throws DriverException */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null); + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void; /** * Fetches the SQLSTATE associated with the last operation on the statement handle. @@ -85,7 +85,7 @@ public function errorInfo(); * @param mixed[]|null $params An array of values with as many elements as there are * bound parameters in the SQL statement being executed. * - * @return bool TRUE on success or FALSE on failure. + * @throws DriverException */ - public function execute($params = null); + public function execute($params = null) : void; } diff --git a/lib/Doctrine/DBAL/Portability/Connection.php b/lib/Doctrine/DBAL/Portability/Connection.php index f152b82dc94..5194b66a611 100644 --- a/lib/Doctrine/DBAL/Portability/Connection.php +++ b/lib/Doctrine/DBAL/Portability/Connection.php @@ -39,41 +39,45 @@ class Connection extends \Doctrine\DBAL\Connection /** * {@inheritdoc} */ - public function connect() + public function connect() : void { - $ret = parent::connect(); - if ($ret) { - $params = $this->getParams(); - if (isset($params['portability'])) { - if ($this->getDatabasePlatform()->getName() === 'oracle') { - $params['portability'] &= self::PORTABILITY_ORACLE; - } elseif ($this->getDatabasePlatform()->getName() === 'postgresql') { - $params['portability'] &= self::PORTABILITY_POSTGRESQL; - } elseif ($this->getDatabasePlatform()->getName() === 'sqlite') { - $params['portability'] &= self::PORTABILITY_SQLITE; - } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') { - $params['portability'] &= self::PORTABILITY_SQLANYWHERE; - } elseif ($this->getDatabasePlatform()->getName() === 'db2') { - $params['portability'] &= self::PORTABILITY_DB2; - } elseif ($this->getDatabasePlatform()->getName() === 'mssql') { - $params['portability'] &= self::PORTABILITY_SQLSRV; - } else { - $params['portability'] &= self::PORTABILITY_OTHERVENDORS; - } - $this->portability = $params['portability']; - } + if ($this->isConnected()) { + return; + } - if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) { - if ($this->_conn instanceof PDOConnection) { - // make use of c-level support for case handling - $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_CASE, $params['fetch_case']); - } else { - $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER; - } + parent::connect(); + + $params = $this->getParams(); + + if (isset($params['portability'])) { + if ($this->getDatabasePlatform()->getName() === 'oracle') { + $params['portability'] &= self::PORTABILITY_ORACLE; + } elseif ($this->getDatabasePlatform()->getName() === 'postgresql') { + $params['portability'] &= self::PORTABILITY_POSTGRESQL; + } elseif ($this->getDatabasePlatform()->getName() === 'sqlite') { + $params['portability'] &= self::PORTABILITY_SQLITE; + } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') { + $params['portability'] &= self::PORTABILITY_SQLANYWHERE; + } elseif ($this->getDatabasePlatform()->getName() === 'db2') { + $params['portability'] &= self::PORTABILITY_DB2; + } elseif ($this->getDatabasePlatform()->getName() === 'mssql') { + $params['portability'] &= self::PORTABILITY_SQLSRV; + } else { + $params['portability'] &= self::PORTABILITY_OTHERVENDORS; } + $this->portability = $params['portability']; } - return $ret; + if (! isset($params['fetch_case']) || ! ($this->portability & self::PORTABILITY_FIX_CASE)) { + return; + } + + if ($this->_conn instanceof PDOConnection) { + // make use of c-level support for case handling + $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_CASE, $params['fetch_case']); + } else { + $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER; + } } /** diff --git a/lib/Doctrine/DBAL/Portability/Statement.php b/lib/Doctrine/DBAL/Portability/Statement.php index 38173c286d4..7e9edfeadb3 100644 --- a/lib/Doctrine/DBAL/Portability/Statement.php +++ b/lib/Doctrine/DBAL/Portability/Statement.php @@ -45,29 +45,29 @@ public function __construct($stmt, Connection $conn) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void { assert($this->stmt instanceof DriverStatement); - return $this->stmt->bindParam($column, $variable, $type, $length); + $this->stmt->bindParam($column, $variable, $type, $length); } /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = ParameterType::STRING) + public function bindValue($param, $value, $type = ParameterType::STRING) : void { assert($this->stmt instanceof DriverStatement); - return $this->stmt->bindValue($param, $value, $type); + $this->stmt->bindValue($param, $value, $type); } /** * {@inheritdoc} */ - public function closeCursor() + public function closeCursor() : void { - return $this->stmt->closeCursor(); + $this->stmt->closeCursor(); } /** @@ -101,21 +101,21 @@ public function errorInfo() /** * {@inheritdoc} */ - public function execute($params = null) + public function execute($params = null) : void { assert($this->stmt instanceof DriverStatement); - return $this->stmt->execute($params); + $this->stmt->execute($params); } /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { $this->defaultFetchMode = $fetchMode; - return $this->stmt->setFetchMode($fetchMode, ...$args); + $this->stmt->setFetchMode($fetchMode, ...$args); } /** diff --git a/lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php b/lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php index 8d8134127d7..649a4e256fe 100644 --- a/lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php +++ b/lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php @@ -166,18 +166,16 @@ public function getPassword() * * @param string|int|null $shardId * - * @return bool - * * @throws ShardingException */ - public function connect($shardId = null) + public function connect($shardId = null) : void { if ($shardId === null && $this->_conn) { - return false; + return; } if ($shardId !== null && $shardId === $this->activeShardId) { - return false; + return; } if ($this->getTransactionNestingLevel() > 0) { @@ -189,17 +187,17 @@ public function connect($shardId = null) if (isset($this->activeConnections[$activeShardId])) { $this->_conn = $this->activeConnections[$activeShardId]; - return false; + return; } $this->_conn = $this->activeConnections[$activeShardId] = $this->connectTo($activeShardId); - if ($this->_eventManager->hasListeners(Events::postConnect)) { - $eventArgs = new ConnectionEventArgs($this); - $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); + if (! $this->_eventManager->hasListeners(Events::postConnect)) { + return; } - return true; + $eventArgs = new ConnectionEventArgs($this); + $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); } /** diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index fd8db886b8b..fc6608fb3d2 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -2,6 +2,7 @@ namespace Doctrine\DBAL; +use Doctrine\DBAL\Driver\DriverException; use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; @@ -84,27 +85,26 @@ public function __construct($sql, Connection $conn) * @param mixed $value The value of the parameter. * @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance. * - * @return bool TRUE on success, FALSE on failure. + * @throws DBALException + * @throws DriverException */ - public function bindValue($name, $value, $type = ParameterType::STRING) + public function bindValue($name, $value, $type = ParameterType::STRING) : void { $this->params[$name] = $value; $this->types[$name] = $type; - if ($type !== null) { - if (is_string($type)) { - $type = Type::getType($type); - } - if ($type instanceof Type) { - $value = $type->convertToDatabaseValue($value, $this->platform); - $bindingType = $type->getBindingType(); - } else { - $bindingType = $type; - } - return $this->stmt->bindValue($name, $value, $bindingType); + if (is_string($type)) { + $type = Type::getType($type); + } + + if ($type instanceof Type) { + $value = $type->convertToDatabaseValue($value, $this->platform); + $bindingType = $type->getBindingType(); + } else { + $bindingType = $type; } - return $this->stmt->bindValue($name, $value); + $this->stmt->bindValue($name, $value, $bindingType); } /** @@ -118,26 +118,22 @@ public function bindValue($name, $value, $type = ParameterType::STRING) * @param int|null $length Must be specified when using an OUT bind * so that PHP allocates enough memory to hold the returned value. * - * @return bool TRUE on success, FALSE on failure. + * @throws DriverException */ - public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null) + public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null) : void { $this->params[$name] = $var; $this->types[$name] = $type; - return $this->stmt->bindParam($name, $var, $type, $length); + $this->stmt->bindParam($name, $var, $type, $length); } /** - * Executes the statement with the currently bound parameters. - * - * @param mixed[]|null $params - * - * @return bool TRUE on success, FALSE on failure. + * {@inheritDoc} * * @throws DBALException */ - public function execute($params = null) + public function execute($params = null) : void { if (is_array($params)) { $this->params = $params; @@ -147,32 +143,28 @@ public function execute($params = null) $logger->startQuery($this->sql, $this->params, $this->types); try { - $stmt = $this->stmt->execute($params); + $this->stmt->execute($params); } catch (Throwable $ex) { - $logger->stopQuery(); throw DBALException::driverExceptionDuringQuery( $this->conn->getDriver(), $ex, $this->sql, $this->conn->resolveParams($this->params, $this->types) ); + } finally { + $logger->stopQuery(); } - $logger->stopQuery(); $this->params = []; $this->types = []; - - return $stmt; } /** - * Closes the cursor, freeing the database resources used by this statement. - * - * @return bool TRUE on success, FALSE on failure. + * {@inheritDoc} */ - public function closeCursor() + public function closeCursor() : void { - return $this->stmt->closeCursor(); + $this->stmt->closeCursor(); } /** @@ -206,9 +198,9 @@ public function errorInfo() /** * {@inheritdoc} */ - public function setFetchMode($fetchMode, ...$args) + public function setFetchMode($fetchMode, ...$args) : void { - return $this->stmt->setFetchMode($fetchMode, ...$args); + $this->stmt->setFetchMode($fetchMode, ...$args); } /** diff --git a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php index 9218d35e0ab..43fd62c02b3 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -282,7 +282,9 @@ public function testConnectWithoutExplicitDatabaseName() : void $this->connection->getEventManager() ); - self::assertTrue($connection->connect()); + $connection->connect(); + + self::assertTrue($connection->isConnected()); $connection->close(); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php b/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php index 25484605bc1..98995bb5d02 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php @@ -241,8 +241,11 @@ public function testFetchFromNonExecutedStatement(callable $fetch, $expected) : public function testCloseCursorOnNonExecutedStatement() : void { + $this->expectNotToPerformAssertions(); + $stmt = $this->connection->prepare('SELECT id FROM stmt_test'); - self::assertTrue($stmt->closeCursor()); + + $stmt->closeCursor(); } /** @@ -250,12 +253,23 @@ public function testCloseCursorOnNonExecutedStatement() : void */ public function testCloseCursorAfterCursorEnd() : void { + $this->expectNotToPerformAssertions(); + $stmt = $this->connection->prepare('SELECT name FROM stmt_test'); $stmt->execute(); $stmt->fetch(); - self::assertTrue($stmt->closeCursor()); + $stmt->closeCursor(); + } + + public function testCloseCursorAfterClosingCursor() : void + { + $this->expectNotToPerformAssertions(); + + $stmt = $this->connection->executeQuery('SELECT name FROM stmt_test'); + $stmt->closeCursor(); + $stmt->closeCursor(); } /** diff --git a/tests/Doctrine/Tests/DBAL/Portability/StatementTest.php b/tests/Doctrine/Tests/DBAL/Portability/StatementTest.php index c8e40214eb0..a7764107f52 100644 --- a/tests/Doctrine/Tests/DBAL/Portability/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/Portability/StatementTest.php @@ -45,10 +45,9 @@ public function testBindParam() : void $this->wrappedStmt->expects($this->once()) ->method('bindParam') - ->with($column, $variable, $type, $length) - ->will($this->returnValue(true)); + ->with($column, $variable, $type, $length); - self::assertTrue($this->stmt->bindParam($column, $variable, $type, $length)); + $this->stmt->bindParam($column, $variable, $type, $length); } public function testBindValue() : void @@ -59,19 +58,17 @@ public function testBindValue() : void $this->wrappedStmt->expects($this->once()) ->method('bindValue') - ->with($param, $value, $type) - ->will($this->returnValue(true)); + ->with($param, $value, $type); - self::assertTrue($this->stmt->bindValue($param, $value, $type)); + $this->stmt->bindValue($param, $value, $type); } public function testCloseCursor() : void { $this->wrappedStmt->expects($this->once()) - ->method('closeCursor') - ->will($this->returnValue(true)); + ->method('closeCursor'); - self::assertTrue($this->stmt->closeCursor()); + $this->stmt->closeCursor(); } public function testColumnCount() : void @@ -116,10 +113,9 @@ public function testExecute() : void $this->wrappedStmt->expects($this->once()) ->method('execute') - ->with($params) - ->will($this->returnValue(true)); + ->with($params); - self::assertTrue($this->stmt->execute($params)); + $this->stmt->execute($params); } public function testSetFetchMode() : void @@ -137,7 +133,7 @@ public function testSetFetchMode() : void $re->setAccessible(true); self::assertSame(FetchMode::MIXED, $re->getValue($this->stmt)); - self::assertTrue($this->stmt->setFetchMode($fetchMode, $arg1, $arg2)); + $this->stmt->setFetchMode($fetchMode, $arg1, $arg2); self::assertSame($fetchMode, $re->getValue($this->stmt)); }