AbstractMySQLDriver.php 6.42 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Driver;

5
use Doctrine\DBAL\Connection;
6 7
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
8
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
9
use Doctrine\DBAL\Platforms\MySQL57Platform;
10
use Doctrine\DBAL\Platforms\MySQL80Platform;
11 12 13
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
14

15 16 17
use function preg_match;
use function stripos;
use function version_compare;
18 19 20 21

/**
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers.
 */
22
abstract class AbstractMySQLDriver implements ExceptionConverterDriver, VersionAwarePlatformDriver
23 24 25 26
{
    /**
     * {@inheritdoc}
     *
27 28
     * @link https://dev.mysql.com/doc/refman/8.0/en/client-error-reference.html
     * @link https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html
29 30 31 32
     */
    public function convertException($message, DriverException $exception)
    {
        switch ($exception->getErrorCode()) {
33 34
            case '1213':
                return new Exception\DeadlockException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
35

36 37
            case '1205':
                return new Exception\LockWaitTimeoutException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
38

39 40 41 42 43 44 45 46 47 48 49
            case '1050':
                return new Exception\TableExistsException($message, $exception);

            case '1051':
            case '1146':
                return new Exception\TableNotFoundException($message, $exception);

            case '1216':
            case '1217':
            case '1451':
            case '1452':
50
            case '1701':
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);

            case '1062':
            case '1557':
            case '1569':
            case '1586':
                return new Exception\UniqueConstraintViolationException($message, $exception);

            case '1054':
            case '1166':
            case '1611':
                return new Exception\InvalidFieldNameException($message, $exception);

            case '1052':
            case '1060':
            case '1110':
                return new Exception\NonUniqueFieldNameException($message, $exception);

            case '1064':
            case '1149':
            case '1287':
            case '1341':
            case '1342':
            case '1343':
            case '1344':
            case '1382':
            case '1479':
            case '1541':
            case '1554':
            case '1626':
                return new Exception\SyntaxErrorException($message, $exception);

            case '1044':
            case '1045':
            case '1046':
            case '1049':
            case '1095':
            case '1142':
            case '1143':
            case '1227':
            case '1370':
92
            case '1429':
93 94 95 96 97 98 99 100 101 102
            case '2002':
            case '2005':
                return new Exception\ConnectionException($message, $exception);

            case '1048':
            case '1121':
            case '1138':
            case '1171':
            case '1252':
            case '1263':
103
            case '1364':
104 105 106 107 108 109 110 111 112
            case '1566':
                return new Exception\NotNullConstraintViolationException($message, $exception);
        }

        return new Exception\DriverException($message, $exception);
    }

    /**
     * {@inheritdoc}
113 114
     *
     * @throws DBALException
115
     */
116
    public function createDatabasePlatformForVersion($version)
117
    {
118
        $mariadb = stripos($version, 'mariadb') !== false;
119
        if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
120
            return new MariaDb1027Platform();
121 122
        }

123 124 125 126 127
        if (! $mariadb) {
            $oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version);
            if (version_compare($oracleMysqlVersion, '8', '>=')) {
                return new MySQL80Platform();
            }
Grégoire Paris's avatar
Grégoire Paris committed
128

129 130 131
            if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) {
                return new MySQL57Platform();
            }
132 133 134 135 136 137 138 139 140 141
        }

        return $this->getDatabasePlatform();
    }

    /**
     * Get a normalized 'version number' from the server string
     * returned by Oracle MySQL servers.
     *
     * @param string $versionString Version string returned by the driver, i.e. '5.7.10'
142
     *
143 144
     * @throws DBALException
     */
145
    private function getOracleMysqlVersionNumber(string $versionString): string
146
    {
147
        if (
148
            preg_match(
149 150 151
                '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
                $versionString,
                $versionParts
152
            ) === 0
153
        ) {
154
            throw DBALException::invalidPlatformVersionSpecified(
155
                $versionString,
156 157 158
                '<major_version>.<minor_version>.<patch_version>'
            );
        }
Grégoire Paris's avatar
Grégoire Paris committed
159

160
        $majorVersion = $versionParts['major'];
161 162
        $minorVersion = $versionParts['minor'] ?? 0;
        $patchVersion = $versionParts['patch'] ?? null;
163

164
        if ($majorVersion === '5' && $minorVersion === '7' && $patchVersion === null) {
165 166 167
            $patchVersion = '9';
        }

168 169
        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
    }
170

171 172 173 174 175
    /**
     * Detect MariaDB server version, including hack for some mariadb distributions
     * that starts with the prefix '5.5.5-'
     *
     * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial'
176
     *
177 178
     * @throws DBALException
     */
179
    private function getMariaDbMysqlVersionNumber(string $versionString): string
180
    {
181
        if (
182
            preg_match(
183 184 185
                '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
                $versionString,
                $versionParts
186
            ) === 0
187
        ) {
188
            throw DBALException::invalidPlatformVersionSpecified(
189 190
                $versionString,
                '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>'
191
            );
192 193
        }

194
        return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
195 196 197 198
    }

    /**
     * {@inheritdoc}
199
     *
200
     * @return MySqlPlatform
201
     */
202
    public function getDatabasePlatform()
203 204 205 206 207 208
    {
        return new MySqlPlatform();
    }

    /**
     * {@inheritdoc}
209
     *
210
     * @return MySqlSchemaManager
211
     */
212
    public function getSchemaManager(Connection $conn)
213 214 215 216
    {
        return new MySqlSchemaManager($conn);
    }
}