DBALException.php 8.68 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
use Doctrine\DBAL\Types\Type;
10 11
use Exception;
use Throwable;
12 13
use function array_map;
use function bin2hex;
14 15
use function get_class;
use function gettype;
16
use function implode;
17
use function is_object;
18 19 20
use function is_resource;
use function is_string;
use function json_encode;
Sergei Morozov's avatar
Sergei Morozov committed
21
use function preg_replace;
22
use function spl_object_hash;
23
use function sprintf;
24

25 26 27
/**
 * @psalm-immutable
 */
28
class DBALException extends Exception
29
{
Benjamin Morel's avatar
Benjamin Morel committed
30 31 32
    /**
     * @param string $method
     *
Grégoire Paris's avatar
Grégoire Paris committed
33
     * @return DBALException
Benjamin Morel's avatar
Benjamin Morel committed
34
     */
35 36
    public static function notSupported($method)
    {
37
        return new self(sprintf("Operation '%s' is not supported by platform.", $method));
38
    }
39

40 41 42
    public static function invalidPlatformSpecified() : self
    {
        return new self(
43
            "Invalid 'platform' option specified, need to give an instance of " . AbstractPlatform::class . '.'
44
        );
45 46
    }

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

62
        return new self(
63
            sprintf(
64 65
                "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
                AbstractPlatform::class,
66
                gettype($invalidPlatform)
67 68
            )
        );
69 70
    }

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    /**
     * 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
91
    /**
Grégoire Paris's avatar
Grégoire Paris committed
92
     * @return DBALException
Benjamin Morel's avatar
Benjamin Morel committed
93
     */
94 95 96
    public static function invalidPdoInstance()
    {
        return new self(
97 98
            "The 'pdo' option was used in DriverManager::getConnection() but no " .
            'instance of PDO was given.'
99 100 101
        );
    }

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

119 120
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO " .
            'instance is given to DriverManager::getConnection().');
121 122
    }

Benjamin Morel's avatar
Benjamin Morel committed
123
    /**
124 125
     * @param string   $unknownDriverName
     * @param string[] $knownDrivers
Benjamin Morel's avatar
Benjamin Morel committed
126
     *
Grégoire Paris's avatar
Grégoire Paris committed
127
     * @return DBALException
Benjamin Morel's avatar
Benjamin Morel committed
128
     */
129 130
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
    {
131 132
        return new self("The given 'driver' " . $unknownDriverName . ' is unknown, ' .
            'Doctrine currently supports only the following drivers: ' . implode(', ', $knownDrivers));
133
    }
134

Benjamin Morel's avatar
Benjamin Morel committed
135
    /**
Sergei Morozov's avatar
Sergei Morozov committed
136 137
     * @param string  $sql
     * @param mixed[] $params
Benjamin Morel's avatar
Benjamin Morel committed
138
     *
Sergei Morozov's avatar
Sergei Morozov committed
139
     * @return self
Benjamin Morel's avatar
Benjamin Morel committed
140
     */
141
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
142
    {
143
        $msg = "An exception occurred while executing '" . $sql . "'";
144
        if ($params) {
145
            $msg .= ' with params ' . self::formatParameters($params);
146
        }
Grégoire Paris's avatar
Grégoire Paris committed
147

148
        $msg .= ":\n\n" . $driverEx->getMessage();
149

150
        return static::wrapException($driver, $driverEx, $msg);
151 152
    }

153
    /**
Sergei Morozov's avatar
Sergei Morozov committed
154
     * @return self
155
     */
156
    public static function driverException(Driver $driver, Throwable $driverEx)
157
    {
158
        return static::wrapException($driver, $driverEx, 'An exception occurred in driver: ' . $driverEx->getMessage());
159
    }
160

161
    /**
Sergei Morozov's avatar
Sergei Morozov committed
162
     * @return self
163
     */
164
    private static function wrapException(Driver $driver, Throwable $driverEx, string $msg)
165
    {
166
        if ($driverEx instanceof DriverException) {
167 168
            return $driverEx;
        }
Grégoire Paris's avatar
Grégoire Paris committed
169

170
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
171
            return $driver->convertException($msg, $driverEx);
172
        }
173 174

        return new self($msg, 0, $driverEx);
175 176
    }

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

192 193
            $json = @json_encode($param);

194
            if (! is_string($json) || $json === 'null' && is_string($param)) {
195
                // JSON encoding failed, this is not a UTF-8 string.
Sergei Morozov's avatar
Sergei Morozov committed
196
                return sprintf('"%s"', preg_replace('/.{2}/', '\\x$0', bin2hex($param)));
197 198 199 200 201 202
            }

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

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

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

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
226
     *
Grégoire Paris's avatar
Grégoire Paris committed
227
     * @return DBALException
228 229 230
     */
    public static function invalidTableName($tableName)
    {
231
        return new self('Invalid table name specified: ' . $tableName);
232 233 234 235
    }

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
236
     *
Grégoire Paris's avatar
Grégoire Paris committed
237
     * @return DBALException
238 239 240
     */
    public static function noColumnsSpecifiedForTable($tableName)
    {
241
        return new self('No columns specified for table ' . $tableName);
242 243
    }

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

Benjamin Morel's avatar
Benjamin Morel committed
252 253 254
    /**
     * @param string $name
     *
Grégoire Paris's avatar
Grégoire Paris committed
255
     * @return DBALException
Benjamin Morel's avatar
Benjamin Morel committed
256
     */
257 258
    public static function typeExists($name)
    {
259
        return new self('Type ' . $name . ' already exists.');
260 261
    }

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

Benjamin Morel's avatar
Benjamin Morel committed
278 279 280
    /**
     * @param string $name
     *
Grégoire Paris's avatar
Grégoire Paris committed
281
     * @return DBALException
Benjamin Morel's avatar
Benjamin Morel committed
282
     */
283 284
    public static function typeNotFound($name)
    {
285
        return new self('Type to be overwritten ' . $name . ' does not exist.');
286
    }
287 288 289 290 291 292 293 294 295 296 297 298

    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))
        );
    }
299
}