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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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