DBALException.php 8.72 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 14
use function array_map;
use function bin2hex;
15
use function count;
16 17
use function get_class;
use function gettype;
18
use function implode;
19
use function is_object;
20 21 22
use function is_resource;
use function is_string;
use function json_encode;
Sergei Morozov's avatar
Sergei Morozov committed
23
use function preg_replace;
24
use function spl_object_hash;
25
use function sprintf;
26

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

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

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

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

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

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

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

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

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

150
        $msg .= ":\n\n" . $driverEx->getMessage();
151

152
        return static::wrapException($driver, $driverEx, $msg);
153 154
    }

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

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

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

        return new self($msg, 0, $driverEx);
177 178
    }

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

194 195
            $json = @json_encode($param);

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

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

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

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

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

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

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

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

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

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

290
    public static function typeNotRegistered(Type $type): self
291 292 293 294
    {
        return new self(sprintf('Type of the class %s@%s is not registered.', get_class($type), spl_object_hash($type)));
    }

295
    public static function typeAlreadyRegistered(Type $type): self
296 297 298 299 300
    {
        return new self(
            sprintf('Type of the class %s@%s is already registered.', get_class($type), spl_object_hash($type))
        );
    }
301
}