DBALException.php 8.82 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
class DBALException extends Exception
26
{
Benjamin Morel's avatar
Benjamin Morel committed
27 28 29 30 31
    /**
     * @param string $method
     *
     * @return \Doctrine\DBAL\DBALException
     */
32 33
    public static function notSupported($method)
    {
34
        return new self(sprintf("Operation '%s' is not supported by platform.", $method));
35
    }
36

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

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

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

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    /**
     * 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
88 89 90
    /**
     * @return \Doctrine\DBAL\DBALException
     */
91 92 93
    public static function invalidPdoInstance()
    {
        return new self(
94 95
            "The 'pdo' option was used in DriverManager::getConnection() but no " .
            'instance of PDO was given.'
96 97 98
        );
    }

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

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

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

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

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

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

157
    /**
Sergei Morozov's avatar
Sergei Morozov committed
158
     * @return self
159
     */
160
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
161
    {
162
        if ($driverEx instanceof DriverException) {
163 164
            return $driverEx;
        }
165
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
166
            return $driver->convertException($msg, $driverEx);
167
        }
168 169

        return new self($msg, 0, $driverEx);
170 171
    }

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

187 188
            $json = @json_encode($param);

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

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

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

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

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

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

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

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

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

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

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