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