Unverified Commit d95ce266 authored by Sergei Morozov's avatar Sergei Morozov

Merge pull request #3525 from doctrine/exceptions

Extract exception factory methods into specific exceptions
parents 6555a467 a2abc86b
...@@ -5,8 +5,8 @@ declare(strict_types=1); ...@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Cache; namespace Doctrine\DBAL\Cache;
use ArrayIterator; use ArrayIterator;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate; use IteratorAggregate;
...@@ -149,7 +149,7 @@ class ArrayStatement implements IteratorAggregate, ResultStatement ...@@ -149,7 +149,7 @@ class ArrayStatement implements IteratorAggregate, ResultStatement
} }
if (! array_key_exists($columnIndex, $row)) { if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row)); throw InvalidColumnIndex::new($columnIndex, count($row));
} }
return $row[$columnIndex]; return $row[$columnIndex];
......
...@@ -8,19 +8,4 @@ use Doctrine\DBAL\DBALException; ...@@ -8,19 +8,4 @@ use Doctrine\DBAL\DBALException;
class CacheException extends DBALException class CacheException extends DBALException
{ {
/**
* @return \Doctrine\DBAL\Cache\CacheException
*/
public static function noCacheKey()
{
return new self('No cache key was set.');
}
/**
* @return \Doctrine\DBAL\Cache\CacheException
*/
public static function noResultDriverConfigured()
{
return new self('Trying to cache a query but no result driver is configured.');
}
} }
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Cache\Exception;
use Doctrine\DBAL\Cache\CacheException;
final class NoCacheKey extends CacheException
{
public static function new() : self
{
return new self('No cache key was set.');
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Cache\Exception;
use Doctrine\DBAL\Cache\CacheException;
final class NoResultDriverConfigured extends CacheException
{
public static function new() : self
{
return new self('Trying to cache a query but no result driver is configured.');
}
}
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Cache; namespace Doctrine\DBAL\Cache;
use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Cache\Exception\NoCacheKey;
use function hash; use function hash;
use function serialize; use function serialize;
use function sha1; use function sha1;
...@@ -60,7 +61,7 @@ class QueryCacheProfile ...@@ -60,7 +61,7 @@ class QueryCacheProfile
public function getCacheKey() public function getCacheKey()
{ {
if ($this->cacheKey === null) { if ($this->cacheKey === null) {
throw CacheException::noCacheKey(); throw NoCacheKey::new();
} }
return $this->cacheKey; return $this->cacheKey;
......
...@@ -6,8 +6,8 @@ namespace Doctrine\DBAL\Cache; ...@@ -6,8 +6,8 @@ namespace Doctrine\DBAL\Cache;
use ArrayIterator; use ArrayIterator;
use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate; use IteratorAggregate;
...@@ -192,7 +192,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement ...@@ -192,7 +192,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
} }
if (! array_key_exists($columnIndex, $row)) { if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row)); throw InvalidColumnIndex::new($columnIndex, count($row));
} }
return $row[$columnIndex]; return $row[$columnIndex];
......
...@@ -8,6 +8,7 @@ use Closure; ...@@ -8,6 +8,7 @@ use Closure;
use Doctrine\Common\EventManager; use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\ArrayStatement; use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\DBAL\Cache\CacheException; use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Cache\Exception\NoResultDriverConfigured;
use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Cache\ResultCacheStatement; use Doctrine\DBAL\Cache\ResultCacheStatement;
use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Driver\Connection as DriverConnection;
...@@ -16,7 +17,13 @@ use Doctrine\DBAL\Driver\PingableConnection; ...@@ -16,7 +17,13 @@ use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception\CommitFailedRollbackOnly;
use Doctrine\DBAL\Exception\EmptyCriteriaNotAllowed;
use Doctrine\DBAL\Exception\InvalidArgumentException; use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\Exception\InvalidPlatformType;
use Doctrine\DBAL\Exception\MayNotAlterNestedTransactionWithSavepointsInTransaction;
use Doctrine\DBAL\Exception\NoActiveTransaction;
use Doctrine\DBAL\Exception\SavepointsNotSupported;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder; use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Query\QueryBuilder;
...@@ -198,7 +205,7 @@ class Connection implements DriverConnection ...@@ -198,7 +205,7 @@ class Connection implements DriverConnection
if (isset($params['platform'])) { if (isset($params['platform'])) {
if (! $params['platform'] instanceof Platforms\AbstractPlatform) { if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
throw DBALException::invalidPlatformType($params['platform']); throw InvalidPlatformType::new($params['platform']);
} }
$this->platform = $params['platform']; $this->platform = $params['platform'];
...@@ -651,7 +658,7 @@ class Connection implements DriverConnection ...@@ -651,7 +658,7 @@ class Connection implements DriverConnection
public function delete($tableExpression, array $identifier, array $types = []) public function delete($tableExpression, array $identifier, array $types = [])
{ {
if (empty($identifier)) { if (empty($identifier)) {
throw InvalidArgumentException::fromEmptyCriteria(); throw EmptyCriteriaNotAllowed::new();
} }
$columns = $values = $conditions = []; $columns = $values = $conditions = [];
...@@ -924,7 +931,7 @@ class Connection implements DriverConnection ...@@ -924,7 +931,7 @@ class Connection implements DriverConnection
$resultCache = $qcp->getResultCacheDriver() ?? $this->_config->getResultCacheImpl(); $resultCache = $qcp->getResultCacheDriver() ?? $this->_config->getResultCacheImpl();
if ($resultCache === null) { if ($resultCache === null) {
throw CacheException::noResultDriverConfigured(); throw NoResultDriverConfigured::new();
} }
$connectionParams = $this->getParams(); $connectionParams = $this->getParams();
...@@ -1134,11 +1141,11 @@ class Connection implements DriverConnection ...@@ -1134,11 +1141,11 @@ class Connection implements DriverConnection
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints) public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
{ {
if ($this->transactionNestingLevel > 0) { if ($this->transactionNestingLevel > 0) {
throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction(); throw MayNotAlterNestedTransactionWithSavepointsInTransaction::new();
} }
if (! $this->getDatabasePlatform()->supportsSavepoints()) { if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported(); throw SavepointsNotSupported::new();
} }
$this->nestTransactionsWithSavepoints = (bool) $nestTransactionsWithSavepoints; $this->nestTransactionsWithSavepoints = (bool) $nestTransactionsWithSavepoints;
...@@ -1200,11 +1207,10 @@ class Connection implements DriverConnection ...@@ -1200,11 +1207,10 @@ class Connection implements DriverConnection
public function commit() : void public function commit() : void
{ {
if ($this->transactionNestingLevel === 0) { if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction(); throw NoActiveTransaction::new();
} }
if ($this->isRollbackOnly) { if ($this->isRollbackOnly) {
throw ConnectionException::commitFailedRollbackOnly(); throw CommitFailedRollbackOnly::new();
} }
$result = true; $result = true;
...@@ -1265,7 +1271,7 @@ class Connection implements DriverConnection ...@@ -1265,7 +1271,7 @@ class Connection implements DriverConnection
public function rollBack() : void public function rollBack() : void
{ {
if ($this->transactionNestingLevel === 0) { if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction(); throw NoActiveTransaction::new();
} }
$connection = $this->getWrappedConnection(); $connection = $this->getWrappedConnection();
...@@ -1309,7 +1315,7 @@ class Connection implements DriverConnection ...@@ -1309,7 +1315,7 @@ class Connection implements DriverConnection
public function createSavepoint($savepoint) public function createSavepoint($savepoint)
{ {
if (! $this->getDatabasePlatform()->supportsSavepoints()) { if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported(); throw SavepointsNotSupported::new();
} }
$this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint)); $this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint));
...@@ -1327,7 +1333,7 @@ class Connection implements DriverConnection ...@@ -1327,7 +1333,7 @@ class Connection implements DriverConnection
public function releaseSavepoint($savepoint) public function releaseSavepoint($savepoint)
{ {
if (! $this->getDatabasePlatform()->supportsSavepoints()) { if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported(); throw SavepointsNotSupported::new();
} }
if (! $this->platform->supportsReleaseSavepoints()) { if (! $this->platform->supportsReleaseSavepoints()) {
...@@ -1349,7 +1355,7 @@ class Connection implements DriverConnection ...@@ -1349,7 +1355,7 @@ class Connection implements DriverConnection
public function rollbackSavepoint($savepoint) public function rollbackSavepoint($savepoint)
{ {
if (! $this->getDatabasePlatform()->supportsSavepoints()) { if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported(); throw SavepointsNotSupported::new();
} }
$this->getWrappedConnection()->exec($this->platform->rollbackSavePoint($savepoint)); $this->getWrappedConnection()->exec($this->platform->rollbackSavePoint($savepoint));
...@@ -1393,7 +1399,7 @@ class Connection implements DriverConnection ...@@ -1393,7 +1399,7 @@ class Connection implements DriverConnection
public function setRollbackOnly() public function setRollbackOnly()
{ {
if ($this->transactionNestingLevel === 0) { if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction(); throw NoActiveTransaction::new();
} }
$this->isRollbackOnly = true; $this->isRollbackOnly = true;
} }
...@@ -1408,7 +1414,7 @@ class Connection implements DriverConnection ...@@ -1408,7 +1414,7 @@ class Connection implements DriverConnection
public function isRollbackOnly() public function isRollbackOnly()
{ {
if ($this->transactionNestingLevel === 0) { if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction(); throw NoActiveTransaction::new();
} }
return $this->isRollbackOnly; return $this->isRollbackOnly;
......
...@@ -6,35 +6,4 @@ namespace Doctrine\DBAL; ...@@ -6,35 +6,4 @@ namespace Doctrine\DBAL;
class ConnectionException extends DBALException class ConnectionException extends DBALException
{ {
/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function commitFailedRollbackOnly()
{
return new self('Transaction commit failed because the transaction has been marked for rollback only.');
}
/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function noActiveTransaction()
{
return new self('There is no active transaction.');
}
/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function savepointsNotSupported()
{
return new self('Savepoints are not supported by this driver.');
}
/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function mayNotAlterNestedTransactionWithSavepointsInTransaction()
{
return new self('May not alter the nested transaction with savepoints behavior while a transaction is open.');
}
} }
...@@ -7,130 +7,19 @@ namespace Doctrine\DBAL; ...@@ -7,130 +7,19 @@ namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface; use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Driver\ExceptionConverterDriver; use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Exception; use Exception;
use Throwable; use Throwable;
use function array_map; use function array_map;
use function bin2hex; use function bin2hex;
use function get_class;
use function gettype;
use function implode; use function implode;
use function is_object;
use function is_resource; use function is_resource;
use function is_string; use function is_string;
use function json_encode; use function json_encode;
use function preg_replace; use function preg_replace;
use function spl_object_hash;
use function sprintf; use function sprintf;
class DBALException extends Exception class DBALException extends Exception
{ {
/**
* @param string $method
*
* @return \Doctrine\DBAL\DBALException
*/
public static function notSupported($method)
{
return new self(sprintf("Operation '%s' is not supported by platform.", $method));
}
public static function invalidPlatformSpecified() : self
{
return new self(
"Invalid 'platform' option specified, need to give an instance of " . AbstractPlatform::class . '.'
);
}
/**
* @param mixed $invalidPlatform
*/
public static function invalidPlatformType($invalidPlatform) : self
{
if (is_object($invalidPlatform)) {
return new self(
sprintf(
"Option 'platform' must be a subtype of '%s', instance of '%s' given",
AbstractPlatform::class,
get_class($invalidPlatform)
)
);
}
return new self(
sprintf(
"Option 'platform' must be an object and subtype of '%s'. Got '%s'",
AbstractPlatform::class,
gettype($invalidPlatform)
)
);
}
/**
* Returns a new instance for an invalid specified platform version.
*
* @param string $version The invalid platform version given.
* @param string $expectedFormat The expected platform version format.
*
* @return DBALException
*/
public static function invalidPlatformVersionSpecified($version, $expectedFormat)
{
return new self(
sprintf(
'Invalid platform version "%s" specified. ' .
'The platform version has to be specified in the format: "%s".',
$version,
$expectedFormat
)
);
}
/**
* @return \Doctrine\DBAL\DBALException
*/
public static function invalidPdoInstance()
{
return new self(
"The 'pdo' option was used in DriverManager::getConnection() but no " .
'instance of PDO was given.'
);
}
/**
* @param string|null $url The URL that was provided in the connection parameters (if any).
*
* @return \Doctrine\DBAL\DBALException
*/
public static function driverRequired($url = null)
{
if ($url) {
return new self(
sprintf(
"The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
'is given to DriverManager::getConnection(). Given URL: %s',
$url
)
);
}
return new self("The options 'driver' or 'driverClass' are mandatory if no PDO " .
'instance is given to DriverManager::getConnection().');
}
/**
* @param string $unknownDriverName
* @param string[] $knownDrivers
*
* @return \Doctrine\DBAL\DBALException
*/
public static function unknownDriver($unknownDriverName, array $knownDrivers)
{
return new self("The given 'driver' " . $unknownDriverName . ' is unknown, ' .
'Doctrine currently supports only the following drivers: ' . implode(', ', $knownDrivers));
}
/** /**
* @param string $sql * @param string $sql
* @param mixed[] $params * @param mixed[] $params
...@@ -196,111 +85,4 @@ class DBALException extends Exception ...@@ -196,111 +85,4 @@ class DBALException extends Exception
return $json; return $json;
}, $params)) . ']'; }, $params)) . ']';
} }
/**
* @param string $wrapperClass
*
* @return \Doctrine\DBAL\DBALException
*/
public static function invalidWrapperClass($wrapperClass)
{
return new self("The given 'wrapperClass' " . $wrapperClass . ' has to be a ' .
'subtype of \Doctrine\DBAL\Connection.');
}
/**
* @param string $driverClass
*
* @return \Doctrine\DBAL\DBALException
*/
public static function invalidDriverClass($driverClass)
{
return new self("The given 'driverClass' " . $driverClass . ' has to implement the ' . Driver::class . ' interface.');
}
/**
* @param string $tableName
*
* @return \Doctrine\DBAL\DBALException
*/
public static function invalidTableName($tableName)
{
return new self('Invalid table name specified: ' . $tableName);
}
/**
* @param string $tableName
*
* @return \Doctrine\DBAL\DBALException
*/
public static function noColumnsSpecifiedForTable($tableName)
{
return new self('No columns specified for table ' . $tableName);
}
/**
* @return \Doctrine\DBAL\DBALException
*/
public static function limitOffsetInvalid()
{
return new self('Invalid Offset in Limit Query, it has to be larger than or equal to 0.');
}
/**
* @param string $name
*
* @return \Doctrine\DBAL\DBALException
*/
public static function typeExists($name)
{
return new self('Type ' . $name . ' already exists.');
}
/**
* @param string $name
*
* @return \Doctrine\DBAL\DBALException
*/
public static function unknownColumnType($name)
{
return new self('Unknown column type "' . $name . '" requested. Any Doctrine type that you use has ' .
'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
'have a problem with the cache or forgot some mapping information.');
}
/**
* @param string $name
*
* @return \Doctrine\DBAL\DBALException
*/
public static function typeNotFound($name)
{
return new self('Type to be overwritten ' . $name . ' does not exist.');
}
public static function typeNotRegistered(Type $type) : self
{
return new self(sprintf('Type of the class %s@%s is not registered.', get_class($type), spl_object_hash($type)));
}
public static function typeAlreadyRegistered(Type $type) : self
{
return new self(
sprintf('Type of the class %s@%s is already registered.', get_class($type), spl_object_hash($type))
);
}
public static function invalidColumnIndex(int $index, int $count) : self
{
return new self(sprintf(
'Invalid column index %d. The statement result contains %d column%s.',
$index,
$count,
$count === 1 ? '' : 's'
));
}
} }
...@@ -8,6 +8,7 @@ use Doctrine\DBAL\Connection; ...@@ -8,6 +8,7 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform; use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySQL80Platform; use Doctrine\DBAL\Platforms\MySQL80Platform;
...@@ -148,7 +149,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -148,7 +149,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
$versionString, $versionString,
$versionParts $versionParts
)) { )) {
throw DBALException::invalidPlatformVersionSpecified( throw InvalidPlatformVersion::new(
$versionString, $versionString,
'<major_version>.<minor_version>.<patch_version>' '<major_version>.<minor_version>.<patch_version>'
); );
...@@ -179,7 +180,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, ...@@ -179,7 +180,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
$versionString, $versionString,
$versionParts $versionParts
)) { )) {
throw DBALException::invalidPlatformVersionSpecified( throw InvalidPlatformVersion::new(
$versionString, $versionString,
'^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>' '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>'
); );
......
...@@ -5,9 +5,9 @@ declare(strict_types=1); ...@@ -5,9 +5,9 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver; namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform; use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform; use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
...@@ -82,7 +82,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri ...@@ -82,7 +82,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri
public function createDatabasePlatformForVersion($version) public function createDatabasePlatformForVersion($version)
{ {
if (! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { if (! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
throw DBALException::invalidPlatformVersionSpecified( throw InvalidPlatformVersion::new(
$version, $version,
'<major_version>.<minor_version>.<patch_version>' '<major_version>.<minor_version>.<patch_version>'
); );
......
...@@ -5,9 +5,9 @@ declare(strict_types=1); ...@@ -5,9 +5,9 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver; namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\SQLAnywherePlatform; use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager; use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver; use Doctrine\DBAL\VersionAwarePlatformDriver;
...@@ -73,7 +73,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr ...@@ -73,7 +73,7 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr
$version, $version,
$versionParts $versionParts
)) { )) {
throw DBALException::invalidPlatformVersionSpecified( throw InvalidPlatformVersion::new(
$version, $version,
'<major_version>.<minor_version>.<patch_version>.<build_version>' '<major_version>.<minor_version>.<patch_version>.<build_version>'
); );
......
...@@ -5,8 +5,8 @@ declare(strict_types=1); ...@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver; namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Platforms\SQLServerPlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\SQLServerSchemaManager; use Doctrine\DBAL\Schema\SQLServerSchemaManager;
...@@ -29,7 +29,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr ...@@ -29,7 +29,7 @@ abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDr
$version, $version,
$versionParts $versionParts
)) { )) {
throw DBALException::invalidPlatformVersionSpecified( throw InvalidPlatformVersion::new(
$version, $version,
'<major_version>.<minor_version>.<patch_version>.<build_version>' '<major_version>.<minor_version>.<patch_version>.<build_version>'
); );
......
...@@ -4,9 +4,9 @@ declare(strict_types=1); ...@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\IBMDB2; namespace Doctrine\DBAL\Driver\IBMDB2;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator; use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
...@@ -318,7 +318,7 @@ class DB2Statement implements IteratorAggregate, Statement ...@@ -318,7 +318,7 @@ class DB2Statement implements IteratorAggregate, Statement
} }
if (! array_key_exists($columnIndex, $row)) { if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row)); throw InvalidColumnIndex::new($columnIndex, count($row));
} }
return $row[$columnIndex]; return $row[$columnIndex];
......
...@@ -4,11 +4,11 @@ declare(strict_types=1); ...@@ -4,11 +4,11 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\DriverException; use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator; use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Exception\InvalidArgumentException; use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
...@@ -382,7 +382,7 @@ class MysqliStatement implements IteratorAggregate, Statement ...@@ -382,7 +382,7 @@ class MysqliStatement implements IteratorAggregate, Statement
} }
if (! array_key_exists($columnIndex, $row)) { if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row)); throw InvalidColumnIndex::new($columnIndex, count($row));
} }
return $row[$columnIndex]; return $row[$columnIndex];
......
...@@ -4,9 +4,9 @@ declare(strict_types=1); ...@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\OCI8; namespace Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator; use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use InvalidArgumentException; use InvalidArgumentException;
...@@ -492,7 +492,7 @@ class OCI8Statement implements IteratorAggregate, Statement ...@@ -492,7 +492,7 @@ class OCI8Statement implements IteratorAggregate, Statement
} }
if (! array_key_exists($columnIndex, $row)) { if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row)); throw InvalidColumnIndex::new($columnIndex, count($row));
} }
return $row[$columnIndex]; return $row[$columnIndex];
......
...@@ -4,7 +4,7 @@ declare(strict_types=1); ...@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver; namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
...@@ -186,7 +186,7 @@ class PDOStatement implements IteratorAggregate, Statement ...@@ -186,7 +186,7 @@ class PDOStatement implements IteratorAggregate, Statement
$columnCount = $this->columnCount(); $columnCount = $this->columnCount();
if ($columnIndex < 0 || $columnIndex >= $columnCount) { if ($columnIndex < 0 || $columnIndex >= $columnCount) {
throw DBALException::invalidColumnIndex($columnIndex, $columnCount); throw InvalidColumnIndex::new($columnIndex, $columnCount);
} }
} }
......
...@@ -4,9 +4,9 @@ declare(strict_types=1); ...@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\SQLAnywhere; namespace Doctrine\DBAL\Driver\SQLAnywhere;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator; use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
...@@ -260,7 +260,7 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement ...@@ -260,7 +260,7 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement
} }
if (! array_key_exists($columnIndex, $row)) { if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row)); throw InvalidColumnIndex::new($columnIndex, count($row));
} }
} }
......
...@@ -4,9 +4,9 @@ declare(strict_types=1); ...@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\SQLSrv; namespace Doctrine\DBAL\Driver\SQLSrv;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator; use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use IteratorAggregate; use IteratorAggregate;
...@@ -391,7 +391,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement ...@@ -391,7 +391,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement
} }
if (! array_key_exists($columnIndex, $row)) { if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row)); throw InvalidColumnIndex::new($columnIndex, count($row));
} }
return $row[$columnIndex]; return $row[$columnIndex];
......
...@@ -15,6 +15,11 @@ use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSQLiteDriver; ...@@ -15,6 +15,11 @@ use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSQLiteDriver;
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSrvDriver; use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSrvDriver;
use Doctrine\DBAL\Driver\SQLAnywhere\Driver as SQLAnywhereDriver; use Doctrine\DBAL\Driver\SQLAnywhere\Driver as SQLAnywhereDriver;
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver; use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
use Doctrine\DBAL\Exception\DriverRequired;
use Doctrine\DBAL\Exception\InvalidDriverClass;
use Doctrine\DBAL\Exception\InvalidPdoInstance;
use Doctrine\DBAL\Exception\InvalidWrapperClass;
use Doctrine\DBAL\Exception\UnknownDriver;
use PDO; use PDO;
use function array_keys; use function array_keys;
use function array_map; use function array_map;
...@@ -172,7 +177,7 @@ final class DriverManager ...@@ -172,7 +177,7 @@ final class DriverManager
// check for existing pdo object // check for existing pdo object
if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) { if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
throw DBALException::invalidPdoInstance(); throw InvalidPdoInstance::new();
} }
if (isset($params['pdo'])) { if (isset($params['pdo'])) {
...@@ -189,7 +194,7 @@ final class DriverManager ...@@ -189,7 +194,7 @@ final class DriverManager
$wrapperClass = Connection::class; $wrapperClass = Connection::class;
if (isset($params['wrapperClass'])) { if (isset($params['wrapperClass'])) {
if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) { if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) {
throw DBALException::invalidWrapperClass($params['wrapperClass']); throw InvalidWrapperClass::new($params['wrapperClass']);
} }
$wrapperClass = $params['wrapperClass']; $wrapperClass = $params['wrapperClass'];
...@@ -221,18 +226,18 @@ final class DriverManager ...@@ -221,18 +226,18 @@ final class DriverManager
// driver // driver
if (! isset($params['driver']) && ! isset($params['driverClass'])) { if (! isset($params['driver']) && ! isset($params['driverClass'])) {
throw DBALException::driverRequired(); throw DriverRequired::new();
} }
// check validity of parameters // check validity of parameters
// driver // driver
if (isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) { if (isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap)); throw UnknownDriver::new($params['driver'], array_keys(self::$_driverMap));
} }
if (isset($params['driverClass']) && ! in_array(Driver::class, class_implements($params['driverClass'], true))) { if (isset($params['driverClass']) && ! in_array(Driver::class, class_implements($params['driverClass'], true))) {
throw DBALException::invalidDriverClass($params['driverClass']); throw InvalidDriverClass::new($params['driverClass']);
} }
} }
...@@ -433,7 +438,7 @@ final class DriverManager ...@@ -433,7 +438,7 @@ final class DriverManager
// If a schemeless connection URL is given, we require a default driver or default custom driver // If a schemeless connection URL is given, we require a default driver or default custom driver
// as connection parameter. // as connection parameter.
if (! isset($params['driverClass']) && ! isset($params['driver'])) { if (! isset($params['driverClass']) && ! isset($params['driver'])) {
throw DBALException::driverRequired($params['url']); throw DriverRequired::new($params['url']);
} }
return $params; return $params;
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\ConnectionException;
final class CommitFailedRollbackOnly extends ConnectionException
{
public static function new() : self
{
return new self('Transaction commit failed because the transaction has been marked for rollback only.');
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class DriverRequired extends DBALException
{
/**
* @param string|null $url The URL that was provided in the connection parameters (if any).
*/
public static function new(?string $url = null) : self
{
if ($url !== null) {
return new self(
sprintf(
"The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme "
. 'is given to DriverManager::getConnection(). Given URL: %s',
$url
)
);
}
return new self(
"The options 'driver' or 'driverClass' are mandatory if no PDO "
. 'instance is given to DriverManager::getConnection().'
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
final class EmptyCriteriaNotAllowed extends InvalidArgumentException
{
public static function new() : self
{
return new self('Empty criteria was used, expected non-empty criteria');
}
}
...@@ -11,11 +11,4 @@ use Doctrine\DBAL\DBALException; ...@@ -11,11 +11,4 @@ use Doctrine\DBAL\DBALException;
*/ */
class InvalidArgumentException extends DBALException class InvalidArgumentException extends DBALException
{ {
/**
* @return self
*/
public static function fromEmptyCriteria()
{
return new self('Empty criteria was used, expected non-empty criteria');
}
} }
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class InvalidColumnIndex extends DBALException
{
public static function new(int $index, int $count) : self
{
return new self(sprintf(
'Invalid column index %d. The statement result contains %d column%s.',
$index,
$count,
$count === 1 ? '' : 's'
));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use function sprintf;
final class InvalidDriverClass extends DBALException
{
public static function new(string $driverClass) : self
{
return new self(
sprintf(
"The given 'driverClass' %s has to implement the %s interface.",
$driverClass,
Driver::class
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\DBALException;
final class InvalidPdoInstance extends DBALException
{
public static function new() : self
{
return new self("The 'pdo' option was used in DriverManager::getConnection() but no instance of PDO was given.");
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use function get_class;
use function gettype;
use function is_object;
use function sprintf;
final class InvalidPlatformType extends DBALException
{
/**
* @param mixed $invalidPlatform
*/
public static function new($invalidPlatform) : self
{
if (is_object($invalidPlatform)) {
return new self(
sprintf(
"Option 'platform' must be a subtype of '%s', instance of '%s' given",
AbstractPlatform::class,
get_class($invalidPlatform)
)
);
}
return new self(
sprintf(
"Option 'platform' must be an object and subtype of '%s'. Got '%s'",
AbstractPlatform::class,
gettype($invalidPlatform)
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class InvalidWrapperClass extends DBALException
{
public static function new(string $wrapperClass) : self
{
return new self(
sprintf(
"The given 'wrapperClass' %s has to be a subtype of %s.",
$wrapperClass,
Connection::class
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\ConnectionException;
final class MayNotAlterNestedTransactionWithSavepointsInTransaction extends ConnectionException
{
public static function new() : self
{
return new self('May not alter the nested transaction with savepoints behavior while a transaction is open.');
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\SQLParserUtilsException;
use function sprintf;
final class MissingSQLParam extends SQLParserUtilsException
{
public static function new(string $paramName) : self
{
return new self(
sprintf('Value for :%1$s not found in params array. Params array key should be "%1$s"', $paramName)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\SQLParserUtilsException;
use function sprintf;
final class MissingSQLType extends SQLParserUtilsException
{
public static function new(string $typeName) : self
{
return new self(
sprintf('Value for :%1$s not found in types array. Types array key should be "%1$s"', $typeName)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\ConnectionException;
final class NoActiveTransaction extends ConnectionException
{
public static function new() : self
{
return new self('There is no active transaction.');
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\ConnectionException;
final class SavepointsNotSupported extends ConnectionException
{
public static function new() : self
{
return new self('Savepoints are not supported by this driver.');
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\DBALException;
use function implode;
use function sprintf;
final class UnknownDriver extends DBALException
{
/**
* @param string[] $knownDrivers
*/
public static function new(string $unknownDriverName, array $knownDrivers) : self
{
return new self(
sprintf(
"The given 'driver' %s is unknown, Doctrine currently supports only the following drivers: %s",
$unknownDriverName,
implode(', ', $knownDrivers)
)
);
}
}
...@@ -4,7 +4,7 @@ declare(strict_types=1); ...@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Platforms; namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Platforms\Exception\NotSupported;
use Doctrine\DBAL\Schema\ColumnDiff; use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Identifier; use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Index;
...@@ -483,7 +483,7 @@ class DB2Platform extends AbstractPlatform ...@@ -483,7 +483,7 @@ class DB2Platform extends AbstractPlatform
public function getIndexDeclarationSQL($name, Index $index) public function getIndexDeclarationSQL($name, Index $index)
{ {
// Index declaration in statements like CREATE TABLE is not supported. // Index declaration in statements like CREATE TABLE is not supported.
throw DBALException::notSupported(__METHOD__); throw NotSupported::new(__METHOD__);
} }
/** /**
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Platforms\Exception;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class InvalidPlatformVersion extends DBALException implements PlatformException
{
/**
* Returns a new instance for an invalid specified platform version.
*
* @param string $version The invalid platform version given.
* @param string $expectedFormat The expected platform version format.
*/
public static function new(string $version, string $expectedFormat) : self
{
return new self(
sprintf(
'Invalid platform version "%s" specified. The platform version has to be specified in the format: "%s".',
$version,
$expectedFormat
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Platforms\Exception;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class NoColumnsSpecifiedForTable extends DBALException implements PlatformException
{
public static function new(string $tableName) : self
{
return new self(sprintf('No columns specified for table %s', $tableName));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Platforms\Exception;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class NotSupported extends DBALException implements PlatformException
{
public static function new(string $method) : self
{
return new self(sprintf('Operation \'%s\' is not supported by platform.', $method));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Platforms\Exception;
interface PlatformException
{
}
...@@ -4,8 +4,8 @@ declare(strict_types=1); ...@@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Platforms; namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\LockMode; use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\Exception\NotSupported;
use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff; use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Constraint; use Doctrine\DBAL\Schema\Constraint;
...@@ -683,7 +683,7 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -683,7 +683,7 @@ class SQLAnywherePlatform extends AbstractPlatform
public function getIndexDeclarationSQL($name, Index $index) public function getIndexDeclarationSQL($name, Index $index)
{ {
// Index declaration in statements like CREATE TABLE is not supported. // Index declaration in statements like CREATE TABLE is not supported.
throw DBALException::notSupported(__METHOD__); throw NotSupported::new(__METHOD__);
} }
/** /**
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query\Exception;
use Doctrine\DBAL\Query\QueryException;
use function implode;
use function sprintf;
final class NonUniqueAlias extends QueryException
{
/**
* @param string[] $registeredAliases
*/
public static function new(string $alias, array $registeredAliases) : self
{
return new self(
sprintf(
"The given alias '%s' is not unique in FROM and JOIN clause table. "
. 'The currently registered aliases are: %s.',
$alias,
implode(', ', $registeredAliases)
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query\Exception;
use Doctrine\DBAL\Query\QueryException;
use function implode;
use function sprintf;
final class UnknownAlias extends QueryException
{
/**
* @param string[] $registeredAliases
*/
public static function new(string $alias, array $registeredAliases) : self
{
return new self(
sprintf(
"The given alias '%s' is not part of any FROM or JOIN clause table. "
. 'The currently registered aliases are: %s.',
$alias,
implode(', ', $registeredAliases)
)
);
}
}
...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Query; ...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Query;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\Exception\NonUniqueAlias;
use Doctrine\DBAL\Query\Exception\UnknownAlias;
use Doctrine\DBAL\Query\Expression\CompositeExpression; use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder; use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use function array_key_exists; use function array_key_exists;
...@@ -1181,7 +1183,7 @@ class QueryBuilder ...@@ -1181,7 +1183,7 @@ class QueryBuilder
{ {
foreach ($this->sqlParts['join'] as $fromAlias => $joins) { foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
if (! isset($knownAliases[$fromAlias])) { if (! isset($knownAliases[$fromAlias])) {
throw QueryException::unknownAlias($fromAlias, array_keys($knownAliases)); throw UnknownAlias::new($fromAlias, array_keys($knownAliases));
} }
} }
} }
...@@ -1339,7 +1341,7 @@ class QueryBuilder ...@@ -1339,7 +1341,7 @@ class QueryBuilder
if (isset($this->sqlParts['join'][$fromAlias])) { if (isset($this->sqlParts['join'][$fromAlias])) {
foreach ($this->sqlParts['join'][$fromAlias] as $join) { foreach ($this->sqlParts['join'][$fromAlias] as $join) {
if (array_key_exists($join['joinAlias'], $knownAliases)) { if (array_key_exists($join['joinAlias'], $knownAliases)) {
throw QueryException::nonUniqueAlias($join['joinAlias'], array_keys($knownAliases)); throw NonUniqueAlias::new($join['joinAlias'], array_keys($knownAliases));
} }
$sql .= ' ' . strtoupper($join['joinType']) $sql .= ' ' . strtoupper($join['joinType'])
. ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias'] . ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias']
......
...@@ -5,33 +5,7 @@ declare(strict_types=1); ...@@ -5,33 +5,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Query; namespace Doctrine\DBAL\Query;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use function implode;
class QueryException extends DBALException class QueryException extends DBALException
{ {
/**
* @param string $alias
* @param string[] $registeredAliases
*
* @return \Doctrine\DBAL\Query\QueryException
*/
public static function unknownAlias($alias, $registeredAliases)
{
return new self("The given alias '" . $alias . "' is not part of " .
'any FROM or JOIN clause table. The currently registered ' .
'aliases are: ' . implode(', ', $registeredAliases) . '.');
}
/**
* @param string $alias
* @param string[] $registeredAliases
*
* @return \Doctrine\DBAL\Query\QueryException
*/
public static function nonUniqueAlias($alias, $registeredAliases)
{
return new self("The given alias '" . $alias . "' is not unique " .
'in FROM and JOIN clause table. The currently registered ' .
'aliases are: ' . implode(', ', $registeredAliases) . '.');
}
} }
...@@ -4,6 +4,8 @@ declare(strict_types=1); ...@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Doctrine\DBAL; namespace Doctrine\DBAL;
use Doctrine\DBAL\Exception\MissingSQLParam;
use Doctrine\DBAL\Exception\MissingSQLType;
use const PREG_OFFSET_CAPTURE; use const PREG_OFFSET_CAPTURE;
use function array_fill; use function array_fill;
use function array_key_exists; use function array_key_exists;
...@@ -285,9 +287,9 @@ class SQLParserUtils ...@@ -285,9 +287,9 @@ class SQLParserUtils
} }
if ($isParam) { if ($isParam) {
throw SQLParserUtilsException::missingParam($paramName); throw MissingSQLParam::new($paramName);
} }
throw SQLParserUtilsException::missingType($paramName); throw MissingSQLType::new($paramName);
} }
} }
...@@ -4,30 +4,9 @@ declare(strict_types=1); ...@@ -4,30 +4,9 @@ declare(strict_types=1);
namespace Doctrine\DBAL; namespace Doctrine\DBAL;
use function sprintf;
/** /**
* Doctrine\DBAL\ConnectionException * Doctrine\DBAL\ConnectionException
*/ */
class SQLParserUtilsException extends DBALException class SQLParserUtilsException extends DBALException
{ {
/**
* @param string $paramName
*
* @return \Doctrine\DBAL\SQLParserUtilsException
*/
public static function missingParam($paramName)
{
return new self(sprintf('Value for :%1$s not found in params array. Params array key should be "%1$s"', $paramName));
}
/**
* @param string $typeName
*
* @return \Doctrine\DBAL\SQLParserUtilsException
*/
public static function missingType($typeName)
{
return new self(sprintf('Value for :%1$s not found in types array. Types array key should be "%1$s"', $typeName));
}
} }
...@@ -11,6 +11,7 @@ use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs; ...@@ -11,6 +11,7 @@ use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs; use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
use Doctrine\DBAL\Events; use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Exception\NotSupported;
use Throwable; use Throwable;
use function array_filter; use function array_filter;
use function array_intersect; use function array_intersect;
...@@ -779,7 +780,7 @@ abstract class AbstractSchemaManager ...@@ -779,7 +780,7 @@ abstract class AbstractSchemaManager
*/ */
protected function _getPortableSequenceDefinition($sequence) protected function _getPortableSequenceDefinition($sequence)
{ {
throw DBALException::notSupported('Sequences'); throw NotSupported::new('Sequences');
} }
/** /**
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class ColumnAlreadyExists extends SchemaException
{
public static function new(string $tableName, string $columnName) : self
{
return new self(
sprintf("The column '%s' on table '%s' already exists.", $columnName, $tableName),
self::COLUMN_ALREADY_EXISTS
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class ColumnDoesNotExist extends SchemaException
{
public static function new(string $columnName, string $table) : self
{
return new self(
sprintf("There is no column with name '%s' on table '%s'.", $columnName, $table),
self::COLUMN_DOESNT_EXIST
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class ForeignKeyDoesNotExist extends SchemaException
{
public static function new(string $foreignKeyName, string $table) : self
{
return new self(
sprintf("There exists no foreign key with the name '%s' on table '%s'.", $foreignKeyName, $table),
self::FOREIGNKEY_DOESNT_EXIST
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class IndexAlreadyExists extends SchemaException
{
public static function new(string $indexName, string $table) : self
{
return new self(
sprintf("An index with name '%s' was already defined on table '%s'.", $indexName, $table),
self::INDEX_ALREADY_EXISTS
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class IndexDoesNotExist extends SchemaException
{
public static function new(string $indexName, string $table) : self
{
return new self(
sprintf("Index '%s' does not exist on table '%s'.", $indexName, $table),
self::INDEX_DOESNT_EXIST
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class IndexNameInvalid extends SchemaException
{
public static function new(string $indexName) : self
{
return new self(
sprintf('Invalid index-name %s given, has to be [a-zA-Z0-9_]', $indexName),
self::INDEX_INVALID_NAME
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class InvalidTableName extends SchemaException
{
public static function new(string $tableName) : self
{
return new self(sprintf('Invalid table name specified: %s', $tableName));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Table;
use function implode;
use function sprintf;
final class NamedForeignKeyRequired extends SchemaException
{
public static function new(Table $localTable, ForeignKeyConstraint $foreignKey) : self
{
return new self(
sprintf(
'The performed schema operation on %s requires a named foreign key, ' .
"but the given foreign key from (%s) onto foreign table '%s' (%s) is currently unnamed.",
$localTable->getName(),
implode(', ', $foreignKey->getColumns()),
$foreignKey->getForeignTableName(),
implode(', ', $foreignKey->getForeignColumns())
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class NamespaceAlreadyExists extends SchemaException
{
public static function new(string $namespaceName) : self
{
return new self(
sprintf("The namespace with name '%s' already exists.", $namespaceName),
self::NAMESPACE_ALREADY_EXISTS
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class SequenceAlreadyExists extends SchemaException
{
public static function new(string $sequenceName) : self
{
return new self(
sprintf("The sequence '%s' already exists.", $sequenceName),
self::SEQUENCE_ALREADY_EXISTS
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class SequenceDoesNotExist extends SchemaException
{
public static function new(string $sequenceName) : self
{
return new self(
sprintf("There exists no sequence with the name '%s'.", $sequenceName),
self::SEQUENCE_DOENST_EXIST
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class TableAlreadyExists extends SchemaException
{
public static function new(string $tableName) : self
{
return new self(
sprintf("The table with name '%s' already exists.", $tableName),
self::TABLE_ALREADY_EXISTS
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class TableDoesNotExist extends SchemaException
{
public static function new(string $tableName) : self
{
return new self(
sprintf("There is no table with name '%s' in the schema.", $tableName),
self::TABLE_DOESNT_EXIST
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use function sprintf;
final class UniqueConstraintDoesNotExist extends SchemaException
{
public static function new(string $constraintName, string $table) : self
{
return new self(
sprintf("There exists no unique constraint with the name '%s' on table '%s'.", $constraintName, $table),
self::CONSTRAINT_DOESNT_EXIST
);
}
}
...@@ -5,6 +5,11 @@ declare(strict_types=1); ...@@ -5,6 +5,11 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Exception\NamespaceAlreadyExists;
use Doctrine\DBAL\Schema\Exception\SequenceAlreadyExists;
use Doctrine\DBAL\Schema\Exception\SequenceDoesNotExist;
use Doctrine\DBAL\Schema\Exception\TableAlreadyExists;
use Doctrine\DBAL\Schema\Exception\TableDoesNotExist;
use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector; use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector; use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
use Doctrine\DBAL\Schema\Visitor\NamespaceVisitor; use Doctrine\DBAL\Schema\Visitor\NamespaceVisitor;
...@@ -104,7 +109,7 @@ class Schema extends AbstractAsset ...@@ -104,7 +109,7 @@ class Schema extends AbstractAsset
$tableName = $table->getFullQualifiedName($this->getName()); $tableName = $table->getFullQualifiedName($this->getName());
if (isset($this->_tables[$tableName])) { if (isset($this->_tables[$tableName])) {
throw SchemaException::tableAlreadyExists($tableName); throw TableAlreadyExists::new($tableName);
} }
if ($namespaceName !== null if ($namespaceName !== null
...@@ -128,7 +133,7 @@ class Schema extends AbstractAsset ...@@ -128,7 +133,7 @@ class Schema extends AbstractAsset
$seqName = $sequence->getFullQualifiedName($this->getName()); $seqName = $sequence->getFullQualifiedName($this->getName());
if (isset($this->_sequences[$seqName])) { if (isset($this->_sequences[$seqName])) {
throw SchemaException::sequenceAlreadyExists($seqName); throw SequenceAlreadyExists::new($seqName);
} }
if ($namespaceName !== null if ($namespaceName !== null
...@@ -171,7 +176,7 @@ class Schema extends AbstractAsset ...@@ -171,7 +176,7 @@ class Schema extends AbstractAsset
{ {
$tableName = $this->getFullQualifiedAssetName($tableName); $tableName = $this->getFullQualifiedAssetName($tableName);
if (! isset($this->_tables[$tableName])) { if (! isset($this->_tables[$tableName])) {
throw SchemaException::tableDoesNotExist($tableName); throw TableDoesNotExist::new($tableName);
} }
return $this->_tables[$tableName]; return $this->_tables[$tableName];
...@@ -270,7 +275,7 @@ class Schema extends AbstractAsset ...@@ -270,7 +275,7 @@ class Schema extends AbstractAsset
{ {
$sequenceName = $this->getFullQualifiedAssetName($sequenceName); $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
if (! $this->hasSequence($sequenceName)) { if (! $this->hasSequence($sequenceName)) {
throw SchemaException::sequenceDoesNotExist($sequenceName); throw SequenceDoesNotExist::new($sequenceName);
} }
return $this->_sequences[$sequenceName]; return $this->_sequences[$sequenceName];
...@@ -298,7 +303,7 @@ class Schema extends AbstractAsset ...@@ -298,7 +303,7 @@ class Schema extends AbstractAsset
$unquotedNamespaceName = strtolower($this->getUnquotedAssetName($namespaceName)); $unquotedNamespaceName = strtolower($this->getUnquotedAssetName($namespaceName));
if (isset($this->namespaces[$unquotedNamespaceName])) { if (isset($this->namespaces[$unquotedNamespaceName])) {
throw SchemaException::namespaceAlreadyExists($unquotedNamespaceName); throw NamespaceAlreadyExists::new($unquotedNamespaceName);
} }
$this->namespaces[$unquotedNamespaceName] = $namespaceName; $this->namespaces[$unquotedNamespaceName] = $namespaceName;
......
...@@ -5,8 +5,6 @@ declare(strict_types=1); ...@@ -5,8 +5,6 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use function implode;
use function sprintf;
class SchemaException extends DBALException class SchemaException extends DBALException
{ {
...@@ -22,180 +20,4 @@ class SchemaException extends DBALException ...@@ -22,180 +20,4 @@ class SchemaException extends DBALException
public const FOREIGNKEY_DOESNT_EXIST = 100; public const FOREIGNKEY_DOESNT_EXIST = 100;
public const CONSTRAINT_DOESNT_EXIST = 110; public const CONSTRAINT_DOESNT_EXIST = 110;
public const NAMESPACE_ALREADY_EXISTS = 120; public const NAMESPACE_ALREADY_EXISTS = 120;
/**
* @param string $tableName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function tableDoesNotExist($tableName)
{
return new self("There is no table with name '" . $tableName . "' in the schema.", self::TABLE_DOESNT_EXIST);
}
/**
* @param string $indexName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function indexNameInvalid($indexName)
{
return new self(
sprintf('Invalid index-name %s given, has to be [a-zA-Z0-9_]', $indexName),
self::INDEX_INVALID_NAME
);
}
/**
* @param string $indexName
* @param string $table
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function indexDoesNotExist($indexName, $table)
{
return new self(
sprintf("Index '%s' does not exist on table '%s'.", $indexName, $table),
self::INDEX_DOESNT_EXIST
);
}
/**
* @param string $indexName
* @param string $table
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function indexAlreadyExists($indexName, $table)
{
return new self(
sprintf("An index with name '%s' was already defined on table '%s'.", $indexName, $table),
self::INDEX_ALREADY_EXISTS
);
}
/**
* @param string $columnName
* @param string $table
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function columnDoesNotExist($columnName, $table)
{
return new self(
sprintf("There is no column with name '%s' on table '%s'.", $columnName, $table),
self::COLUMN_DOESNT_EXIST
);
}
/**
* @param string $namespaceName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function namespaceAlreadyExists($namespaceName)
{
return new self(
sprintf("The namespace with name '%s' already exists.", $namespaceName),
self::NAMESPACE_ALREADY_EXISTS
);
}
/**
* @param string $tableName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function tableAlreadyExists($tableName)
{
return new self("The table with name '" . $tableName . "' already exists.", self::TABLE_ALREADY_EXISTS);
}
/**
* @param string $tableName
* @param string $columnName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function columnAlreadyExists($tableName, $columnName)
{
return new self(
"The column '" . $columnName . "' on table '" . $tableName . "' already exists.",
self::COLUMN_ALREADY_EXISTS
);
}
/**
* @param string $sequenceName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function sequenceAlreadyExists($sequenceName)
{
return new self("The sequence '" . $sequenceName . "' already exists.", self::SEQUENCE_ALREADY_EXISTS);
}
/**
* @param string $sequenceName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function sequenceDoesNotExist($sequenceName)
{
return new self("There exists no sequence with the name '" . $sequenceName . "'.", self::SEQUENCE_DOENST_EXIST);
}
/**
* @param string $constraintName
* @param string $table
*
* @return self
*/
public static function uniqueConstraintDoesNotExist($constraintName, $table)
{
return new self(sprintf(
'There exists no unique constraint with the name "%s" on table "%s".',
$constraintName,
$table
), self::CONSTRAINT_DOESNT_EXIST);
}
/**
* @param string $fkName
* @param string $table
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function foreignKeyDoesNotExist($fkName, $table)
{
return new self(
sprintf("There exists no foreign key with the name '%s' on table '%s'.", $fkName, $table),
self::FOREIGNKEY_DOESNT_EXIST
);
}
/**
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
{
return new self(
'The performed schema operation on ' . $localTable->getName() . ' requires a named foreign key, ' .
'but the given foreign key from (' . implode(', ', $foreignKey->getColumns()) . ') onto foreign table ' .
"'" . $foreignKey->getForeignTableName() . "' (" . implode(', ', $foreignKey->getForeignColumns()) . ') is currently ' .
'unnamed.'
);
}
/**
* @param string $changeName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
public static function alterTableChangeNotSupported($changeName)
{
return new self(
sprintf("Alter table change not supported, given '%s'", $changeName)
);
}
} }
...@@ -5,6 +5,14 @@ declare(strict_types=1); ...@@ -5,6 +5,14 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Exception\ColumnAlreadyExists;
use Doctrine\DBAL\Schema\Exception\ColumnDoesNotExist;
use Doctrine\DBAL\Schema\Exception\ForeignKeyDoesNotExist;
use Doctrine\DBAL\Schema\Exception\IndexAlreadyExists;
use Doctrine\DBAL\Schema\Exception\IndexDoesNotExist;
use Doctrine\DBAL\Schema\Exception\IndexNameInvalid;
use Doctrine\DBAL\Schema\Exception\InvalidTableName;
use Doctrine\DBAL\Schema\Exception\UniqueConstraintDoesNotExist;
use Doctrine\DBAL\Schema\Visitor\Visitor; use Doctrine\DBAL\Schema\Visitor\Visitor;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use function array_keys; use function array_keys;
...@@ -68,7 +76,7 @@ class Table extends AbstractAsset ...@@ -68,7 +76,7 @@ class Table extends AbstractAsset
array $options = [] array $options = []
) { ) {
if (strlen($tableName) === 0) { if (strlen($tableName) === 0) {
throw DBALException::invalidTableName($tableName); throw InvalidTableName::new($tableName);
} }
$this->_setName($tableName); $this->_setName($tableName);
...@@ -187,7 +195,7 @@ class Table extends AbstractAsset ...@@ -187,7 +195,7 @@ class Table extends AbstractAsset
$indexName = $this->normalizeIdentifier($indexName); $indexName = $this->normalizeIdentifier($indexName);
if (! $this->hasIndex($indexName)) { if (! $this->hasIndex($indexName)) {
throw SchemaException::indexDoesNotExist($indexName, $this->_name); throw IndexDoesNotExist::new($indexName, $this->_name);
} }
unset($this->_indexes[$indexName]); unset($this->_indexes[$indexName]);
...@@ -235,11 +243,11 @@ class Table extends AbstractAsset ...@@ -235,11 +243,11 @@ class Table extends AbstractAsset
} }
if (! $this->hasIndex($oldIndexName)) { if (! $this->hasIndex($oldIndexName)) {
throw SchemaException::indexDoesNotExist($oldIndexName, $this->_name); throw IndexDoesNotExist::new($oldIndexName, $this->_name);
} }
if ($this->hasIndex($normalizedNewIndexName)) { if ($this->hasIndex($normalizedNewIndexName)) {
throw SchemaException::indexAlreadyExists($normalizedNewIndexName, $this->_name); throw IndexAlreadyExists::new($normalizedNewIndexName, $this->_name);
} }
$oldIndex = $this->_indexes[$oldIndexName]; $oldIndex = $this->_indexes[$oldIndexName];
...@@ -411,14 +419,14 @@ class Table extends AbstractAsset ...@@ -411,14 +419,14 @@ class Table extends AbstractAsset
if ($foreignTable instanceof Table) { if ($foreignTable instanceof Table) {
foreach ($foreignColumnNames as $columnName) { foreach ($foreignColumnNames as $columnName) {
if (! $foreignTable->hasColumn($columnName)) { if (! $foreignTable->hasColumn($columnName)) {
throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName()); throw ColumnDoesNotExist::new($columnName, $foreignTable->getName());
} }
} }
} }
foreach ($localColumnNames as $columnName) { foreach ($localColumnNames as $columnName) {
if (! $this->hasColumn($columnName)) { if (! $this->hasColumn($columnName)) {
throw SchemaException::columnDoesNotExist($columnName, $this->_name); throw ColumnDoesNotExist::new($columnName, $this->_name);
} }
} }
...@@ -474,7 +482,7 @@ class Table extends AbstractAsset ...@@ -474,7 +482,7 @@ class Table extends AbstractAsset
$constraintName = $this->normalizeIdentifier($constraintName); $constraintName = $this->normalizeIdentifier($constraintName);
if (! $this->hasForeignKey($constraintName)) { if (! $this->hasForeignKey($constraintName)) {
throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name); throw ForeignKeyDoesNotExist::new($constraintName, $this->_name);
} }
return $this->_fkConstraints[$constraintName]; return $this->_fkConstraints[$constraintName];
...@@ -494,7 +502,7 @@ class Table extends AbstractAsset ...@@ -494,7 +502,7 @@ class Table extends AbstractAsset
$constraintName = $this->normalizeIdentifier($constraintName); $constraintName = $this->normalizeIdentifier($constraintName);
if (! $this->hasForeignKey($constraintName)) { if (! $this->hasForeignKey($constraintName)) {
throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name); throw ForeignKeyDoesNotExist::new($constraintName, $this->_name);
} }
unset($this->_fkConstraints[$constraintName]); unset($this->_fkConstraints[$constraintName]);
...@@ -528,7 +536,7 @@ class Table extends AbstractAsset ...@@ -528,7 +536,7 @@ class Table extends AbstractAsset
$constraintName = $this->normalizeIdentifier($constraintName); $constraintName = $this->normalizeIdentifier($constraintName);
if (! $this->hasUniqueConstraint($constraintName)) { if (! $this->hasUniqueConstraint($constraintName)) {
throw SchemaException::uniqueConstraintDoesNotExist($constraintName, $this->_name); throw UniqueConstraintDoesNotExist::new($constraintName, $this->_name);
} }
return $this->_uniqueConstraints[$constraintName]; return $this->_uniqueConstraints[$constraintName];
...@@ -548,7 +556,7 @@ class Table extends AbstractAsset ...@@ -548,7 +556,7 @@ class Table extends AbstractAsset
$constraintName = $this->normalizeIdentifier($constraintName); $constraintName = $this->normalizeIdentifier($constraintName);
if (! $this->hasUniqueConstraint($constraintName)) { if (! $this->hasUniqueConstraint($constraintName)) {
throw SchemaException::uniqueConstraintDoesNotExist($constraintName, $this->_name); throw UniqueConstraintDoesNotExist::new($constraintName, $this->_name);
} }
unset($this->_uniqueConstraints[$constraintName]); unset($this->_uniqueConstraints[$constraintName]);
...@@ -613,7 +621,7 @@ class Table extends AbstractAsset ...@@ -613,7 +621,7 @@ class Table extends AbstractAsset
$columnName = $this->normalizeIdentifier($columnName); $columnName = $this->normalizeIdentifier($columnName);
if (! $this->hasColumn($columnName)) { if (! $this->hasColumn($columnName)) {
throw SchemaException::columnDoesNotExist($columnName, $this->_name); throw ColumnDoesNotExist::new($columnName, $this->_name);
} }
return $this->_columns[$columnName]; return $this->_columns[$columnName];
...@@ -687,7 +695,7 @@ class Table extends AbstractAsset ...@@ -687,7 +695,7 @@ class Table extends AbstractAsset
$indexName = $this->normalizeIdentifier($indexName); $indexName = $this->normalizeIdentifier($indexName);
if (! $this->hasIndex($indexName)) { if (! $this->hasIndex($indexName)) {
throw SchemaException::indexDoesNotExist($indexName, $this->_name); throw IndexDoesNotExist::new($indexName, $this->_name);
} }
return $this->_indexes[$indexName]; return $this->_indexes[$indexName];
...@@ -811,7 +819,7 @@ class Table extends AbstractAsset ...@@ -811,7 +819,7 @@ class Table extends AbstractAsset
$columnName = $this->normalizeIdentifier($columnName); $columnName = $this->normalizeIdentifier($columnName);
if (isset($this->_columns[$columnName])) { if (isset($this->_columns[$columnName])) {
throw SchemaException::columnAlreadyExists($this->getName(), $columnName); throw ColumnAlreadyExists::new($this->getName(), $columnName);
} }
$this->_columns[$columnName] = $column; $this->_columns[$columnName] = $column;
...@@ -841,7 +849,7 @@ class Table extends AbstractAsset ...@@ -841,7 +849,7 @@ class Table extends AbstractAsset
if ((isset($this->_indexes[$indexName]) && ! in_array($indexName, $replacedImplicitIndexes, true)) || if ((isset($this->_indexes[$indexName]) && ! in_array($indexName, $replacedImplicitIndexes, true)) ||
($this->_primaryKeyName !== false && $indexCandidate->isPrimary()) ($this->_primaryKeyName !== false && $indexCandidate->isPrimary())
) { ) {
throw SchemaException::indexAlreadyExists($indexName, $this->_name); throw IndexAlreadyExists::new($indexName, $this->_name);
} }
foreach ($replacedImplicitIndexes as $name) { foreach ($replacedImplicitIndexes as $name) {
...@@ -983,7 +991,7 @@ class Table extends AbstractAsset ...@@ -983,7 +991,7 @@ class Table extends AbstractAsset
private function _createUniqueConstraint(array $columns, $indexName, array $flags = [], array $options = []) private function _createUniqueConstraint(array $columns, $indexName, array $flags = [], array $options = [])
{ {
if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) { if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) {
throw SchemaException::indexNameInvalid($indexName); throw IndexNameInvalid::new($indexName);
} }
foreach ($columns as $index => $value) { foreach ($columns as $index => $value) {
...@@ -994,7 +1002,7 @@ class Table extends AbstractAsset ...@@ -994,7 +1002,7 @@ class Table extends AbstractAsset
} }
if (! $this->hasColumn($columnName)) { if (! $this->hasColumn($columnName)) {
throw SchemaException::columnDoesNotExist($columnName, $this->_name); throw ColumnDoesNotExist::new($columnName, $this->_name);
} }
} }
...@@ -1016,7 +1024,7 @@ class Table extends AbstractAsset ...@@ -1016,7 +1024,7 @@ class Table extends AbstractAsset
private function _createIndex(array $columns, $indexName, $isUnique, $isPrimary, array $flags = [], array $options = []) private function _createIndex(array $columns, $indexName, $isUnique, $isPrimary, array $flags = [], array $options = [])
{ {
if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) { if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) {
throw SchemaException::indexNameInvalid($indexName); throw IndexNameInvalid::new($indexName);
} }
foreach ($columns as $index => $value) { foreach ($columns as $index => $value) {
...@@ -1027,7 +1035,7 @@ class Table extends AbstractAsset ...@@ -1027,7 +1035,7 @@ class Table extends AbstractAsset
} }
if (! $this->hasColumn($columnName)) { if (! $this->hasColumn($columnName)) {
throw SchemaException::columnDoesNotExist($columnName, $this->_name); throw ColumnDoesNotExist::new($columnName, $this->_name);
} }
} }
......
...@@ -5,8 +5,8 @@ declare(strict_types=1); ...@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Schema\Visitor; namespace Doctrine\DBAL\Schema\Visitor;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Exception\NamedForeignKeyRequired;
use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use SplObjectStorage; use SplObjectStorage;
...@@ -49,7 +49,7 @@ class DropSchemaSqlCollector extends AbstractVisitor ...@@ -49,7 +49,7 @@ class DropSchemaSqlCollector extends AbstractVisitor
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
{ {
if (strlen($fkConstraint->getName()) === 0) { if (strlen($fkConstraint->getName()) === 0) {
throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint); throw NamedForeignKeyRequired::new($localTable, $fkConstraint);
} }
$this->constraints->attach($fkConstraint, $localTable); $this->constraints->attach($fkConstraint, $localTable);
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Sharding\Exception;
use Doctrine\DBAL\Sharding\ShardingException;
final class ActiveTransaction extends ShardingException
{
public static function new() : self
{
return new self('Cannot switch shard during an active transaction.', 1332141766);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Sharding\Exception;
use Doctrine\DBAL\Sharding\ShardingException;
final class MissingDefaultDistributionKey extends ShardingException
{
public static function new() : self
{
return new self('SQLAzure requires a distribution key to be set during sharding configuration.', 1332141329);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Sharding\Exception;
use Doctrine\DBAL\Sharding\ShardingException;
final class MissingDefaultFederationName extends ShardingException
{
public static function new() : self
{
return new self('SQLAzure requires a federation name to be set during sharding configuration.', 1332141280);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Sharding\Exception;
use Doctrine\DBAL\Sharding\ShardingException;
final class MissingDistributionType extends ShardingException
{
public static function new() : self
{
return new self("You have to specify a sharding distribution type such as 'integer', 'string', 'guid'.");
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Sharding\Exception;
use Doctrine\DBAL\Sharding\ShardingException;
final class NoShardDistributionValue extends ShardingException
{
public static function new() : self
{
return new self('You have to specify a string or integer as shard distribution value.', 1332142103);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Sharding\Exception;
use Doctrine\DBAL\Sharding\ShardingException;
final class NotImplemented extends ShardingException
{
public static function new() : self
{
return new self('This functionality is not implemented with this sharding provider.', 1331557937);
}
}
...@@ -5,6 +5,10 @@ declare(strict_types=1); ...@@ -5,6 +5,10 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Sharding\SQLAzure; namespace Doctrine\DBAL\Sharding\SQLAzure;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Sharding\Exception\ActiveTransaction;
use Doctrine\DBAL\Sharding\Exception\MissingDefaultDistributionKey;
use Doctrine\DBAL\Sharding\Exception\MissingDefaultFederationName;
use Doctrine\DBAL\Sharding\Exception\MissingDistributionType;
use Doctrine\DBAL\Sharding\ShardingException; use Doctrine\DBAL\Sharding\ShardingException;
use Doctrine\DBAL\Sharding\ShardManager; use Doctrine\DBAL\Sharding\ShardManager;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
...@@ -43,15 +47,15 @@ class SQLAzureShardManager implements ShardManager ...@@ -43,15 +47,15 @@ class SQLAzureShardManager implements ShardManager
$params = $conn->getParams(); $params = $conn->getParams();
if (! isset($params['sharding']['federationName'])) { if (! isset($params['sharding']['federationName'])) {
throw ShardingException::missingDefaultFederationName(); throw MissingDefaultFederationName::new();
} }
if (! isset($params['sharding']['distributionKey'])) { if (! isset($params['sharding']['distributionKey'])) {
throw ShardingException::missingDefaultDistributionKey(); throw MissingDefaultDistributionKey::new();
} }
if (! isset($params['sharding']['distributionType'])) { if (! isset($params['sharding']['distributionType'])) {
throw ShardingException::missingDistributionType(); throw MissingDistributionType::new();
} }
$this->federationName = $params['sharding']['federationName']; $this->federationName = $params['sharding']['federationName'];
...@@ -108,7 +112,7 @@ class SQLAzureShardManager implements ShardManager ...@@ -108,7 +112,7 @@ class SQLAzureShardManager implements ShardManager
public function selectGlobal() public function selectGlobal()
{ {
if ($this->conn->isTransactionActive()) { if ($this->conn->isTransactionActive()) {
throw ShardingException::activeTransaction(); throw ActiveTransaction::new();
} }
$sql = 'USE FEDERATION ROOT WITH RESET'; $sql = 'USE FEDERATION ROOT WITH RESET';
...@@ -122,7 +126,7 @@ class SQLAzureShardManager implements ShardManager ...@@ -122,7 +126,7 @@ class SQLAzureShardManager implements ShardManager
public function selectShard($distributionValue) public function selectShard($distributionValue)
{ {
if ($this->conn->isTransactionActive()) { if ($this->conn->isTransactionActive()) {
throw ShardingException::activeTransaction(); throw ActiveTransaction::new();
} }
$platform = $this->conn->getDatabasePlatform(); $platform = $this->conn->getDatabasePlatform();
......
...@@ -11,51 +11,4 @@ use Doctrine\DBAL\DBALException; ...@@ -11,51 +11,4 @@ use Doctrine\DBAL\DBALException;
*/ */
class ShardingException extends DBALException class ShardingException extends DBALException
{ {
/**
* @return \Doctrine\DBAL\Sharding\ShardingException
*/
public static function notImplemented()
{
return new self('This functionality is not implemented with this sharding provider.', 1331557937);
}
/**
* @return \Doctrine\DBAL\Sharding\ShardingException
*/
public static function missingDefaultFederationName()
{
return new self('SQLAzure requires a federation name to be set during sharding configuration.', 1332141280);
}
/**
* @return \Doctrine\DBAL\Sharding\ShardingException
*/
public static function missingDefaultDistributionKey()
{
return new self('SQLAzure requires a distribution key to be set during sharding configuration.', 1332141329);
}
/**
* @return \Doctrine\DBAL\Sharding\ShardingException
*/
public static function activeTransaction()
{
return new self('Cannot switch shard during an active transaction.', 1332141766);
}
/**
* @return \Doctrine\DBAL\Sharding\ShardingException
*/
public static function noShardDistributionValue()
{
return new self('You have to specify a string or integer as shard distribution value.', 1332142103);
}
/**
* @return \Doctrine\DBAL\Sharding\ShardingException
*/
public static function missingDistributionType()
{
return new self("You have to specify a sharding distribution type such as 'integer', 'string', 'guid'.");
}
} }
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Types; namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use function is_resource; use function is_resource;
use function restore_error_handler; use function restore_error_handler;
use function serialize; use function serialize;
...@@ -45,8 +46,8 @@ class ArrayType extends Type ...@@ -45,8 +46,8 @@ class ArrayType extends Type
$value = is_resource($value) ? stream_get_contents($value) : $value; $value = is_resource($value) ? stream_get_contents($value) : $value;
set_error_handler(function (int $code, string $message) : bool { set_error_handler(function (int $code, string $message) use ($value) : bool {
throw ConversionException::conversionFailedUnserialization($this->getName(), $message); throw ValueNotConvertible::new($value, $this->getName(), $message);
}); });
try { try {
......
...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Types; ...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use function is_resource; use function is_resource;
use function is_string; use function is_string;
use function stream_get_contents; use function stream_get_contents;
...@@ -37,7 +38,7 @@ class BinaryType extends Type ...@@ -37,7 +38,7 @@ class BinaryType extends Type
} }
if (! is_string($value)) { if (! is_string($value)) {
throw ConversionException::conversionFailed($value, Types::BINARY); throw ValueNotConvertible::new($value, self::BINARY);
} }
return $value; return $value;
......
...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Types; ...@@ -6,6 +6,7 @@ namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use function assert; use function assert;
use function fopen; use function fopen;
use function fseek; use function fseek;
...@@ -44,7 +45,7 @@ class BlobType extends Type ...@@ -44,7 +45,7 @@ class BlobType extends Type
} }
if (! is_resource($value)) { if (! is_resource($value)) {
throw ConversionException::conversionFailed($value, Types::BLOB); throw ValueNotConvertible::new($value, self::BLOB);
} }
return $value; return $value;
......
...@@ -5,108 +5,10 @@ declare(strict_types=1); ...@@ -5,108 +5,10 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Types; namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Throwable;
use function get_class;
use function gettype;
use function implode;
use function is_object;
use function is_scalar;
use function is_string;
use function sprintf;
use function strlen;
use function substr;
/** /**
* Conversion Exception is thrown when the database to PHP conversion fails. * Conversion Exception is thrown when the database to PHP conversion fails.
*/ */
class ConversionException extends DBALException class ConversionException extends DBALException
{ {
/**
* Thrown when a Database to Doctrine Type Conversion fails.
*
* @param mixed $value
* @param string $toType
*
* @return \Doctrine\DBAL\Types\ConversionException
*/
public static function conversionFailed($value, $toType)
{
$value = is_string($value) && strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value;
return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType);
}
/**
* Thrown when a Database to Doctrine Type Conversion fails and we can make a statement
* about the expected format.
*
* @param mixed $value
* @param string $toType
* @param string $expectedFormat
*
* @return \Doctrine\DBAL\Types\ConversionException
*/
public static function conversionFailedFormat($value, $toType, $expectedFormat, ?Throwable $previous = null)
{
$value = is_string($value) && strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value;
return new self(
'Could not convert database value "' . $value . '" to Doctrine Type ' .
$toType . '. Expected format: ' . $expectedFormat,
0,
$previous
);
}
/**
* Thrown when the PHP value passed to the converter was not of the expected type.
*
* @param mixed $value
* @param string $toType
* @param string[] $possibleTypes
*
* @return \Doctrine\DBAL\Types\ConversionException
*/
public static function conversionFailedInvalidType($value, $toType, array $possibleTypes)
{
$actualType = is_object($value) ? get_class($value) : gettype($value);
if (is_scalar($value)) {
return new self(sprintf(
"Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s",
$value,
$actualType,
$toType,
implode(', ', $possibleTypes)
));
}
return new self(sprintf(
"Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
$actualType,
$toType,
implode(', ', $possibleTypes)
));
}
public static function conversionFailedSerialization($value, $format, $error)
{
$actualType = is_object($value) ? get_class($value) : gettype($value);
return new self(sprintf(
"Could not convert PHP type '%s' to '%s', as an '%s' error was triggered by the serialization",
$actualType,
$format,
$error
));
}
public static function conversionFailedUnserialization(string $format, string $error) : self
{
return new self(sprintf(
"Could not convert database value to '%s' as an error was triggered by the unserialization: '%s'",
$format,
$error
));
}
} }
...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types; ...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
/** /**
* Immutable type of {@see DateType}. * Immutable type of {@see DateType}.
...@@ -33,7 +35,7 @@ class DateImmutableType extends DateType ...@@ -33,7 +35,7 @@ class DateImmutableType extends DateType
return $value->format($platform->getDateFormatString()); return $value->format($platform->getDateFormatString());
} }
throw ConversionException::conversionFailedInvalidType( throw InvalidType::new(
$value, $value,
$this->getName(), $this->getName(),
['null', DateTimeImmutable::class] ['null', DateTimeImmutable::class]
...@@ -52,7 +54,7 @@ class DateImmutableType extends DateType ...@@ -52,7 +54,7 @@ class DateImmutableType extends DateType
$dateTime = DateTimeImmutable::createFromFormat('!' . $platform->getDateFormatString(), $value); $dateTime = DateTimeImmutable::createFromFormat('!' . $platform->getDateFormatString(), $value);
if (! $dateTime) { if (! $dateTime) {
throw ConversionException::conversionFailedFormat( throw InvalidFormat::new(
$value, $value,
$this->getName(), $this->getName(),
$platform->getDateFormatString() $platform->getDateFormatString()
......
...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types; ...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types;
use DateInterval; use DateInterval;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Throwable; use Throwable;
use function substr; use function substr;
...@@ -47,7 +49,7 @@ class DateIntervalType extends Type ...@@ -47,7 +49,7 @@ class DateIntervalType extends Type
return $value->format(self::FORMAT); return $value->format(self::FORMAT);
} }
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateInterval']); throw InvalidType::new($value, $this->getName(), ['null', 'DateInterval']);
} }
/** /**
...@@ -75,7 +77,7 @@ class DateIntervalType extends Type ...@@ -75,7 +77,7 @@ class DateIntervalType extends Type
return $interval; return $interval;
} catch (Throwable $exception) { } catch (Throwable $exception) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), self::FORMAT, $exception); throw InvalidFormat::new($value, $this->getName(), self::FORMAT, $exception);
} }
} }
......
...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types; ...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
use function date_create_immutable; use function date_create_immutable;
/** /**
...@@ -34,7 +36,7 @@ class DateTimeImmutableType extends DateTimeType ...@@ -34,7 +36,7 @@ class DateTimeImmutableType extends DateTimeType
return $value->format($platform->getDateTimeFormatString()); return $value->format($platform->getDateTimeFormatString());
} }
throw ConversionException::conversionFailedInvalidType( throw InvalidType::new(
$value, $value,
$this->getName(), $this->getName(),
['null', DateTimeImmutable::class] ['null', DateTimeImmutable::class]
...@@ -57,7 +59,7 @@ class DateTimeImmutableType extends DateTimeType ...@@ -57,7 +59,7 @@ class DateTimeImmutableType extends DateTimeType
} }
if (! $dateTime) { if (! $dateTime) {
throw ConversionException::conversionFailedFormat( throw InvalidFormat::new(
$value, $value,
$this->getName(), $this->getName(),
$platform->getDateTimeFormatString() $platform->getDateTimeFormatString()
......
...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Types; ...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Types;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
use function date_create; use function date_create;
/** /**
...@@ -43,7 +45,7 @@ class DateTimeType extends Type implements PhpDateTimeMappingType ...@@ -43,7 +45,7 @@ class DateTimeType extends Type implements PhpDateTimeMappingType
return $value->format($platform->getDateTimeFormatString()); return $value->format($platform->getDateTimeFormatString());
} }
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']); throw InvalidType::new($value, $this->getName(), ['null', 'DateTime']);
} }
/** /**
...@@ -62,7 +64,7 @@ class DateTimeType extends Type implements PhpDateTimeMappingType ...@@ -62,7 +64,7 @@ class DateTimeType extends Type implements PhpDateTimeMappingType
} }
if (! $val) { if (! $val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString()); throw InvalidFormat::new($value, $this->getName(), $platform->getDateTimeFormatString());
} }
return $val; return $val;
......
...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types; ...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
/** /**
* Immutable type of {@see DateTimeTzType}. * Immutable type of {@see DateTimeTzType}.
...@@ -33,7 +35,7 @@ class DateTimeTzImmutableType extends DateTimeTzType ...@@ -33,7 +35,7 @@ class DateTimeTzImmutableType extends DateTimeTzType
return $value->format($platform->getDateTimeTzFormatString()); return $value->format($platform->getDateTimeTzFormatString());
} }
throw ConversionException::conversionFailedInvalidType( throw InvalidType::new(
$value, $value,
$this->getName(), $this->getName(),
['null', DateTimeImmutable::class] ['null', DateTimeImmutable::class]
...@@ -52,7 +54,7 @@ class DateTimeTzImmutableType extends DateTimeTzType ...@@ -52,7 +54,7 @@ class DateTimeTzImmutableType extends DateTimeTzType
$dateTime = DateTimeImmutable::createFromFormat($platform->getDateTimeTzFormatString(), $value); $dateTime = DateTimeImmutable::createFromFormat($platform->getDateTimeTzFormatString(), $value);
if (! $dateTime) { if (! $dateTime) {
throw ConversionException::conversionFailedFormat( throw InvalidFormat::new(
$value, $value,
$this->getName(), $this->getName(),
$platform->getDateTimeTzFormatString() $platform->getDateTimeTzFormatString()
......
...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Types; ...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Types;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
/** /**
* DateTime type saving additional timezone information. * DateTime type saving additional timezone information.
...@@ -55,7 +57,7 @@ class DateTimeTzType extends Type implements PhpDateTimeMappingType ...@@ -55,7 +57,7 @@ class DateTimeTzType extends Type implements PhpDateTimeMappingType
return $value->format($platform->getDateTimeTzFormatString()); return $value->format($platform->getDateTimeTzFormatString());
} }
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']); throw InvalidType::new($value, $this->getName(), ['null', 'DateTime']);
} }
/** /**
...@@ -69,7 +71,7 @@ class DateTimeTzType extends Type implements PhpDateTimeMappingType ...@@ -69,7 +71,7 @@ class DateTimeTzType extends Type implements PhpDateTimeMappingType
$val = DateTime::createFromFormat($platform->getDateTimeTzFormatString(), $value); $val = DateTime::createFromFormat($platform->getDateTimeTzFormatString(), $value);
if (! $val) { if (! $val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeTzFormatString()); throw InvalidFormat::new($value, $this->getName(), $platform->getDateTimeTzFormatString());
} }
return $val; return $val;
......
...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Types; ...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Types;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
/** /**
* Type that maps an SQL DATE to a PHP Date object. * Type that maps an SQL DATE to a PHP Date object.
...@@ -42,7 +44,7 @@ class DateType extends Type ...@@ -42,7 +44,7 @@ class DateType extends Type
return $value->format($platform->getDateFormatString()); return $value->format($platform->getDateFormatString());
} }
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']); throw InvalidType::new($value, $this->getName(), ['null', 'DateTime']);
} }
/** /**
...@@ -56,7 +58,7 @@ class DateType extends Type ...@@ -56,7 +58,7 @@ class DateType extends Type
$val = DateTime::createFromFormat('!' . $platform->getDateFormatString(), $value); $val = DateTime::createFromFormat('!' . $platform->getDateFormatString(), $value);
if (! $val) { if (! $val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateFormatString()); throw InvalidFormat::new($value, $this->getName(), $platform->getDateFormatString());
} }
return $val; return $val;
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\Types\ConversionException;
use Throwable;
use function sprintf;
use function strlen;
use function substr;
/**
* Thrown when a Database to Doctrine Type Conversion fails and we can make a statement
* about the expected format.
*/
final class InvalidFormat extends ConversionException implements TypesException
{
public static function new(
string $value,
string $toType,
?string $expectedFormat,
?Throwable $previous = null
) : self {
return new self(
sprintf(
'Could not convert database value "%s" to Doctrine Type %s. Expected format: %s',
strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value,
$toType,
$expectedFormat ?? ''
),
0,
$previous
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\Types\ConversionException;
use function get_class;
use function gettype;
use function implode;
use function is_object;
use function is_scalar;
use function sprintf;
/**
* Thrown when the PHP value passed to the converter was not of the expected type.
*/
final class InvalidType extends ConversionException implements TypesException
{
/**
* @param mixed $value
* @param string[] $possibleTypes
*
* @todo split into two methods
* @todo sanitize value
*/
public static function new($value, string $toType, array $possibleTypes) : self
{
$actualType = is_object($value) ? get_class($value) : gettype($value);
if (is_scalar($value)) {
return new self(
sprintf(
"Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s",
$value,
$actualType,
$toType,
implode(', ', $possibleTypes)
)
);
}
return new self(
sprintf(
"Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
$actualType,
$toType,
implode(', ', $possibleTypes)
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\Types\ConversionException;
use function get_class;
use function gettype;
use function is_object;
use function sprintf;
final class SerializationFailed extends ConversionException implements TypesException
{
/**
* @param mixed $value
*/
public static function new($value, string $format, string $error) : self
{
$actualType = is_object($value) ? get_class($value) : gettype($value);
return new self(
sprintf(
"Could not convert PHP type '%s' to '%s', as an '%s' error was triggered by the serialization",
$actualType,
$format,
$error
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Types\Type;
use function get_class;
use function spl_object_hash;
use function sprintf;
final class TypeAlreadyRegistered extends DBALException implements TypesException
{
public static function new(Type $type) : self
{
return new self(sprintf(
'Type of the class %s@%s is already registered.',
get_class($type),
spl_object_hash($type)
));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class TypeNotFound extends DBALException implements TypesException
{
public static function new(string $name) : self
{
return new self(sprintf('Type to be overwritten %s does not exist.', $name));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Types\Type;
use function get_class;
use function spl_object_hash;
use function sprintf;
final class TypeNotRegistered extends DBALException implements TypesException
{
public static function new(Type $type) : self
{
return new self(sprintf('Type of the class %s@%s is not registered.', get_class($type), spl_object_hash($type)));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class TypesAlreadyExists extends DBALException implements TypesException
{
public static function new(string $name) : self
{
return new self(sprintf('Type %s already exists.', $name));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
interface TypesException
{
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\DBALException;
use function sprintf;
final class UnknownColumnType extends DBALException implements TypesException
{
public static function new(string $name) : self
{
return new self(
sprintf(
'Unknown column type "%s" requested. Any Doctrine type that you use has '
. 'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the '
. 'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database '
. 'introspection then you might have forgotten to register all database types for a Doctrine Type. '
. 'Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement '
. 'Type#getMappedDatabaseTypes(). If the type name is empty you might '
. 'have a problem with the cache or forgot some mapping information.',
$name
)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types\Exception;
use Doctrine\DBAL\Types\ConversionException;
use function is_string;
use function sprintf;
use function strlen;
use function substr;
/**
* Thrown when a Database to Doctrine Type Conversion fails.
*/
final class ValueNotConvertible extends ConversionException implements TypesException
{
public static function new($value, $toType, ?string $message = null) : self
{
if ($message !== null) {
return new self(
sprintf(
"Could not convert database value to '%s' as an error was triggered by the unserialization: '%s'",
$toType,
$message
)
);
}
return new self(
sprintf(
'Could not convert database value "%s" to Doctrine Type %s',
is_string($value) && strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value,
$toType
)
);
}
}
...@@ -5,6 +5,8 @@ declare(strict_types=1); ...@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Types; namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\SerializationFailed;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use const JSON_ERROR_NONE; use const JSON_ERROR_NONE;
use function is_resource; use function is_resource;
use function json_decode; use function json_decode;
...@@ -38,7 +40,7 @@ class JsonType extends Type ...@@ -38,7 +40,7 @@ class JsonType extends Type
$encoded = json_encode($value); $encoded = json_encode($value);
if (json_last_error() !== JSON_ERROR_NONE) { if (json_last_error() !== JSON_ERROR_NONE) {
throw ConversionException::conversionFailedSerialization($value, 'json', json_last_error_msg()); throw SerializationFailed::new($value, 'json', json_last_error_msg());
} }
return $encoded; return $encoded;
...@@ -60,7 +62,7 @@ class JsonType extends Type ...@@ -60,7 +62,7 @@ class JsonType extends Type
$val = json_decode($value, true); $val = json_decode($value, true);
if (json_last_error() !== JSON_ERROR_NONE) { if (json_last_error() !== JSON_ERROR_NONE) {
throw ConversionException::conversionFailed($value, $this->getName()); throw ValueNotConvertible::new($value, $this->getName());
} }
return $val; return $val;
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Types; namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use function is_resource; use function is_resource;
use function restore_error_handler; use function restore_error_handler;
use function serialize; use function serialize;
...@@ -44,8 +45,8 @@ class ObjectType extends Type ...@@ -44,8 +45,8 @@ class ObjectType extends Type
$value = is_resource($value) ? stream_get_contents($value) : $value; $value = is_resource($value) ? stream_get_contents($value) : $value;
set_error_handler(function (int $code, string $message) : bool { set_error_handler(function (int $code, string $message) use ($value) : bool {
throw ConversionException::conversionFailedUnserialization($this->getName(), $message); throw ValueNotConvertible::new($value, $this->getName(), $message);
}); });
try { try {
......
...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types; ...@@ -6,6 +6,8 @@ namespace Doctrine\DBAL\Types;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
/** /**
* Immutable type of {@see TimeType}. * Immutable type of {@see TimeType}.
...@@ -33,7 +35,7 @@ class TimeImmutableType extends TimeType ...@@ -33,7 +35,7 @@ class TimeImmutableType extends TimeType
return $value->format($platform->getTimeFormatString()); return $value->format($platform->getTimeFormatString());
} }
throw ConversionException::conversionFailedInvalidType( throw InvalidType::new(
$value, $value,
$this->getName(), $this->getName(),
['null', DateTimeImmutable::class] ['null', DateTimeImmutable::class]
...@@ -52,7 +54,7 @@ class TimeImmutableType extends TimeType ...@@ -52,7 +54,7 @@ class TimeImmutableType extends TimeType
$dateTime = DateTimeImmutable::createFromFormat('!' . $platform->getTimeFormatString(), $value); $dateTime = DateTimeImmutable::createFromFormat('!' . $platform->getTimeFormatString(), $value);
if (! $dateTime) { if (! $dateTime) {
throw ConversionException::conversionFailedFormat( throw InvalidFormat::new(
$value, $value,
$this->getName(), $this->getName(),
$platform->getTimeFormatString() $platform->getTimeFormatString()
......
...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Types; ...@@ -7,6 +7,8 @@ namespace Doctrine\DBAL\Types;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
/** /**
* Type that maps an SQL TIME to a PHP DateTime object. * Type that maps an SQL TIME to a PHP DateTime object.
...@@ -42,7 +44,7 @@ class TimeType extends Type ...@@ -42,7 +44,7 @@ class TimeType extends Type
return $value->format($platform->getTimeFormatString()); return $value->format($platform->getTimeFormatString());
} }
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']); throw InvalidType::new($value, $this->getName(), ['null', 'DateTime']);
} }
/** /**
...@@ -56,7 +58,7 @@ class TimeType extends Type ...@@ -56,7 +58,7 @@ class TimeType extends Type
$val = DateTime::createFromFormat('!' . $platform->getTimeFormatString(), $value); $val = DateTime::createFromFormat('!' . $platform->getTimeFormatString(), $value);
if (! $val) { if (! $val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getTimeFormatString()); throw InvalidFormat::new($value, $this->getName(), $platform->getTimeFormatString());
} }
return $val; return $val;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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