Failed Conditions
Pull Request — develop (#3581)
by Jonathan
12:44
created

AbstractMySQLDriver::getSchemaManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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