vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php line 166

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL;
  3. use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
  4. use Doctrine\DBAL\Driver\ExceptionConverterDriver;
  5. use Doctrine\DBAL\Exception\DriverException;
  6. use Doctrine\DBAL\Platforms\AbstractPlatform;
  7. use Doctrine\DBAL\Types\Type;
  8. use Exception;
  9. use Throwable;
  10. use function array_map;
  11. use function bin2hex;
  12. use function get_class;
  13. use function gettype;
  14. use function implode;
  15. use function is_object;
  16. use function is_resource;
  17. use function is_string;
  18. use function json_encode;
  19. use function preg_replace;
  20. use function spl_object_hash;
  21. use function sprintf;
  22. class DBALException extends Exception
  23. {
  24.     /**
  25.      * @param string $method
  26.      *
  27.      * @return \Doctrine\DBAL\DBALException
  28.      */
  29.     public static function notSupported($method)
  30.     {
  31.         return new self(sprintf("Operation '%s' is not supported by platform."$method));
  32.     }
  33.     public static function invalidPlatformSpecified() : self
  34.     {
  35.         return new self(
  36.             "Invalid 'platform' option specified, need to give an instance of " AbstractPlatform::class . '.'
  37.         );
  38.     }
  39.     /**
  40.      * @param mixed $invalidPlatform
  41.      */
  42.     public static function invalidPlatformType($invalidPlatform) : self
  43.     {
  44.         if (is_object($invalidPlatform)) {
  45.             return new self(
  46.                 sprintf(
  47.                     "Option 'platform' must be a subtype of '%s', instance of '%s' given",
  48.                     AbstractPlatform::class,
  49.                     get_class($invalidPlatform)
  50.                 )
  51.             );
  52.         }
  53.         return new self(
  54.             sprintf(
  55.                 "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
  56.                 AbstractPlatform::class,
  57.                 gettype($invalidPlatform)
  58.             )
  59.         );
  60.     }
  61.     /**
  62.      * Returns a new instance for an invalid specified platform version.
  63.      *
  64.      * @param string $version        The invalid platform version given.
  65.      * @param string $expectedFormat The expected platform version format.
  66.      *
  67.      * @return DBALException
  68.      */
  69.     public static function invalidPlatformVersionSpecified($version$expectedFormat)
  70.     {
  71.         return new self(
  72.             sprintf(
  73.                 'Invalid platform version "%s" specified. ' .
  74.                 'The platform version has to be specified in the format: "%s".',
  75.                 $version,
  76.                 $expectedFormat
  77.             )
  78.         );
  79.     }
  80.     /**
  81.      * @return \Doctrine\DBAL\DBALException
  82.      */
  83.     public static function invalidPdoInstance()
  84.     {
  85.         return new self(
  86.             "The 'pdo' option was used in DriverManager::getConnection() but no " .
  87.             'instance of PDO was given.'
  88.         );
  89.     }
  90.     /**
  91.      * @param string|null $url The URL that was provided in the connection parameters (if any).
  92.      *
  93.      * @return \Doctrine\DBAL\DBALException
  94.      */
  95.     public static function driverRequired($url null)
  96.     {
  97.         if ($url) {
  98.             return new self(
  99.                 sprintf(
  100.                     "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
  101.                     'is given to DriverManager::getConnection(). Given URL: %s',
  102.                     $url
  103.                 )
  104.             );
  105.         }
  106.         return new self("The options 'driver' or 'driverClass' are mandatory if no PDO " .
  107.             'instance is given to DriverManager::getConnection().');
  108.     }
  109.     /**
  110.      * @param string   $unknownDriverName
  111.      * @param string[] $knownDrivers
  112.      *
  113.      * @return \Doctrine\DBAL\DBALException
  114.      */
  115.     public static function unknownDriver($unknownDriverName, array $knownDrivers)
  116.     {
  117.         return new self("The given 'driver' " $unknownDriverName ' is unknown, ' .
  118.             'Doctrine currently supports only the following drivers: ' implode(', '$knownDrivers));
  119.     }
  120.     /**
  121.      * @param string  $sql
  122.      * @param mixed[] $params
  123.      *
  124.      * @return self
  125.      */
  126.     public static function driverExceptionDuringQuery(Driver $driverThrowable $driverEx$sql, array $params = [])
  127.     {
  128.         $msg "An exception occurred while executing '" $sql "'";
  129.         if ($params) {
  130.             $msg .= ' with params ' self::formatParameters($params);
  131.         }
  132.         $msg .= ":\n\n" $driverEx->getMessage();
  133.         return static::wrapException($driver$driverEx$msg);
  134.     }
  135.     /**
  136.      * @return self
  137.      */
  138.     public static function driverException(Driver $driverThrowable $driverEx)
  139.     {
  140.         return static::wrapException($driver$driverEx'An exception occurred in driver: ' $driverEx->getMessage());
  141.     }
  142.     /**
  143.      * @return self
  144.      */
  145.     private static function wrapException(Driver $driverThrowable $driverEx$msg)
  146.     {
  147.         if ($driverEx instanceof DriverException) {
  148.             return $driverEx;
  149.         }
  150.         if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
  151.             return $driver->convertException($msg$driverEx);
  152.         }
  153.         return new self($msg0$driverEx);
  154.     }
  155.     /**
  156.      * Returns a human-readable representation of an array of parameters.
  157.      * This properly handles binary data by returning a hex representation.
  158.      *
  159.      * @param mixed[] $params
  160.      *
  161.      * @return string
  162.      */
  163.     private static function formatParameters(array $params)
  164.     {
  165.         return '[' implode(', 'array_map(static function ($param) {
  166.             if (is_resource($param)) {
  167.                 return (string) $param;
  168.             }
  169.             $json = @json_encode($param);
  170.             if (! is_string($json) || $json === 'null' && is_string($param)) {
  171.                 // JSON encoding failed, this is not a UTF-8 string.
  172.                 return sprintf('"%s"'preg_replace('/.{2}/''\\x$0'bin2hex($param)));
  173.             }
  174.             return $json;
  175.         }, $params)) . ']';
  176.     }
  177.     /**
  178.      * @param string $wrapperClass
  179.      *
  180.      * @return \Doctrine\DBAL\DBALException
  181.      */
  182.     public static function invalidWrapperClass($wrapperClass)
  183.     {
  184.         return new self("The given 'wrapperClass' " $wrapperClass ' has to be a ' .
  185.             'subtype of \Doctrine\DBAL\Connection.');
  186.     }
  187.     /**
  188.      * @param string $driverClass
  189.      *
  190.      * @return \Doctrine\DBAL\DBALException
  191.      */
  192.     public static function invalidDriverClass($driverClass)
  193.     {
  194.         return new self("The given 'driverClass' " $driverClass ' has to implement the ' Driver::class . ' interface.');
  195.     }
  196.     /**
  197.      * @param string $tableName
  198.      *
  199.      * @return \Doctrine\DBAL\DBALException
  200.      */
  201.     public static function invalidTableName($tableName)
  202.     {
  203.         return new self('Invalid table name specified: ' $tableName);
  204.     }
  205.     /**
  206.      * @param string $tableName
  207.      *
  208.      * @return \Doctrine\DBAL\DBALException
  209.      */
  210.     public static function noColumnsSpecifiedForTable($tableName)
  211.     {
  212.         return new self('No columns specified for table ' $tableName);
  213.     }
  214.     /**
  215.      * @return \Doctrine\DBAL\DBALException
  216.      */
  217.     public static function limitOffsetInvalid()
  218.     {
  219.         return new self('Invalid Offset in Limit Query, it has to be larger than or equal to 0.');
  220.     }
  221.     /**
  222.      * @param string $name
  223.      *
  224.      * @return \Doctrine\DBAL\DBALException
  225.      */
  226.     public static function typeExists($name)
  227.     {
  228.         return new self('Type ' $name ' already exists.');
  229.     }
  230.     /**
  231.      * @param string $name
  232.      *
  233.      * @return \Doctrine\DBAL\DBALException
  234.      */
  235.     public static function unknownColumnType($name)
  236.     {
  237.         return new self('Unknown column type "' $name '" requested. Any Doctrine type that you use has ' .
  238.             'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
  239.             'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
  240.             'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
  241.             'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
  242.             'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
  243.             'have a problem with the cache or forgot some mapping information.');
  244.     }
  245.     /**
  246.      * @param string $name
  247.      *
  248.      * @return \Doctrine\DBAL\DBALException
  249.      */
  250.     public static function typeNotFound($name)
  251.     {
  252.         return new self('Type to be overwritten ' $name ' does not exist.');
  253.     }
  254.     public static function typeNotRegistered(Type $type) : self
  255.     {
  256.         return new self(sprintf('Type of the class %s@%s is not registered.'get_class($type), spl_object_hash($type)));
  257.     }
  258.     public static function typeAlreadyRegistered(Type $type) : self
  259.     {
  260.         return new self(
  261.             sprintf('Type of the class %s@%s is already registered.'get_class($type), spl_object_hash($type))
  262.         );
  263.     }
  264. }