DBALException.php 9.13 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL;

7
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
8
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
9
use Doctrine\DBAL\Exception\DriverException;
10
use Doctrine\DBAL\Platforms\AbstractPlatform;
11
use Doctrine\DBAL\Types\Type;
12 13
use Exception;
use Throwable;
14 15
use function array_map;
use function bin2hex;
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
class DBALException extends Exception
28
{
Benjamin Morel's avatar
Benjamin Morel committed
29 30 31 32 33
    /**
     * @param string $method
     *
     * @return \Doctrine\DBAL\DBALException
     */
34 35
    public static function notSupported($method)
    {
36
        return new self(sprintf("Operation '%s' is not supported by platform.", $method));
37
    }
38

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

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

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

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

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

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

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

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

148
        return static::wrapException($driver, $driverEx, $msg);
149 150
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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))
        );
    }
296 297 298 299 300 301 302 303 304 305

    public static function invalidColumnIndex(int $index, int $count) : self
    {
        return new self(sprintf(
            'Invalid column index %d. The statement result contains %d column%s.',
            $index,
            $count,
            $count === 1 ? '' : 's'
        ));
    }
306
}