Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some missing @throws annotations and fix thrown exceptions #4160

Merged
merged 10 commits into from
Jul 10, 2020
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ The following classes have been renamed:
3. The `PDOSqlsrv\Connection` and `PDOSqlsrv\Statement` classes have been made final and no longer extend the corresponding PDO classes.
4. The `SQLSrv\LastInsertId` class has been made final.

## BC BREAK: Changes in wrapper-level exceptions

1. `DBALException::invalidTableName()` has been replaced with the `InvalidTableName` class.

## BC BREAK: Changes in driver-level exception handling

1. The `convertException()` method has been removed from the `Driver` interface. The logic of exception conversion has been moved to the `ExceptionConverter` interface. The drivers now must implement the `getExceptionConverter()` method.
Expand Down
57 changes: 43 additions & 14 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function getDatabase()
{
$platform = $this->getDatabasePlatform();
$query = $platform->getDummySelectSQL($platform->getCurrentDatabaseExpression());
$database = $this->query($query)->fetchOne();
$database = $this->fetchOne($query);

assert(is_string($database) || $database === null);

Expand Down Expand Up @@ -344,7 +344,7 @@ private function detectDatabasePlatform(): void
*
* @return string|null
*
* @throws Exception
* @throws DBALException
*/
private function getDatabasePlatformVersion()
{
Expand Down Expand Up @@ -401,14 +401,20 @@ private function getDatabasePlatformVersion()
* Returns the database server version if the underlying driver supports it.
*
* @return string|null
*
* @throws DBALException
*/
private function getServerVersion()
{
$connection = $this->getWrappedConnection();

// Automatic platform version detection.
if ($connection instanceof ServerInfoAwareConnection) {
return $connection->getServerVersion();
try {
return $connection->getServerVersion();
} catch (DriverException $e) {
throw $this->convertException($e);
}
}

// Unable to detect platform version.
Expand Down Expand Up @@ -622,6 +628,8 @@ public function close()
* @param int $level The level to set.
*
* @return int
*
* @throws DBALException
*/
public function setTransactionIsolation($level)
{
Expand All @@ -634,6 +642,8 @@ public function setTransactionIsolation($level)
* Gets the currently active transaction isolation level.
*
* @return int The current transaction isolation level.
*
* @throws DBALException
*/
public function getTransactionIsolation()
{
Expand Down Expand Up @@ -1131,10 +1141,16 @@ public function getTransactionNestingLevel()
* @param string|null $seqName Name of the sequence object from which the ID should be returned.
*
* @return string A string representation of the last inserted ID.
*
* @throws DBALException
*/
public function lastInsertId($seqName = null)
{
return $this->getWrappedConnection()->lastInsertId($seqName);
try {
return $this->getWrappedConnection()->lastInsertId($seqName);
} catch (DriverException $e) {
throw $this->convertException($e);
}
}

/**
Expand Down Expand Up @@ -1178,7 +1194,7 @@ public function transactional(Closure $func)
*
* @return void
*
* @throws ConnectionException
* @throws DBALException
*/
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
{
Expand Down Expand Up @@ -1216,6 +1232,8 @@ protected function _getNestedTransactionSavePointName()

/**
* @return bool
*
* @throws DBALException
*/
public function beginTransaction()
{
Expand Down Expand Up @@ -1252,8 +1270,7 @@ public function beginTransaction()
/**
* @return bool
*
* @throws ConnectionException If the commit failed due to no active transaction or
* because the transaction was marked for rollback only.
* @throws DBALException
*/
public function commit()
{
Expand Down Expand Up @@ -1305,6 +1322,8 @@ public function commit()

/**
* Commits all current nesting transactions.
*
* @throws DBALException
*/
private function commitAll(): void
{
Expand All @@ -1326,7 +1345,7 @@ private function commitAll(): void
*
* @return bool
*
* @throws ConnectionException If the rollback operation failed.
* @throws DBALException
*/
public function rollBack()
{
Expand Down Expand Up @@ -1378,15 +1397,15 @@ public function rollBack()
*
* @return void
*
* @throws ConnectionException
* @throws DBALException
*/
public function createSavepoint($savepoint)
{
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported();
}

$this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint));
$this->executeUpdate($this->platform->createSavePoint($savepoint));
}

/**
Expand All @@ -1396,7 +1415,7 @@ public function createSavepoint($savepoint)
*
* @return void
*
* @throws ConnectionException
* @throws DBALException
*/
public function releaseSavepoint($savepoint)
{
Expand All @@ -1408,7 +1427,7 @@ public function releaseSavepoint($savepoint)
return;
}

$this->getWrappedConnection()->exec($this->platform->releaseSavePoint($savepoint));
$this->executeUpdate($this->platform->releaseSavePoint($savepoint));
}

/**
Expand All @@ -1418,21 +1437,23 @@ public function releaseSavepoint($savepoint)
*
* @return void
*
* @throws ConnectionException
* @throws DBALException
*/
public function rollbackSavepoint($savepoint)
{
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported();
}

$this->getWrappedConnection()->exec($this->platform->rollbackSavePoint($savepoint));
$this->executeUpdate($this->platform->rollbackSavePoint($savepoint));
}

/**
* Gets the wrapped driver connection.
*
* @return DriverConnection
*
* @throws DBALException
*/
public function getWrappedConnection()
{
Expand Down Expand Up @@ -1504,6 +1525,8 @@ public function isRollbackOnly()
* @param string $type The name of the DBAL mapping type.
*
* @return mixed The converted value.
*
* @throws DBALException
*/
public function convertToDatabaseValue($value, $type)
{
Expand All @@ -1518,6 +1541,8 @@ public function convertToDatabaseValue($value, $type)
* @param string $type The name of the DBAL mapping type.
*
* @return mixed The converted type.
*
* @throws DBALException
*/
public function convertToPHPValue($value, $type)
{
Expand All @@ -1531,6 +1556,8 @@ public function convertToPHPValue($value, $type)
* @param DriverStatement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types).
*
* @throws DBALException
*/
private function _bindTypedValues(DriverStatement $stmt, array $params, array $types): void
{
Expand Down Expand Up @@ -1572,6 +1599,8 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t
* @param int|string|null $type The type to bind (PDO or DBAL).
*
* @return mixed[] [0] => the (escaped) value, [1] => the binding type.
*
* @throws DBALException
*/
private function getBindingInfo($value, $type)
{
Expand Down
1 change: 1 addition & 0 deletions src/Connections/PrimaryReadReplicaConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class PrimaryReadReplicaConnection extends Connection
*
* @param mixed[] $params
*
* @throws DBALException
* @throws InvalidArgumentException
*/
public function __construct(array $params, Driver $driver, ?Configuration $config = null, ?EventManager $eventManager = null)
Expand Down
10 changes: 0 additions & 10 deletions src/DBALException.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ public static function invalidDriverClass($driverClass)
return new self("The given 'driverClass' " . $driverClass . ' has to implement the ' . Driver::class . ' interface.');
}

/**
* @param string $tableName
*
* @return DBALException
*/
public static function invalidTableName($tableName)
{
return new self('Invalid table name specified: ' . $tableName);
}

/**
* @param string $tableName
*
Expand Down
2 changes: 2 additions & 0 deletions src/Driver/PDO/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function getServerVersion()
}

/**
* {@inheritDoc}
*
* @return Statement
*/
public function prepare(string $sql): StatementInterface
Expand Down
3 changes: 3 additions & 0 deletions src/Event/Listeners/OracleSessionInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Event\Listeners;

use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;

Expand Down Expand Up @@ -44,6 +45,8 @@ public function __construct(array $oracleSessionVars = [])

/**
* @return void
*
* @throws DBALException
*/
public function postConnect(ConnectionEventArgs $args)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Event/Listeners/SQLSessionInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Event\Listeners;

use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;

Expand All @@ -24,11 +25,12 @@ public function __construct($sql)

/**
* @return void
*
* @throws DBALException
*/
public function postConnect(ConnectionEventArgs $args)
{
$conn = $args->getConnection();
$conn->exec($this->sql);
$args->getConnection()->executeUpdate($this->sql);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,8 @@ public function getColumnDeclarationListSQL(array $fields)
* a string that defines the complete column
*
* @return string DBMS specific SQL code portion that should be used to declare the column.
*
* @throws DBALException
*/
public function getColumnDeclarationSQL($name, array $field)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Platforms;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
Expand Down Expand Up @@ -694,6 +695,8 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)

/**
* @return string[]
*
* @throws DBALException
*/
private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index)
{
Expand Down Expand Up @@ -733,6 +736,8 @@ private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $inde
* @param TableDiff $diff The table diff to gather the SQL for.
*
* @return string[]
*
* @throws DBALException
*/
private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,8 @@ public function getAlterTableSQL(TableDiff $diff)

/**
* @return string[]|false
*
* @throws DBALException
*/
private function getSimpleAlterTableSQL(TableDiff $diff)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Query;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
Expand Down Expand Up @@ -202,6 +203,8 @@ public function getState()
* for insert, update and delete statements.
*
* @return Result|int
*
* @throws DBALException
*/
public function execute()
{
Expand Down Expand Up @@ -1159,6 +1162,8 @@ private function getSQLForSelect()

/**
* @return string[]
*
* @throws QueryException
*/
private function getFromClauses()
{
Expand Down
Loading