1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Driver; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\DBALException; |
23
|
|
|
use Doctrine\DBAL\Driver; |
24
|
|
|
use Doctrine\DBAL\Exception; |
25
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
26
|
|
|
use Doctrine\DBAL\Platforms\MariaDb102Platform; |
27
|
|
|
use Doctrine\DBAL\Platforms\MySQL57Platform; |
28
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
29
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
30
|
|
|
use Doctrine\DBAL\Schema\MySqlSchemaManager; |
31
|
|
|
use Doctrine\DBAL\VersionAwarePlatformDriver; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers. |
35
|
|
|
* |
36
|
|
|
* @author Steve Müller <[email protected]> |
37
|
|
|
* @link www.doctrine-project.org |
38
|
|
|
* @since 2.5 |
39
|
|
|
*/ |
40
|
|
|
abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
* |
45
|
|
|
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html |
46
|
|
|
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html |
47
|
|
|
*/ |
48
|
4 |
|
public function convertException($message, DriverException $exception) |
49
|
|
|
{ |
50
|
4 |
|
switch ($exception->getErrorCode()) { |
51
|
4 |
|
case '1213': |
52
|
4 |
|
return new Exception\DeadlockException($message, $exception); |
53
|
4 |
|
case '1205': |
54
|
4 |
|
return new Exception\LockWaitTimeoutException($message, $exception); |
55
|
4 |
|
case '1050': |
56
|
4 |
|
return new Exception\TableExistsException($message, $exception); |
57
|
|
|
|
58
|
4 |
|
case '1051': |
59
|
4 |
|
case '1146': |
60
|
4 |
|
return new Exception\TableNotFoundException($message, $exception); |
61
|
|
|
|
62
|
4 |
|
case '1216': |
63
|
4 |
|
case '1217': |
64
|
4 |
|
case '1451': |
65
|
4 |
|
case '1452': |
66
|
4 |
|
case '1701': |
67
|
4 |
|
return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
68
|
|
|
|
69
|
4 |
|
case '1062': |
70
|
4 |
|
case '1557': |
71
|
4 |
|
case '1569': |
72
|
4 |
|
case '1586': |
73
|
4 |
|
return new Exception\UniqueConstraintViolationException($message, $exception); |
74
|
|
|
|
75
|
4 |
|
case '1054': |
76
|
4 |
|
case '1166': |
77
|
4 |
|
case '1611': |
78
|
4 |
|
return new Exception\InvalidFieldNameException($message, $exception); |
79
|
|
|
|
80
|
4 |
|
case '1052': |
81
|
4 |
|
case '1060': |
82
|
4 |
|
case '1110': |
83
|
4 |
|
return new Exception\NonUniqueFieldNameException($message, $exception); |
84
|
|
|
|
85
|
4 |
|
case '1064': |
86
|
4 |
|
case '1149': |
87
|
4 |
|
case '1287': |
88
|
4 |
|
case '1341': |
89
|
4 |
|
case '1342': |
90
|
4 |
|
case '1343': |
91
|
4 |
|
case '1344': |
92
|
4 |
|
case '1382': |
93
|
4 |
|
case '1479': |
94
|
4 |
|
case '1541': |
95
|
4 |
|
case '1554': |
96
|
4 |
|
case '1626': |
97
|
4 |
|
return new Exception\SyntaxErrorException($message, $exception); |
98
|
|
|
|
99
|
4 |
|
case '1044': |
100
|
4 |
|
case '1045': |
101
|
4 |
|
case '1046': |
102
|
4 |
|
case '1049': |
103
|
4 |
|
case '1095': |
104
|
4 |
|
case '1142': |
105
|
4 |
|
case '1143': |
106
|
4 |
|
case '1227': |
107
|
4 |
|
case '1370': |
108
|
4 |
|
case '1429': |
109
|
4 |
|
case '2002': |
110
|
4 |
|
case '2005': |
111
|
4 |
|
return new Exception\ConnectionException($message, $exception); |
112
|
|
|
|
113
|
4 |
|
case '1048': |
114
|
4 |
|
case '1121': |
115
|
4 |
|
case '1138': |
116
|
4 |
|
case '1171': |
117
|
4 |
|
case '1252': |
118
|
4 |
|
case '1263': |
119
|
4 |
|
case '1364': |
120
|
4 |
|
case '1566': |
121
|
4 |
|
return new Exception\NotNullConstraintViolationException($message, $exception); |
122
|
|
|
} |
123
|
|
|
|
124
|
4 |
|
return new Exception\DriverException($message, $exception); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
* |
130
|
|
|
* @return AbstractPlatform|MariaDb102Platform|MySQL57Platform|MySqlPlatform |
131
|
|
|
* @throws DBALException |
132
|
|
|
*/ |
133
|
6 |
|
public function createDatabasePlatformForVersion($version): AbstractPlatform |
134
|
|
|
{ |
135
|
6 |
|
if (false !== stripos($version, 'mariadb')) { |
136
|
3 |
|
$versionNumber = $this->getMariaDbMysqlVersionNumber($version); |
137
|
3 |
|
if (version_compare($versionNumber, '10.2.7', '>=')) { |
138
|
3 |
|
return new MariaDb102Platform(); |
139
|
|
|
} |
140
|
|
|
} else { |
141
|
6 |
|
$versionNumber = $this->getOracleMysqlVersionNumber($version); |
142
|
3 |
|
if (version_compare($versionNumber, '5.7.9', '>=')) { |
143
|
3 |
|
return new MySQL57Platform(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
return $this->getDatabasePlatform(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get a normalized 'version number' from the server string |
152
|
|
|
* returned by Oracle MySQL servers. |
153
|
|
|
* |
154
|
|
|
* @param string $versionString Version string returned by the driver, i.e. '5.7.10' |
155
|
|
|
* @throws DBALException |
156
|
|
|
*/ |
157
|
6 |
|
private function getOracleMysqlVersionNumber(string $versionString): string |
158
|
|
|
{ |
159
|
6 |
|
if (!preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $versionString, $versionParts)) { |
160
|
3 |
|
throw DBALException::invalidPlatformVersionSpecified( |
161
|
3 |
|
$versionString, |
162
|
3 |
|
'<major_version>.<minor_version>.<patch_version>' |
163
|
|
|
); |
164
|
|
|
} |
165
|
3 |
|
$majorVersion = $versionParts['major']; |
166
|
3 |
|
$minorVersion = $versionParts['minor'] ?? 0; |
167
|
3 |
|
$patchVersion = $versionParts['patch'] ?? null; |
168
|
|
|
|
169
|
3 |
|
if ('5' === $majorVersion && '7' === $minorVersion && null === $patchVersion) { |
170
|
3 |
|
$patchVersion = '9'; |
171
|
|
|
} |
172
|
|
|
|
173
|
3 |
|
return $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Detect MariaDB server version, including hack for some mariadb distributions |
178
|
|
|
* that starts with the prefix '5.5.5-' |
179
|
|
|
* |
180
|
|
|
* @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial' |
181
|
|
|
* @throws DBALException |
182
|
|
|
*/ |
183
|
3 |
|
private function getMariaDbMysqlVersionNumber(string $versionString): string |
184
|
|
|
{ |
185
|
3 |
|
$version = str_replace('5.5.5-', '', $versionString); |
186
|
|
|
|
187
|
3 |
|
if (!preg_match('/^(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/', strtolower($version), $versionParts)) { |
188
|
|
|
throw DBALException::invalidPlatformVersionSpecified( |
189
|
|
|
$version, |
190
|
|
|
'(mariadb-)?<major_version>.<minor_version>.<patch_version>' |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
3 |
|
return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch']; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
4 |
|
public function getDatabase(\Doctrine\DBAL\Connection $conn): ?string |
201
|
|
|
{ |
202
|
4 |
|
$params = $conn->getParams(); |
203
|
|
|
|
204
|
4 |
|
if (isset($params['dbname'])) { |
205
|
4 |
|
return $params['dbname']; |
206
|
|
|
} |
207
|
|
|
|
208
|
4 |
|
return $conn->query('SELECT DATABASE()')->fetchColumn(); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
* @return MySqlPlatform |
214
|
|
|
*/ |
215
|
6 |
|
public function getDatabasePlatform(): AbstractPlatform |
216
|
|
|
{ |
217
|
6 |
|
return new MySqlPlatform(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
* @return MySqlSchemaManager |
223
|
|
|
*/ |
224
|
3 |
|
public function getSchemaManager(\Doctrine\DBAL\Connection $conn): AbstractSchemaManager |
225
|
|
|
{ |
226
|
3 |
|
return new MySqlSchemaManager($conn); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|