AbstractMySQLDriver.php 7.45 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\Driver;
8 9 10 11 12 13 14 15 16 17 18 19 20
use Doctrine\DBAL\Driver\DriverException as DeprecatedDriverException;
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\DBAL\Exception\DeadlockException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\DBAL\Exception\InvalidFieldNameException;
use Doctrine\DBAL\Exception\LockWaitTimeoutException;
use Doctrine\DBAL\Exception\NonUniqueFieldNameException;
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
use Doctrine\DBAL\Exception\SyntaxErrorException;
use Doctrine\DBAL\Exception\TableExistsException;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
21
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
22
use Doctrine\DBAL\Platforms\MySQL57Platform;
23
use Doctrine\DBAL\Platforms\MySQL80Platform;
24 25 26
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
27

28
use function assert;
29 30 31
use function preg_match;
use function stripos;
use function version_compare;
32 33

/**
34
 * Abstract base implementation of the {@link Driver} interface for MySQL based drivers.
35 36 37 38 39 40
 */
abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
{
    /**
     * {@inheritdoc}
     *
41 42
     * @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
43
     */
44
    public function convertException($message, DeprecatedDriverException $exception)
45 46
    {
        switch ($exception->getErrorCode()) {
47
            case '1213':
48
                return new DeadlockException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
49

50
            case '1205':
51
                return new LockWaitTimeoutException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
52

53
            case '1050':
54
                return new TableExistsException($message, $exception);
55 56 57

            case '1051':
            case '1146':
58
                return new TableNotFoundException($message, $exception);
59 60 61 62 63

            case '1216':
            case '1217':
            case '1451':
            case '1452':
64
            case '1701':
65
                return new ForeignKeyConstraintViolationException($message, $exception);
66 67 68 69 70

            case '1062':
            case '1557':
            case '1569':
            case '1586':
71
                return new UniqueConstraintViolationException($message, $exception);
72 73 74 75

            case '1054':
            case '1166':
            case '1611':
76
                return new InvalidFieldNameException($message, $exception);
77 78 79 80

            case '1052':
            case '1060':
            case '1110':
81
                return new NonUniqueFieldNameException($message, $exception);
82 83 84 85 86 87 88 89 90 91 92 93 94

            case '1064':
            case '1149':
            case '1287':
            case '1341':
            case '1342':
            case '1343':
            case '1344':
            case '1382':
            case '1479':
            case '1541':
            case '1554':
            case '1626':
95
                return new SyntaxErrorException($message, $exception);
96 97 98 99 100 101 102 103 104 105

            case '1044':
            case '1045':
            case '1046':
            case '1049':
            case '1095':
            case '1142':
            case '1143':
            case '1227':
            case '1370':
106
            case '1429':
107 108
            case '2002':
            case '2005':
109
                return new ConnectionException($message, $exception);
110 111 112 113 114 115 116

            case '1048':
            case '1121':
            case '1138':
            case '1171':
            case '1252':
            case '1263':
117
            case '1364':
118
            case '1566':
119
                return new NotNullConstraintViolationException($message, $exception);
120 121
        }

122
        return new DriverException($message, $exception);
123 124 125 126
    }

    /**
     * {@inheritdoc}
127 128
     *
     * @throws DBALException
129
     */
130
    public function createDatabasePlatformForVersion($version)
131
    {
132
        $mariadb = stripos($version, 'mariadb') !== false;
133
        if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
134
            return new MariaDb1027Platform();
135 136
        }

137 138 139 140 141
        if (! $mariadb) {
            $oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version);
            if (version_compare($oracleMysqlVersion, '8', '>=')) {
                return new MySQL80Platform();
            }
Grégoire Paris's avatar
Grégoire Paris committed
142

143 144 145
            if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) {
                return new MySQL57Platform();
            }
146 147 148 149 150 151 152 153 154 155
        }

        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'
156
     *
157 158
     * @throws DBALException
     */
159
    private function getOracleMysqlVersionNumber(string $versionString): string
160
    {
161 162 163 164 165 166 167
        if (
            ! preg_match(
                '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
                $versionString,
                $versionParts
            )
        ) {
168
            throw DBALException::invalidPlatformVersionSpecified(
169
                $versionString,
170 171 172
                '<major_version>.<minor_version>.<patch_version>'
            );
        }
Grégoire Paris's avatar
Grégoire Paris committed
173

174
        $majorVersion = $versionParts['major'];
175 176
        $minorVersion = $versionParts['minor'] ?? 0;
        $patchVersion = $versionParts['patch'] ?? null;
177

178
        if ($majorVersion === '5' && $minorVersion === '7' && $patchVersion === null) {
179 180 181
            $patchVersion = '9';
        }

182 183
        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
    }
184

185 186 187 188 189
    /**
     * 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'
190
     *
191 192
     * @throws DBALException
     */
193
    private function getMariaDbMysqlVersionNumber(string $versionString): string
194
    {
195 196 197 198 199 200 201
        if (
            ! preg_match(
                '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
                $versionString,
                $versionParts
            )
        ) {
202
            throw DBALException::invalidPlatformVersionSpecified(
203 204
                $versionString,
                '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>'
205
            );
206 207
        }

208
        return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
209 210 211 212
    }

    /**
     * {@inheritdoc}
213 214
     *
     * @deprecated Use Connection::getDatabase() instead.
215
     */
216
    public function getDatabase(Connection $conn)
217 218 219
    {
        $params = $conn->getParams();

220 221 222 223 224 225 226 227 228
        if (isset($params['dbname'])) {
            return $params['dbname'];
        }

        $database = $conn->query('SELECT DATABASE()')->fetchColumn();

        assert($database !== false);

        return $database;
229 230 231 232
    }

    /**
     * {@inheritdoc}
233
     *
234
     * @return MySqlPlatform
235
     */
236
    public function getDatabasePlatform()
237 238 239 240 241 242
    {
        return new MySqlPlatform();
    }

    /**
     * {@inheritdoc}
243
     *
244
     * @return MySqlSchemaManager
245
     */
246
    public function getSchemaManager(Connection $conn)
247 248 249 250
    {
        return new MySqlSchemaManager($conn);
    }
}