Completed
Pull Request — develop (#3533)
by
unknown
65:15
created

createDatabasePlatformForVersion()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
rs 8.8333
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 7
nc 10
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver;
6
7
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\DBAL\Driver\Connection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

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