Failed Conditions
Push — develop ( e39bc0...ffeeb0 )
by Sergei
19s queued 13s
created

DBALException::driverRequired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
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 preg_replace;
21
use function sprintf;
22
23
class DBALException extends Exception
24
{
25
    /**
26
     * @param string $method
27
     *
28
     * @return \Doctrine\DBAL\DBALException
29
     */
30 691
    public static function notSupported($method)
31
    {
32 691
        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 string  $sql
132
     * @param mixed[] $params
133
     *
134
     * @return self
135
     */
136
    public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = [])
137 3174
    {
138
        $msg = "An exception occurred while executing '" . $sql . "'";
139 3174
        if ($params) {
140 3174
            $msg .= ' with params ' . self::formatParameters($params);
141 261
        }
142
        $msg .= ":\n\n" . $driverEx->getMessage();
143 3174
144
        return static::wrapException($driver, $driverEx, $msg);
145 3174
    }
146
147
    /**
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 70
    }
154
155 70
    /**
156
     * @return self
157
     */
158
    private static function wrapException(Driver $driver, Throwable $driverEx, $msg)
159
    {
160
        if ($driverEx instanceof DriverException) {
161
            return $driverEx;
162
        }
163 3244
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) {
164
            return $driver->convertException($msg, $driverEx);
165 3244
        }
166 23
167
        return new self($msg, 0, $driverEx);
168 3221
    }
169 2800
170
    /**
171
     * Returns a human-readable representation of an array of parameters.
172 421
     * This properly handles binary data by returning a hex representation.
173
     *
174
     * @param mixed[] $params
175
     *
176
     * @return string
177
     */
178
    private static function formatParameters(array $params)
179
    {
180
        return '[' . implode(', ', array_map(static function ($param) {
181
            if (is_resource($param)) {
182
                return (string) $param;
183 261
            }
184
185
            $json = @json_encode($param);
186 261
187 23
            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
                return sprintf('"%s"', preg_replace('/.{2}/', '\\x$0', bin2hex($param)));
190 238
            }
191
192 238
            return $json;
193
        }, $params)) . ']';
194 23
    }
195
196
    /**
197 238
     * @param string $wrapperClass
198 261
     *
199
     * @return \Doctrine\DBAL\DBALException
200
     */
201
    public static function invalidWrapperClass($wrapperClass)
202
    {
203
        return new self("The given 'wrapperClass' " . $wrapperClass . ' has to be a ' .
204
            'subtype of \Doctrine\DBAL\Connection.');
205
    }
206 23
207
    /**
208 23
     * @param string $driverClass
209 23
     *
210
     * @return \Doctrine\DBAL\DBALException
211
     */
212
    public static function invalidDriverClass($driverClass)
213
    {
214
        return new self("The given 'driverClass' " . $driverClass . ' has to implement the ' . Driver::class . ' interface.');
215
    }
216
217 23
    /**
218
     * @param string $tableName
219 23
     *
220
     * @return \Doctrine\DBAL\DBALException
221
     */
222
    public static function invalidTableName($tableName)
223
    {
224
        return new self('Invalid table name specified: ' . $tableName);
225
    }
226
227 23
    /**
228
     * @param string $tableName
229 23
     *
230
     * @return \Doctrine\DBAL\DBALException
231
     */
232
    public static function noColumnsSpecifiedForTable($tableName)
233
    {
234
        return new self('No columns specified for table ' . $tableName);
235
    }
236
237 276
    /**
238
     * @return \Doctrine\DBAL\DBALException
239 276
     */
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
    public static function typeNotFound($name)
277
    {
278
        return new self('Type to be overwritten ' . $name . ' does not exist.');
279
    }
280
281 276
    public static function invalidColumnIndex(int $index, int $count) : self
282
    {
283 276
        return new self(sprintf(
284
            'Invalid column index %d. The statement result contains %d column%s.',
285
            $index,
286
            $count,
287
            $count === 1 ? '' : 's'
288
        ));
289
    }
290
}
291