Unverified Commit 8002ccc9 authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #4160 from morozov/some-missing-exceptions

Add some missing @throws annotations and fix thrown exceptions
parents d415d040 06a8fac5
......@@ -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.
......
......@@ -215,7 +215,7 @@ class Connection
{
$platform = $this->getDatabasePlatform();
$query = $platform->getDummySelectSQL($platform->getCurrentDatabaseExpression());
$database = $this->query($query)->fetchOne();
$database = $this->fetchOne($query);
assert(is_string($database) || $database === null);
......@@ -344,7 +344,7 @@ class Connection
*
* @return string|null
*
* @throws Exception
* @throws DBALException
*/
private function getDatabasePlatformVersion()
{
......@@ -401,6 +401,8 @@ class Connection
* Returns the database server version if the underlying driver supports it.
*
* @return string|null
*
* @throws DBALException
*/
private function getServerVersion()
{
......@@ -408,7 +410,11 @@ class Connection
// Automatic platform version detection.
if ($connection instanceof ServerInfoAwareConnection) {
try {
return $connection->getServerVersion();
} catch (DriverException $e) {
throw $this->convertException($e);
}
}
// Unable to detect platform version.
......@@ -622,6 +628,8 @@ class Connection
* @param int $level The level to set.
*
* @return int
*
* @throws DBALException
*/
public function setTransactionIsolation($level)
{
......@@ -634,6 +642,8 @@ class Connection
* Gets the currently active transaction isolation level.
*
* @return int The current transaction isolation level.
*
* @throws DBALException
*/
public function getTransactionIsolation()
{
......@@ -1131,10 +1141,16 @@ class Connection
* @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)
{
try {
return $this->getWrappedConnection()->lastInsertId($seqName);
} catch (DriverException $e) {
throw $this->convertException($e);
}
}
/**
......@@ -1178,7 +1194,7 @@ class Connection
*
* @return void
*
* @throws ConnectionException
* @throws DBALException
*/
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
{
......@@ -1216,6 +1232,8 @@ class Connection
/**
* @return bool
*
* @throws DBALException
*/
public function beginTransaction()
{
......@@ -1252,8 +1270,7 @@ class Connection
/**
* @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()
{
......@@ -1305,6 +1322,8 @@ class Connection
/**
* Commits all current nesting transactions.
*
* @throws DBALException
*/
private function commitAll(): void
{
......@@ -1326,7 +1345,7 @@ class Connection
*
* @return bool
*
* @throws ConnectionException If the rollback operation failed.
* @throws DBALException
*/
public function rollBack()
{
......@@ -1378,7 +1397,7 @@ class Connection
*
* @return void
*
* @throws ConnectionException
* @throws DBALException
*/
public function createSavepoint($savepoint)
{
......@@ -1386,7 +1405,7 @@ class Connection
throw ConnectionException::savepointsNotSupported();
}
$this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint));
$this->executeUpdate($this->platform->createSavePoint($savepoint));
}
/**
......@@ -1396,7 +1415,7 @@ class Connection
*
* @return void
*
* @throws ConnectionException
* @throws DBALException
*/
public function releaseSavepoint($savepoint)
{
......@@ -1408,7 +1427,7 @@ class Connection
return;
}
$this->getWrappedConnection()->exec($this->platform->releaseSavePoint($savepoint));
$this->executeUpdate($this->platform->releaseSavePoint($savepoint));
}
/**
......@@ -1418,7 +1437,7 @@ class Connection
*
* @return void
*
* @throws ConnectionException
* @throws DBALException
*/
public function rollbackSavepoint($savepoint)
{
......@@ -1426,13 +1445,15 @@ class Connection
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()
{
......@@ -1504,6 +1525,8 @@ class Connection
* @param string $type The name of the DBAL mapping type.
*
* @return mixed The converted value.
*
* @throws DBALException
*/
public function convertToDatabaseValue($value, $type)
{
......@@ -1518,6 +1541,8 @@ class Connection
* @param string $type The name of the DBAL mapping type.
*
* @return mixed The converted type.
*
* @throws DBALException
*/
public function convertToPHPValue($value, $type)
{
......@@ -1531,6 +1556,8 @@ class Connection
* @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
{
......@@ -1572,6 +1599,8 @@ class Connection
* @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)
{
......
......@@ -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)
......
......@@ -126,16 +126,6 @@ class DBALException extends Exception
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
*
......
......@@ -60,6 +60,8 @@ final class Connection implements ServerInfoAwareConnection
}
/**
* {@inheritDoc}
*
* @return Statement
*/
public function prepare(string $sql): StatementInterface
......
......@@ -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;
......@@ -44,6 +45,8 @@ class OracleSessionInit implements EventSubscriber
/**
* @return void
*
* @throws DBALException
*/
public function postConnect(ConnectionEventArgs $args)
{
......
......@@ -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;
......@@ -24,11 +25,12 @@ class SQLSessionInit implements EventSubscriber
/**
* @return void
*
* @throws DBALException
*/
public function postConnect(ConnectionEventArgs $args)
{
$conn = $args->getConnection();
$conn->exec($this->sql);
$args->getConnection()->executeUpdate($this->sql);
}
/**
......
......@@ -2165,6 +2165,8 @@ abstract class AbstractPlatform
* 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)
{
......
......@@ -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;
......@@ -694,6 +695,8 @@ SQL
/**
* @return string[]
*
* @throws DBALException
*/
private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index)
{
......@@ -733,6 +736,8 @@ SQL
* @param TableDiff $diff The table diff to gather the SQL for.
*
* @return string[]
*
* @throws DBALException
*/
private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff)
{
......
......@@ -963,6 +963,8 @@ class SqlitePlatform extends AbstractPlatform
/**
* @return string[]|false
*
* @throws DBALException
*/
private function getSimpleAlterTableSQL(TableDiff $diff)
{
......
......@@ -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;
......@@ -202,6 +203,8 @@ class QueryBuilder
* for insert, update and delete statements.
*
* @return Result|int
*
* @throws DBALException
*/
public function execute()
{
......@@ -1159,6 +1162,8 @@ class QueryBuilder
/**
* @return string[]
*
* @throws QueryException
*/
private function getFromClauses()
{
......
This diff is collapsed.
......@@ -58,6 +58,8 @@ class Column extends AbstractAsset
*
* @param string $columnName
* @param mixed[] $options
*
* @throws SchemaException
*/
public function __construct($columnName, Type $type, array $options = [])
{
......@@ -70,6 +72,8 @@ class Column extends AbstractAsset
* @param mixed[] $options
*
* @return Column
*
* @throws SchemaException
*/
public function setOptions(array $options)
{
......
......@@ -22,6 +22,8 @@ class Comparator
{
/**
* @return SchemaDiff
*
* @throws SchemaException
*/
public static function compareSchemas(Schema $fromSchema, Schema $toSchema)
{
......@@ -38,6 +40,8 @@ class Comparator
* stored in $toSchema.
*
* @return SchemaDiff
*
* @throws SchemaException
*/
public function compare(Schema $fromSchema, Schema $toSchema)
{
......@@ -188,6 +192,8 @@ class Comparator
* If there are no differences this method returns the boolean false.
*
* @return TableDiff|false
*
* @throws SchemaException
*/
public function diffTable(Table $table1, Table $table2)
{
......
......@@ -2,6 +2,7 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Types\Type;
......@@ -37,6 +38,8 @@ class DB2SchemaManager extends AbstractSchemaManager
/**
* {@inheritdoc}
*
* @throws DBALException
*/
protected function _getPortableTableColumnDefinition($tableColumn)
{
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
/**
* @psalm-immutable
*/
final class InvalidTableName extends SchemaException
{
public static function new(string $tableName): self
{
return new self(sprintf('Invalid table name specified "%s".', $tableName));
}
}
......@@ -297,6 +297,8 @@ class OracleSchemaManager extends AbstractSchemaManager
* @param string $table
*
* @return bool
*
* @throws DBALException
*/
public function dropAutoincrement($table)
{
......@@ -347,6 +349,8 @@ class OracleSchemaManager extends AbstractSchemaManager
* @param string $user The name of the user to kill sessions for.
*
* @return void
*
* @throws DBALException
*/
private function killUserSessions($user)
{
......
......@@ -2,6 +2,7 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Types\Type;
......@@ -39,6 +40,8 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
* Gets all the existing schema names.
*
* @return string[]
*
* @throws DBALException
*/
public function getSchemaNames()
{
......@@ -46,11 +49,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
}
/**
* Returns an array of schema search paths.
*
* This is a PostgreSQL only function.
*
* @return string[]
* {@inheritDoc}
*/
public function getSchemaSearchPaths()
{
......
......@@ -319,6 +319,8 @@ class SQLServerSchemaManager extends AbstractSchemaManager
* @param string $database The name of the database to close currently active connections for.
*
* @return void
*
* @throws DBALException
*/
private function closeActiveDatabaseConnections($database)
{
......@@ -334,6 +336,8 @@ class SQLServerSchemaManager extends AbstractSchemaManager
/**
* @param string $tableName
*
* @throws DBALException
*/
public function listTableDetails($tableName): Table
{
......
......@@ -58,6 +58,8 @@ class Schema extends AbstractAsset
* @param Table[] $tables
* @param Sequence[] $sequences
* @param string[] $namespaces
*
* @throws SchemaException
*/
public function __construct(
array $tables = [],
......@@ -316,6 +318,8 @@ class Schema extends AbstractAsset
* @param string $tableName
*
* @return Table
*
* @throws SchemaException
*/
public function createTable($tableName)
{
......@@ -336,6 +340,8 @@ class Schema extends AbstractAsset
* @param string $newTableName
*
* @return Schema
*
* @throws SchemaException
*/
public function renameTable($oldTableName, $newTableName)
{
......@@ -354,6 +360,8 @@ class Schema extends AbstractAsset
* @param string $tableName
*
* @return Schema
*
* @throws SchemaException
*/
public function dropTable($tableName)
{
......@@ -372,6 +380,8 @@ class Schema extends AbstractAsset
* @param int $initialValue
*
* @return Sequence
*
* @throws SchemaException
*/
public function createSequence($sequenceName, $allocationSize = 1, $initialValue = 1)
{
......@@ -422,6 +432,8 @@ class Schema extends AbstractAsset
/**
* @return string[]
*
* @throws SchemaException
*/
public function getMigrateToSql(Schema $toSchema, AbstractPlatform $platform)
{
......@@ -433,6 +445,8 @@ class Schema extends AbstractAsset
/**
* @return string[]
*
* @throws SchemaException
*/
public function getMigrateFromSql(Schema $fromSchema, AbstractPlatform $platform)
{
......
......@@ -501,6 +501,9 @@ CREATE\sTABLE # Match "CREATE TABLE"
return $comment === '' ? null : $comment;
}
/**
* @throws DBALException
*/
private function getCreateTableSQL(string $table): string
{
$sql = $this->_conn->fetchOne(
......@@ -528,6 +531,8 @@ SQL
}
/**
* {@inheritDoc}
*
* @param string $tableName
*/
public function listTableDetails($tableName): Table
......
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Schema\Synchronizer;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Throwable;
/**
......@@ -37,6 +38,8 @@ abstract class AbstractSchemaSynchronizer implements SchemaSynchronizer
* @param string[] $sql
*
* @return void
*
* @throws DBALException
*/
protected function processSql(array $sql)
{
......
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Exception\InvalidTableName;
use Doctrine\DBAL\Schema\Visitor\Visitor;
use Doctrine\DBAL\Types\Type;
......@@ -51,12 +52,12 @@ class Table extends AbstractAsset
* @param int $idGeneratorType
* @param mixed[] $options
*
* @throws DBALException
* @throws SchemaException
*/
public function __construct($tableName, array $columns = [], array $indexes = [], array $fkConstraints = [], $idGeneratorType = 0, array $options = [])
{
if (strlen($tableName) === 0) {
throw DBALException::invalidTableName($tableName);
throw InvalidTableName::new($tableName);
}
$this->_setName($tableName);
......@@ -103,6 +104,8 @@ class Table extends AbstractAsset
* @param string|false $indexName
*
* @return self
*
* @throws SchemaException
*/
public function setPrimaryKey(array $columnNames, $indexName = false)
{
......@@ -127,6 +130,8 @@ class Table extends AbstractAsset
* @param mixed[] $options
*
* @return self
*
* @throws SchemaException
*/
public function addIndex(array $columnNames, $indexName = null, array $flags = [], array $options = [])
{
......@@ -145,6 +150,8 @@ class Table extends AbstractAsset
* Drops the primary key from this table.
*
* @return void
*
* @throws SchemaException
*/
public function dropPrimaryKey()
{
......@@ -181,6 +188,8 @@ class Table extends AbstractAsset
* @param mixed[] $options
*
* @return self
*
* @throws SchemaException
*/
public function addUniqueIndex(array $columnNames, $indexName = null, array $options = [])
{
......@@ -292,6 +301,8 @@ class Table extends AbstractAsset
* @param mixed[] $options
*
* @return Column
*
* @throws SchemaException
*/
public function addColumn($columnName, $typeName, array $options = [])
{
......@@ -309,6 +320,8 @@ class Table extends AbstractAsset
* @param mixed[] $options
*
* @return self
*
* @throws SchemaException
*/
public function changeColumn($columnName, array $options)
{
......@@ -345,6 +358,8 @@ class Table extends AbstractAsset
* @param string|null $name
*
* @return self
*
* @throws SchemaException
*/
public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [], $name = null)
{
......@@ -451,6 +466,8 @@ class Table extends AbstractAsset
/**
* @return void
*
* @throws SchemaException
*/
protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
{
......@@ -744,6 +761,8 @@ class Table extends AbstractAsset
/**
* @return void
*
* @throws SchemaException
*/
public function visit(Visitor $visitor)
{
......
......@@ -6,6 +6,7 @@ use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
......@@ -16,6 +17,8 @@ interface Visitor
{
/**
* @return void
*
* @throws SchemaException
*/
public function acceptSchema(Schema $schema);
......@@ -31,6 +34,8 @@ interface Visitor
/**
* @return void
*
* @throws SchemaException
*/
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint);
......
......@@ -95,27 +95,34 @@ class Statement
* @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
*/
public function bindValue($name, $value, $type = ParameterType::STRING)
{
$this->params[$name] = $value;
$this->types[$name] = $type;
$bindingType = ParameterType::STRING;
if ($type !== null) {
if (is_string($type)) {
$type = Type::getType($type);
}
$bindingType = $type;
if ($type instanceof Type) {
$value = $type->convertToDatabaseValue($value, $this->platform);
$bindingType = $type->getBindingType();
} else {
$bindingType = $type;
}
}
try {
return $this->stmt->bindValue($name, $value, $bindingType);
} catch (Exception $e) {
throw $this->conn->convertException($e);
}
return $this->stmt->bindValue($name, $value);
}
/**
......@@ -130,13 +137,19 @@ class Statement
* so that PHP allocates enough memory to hold the returned value.
*
* @return bool TRUE on success, FALSE on failure.
*
* @throws DBALException
*/
public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null)
{
$this->params[$name] = $var;
$this->types[$name] = $type;
try {
return $this->stmt->bindParam($name, $var, $type, $length);
} catch (Exception $e) {
throw $this->conn->convertException($e);
}
}
/**
......
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Tools\Console\Command;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\Keywords\DB2Keywords;
use Doctrine\DBAL\Platforms\Keywords\MariaDb102Keywords;
use Doctrine\DBAL\Platforms\Keywords\MySQL57Keywords;
......@@ -113,6 +114,8 @@ EOT
/**
* {@inheritdoc}
*
* @throws DBALException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
......
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Tools\Console\Command;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
use Doctrine\DBAL\Tools\Dumper;
use LogicException;
......@@ -57,6 +58,8 @@ EOT
/**
* {@inheritdoc}
*
* @throws DBALException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
......
......@@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tools\Console;
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Exception;
use PackageVersions\Versions;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
......@@ -19,6 +20,8 @@ class ConsoleRunner
* @param Command[] $commands
*
* @return void
*
* @throws Exception
*/
public static function run(ConnectionProvider $connectionProvider, $commands = [])
{
......
......@@ -64,6 +64,8 @@ abstract class Type
* @param AbstractPlatform $platform The currently used database platform.
*
* @return mixed The database representation of the value.
*
* @throws ConversionException
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
......@@ -78,6 +80,8 @@ abstract class Type
* @param AbstractPlatform $platform The currently used database platform.
*
* @return mixed The PHP representation of the value.
*
* @throws ConversionException
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
......
......@@ -17,7 +17,7 @@ class SQLSessionInitTest extends TestCase
{
$connectionMock = $this->createMock(Connection::class);
$connectionMock->expects(self::once())
->method('exec')
->method('executeUpdate')
->with(self::equalTo("SET SEARCH_PATH TO foo, public, TIMEZONE TO 'Europe/Berlin'"));
$eventArgs = new ConnectionEventArgs($connectionMock);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment