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