Completed
Push — master ( 11b67b...3a0a1d )
by Sergei
19:09 queued 19:04
created

DBALException   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Test Coverage

Coverage 94.25%

Importance

Changes 0
Metric Value
wmc 31
eloc 70
dl 0
loc 267
ccs 82
cts 87
cp 0.9425
rs 9.92
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidPlatformSpecified() 0 4 1
A unknownDriver() 0 4 1
A driverRequired() 0 14 2
A notSupported() 0 3 1
A invalidPlatformVersionSpecified() 0 8 1
A invalidPdoInstance() 0 5 1
A invalidPlatformType() 0 17 2
A typeAlreadyRegistered() 0 4 1
A typeExists() 0 3 1
A typeNotFound() 0 3 1
A limitOffsetInvalid() 0 3 1
A unknownColumnType() 0 9 1
A invalidWrapperClass() 0 4 1
A invalidTableName() 0 3 1
A typeNotRegistered() 0 3 1
A invalidDriverClass() 0 3 1
A wrapException() 0 10 4
A driverExceptionDuringQuery() 0 9 2
A formatParameters() 0 16 5
A noColumnsSpecifiedForTable() 0 3 1
A driverException() 0 3 1
1
<?php
2
3
namespace Doctrine\DBAL;
4
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
use Exception;
11
use Throwable;
12
use function array_map;
13
use function bin2hex;
14
use function get_class;
15
use function gettype;
16
use function implode;
17
use function is_object;
18
use function is_resource;
19
use function is_string;
20
use function json_encode;
21
use function preg_replace;
22
use function spl_object_hash;
23
use function sprintf;
24
25
class DBALException extends Exception
26
{
27
    /**
28
     * @param string $method
29
     *
30
     * @return \Doctrine\DBAL\DBALException
31
     */
32 2613
    public static function notSupported($method)
33
    {
34 2613
        return new self(sprintf("Operation '%s' is not supported by platform.", $method));
35
    }
36
37
    public static function invalidPlatformSpecified() : self
38
    {
39
        return new self(
40
            "Invalid 'platform' option specified, need to give an instance of " . AbstractPlatform::class . '.'
41
        );
42
    }
43
44
    /**
45
     * @param mixed $invalidPlatform
46
     */
47 6514
    public static function invalidPlatformType($invalidPlatform) : self
48
    {
49 6514
        if (is_object($invalidPlatform)) {
50 6512
            return new self(
51 6512
                sprintf(
52 4
                    "Option 'platform' must be a subtype of '%s', instance of '%s' given",
53 4
                    AbstractPlatform::class,
54 6512
                    get_class($invalidPlatform)
55
                )
56
            );
57
        }
58
59 6348
        return new self(
60 6348
            sprintf(
61 2
                "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
62 2
                AbstractPlatform::class,
63 6348
                gettype($invalidPlatform)
64
            )
65
        );
66
    }
67
68
    /**
69
     * Returns a new instance for an invalid specified platform version.
70
     *
71
     * @param string $version        The invalid platform version given.
72
     * @param string $expectedFormat The expected platform version format.
73
     *
74
     * @return DBALException
75
     */
76 6366
    public static function invalidPlatformVersionSpecified($version, $expectedFormat)
77
    {
78 6366
        return new self(
79 6366
            sprintf(
80
                'Invalid platform version "%s" specified. ' .
81 20
                'The platform version has to be specified in the format: "%s".',
82 6366
                $version,
83 6366
                $expectedFormat
84
            )
85
        );
86
    }
87
88
    /**
89
     * @return \Doctrine\DBAL\DBALException
90
     */
91 6078
    public static function invalidPdoInstance()
92
    {
93 6078
        return new self(
94
            "The 'pdo' option was used in DriverManager::getConnection() but no " .
95 6078
            'instance of PDO was given.'
96
        );
97
    }
98
99
    /**
100
     * @param string|null $url The URL that was provided in the connection parameters (if any).
101
     *
102
     * @return \Doctrine\DBAL\DBALException
103
     */
104 6410
    public static function driverRequired($url = null)
105
    {
106 6410
        if ($url) {
107 6408
            return new self(
108 6408
                sprintf(
109
                    "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
110 8
                    'is given to DriverManager::getConnection(). Given URL: %s',
111 6408
                    $url
112
                )
113
            );
114
        }
115
116 6051
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO " .
117 6051
            'instance is given to DriverManager::getConnection().');
118
    }
119
120
    /**
121
     * @param string   $unknownDriverName
122
     * @param string[] $knownDrivers
123
     *
124
     * @return \Doctrine\DBAL\DBALException
125
     */
126 6024
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
127
    {
128 6024
        return new self("The given 'driver' " . $unknownDriverName . ' is unknown, ' .
129 6024
            'Doctrine currently supports only the following drivers: ' . implode(', ', $knownDrivers));
130
    }
131
132
    /**
133
     * @param string  $sql
134
     * @param mixed[] $params
135
     *
136
     * @return self
137
     */
138 6943
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
139
    {
140 6943
        $msg = "An exception occurred while executing '" . $sql . "'";
141 6943
        if ($params) {
142 6507
            $msg .= ' with params ' . self::formatParameters($params);
143
        }
144 6943
        $msg .= ":\n\n" . $driverEx->getMessage();
145
146 6943
        return static::wrapException($driver, $driverEx, $msg);
147
    }
148
149
    /**
150
     * @return self
151
     */
152 5306
    public static function driverException(Driver $driver, Throwable $driverEx)
153
    {
154 5306
        return static::wrapException($driver, $driverEx, 'An exception occurred in driver: ' . $driverEx->getMessage());
155
    }
156
157
    /**
158
     * @return self
159
     */
160 6949
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
161
    {
162 6949
        if ($driverEx instanceof DriverException) {
163 6429
            return $driverEx;
164
        }
165 6947
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
166 6933
            return $driver->convertException($msg, $driverEx);
167
        }
168
169 6495
        return new self($msg, 0, $driverEx);
170
    }
171
172
    /**
173
     * Returns a human-readable representation of an array of parameters.
174
     * This properly handles binary data by returning a hex representation.
175
     *
176
     * @param mixed[] $params
177
     *
178
     * @return string
179
     */
180 6507
    private static function formatParameters(array $params)
181
    {
182
        return '[' . implode(', ', array_map(static function ($param) {
183 6507
            if (is_resource($param)) {
184 6456
                return (string) $param;
185
            }
186
187 6505
            $json = @json_encode($param);
188
189 6505
            if (! is_string($json) || $json === 'null' && is_string($param)) {
0 ignored issues
show
introduced by
The condition is_string($json) is always true.
Loading history...
190
                // JSON encoding failed, this is not a UTF-8 string.
191 6483
                return sprintf('"%s"', preg_replace('/.{2}/', '\\x$0', bin2hex($param)));
192
            }
193
194 6505
            return $json;
195 6507
        }, $params)) . ']';
196
    }
197
198
    /**
199
     * @param string $wrapperClass
200
     *
201
     * @return \Doctrine\DBAL\DBALException
202
     */
203 5997
    public static function invalidWrapperClass($wrapperClass)
204
    {
205 5997
        return new self("The given 'wrapperClass' " . $wrapperClass . ' has to be a ' .
206 5997
            'subtype of \Doctrine\DBAL\Connection.');
207
    }
208
209
    /**
210
     * @param string $driverClass
211
     *
212
     * @return \Doctrine\DBAL\DBALException
213
     */
214 5970
    public static function invalidDriverClass($driverClass)
215
    {
216 5970
        return new self("The given 'driverClass' " . $driverClass . ' has to implement the ' . Driver::class . ' interface.');
217
    }
218
219
    /**
220
     * @param string $tableName
221
     *
222
     * @return \Doctrine\DBAL\DBALException
223
     */
224 218
    public static function invalidTableName($tableName)
225
    {
226 218
        return new self('Invalid table name specified: ' . $tableName);
227
    }
228
229
    /**
230
     * @param string $tableName
231
     *
232
     * @return \Doctrine\DBAL\DBALException
233
     */
234 2574
    public static function noColumnsSpecifiedForTable($tableName)
235
    {
236 2574
        return new self('No columns specified for table ' . $tableName);
237
    }
238
239
    /**
240
     * @return \Doctrine\DBAL\DBALException
241
     */
242
    public static function limitOffsetInvalid()
243
    {
244
        return new self('Invalid Offset in Limit Query, it has to be larger than or equal to 0.');
245
    }
246
247
    /**
248
     * @param string $name
249
     *
250
     * @return \Doctrine\DBAL\DBALException
251
     */
252 112
    public static function typeExists($name)
253
    {
254 112
        return new self('Type ' . $name . ' already exists.');
255
    }
256
257
    /**
258
     * @param string $name
259
     *
260
     * @return \Doctrine\DBAL\DBALException
261
     */
262 164
    public static function unknownColumnType($name)
263
    {
264 164
        return new self('Unknown column type "' . $name . '" requested. Any Doctrine type that you use has ' .
265 164
            'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
266 164
            'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
267 164
            'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
268 164
            'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
269 164
            'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
270 164
            'have a problem with the cache or forgot some mapping information.');
271
    }
272
273
    /**
274
     * @param string $name
275
     *
276
     * @return \Doctrine\DBAL\DBALException
277
     */
278 2603
    public static function typeNotFound($name)
279
    {
280 2603
        return new self('Type to be overwritten ' . $name . ' does not exist.');
281
    }
282
283 137
    public static function typeNotRegistered(Type $type) : self
284
    {
285 137
        return new self(sprintf('Type of the class %s@%s is not registered.', get_class($type), spl_object_hash($type)));
286
    }
287
288 56
    public static function typeAlreadyRegistered(Type $type) : self
289
    {
290 56
        return new self(
291 56
            sprintf('Type of the class %s@%s is already registered.', get_class($type), spl_object_hash($type))
292
        );
293
    }
294
}
295