DBALException.php 8.34 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 10
use Exception;
use Throwable;
11 12
use function array_map;
use function bin2hex;
13 14
use function get_class;
use function gettype;
15
use function implode;
16
use function is_object;
17 18 19
use function is_resource;
use function is_string;
use function json_encode;
Sergei Morozov's avatar
Sergei Morozov committed
20
use function preg_replace;
21
use function sprintf;
22

23
class DBALException extends Exception
24
{
Benjamin Morel's avatar
Benjamin Morel committed
25 26 27 28 29
    /**
     * @param string $method
     *
     * @return \Doctrine\DBAL\DBALException
     */
30 31
    public static function notSupported($method)
    {
32
        return new self(sprintf("Operation '%s' is not supported by platform.", $method));
33
    }
34

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

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

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

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

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

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

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

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

144
        return static::wrapException($driver, $driverEx, $msg);
145 146
    }

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

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

        return new self($msg, 0, $driverEx);
168 169
    }

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

185 186
            $json = @json_encode($param);

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

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

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

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

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

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

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

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

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

Benjamin Morel's avatar
Benjamin Morel committed
271 272 273 274 275
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
276 277
    public static function typeNotFound($name)
    {
278
        return new self('Type to be overwritten ' . $name . ' does not exist.');
279
    }
280
}