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

namespace Doctrine\DBAL;

5
use Doctrine\DBAL\Driver\Exception as TheDriverException;
6
use Doctrine\DBAL\Exception\DriverException;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Types\Type;
9 10
use Exception;
use Throwable;
11

12 13
use function array_map;
use function bin2hex;
14
use function count;
15 16
use function get_class;
use function gettype;
17
use function implode;
18
use function is_object;
19 20 21
use function is_resource;
use function is_string;
use function json_encode;
Sergei Morozov's avatar
Sergei Morozov committed
22
use function preg_replace;
23
use function spl_object_hash;
24
use function sprintf;
25

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

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

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

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

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

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

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

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

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

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

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

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

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

171
        if ($driverEx instanceof TheDriverException) {
172
            return $driver->convertException($msg, $driverEx);
173
        }
174 175

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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