Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php (2 issues)

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\MySQL57Platform;
26
use Doctrine\DBAL\Platforms\MySqlPlatform;
27
use Doctrine\DBAL\Schema\MySqlSchemaManager;
28
use Doctrine\DBAL\VersionAwarePlatformDriver;
29
30
/**
31
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers.
32
 *
33
 * @author Steve Müller <[email protected]>
34
 * @link   www.doctrine-project.org
35
 * @since  2.5
36
 */
37
abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
38
{
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
43
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
44
     */
45 4
    public function convertException($message, DriverException $exception)
46
    {
47 4
        switch ($exception->getErrorCode()) {
48 4
            case '1213':
49 4
                return new Exception\DeadlockException($message, $exception);
50 4
            case '1205':
51 4
                return new Exception\LockWaitTimeoutException($message, $exception);
52 4
            case '1050':
53 4
                return new Exception\TableExistsException($message, $exception);
54
55 4
            case '1051':
56 4
            case '1146':
57 4
                return new Exception\TableNotFoundException($message, $exception);
58
59 4
            case '1216':
60 4
            case '1217':
61 4
            case '1451':
62 4
            case '1452':
63 4
            case '1701':
64 4
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
65
66 4
            case '1062':
67 4
            case '1557':
68 4
            case '1569':
69 4
            case '1586':
70 4
                return new Exception\UniqueConstraintViolationException($message, $exception);
71
72 4
            case '1054':
73 4
            case '1166':
74 4
            case '1611':
75 4
                return new Exception\InvalidFieldNameException($message, $exception);
76
77 4
            case '1052':
78 4
            case '1060':
79 4
            case '1110':
80 4
                return new Exception\NonUniqueFieldNameException($message, $exception);
81
82 4
            case '1064':
83 4
            case '1149':
84 4
            case '1287':
85 4
            case '1341':
86 4
            case '1342':
87 4
            case '1343':
88 4
            case '1344':
89 4
            case '1382':
90 4
            case '1479':
91 4
            case '1541':
92 4
            case '1554':
93 4
            case '1626':
94 4
                return new Exception\SyntaxErrorException($message, $exception);
95
96 4
            case '1044':
97 4
            case '1045':
98 4
            case '1046':
99 4
            case '1049':
100 4
            case '1095':
101 4
            case '1142':
102 4
            case '1143':
103 4
            case '1227':
104 4
            case '1370':
105 4
            case '1429':
106 4
            case '2002':
107 4
            case '2005':
108 4
                return new Exception\ConnectionException($message, $exception);
109
110 4
            case '1048':
111 4
            case '1121':
112 4
            case '1138':
113 4
            case '1171':
114 4
            case '1252':
115 4
            case '1263':
116 4
            case '1364':
117 4
            case '1566':
118 4
                return new Exception\NotNullConstraintViolationException($message, $exception);
119
        }
120
121 4
        return new Exception\DriverException($message, $exception);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 6
    public function createDatabasePlatformForVersion($version)
128
    {
129 6
        if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
130 3
            throw DBALException::invalidPlatformVersionSpecified(
131 3
                $version,
132 3
                '<major_version>.<minor_version>.<patch_version>'
133
            );
134
        }
135
136 3
        if (false !== stripos($version, 'mariadb')) {
137 3
            return $this->getDatabasePlatform();
138
        }
139
140 3
        $majorVersion = $versionParts['major'];
141 3
        $minorVersion = isset($versionParts['minor']) ? $versionParts['minor'] : 0;
142 3
        $patchVersion = isset($versionParts['patch']) ? $versionParts['patch'] : null;
143
144 3
        if ('5' === $majorVersion && '7' === $minorVersion && null === $patchVersion) {
145 3
            $patchVersion = '9';
146
        }
147
148 3
        $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
149
150 3
        if (version_compare($version, '5.7.9', '>=')) {
151 3
            return new MySQL57Platform();
152
        }
153
154 3
        return $this->getDatabasePlatform();
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 4 View Code Duplication
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
0 ignored issues
show
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...
161
    {
162 4
        $params = $conn->getParams();
163
164 4
        if (isset($params['dbname'])) {
165 4
            return $params['dbname'];
166
        }
167
168 4
        return $conn->query('SELECT DATABASE()')->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $conn->query('SEL...BASE()')->fetchColumn() also could return the type boolean which is incompatible with the return type mandated by Doctrine\DBAL\Driver::getDatabase() of string.
Loading history...
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 6
    public function getDatabasePlatform()
175
    {
176 6
        return new MySqlPlatform();
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 3
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
183
    {
184 3
        return new MySqlSchemaManager($conn);
185
    }
186
}
187