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

namespace Doctrine\DBAL\Driver;

5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\DBALException;
7 8
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Driver\API\MySQL;
9
use Doctrine\DBAL\Platforms\AbstractPlatform;
10
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
11
use Doctrine\DBAL\Platforms\MySQL57Platform;
12
use Doctrine\DBAL\Platforms\MySQL80Platform;
13 14 15
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
16

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

/**
22
 * Abstract base implementation of the {@link Driver} interface for MySQL based drivers.
23
 */
24
abstract class AbstractMySQLDriver implements VersionAwarePlatformDriver
25 26 27
{
    /**
     * {@inheritdoc}
28 29
     *
     * @throws DBALException
30
     */
31
    public function createDatabasePlatformForVersion($version)
32
    {
33
        $mariadb = stripos($version, 'mariadb') !== false;
34
        if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
35
            return new MariaDb1027Platform();
36 37
        }

38 39 40 41 42
        if (! $mariadb) {
            $oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version);
            if (version_compare($oracleMysqlVersion, '8', '>=')) {
                return new MySQL80Platform();
            }
Grégoire Paris's avatar
Grégoire Paris committed
43

44 45 46
            if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) {
                return new MySQL57Platform();
            }
47 48 49 50 51 52 53 54 55 56
        }

        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'
57
     *
58 59
     * @throws DBALException
     */
60
    private function getOracleMysqlVersionNumber(string $versionString): string
61
    {
62
        if (
63
            preg_match(
64 65 66
                '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
                $versionString,
                $versionParts
67
            ) === 0
68
        ) {
69
            throw DBALException::invalidPlatformVersionSpecified(
70
                $versionString,
71 72 73
                '<major_version>.<minor_version>.<patch_version>'
            );
        }
Grégoire Paris's avatar
Grégoire Paris committed
74

75
        $majorVersion = $versionParts['major'];
76 77
        $minorVersion = $versionParts['minor'] ?? 0;
        $patchVersion = $versionParts['patch'] ?? null;
78

79
        if ($majorVersion === '5' && $minorVersion === '7' && $patchVersion === null) {
80 81 82
            $patchVersion = '9';
        }

83 84
        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
    }
85

86 87 88 89 90
    /**
     * 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'
91
     *
92 93
     * @throws DBALException
     */
94
    private function getMariaDbMysqlVersionNumber(string $versionString): string
95
    {
96
        if (
97
            preg_match(
98 99 100
                '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
                $versionString,
                $versionParts
101
            ) === 0
102
        ) {
103
            throw DBALException::invalidPlatformVersionSpecified(
104 105
                $versionString,
                '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>'
106
            );
107 108
        }

109
        return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
110 111 112 113
    }

    /**
     * {@inheritdoc}
114
     *
115
     * @return MySqlPlatform
116
     */
117
    public function getDatabasePlatform()
118 119 120 121 122 123
    {
        return new MySqlPlatform();
    }

    /**
     * {@inheritdoc}
124
     *
125
     * @return MySqlSchemaManager
126
     */
127
    public function getSchemaManager(Connection $conn, AbstractPlatform $platform)
128
    {
129
        return new MySqlSchemaManager($conn, $platform);
130
    }
131 132 133 134 135

    public function getExceptionConverter(): ExceptionConverter
    {
        return new MySQL\ExceptionConverter();
    }
136
}