DBALException.php 8.52 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL;

5
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
6
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
7
use Doctrine\DBAL\Exception\DriverException;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9 10
use Exception;
use Throwable;
11 12
use function array_map;
use function bin2hex;
13 14
use function get_class;
use function gettype;
15
use function implode;
16
use function is_object;
17 18 19 20 21
use function is_resource;
use function is_string;
use function json_encode;
use function sprintf;
use function str_split;
22

23
class DBALException extends Exception
24
{
Benjamin Morel's avatar
Benjamin Morel committed
25 26 27 28 29
    /**
     * @param string $method
     *
     * @return \Doctrine\DBAL\DBALException
     */
30 31
    public static function notSupported($method)
    {
32
        return new self(sprintf("Operation '%s' is not supported by platform.", $method));
33
    }
34

35 36 37
    public static function invalidPlatformSpecified() : self
    {
        return new self(
38
            "Invalid 'platform' option specified, need to give an instance of " . AbstractPlatform::class . '.'
39
        );
40 41
    }

Benjamin Morel's avatar
Benjamin Morel committed
42
    /**
43
     * @param mixed $invalidPlatform
Benjamin Morel's avatar
Benjamin Morel committed
44
     */
45
    public static function invalidPlatformType($invalidPlatform) : self
46
    {
47
        if (is_object($invalidPlatform)) {
48 49 50 51
            return new self(
                sprintf(
                    "Option 'platform' must be a subtype of '%s', instance of '%s' given",
                    AbstractPlatform::class,
52
                    get_class($invalidPlatform)
53 54 55 56
                )
            );
        }

57
        return new self(
58
            sprintf(
59 60
                "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
                AbstractPlatform::class,
61
                gettype($invalidPlatform)
62 63
            )
        );
64 65
    }

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    /**
     * 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
            )
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
86 87 88
    /**
     * @return \Doctrine\DBAL\DBALException
     */
89 90 91
    public static function invalidPdoInstance()
    {
        return new self(
92 93
            "The 'pdo' option was used in DriverManager::getConnection() but no " .
            'instance of PDO was given.'
94 95 96
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
97
    /**
98
     * @param string|null $url The URL that was provided in the connection parameters (if any).
99
     *
Benjamin Morel's avatar
Benjamin Morel committed
100 101
     * @return \Doctrine\DBAL\DBALException
     */
102
    public static function driverRequired($url = null)
103
    {
104 105 106 107
        if ($url) {
            return new self(
                sprintf(
                    "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
108
                    'is given to DriverManager::getConnection(). Given URL: %s',
109 110 111 112 113
                    $url
                )
            );
        }

114 115
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO " .
            'instance is given to DriverManager::getConnection().');
116 117
    }

Benjamin Morel's avatar
Benjamin Morel committed
118
    /**
119 120
     * @param string   $unknownDriverName
     * @param string[] $knownDrivers
Benjamin Morel's avatar
Benjamin Morel committed
121 122 123
     *
     * @return \Doctrine\DBAL\DBALException
     */
124 125
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
    {
126 127
        return new self("The given 'driver' " . $unknownDriverName . ' is unknown, ' .
            'Doctrine currently supports only the following drivers: ' . implode(', ', $knownDrivers));
128
    }
129

Benjamin Morel's avatar
Benjamin Morel committed
130
    /**
131 132
     * @param Exception $driverEx
     * @param string    $sql
133
     * @param mixed[]   $params
Benjamin Morel's avatar
Benjamin Morel committed
134 135 136
     *
     * @return \Doctrine\DBAL\DBALException
     */
137
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
138
    {
139
        $msg = "An exception occurred while executing '" . $sql . "'";
140
        if ($params) {
141
            $msg .= ' with params ' . self::formatParameters($params);
142
        }
143
        $msg .= ":\n\n" . $driverEx->getMessage();
144

145
        return static::wrapException($driver, $driverEx, $msg);
146 147
    }

148
    /**
149
     * @param Exception $driverEx
150 151 152
     *
     * @return \Doctrine\DBAL\DBALException
     */
153
    public static function driverException(Driver $driver, Throwable $driverEx)
154
    {
155
        return static::wrapException($driver, $driverEx, 'An exception occurred in driver: ' . $driverEx->getMessage());
156
    }
157

158
    /**
159
     * @param Exception $driverEx
160 161 162
     *
     * @return \Doctrine\DBAL\DBALException
     */
163
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
164
    {
165
        if ($driverEx instanceof DriverException) {
166 167
            return $driverEx;
        }
168
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
169
            return $driver->convertException($msg, $driverEx);
170
        }
171 172

        return new self($msg, 0, $driverEx);
173 174
    }

175 176 177 178
    /**
     * Returns a human-readable representation of an array of parameters.
     * This properly handles binary data by returning a hex representation.
     *
179
     * @param mixed[] $params
180 181 182 183 184
     *
     * @return string
     */
    private static function formatParameters(array $params)
    {
185
        return '[' . implode(', ', array_map(static function ($param) {
186 187 188
            if (is_resource($param)) {
                return (string) $param;
            }
189

190 191
            $json = @json_encode($param);

192
            if (! is_string($json) || $json === 'null' && is_string($param)) {
193 194 195 196 197 198 199 200
                // JSON encoding failed, this is not a UTF-8 string.
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
            }

            return $json;
        }, $params)) . ']';
    }

Benjamin Morel's avatar
Benjamin Morel committed
201 202 203 204 205
    /**
     * @param string $wrapperClass
     *
     * @return \Doctrine\DBAL\DBALException
     */
206 207
    public static function invalidWrapperClass($wrapperClass)
    {
208 209
        return new self("The given 'wrapperClass' " . $wrapperClass . ' has to be a ' .
            'subtype of \Doctrine\DBAL\Connection.');
210 211
    }

Benjamin Morel's avatar
Benjamin Morel committed
212 213 214 215 216
    /**
     * @param string $driverClass
     *
     * @return \Doctrine\DBAL\DBALException
     */
217 218
    public static function invalidDriverClass($driverClass)
    {
219
        return new self("The given 'driverClass' " . $driverClass . ' has to implement the ' . Driver::class . ' interface.');
220
    }
221 222 223

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
224 225
     *
     * @return \Doctrine\DBAL\DBALException
226 227 228
     */
    public static function invalidTableName($tableName)
    {
229
        return new self('Invalid table name specified: ' . $tableName);
230 231 232 233
    }

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
234 235
     *
     * @return \Doctrine\DBAL\DBALException
236 237 238
     */
    public static function noColumnsSpecifiedForTable($tableName)
    {
239
        return new self('No columns specified for table ' . $tableName);
240 241
    }

Benjamin Morel's avatar
Benjamin Morel committed
242 243 244
    /**
     * @return \Doctrine\DBAL\DBALException
     */
245 246
    public static function limitOffsetInvalid()
    {
247
        return new self('Invalid Offset in Limit Query, it has to be larger than or equal to 0.');
248
    }
249

Benjamin Morel's avatar
Benjamin Morel committed
250 251 252 253 254
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
255 256
    public static function typeExists($name)
    {
257
        return new self('Type ' . $name . ' already exists.');
258 259
    }

Benjamin Morel's avatar
Benjamin Morel committed
260 261 262 263 264
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
265 266
    public static function unknownColumnType($name)
    {
267
        return new self('Unknown column type "' . $name . '" requested. Any Doctrine type that you use has ' .
268
            'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
269
            'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
270
            'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
271 272
            'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
            'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
273
            'have a problem with the cache or forgot some mapping information.');
274 275
    }

Benjamin Morel's avatar
Benjamin Morel committed
276 277 278 279 280
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
281 282
    public static function typeNotFound($name)
    {
283
        return new self('Type to be overwritten ' . $name . ' does not exist.');
284
    }
285
}