Completed
Pull Request — master (#3189)
by Jarek
28:19 queued 25:37
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 1378
    public static function notSupported($method)
31
    {
32 1378
        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 81
    public static function invalidPlatformType($invalidPlatform) : self
46
    {
47 81
        if (is_object($invalidPlatform)) {
48 54
            return new self(
49 54
                sprintf(
50 54
                    "Option 'platform' must be a subtype of '%s', instance of '%s' given",
51 54
                    AbstractPlatform::class,
52 54
                    get_class($invalidPlatform)
53
                )
54
            );
55
        }
56
57 27
        return new self(
58 27
            sprintf(
59 27
                "Option 'platform' must be an object and subtype of '%s'. Got '%s'",
60 27
                AbstractPlatform::class,
61 27
                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 270
    public static function invalidPlatformVersionSpecified($version, $expectedFormat)
75
    {
76 270
        return new self(
77 270
            sprintf(
78
                'Invalid platform version "%s" specified. ' .
79 270
                'The platform version has to be specified in the format: "%s".',
80 270
                $version,
81 270
                $expectedFormat
82
            )
83
        );
84
    }
85
86
    /**
87
     * @return \Doctrine\DBAL\DBALException
88
     */
89 27
    public static function invalidPdoInstance()
90
    {
91 27
        return new self(
92
            "The 'pdo' option was used in DriverManager::getConnection() but no " .
93 27
            '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 135
    public static function driverRequired($url = null)
103
    {
104 135
        if ($url) {
105 108
            return new self(
106 108
                sprintf(
107
                    "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
108 108
                    'is given to DriverManager::getConnection(). Given URL: %s',
109 108
                    $url
110
                )
111
            );
112
        }
113
114 27
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO " .
115 27
            '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 27
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
125
    {
126 27
        return new self("The given 'driver' " . $unknownDriverName . ' is unknown, ' .
127 27
            'Doctrine currently supports only the following drivers: ' . implode(', ', $knownDrivers));
128
    }
129
130
    /**
131
     * @param string  $sql
132
     * @param mixed[] $params
133
     *
134
     * @return self
135
     */
136 3889
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
137
    {
138 3889
        $msg = "An exception occurred while executing '" . $sql . "'";
139 3889
        if ($params) {
140 295
            $msg .= ' with params ' . self::formatParameters($params);
141
        }
142 3889
        $msg .= ":\n\n" . $driverEx->getMessage();
143
144 3889
        return static::wrapException($driver, $driverEx, $msg);
145
    }
146
147
    /**
148
     * @return self
149
     */
150 84
    public static function driverException(Driver $driver, Throwable $driverEx)
151
    {
152 84
        return static::wrapException($driver, $driverEx, 'An exception occurred in driver: ' . $driverEx->getMessage());
153
    }
154
155
    /**
156
     * @return self
157
     */
158 3973
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
159
    {
160 3973
        if ($driverEx instanceof DriverException) {
161 27
            return $driverEx;
162
        }
163 3946
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
164 3498
            return $driver->convertException($msg, $driverEx);
165
        }
166
167 448
        return new self($msg, 0, $driverEx);
168
    }
169
170
    /**
171
     * Returns a human-readable representation of an array of parameters.
172
     * This properly handles binary data by returning a hex representation.
173
     *
174
     * @param mixed[] $params
175
     *
176
     * @return string
177
     */
178 295
    private static function formatParameters(array $params)
179
    {
180
        return '[' . implode(', ', array_map(static function ($param) {
181 295
            if (is_resource($param)) {
182 27
                return (string) $param;
183
            }
184
185 268
            $json = @json_encode($param);
186
187 268
            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...
188
                // JSON encoding failed, this is not a UTF-8 string.
189 27
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
190
            }
191
192 268
            return $json;
193 295
        }, $params)) . ']';
194
    }
195
196
    /**
197
     * @param string $wrapperClass
198
     *
199
     * @return \Doctrine\DBAL\DBALException
200
     */
201 27
    public static function invalidWrapperClass($wrapperClass)
202
    {
203 27
        return new self("The given 'wrapperClass' " . $wrapperClass . ' has to be a ' .
204 27
            'subtype of \Doctrine\DBAL\Connection.');
205
    }
206
207
    /**
208
     * @param string $driverClass
209
     *
210
     * @return \Doctrine\DBAL\DBALException
211
     */
212 27
    public static function invalidDriverClass($driverClass)
213
    {
214 27
        return new self("The given 'driverClass' " . $driverClass . ' has to implement the ' . Driver::class . ' interface.');
215
    }
216
217
    /**
218
     * @param string $tableName
219
     *
220
     * @return \Doctrine\DBAL\DBALException
221
     */
222 27
    public static function invalidTableName($tableName)
223
    {
224 27
        return new self('Invalid table name specified: ' . $tableName);
225
    }
226
227
    /**
228
     * @param string $tableName
229
     *
230
     * @return \Doctrine\DBAL\DBALException
231
     */
232 486
    public static function noColumnsSpecifiedForTable($tableName)
233
    {
234 486
        return new self('No columns specified for table ' . $tableName);
235
    }
236
237
    /**
238
     * @return \Doctrine\DBAL\DBALException
239
     */
240
    public static function limitOffsetInvalid()
241
    {
242
        return new self('Invalid Offset in Limit Query, it has to be larger than or equal to 0.');
243
    }
244
245
    /**
246
     * @param string $name
247
     *
248
     * @return \Doctrine\DBAL\DBALException
249
     */
250
    public static function typeExists($name)
251
    {
252
        return new self('Type ' . $name . ' already exists.');
253
    }
254
255
    /**
256
     * @param string $name
257
     *
258
     * @return \Doctrine\DBAL\DBALException
259
     */
260
    public static function unknownColumnType($name)
261
    {
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
            'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
267
            'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
268
            'have a problem with the cache or forgot some mapping information.');
269
    }
270
271
    /**
272
     * @param string $name
273
     *
274
     * @return \Doctrine\DBAL\DBALException
275
     */
276 486
    public static function typeNotFound($name)
277
    {
278 486
        return new self('Type to be overwritten ' . $name . ' does not exist.');
279
    }
280
}
281