Failed Conditions
Pull Request — master (#3359)
by Sergei
25:01 queued 22:04
created

DBALException::unknownColumnType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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