1 | <?php |
||
2 | |||
3 | namespace Doctrine\DBAL\Driver; |
||
4 | |||
5 | use Doctrine\DBAL\Connection; |
||
0 ignored issues
–
show
|
|||
6 | use Doctrine\DBAL\DBALException; |
||
7 | use Doctrine\DBAL\Driver; |
||
8 | use Doctrine\DBAL\Exception; |
||
9 | use Doctrine\DBAL\Platforms\MariaDb1027Platform; |
||
10 | use Doctrine\DBAL\Platforms\MySQL57Platform; |
||
11 | use Doctrine\DBAL\Platforms\MySQL80Platform; |
||
12 | use Doctrine\DBAL\Platforms\MySqlPlatform; |
||
13 | use Doctrine\DBAL\Schema\MySqlSchemaManager; |
||
14 | use Doctrine\DBAL\VersionAwarePlatformDriver; |
||
15 | use function preg_match; |
||
16 | use function stripos; |
||
17 | use function version_compare; |
||
18 | |||
19 | /** |
||
20 | * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers. |
||
21 | */ |
||
22 | abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | * |
||
27 | * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html |
||
28 | * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html |
||
29 | */ |
||
30 | 2422 | public function convertException($message, DriverException $exception) |
|
31 | { |
||
32 | 2422 | switch ($exception->getErrorCode()) { |
|
33 | 2422 | case '1213': |
|
34 | 2422 | return new Exception\DeadlockException($message, $exception); |
|
35 | 2422 | case '1205': |
|
36 | 2422 | return new Exception\LockWaitTimeoutException($message, $exception); |
|
37 | 2422 | case '1050': |
|
38 | 2422 | return new Exception\TableExistsException($message, $exception); |
|
39 | |||
40 | 2422 | case '1051': |
|
41 | 2422 | case '1146': |
|
42 | 2422 | return new Exception\TableNotFoundException($message, $exception); |
|
43 | |||
44 | 2422 | case '1216': |
|
45 | 2422 | case '1217': |
|
46 | 2422 | case '1451': |
|
47 | 2422 | case '1452': |
|
48 | 2422 | case '1701': |
|
49 | 2422 | return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
|
50 | |||
51 | 2422 | case '1062': |
|
52 | 2422 | case '1557': |
|
53 | 2422 | case '1569': |
|
54 | 2422 | case '1586': |
|
55 | 2422 | return new Exception\UniqueConstraintViolationException($message, $exception); |
|
56 | |||
57 | 2422 | case '1054': |
|
58 | 2422 | case '1166': |
|
59 | 2422 | case '1611': |
|
60 | 2422 | return new Exception\InvalidFieldNameException($message, $exception); |
|
61 | |||
62 | 2422 | case '1052': |
|
63 | 2422 | case '1060': |
|
64 | 2422 | case '1110': |
|
65 | 2422 | return new Exception\NonUniqueFieldNameException($message, $exception); |
|
66 | |||
67 | 2422 | case '1064': |
|
68 | 2422 | case '1149': |
|
69 | 2422 | case '1287': |
|
70 | 2422 | case '1341': |
|
71 | 2422 | case '1342': |
|
72 | 2422 | case '1343': |
|
73 | 2422 | case '1344': |
|
74 | 2422 | case '1382': |
|
75 | 2422 | case '1479': |
|
76 | 2422 | case '1541': |
|
77 | 2422 | case '1554': |
|
78 | 2422 | case '1626': |
|
79 | 2422 | return new Exception\SyntaxErrorException($message, $exception); |
|
80 | |||
81 | 2422 | case '1044': |
|
82 | 2422 | case '1045': |
|
83 | 2422 | case '1046': |
|
84 | 2422 | case '1049': |
|
85 | 2422 | case '1095': |
|
86 | 2422 | case '1142': |
|
87 | 2422 | case '1143': |
|
88 | 2422 | case '1227': |
|
89 | 2422 | case '1370': |
|
90 | 2422 | case '1429': |
|
91 | 2422 | case '2002': |
|
92 | 2422 | case '2005': |
|
93 | 2422 | return new Exception\ConnectionException($message, $exception); |
|
94 | |||
95 | 2422 | case '1048': |
|
96 | 2422 | case '1121': |
|
97 | 2422 | case '1138': |
|
98 | 2422 | case '1171': |
|
99 | 2422 | case '1252': |
|
100 | 2422 | case '1263': |
|
101 | 2422 | case '1364': |
|
102 | 2422 | case '1566': |
|
103 | 2422 | return new Exception\NotNullConstraintViolationException($message, $exception); |
|
104 | } |
||
105 | |||
106 | 2422 | return new Exception\DriverException($message, $exception); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | * |
||
112 | * @throws DBALException |
||
113 | */ |
||
114 | 2401 | public function createDatabasePlatformForVersion($version) |
|
115 | { |
||
116 | 2401 | $mariadb = stripos($version, 'mariadb') !== false; |
|
117 | 2401 | if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) { |
|
118 | 2395 | return new MariaDb1027Platform(); |
|
119 | } |
||
120 | |||
121 | 2401 | if (! $mariadb) { |
|
122 | 2401 | $oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version); |
|
123 | 2395 | if (version_compare($oracleMysqlVersion, '8', '>=')) { |
|
124 | 2395 | return new MySQL80Platform(); |
|
125 | } |
||
126 | 2395 | if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) { |
|
127 | 2395 | return new MySQL57Platform(); |
|
128 | } |
||
129 | } |
||
130 | |||
131 | 2395 | return $this->getDatabasePlatform(); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get a normalized 'version number' from the server string |
||
136 | * returned by Oracle MySQL servers. |
||
137 | * |
||
138 | * @param string $versionString Version string returned by the driver, i.e. '5.7.10' |
||
139 | * |
||
140 | * @throws DBALException |
||
141 | */ |
||
142 | 2401 | private function getOracleMysqlVersionNumber(string $versionString) : string |
|
143 | { |
||
144 | 2401 | if (! preg_match( |
|
145 | 12 | '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', |
|
146 | 2401 | $versionString, |
|
147 | 2401 | $versionParts |
|
148 | )) { |
||
149 | 2370 | throw DBALException::invalidPlatformVersionSpecified( |
|
150 | 6 | $versionString, |
|
151 | 2370 | '<major_version>.<minor_version>.<patch_version>' |
|
152 | ); |
||
153 | } |
||
154 | 2395 | $majorVersion = $versionParts['major']; |
|
155 | 2395 | $minorVersion = $versionParts['minor'] ?? 0; |
|
156 | 2395 | $patchVersion = $versionParts['patch'] ?? null; |
|
157 | |||
158 | 2395 | if ($majorVersion === '5' && $minorVersion === '7' && $patchVersion === null) { |
|
159 | 2395 | $patchVersion = '9'; |
|
160 | } |
||
161 | |||
162 | 2395 | return $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Detect MariaDB server version, including hack for some mariadb distributions |
||
167 | * that starts with the prefix '5.5.5-' |
||
168 | * |
||
169 | * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial' |
||
170 | * |
||
171 | * @throws DBALException |
||
172 | */ |
||
173 | 2395 | private function getMariaDbMysqlVersionNumber(string $versionString) : string |
|
174 | { |
||
175 | 2395 | if (! preg_match( |
|
176 | 6 | '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i', |
|
177 | 2395 | $versionString, |
|
178 | 2395 | $versionParts |
|
179 | )) { |
||
180 | throw DBALException::invalidPlatformVersionSpecified( |
||
181 | $versionString, |
||
182 | '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>' |
||
183 | ); |
||
184 | } |
||
185 | |||
186 | 2395 | return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch']; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 2447 | public function getDatabase(Connection $conn) |
|
193 | { |
||
194 | 2447 | $params = $conn->getParams(); |
|
195 | |||
196 | 2447 | return $params['dbname'] ?? $conn->query('SELECT DATABASE()')->fetchColumn(); |
|
0 ignored issues
–
show
|
|||
197 | } |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | * |
||
202 | * @return MySqlPlatform |
||
203 | */ |
||
204 | 2401 | public function getDatabasePlatform() |
|
205 | { |
||
206 | 2401 | return new MySqlPlatform(); |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | * |
||
212 | * @return MySqlSchemaManager |
||
213 | */ |
||
214 | 2320 | public function getSchemaManager(Connection $conn) |
|
215 | { |
||
216 | 2320 | return new MySqlSchemaManager($conn); |
|
217 | } |
||
218 | } |
||
219 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: