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 Exception $driverEx |
||
132 | * @param string $sql |
||
133 | * @param mixed[] $params |
||
134 | * |
||
135 | * @return \Doctrine\DBAL\DBALException |
||
136 | */ |
||
137 | 3761 | public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = []) |
|
138 | { |
||
139 | 3761 | $msg = "An exception occurred while executing '" . $sql . "'"; |
|
140 | 3761 | if ($params) { |
|
141 | 295 | $msg .= ' with params ' . self::formatParameters($params); |
|
142 | } |
||
143 | 3761 | $msg .= ":\n\n" . $driverEx->getMessage(); |
|
144 | |||
145 | 3761 | return static::wrapException($driver, $driverEx, $msg); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param Exception $driverEx |
||
150 | * |
||
151 | * @return \Doctrine\DBAL\DBALException |
||
152 | */ |
||
153 | 84 | public static function driverException(Driver $driver, Throwable $driverEx) |
|
154 | { |
||
155 | 84 | 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 | 3845 | private static function wrapException(Driver $driver, Throwable $driverEx, $msg) |
|
164 | { |
||
165 | 3845 | if ($driverEx instanceof DriverException) { |
|
166 | 27 | return $driverEx; |
|
167 | } |
||
168 | 3818 | if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverExceptionInterface) { |
|
169 | 3370 | return $driver->convertException($msg, $driverEx); |
|
170 | } |
||
171 | |||
172 | 448 | 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 | 295 | private static function formatParameters(array $params) |
|
184 | { |
||
185 | return '[' . implode(', ', array_map(static function ($param) { |
||
186 | 295 | if (is_resource($param)) { |
|
187 | 27 | return (string) $param; |
|
188 | } |
||
189 | |||
190 | 268 | $json = @json_encode($param); |
|
191 | |||
192 | 268 | if (! is_string($json) || $json === 'null' && is_string($param)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
193 | // JSON encoding failed, this is not a UTF-8 string. |
||
194 | 27 | return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"'; |
|
195 | } |
||
196 | |||
197 | 268 | return $json; |
|
198 | 295 | }, $params)) . ']'; |
|
199 | } |
||
200 | |||
201 | /** |
||
202 | * @param string $wrapperClass |
||
203 | * |
||
204 | * @return \Doctrine\DBAL\DBALException |
||
205 | */ |
||
206 | 27 | public static function invalidWrapperClass($wrapperClass) |
|
207 | { |
||
208 | 27 | return new self("The given 'wrapperClass' " . $wrapperClass . ' has to be a ' . |
|
209 | 27 | 'subtype of \Doctrine\DBAL\Connection.'); |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param string $driverClass |
||
214 | * |
||
215 | * @return \Doctrine\DBAL\DBALException |
||
216 | */ |
||
217 | 27 | public static function invalidDriverClass($driverClass) |
|
218 | { |
||
219 | 27 | 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 | 27 | public static function invalidTableName($tableName) |
|
228 | { |
||
229 | 27 | return new self('Invalid table name specified: ' . $tableName); |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string $tableName |
||
234 | * |
||
235 | * @return \Doctrine\DBAL\DBALException |
||
236 | */ |
||
237 | 486 | public static function noColumnsSpecifiedForTable($tableName) |
|
238 | { |
||
239 | 486 | 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 | 486 | public static function typeNotFound($name) |
|
282 | { |
||
283 | 486 | return new self('Type to be overwritten ' . $name . ' does not exist.'); |
|
284 | } |
||
285 | } |
||
286 |