Failed Conditions
Pull Request — master (#2825)
by Sébastien
08:30
created

getMariaDbMysqlVersionNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 4
cts 7
cp 0.5714
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.3149
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver;
21
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver;
24
use Doctrine\DBAL\Exception;
25
use Doctrine\DBAL\Platforms\AbstractPlatform;
26
use Doctrine\DBAL\Platforms\MariaDb102Platform;
27
use Doctrine\DBAL\Platforms\MySQL57Platform;
28
use Doctrine\DBAL\Platforms\MySqlPlatform;
29
use Doctrine\DBAL\Schema\MySqlSchemaManager;
30
use Doctrine\DBAL\VersionAwarePlatformDriver;
31
32
/**
33
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers.
34
 *
35
 * @author Steve Müller <[email protected]>
36
 * @link   www.doctrine-project.org
37
 * @since  2.5
38
 */
39
abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
40
{
41
    /**
42
     * {@inheritdoc}
43
     *
44
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
45
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
46
     */
47 4
    public function convertException($message, DriverException $exception)
48
    {
49 4
        switch ($exception->getErrorCode()) {
50 4
            case '1213':
51 4
                return new Exception\DeadlockException($message, $exception);
52 4
            case '1205':
53 4
                return new Exception\LockWaitTimeoutException($message, $exception);
54 4
            case '1050':
55 4
                return new Exception\TableExistsException($message, $exception);
56
57 4
            case '1051':
58 4
            case '1146':
59 4
                return new Exception\TableNotFoundException($message, $exception);
60
61 4
            case '1216':
62 4
            case '1217':
63 4
            case '1451':
64 4
            case '1452':
65 4
            case '1701':
66 4
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
67
68 4
            case '1062':
69 4
            case '1557':
70 4
            case '1569':
71 4
            case '1586':
72 4
                return new Exception\UniqueConstraintViolationException($message, $exception);
73
74 4
            case '1054':
75 4
            case '1166':
76 4
            case '1611':
77 4
                return new Exception\InvalidFieldNameException($message, $exception);
78
79 4
            case '1052':
80 4
            case '1060':
81 4
            case '1110':
82 4
                return new Exception\NonUniqueFieldNameException($message, $exception);
83
84 4
            case '1064':
85 4
            case '1149':
86 4
            case '1287':
87 4
            case '1341':
88 4
            case '1342':
89 4
            case '1343':
90 4
            case '1344':
91 4
            case '1382':
92 4
            case '1479':
93 4
            case '1541':
94 4
            case '1554':
95 4
            case '1626':
96 4
                return new Exception\SyntaxErrorException($message, $exception);
97
98 4
            case '1044':
99 4
            case '1045':
100 4
            case '1046':
101 4
            case '1049':
102 4
            case '1095':
103 4
            case '1142':
104 4
            case '1143':
105 4
            case '1227':
106 4
            case '1370':
107 4
            case '1429':
108 4
            case '2002':
109 4
            case '2005':
110 4
                return new Exception\ConnectionException($message, $exception);
111
112 4
            case '1048':
113 4
            case '1121':
114 4
            case '1138':
115 4
            case '1171':
116 4
            case '1252':
117 4
            case '1263':
118 4
            case '1364':
119 4
            case '1566':
120 4
                return new Exception\NotNullConstraintViolationException($message, $exception);
121
        }
122
123 4
        return new Exception\DriverException($message, $exception);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     * @return AbstractPlatform|MariaDb102Platform|MySQL57Platform|MySqlPlatform
129
     * @throws DBALException
130
     */
131 6
    public function createDatabasePlatformForVersion($version): AbstractPlatform
132
    {
133
        // Detect vendor and version
134 6
        if (false !== stripos($version, 'mariadb')) {
135
            // MariaDB MySQL
136 3
            $versionNumber = $this->getMariaDbMysqlVersionNumber($version);
137 3
            if (version_compare($versionNumber, '10.2.7', '>=')) {
138 3
                return new MariaDb102Platform();
139
            }
140
        } else {
141
            // Oracle MySQL
142 6
            $versionNumber = $this->getOracleMysqlVersionNumber($version);
143 3
            if (version_compare($versionNumber, '5.7.9', '>=')) {
144 3
                return new MySQL57Platform();
145
            }
146
        }
147
148 3
        return $this->getDatabasePlatform();
149
    }
150
151
    /**
152
     * Get a normalized 'version number' from the server string
153
     * returned by Oracle MySQL servers.
154
     *
155
     * @param string $versionString Version string returned by the driver, i.e. '5.7.10'
156
     * @throws DBALException
157
     */
158 6
    private function getOracleMysqlVersionNumber(string $versionString): string
159
    {
160 6
        if (!preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $versionString, $versionParts)) {
161 3
            throw DBALException::invalidPlatformVersionSpecified(
162 3
                $versionString,
163 3
                '<major_version>.<minor_version>.<patch_version>'
164
            );
165
        }
166 3
        $majorVersion = $versionParts['major'];
167 3
        $minorVersion = isset($versionParts['minor']) ? $versionParts['minor'] : 0;
168 3
        $patchVersion = isset($versionParts['patch']) ? $versionParts['patch'] : null;
169
170 3
        if ('5' === $majorVersion && '7' === $minorVersion && null === $patchVersion) {
171 3
            $patchVersion = '9';
172
        }
173
174 3
        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
175
    }
176
177
    /**
178
     * Get a normalized 'version number' from the server string
179
     * returned by MariaDB MySQL servers.
180
     *
181
     * @link https://github.com/doctrine/dbal/pull/2825
182
     *
183
     * @param string $versionString  Version string returned by the server, i.e. '5.5.5-Mariadb-10.0.8-xenial'
184
     * @throws DBALException
185
     */
186 3
    private function getMariaDbMysqlVersionNumber(string $versionString): string
187
    {
188
        // Starting from v10, mariadb prefixed version strings
189
        // by '5.5.5-' to bypass some limitation in the replication protocol.
190 3
        $version = str_replace('5.5.5-', '', $versionString);
191
192 3
        if (!preg_match('/^(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/', strtolower($version), $versionParts)) {
193
            throw DBALException::invalidPlatformVersionSpecified(
194
                $version,
195
                '(mariadb-)?<major_version>.<minor_version>.<patch_version>'
196
            );
197
        }
198
199 3
        return $versionParts['major'] . '.' . $versionParts['minor'] . '.' . $versionParts['patch'];
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205 4 View Code Duplication
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
    {
207 4
        $params = $conn->getParams();
208
209 4
        if (isset($params['dbname'])) {
210 4
            return $params['dbname'];
211
        }
212
213 4
        return $conn->query('SELECT DATABASE()')->fetchColumn();
0 ignored issues
show
Bug Compatibility introduced by
The expression $conn->query('SELECT DATABASE()')->fetchColumn(); of type string|boolean adds the type boolean to the return on line 213 which is incompatible with the return type declared by the interface Doctrine\DBAL\Driver::getDatabase of type string.
Loading history...
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219 6
    public function getDatabasePlatform()
220
    {
221 6
        return new MySqlPlatform();
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 3
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
228
    {
229 3
        return new MySqlSchemaManager($conn);
230
    }
231
}
232