Failed Conditions
Push — master ( ba421d...be3478 )
by Marco
14s
created

createDatabasePlatformForVersion()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 5
nop 1
crap 5
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\PostgreSQL91Platform;
26
use Doctrine\DBAL\Platforms\PostgreSQL92Platform;
27
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
28
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
29
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
30
use Doctrine\DBAL\VersionAwarePlatformDriver;
31
32
/**
33
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for PostgreSQL based drivers.
34
 *
35
 * @author Steve Müller <[email protected]>
36
 * @link   www.doctrine-project.org
37
 * @since  2.5
38
 */
39
abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
40
{
41
    /**
42
     * {@inheritdoc}
43
     *
44
     * @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
45
     */
46 2
    public function convertException($message, DriverException $exception)
47
    {
48 2
        switch ($exception->getSQLState()) {
49 2
            case '40001':
50 2
            case '40P01':
51 2
                return new Exception\DeadlockException($message, $exception);
52 2
            case '0A000':
53
                // Foreign key constraint violations during a TRUNCATE operation
54
                // are considered "feature not supported" in PostgreSQL.
55
                if (strpos($exception->getMessage(), 'truncate') !== false) {
56
                    return new Exception\ForeignKeyConstraintViolationException($message, $exception);
57
                }
58
59
                break;
60 2
            case '23502':
61 2
                return new Exception\NotNullConstraintViolationException($message, $exception);
62
63 2
            case '23503':
64 2
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
65
66 2
            case '23505':
67 2
                return new Exception\UniqueConstraintViolationException($message, $exception);
68
69 2
            case '42601':
70 2
                return new Exception\SyntaxErrorException($message, $exception);
71
72 2
            case '42702':
73 2
                return new Exception\NonUniqueFieldNameException($message, $exception);
74
75 2
            case '42703':
76 2
                return new Exception\InvalidFieldNameException($message, $exception);
77
78 2
            case '42P01':
79 2
                return new Exception\TableNotFoundException($message, $exception);
80
81 2
            case '42P07':
82 2
                return new Exception\TableExistsException($message, $exception);
83
84 2
            case '7':
85
                // In some case (mainly connection errors) the PDO exception does not provide a SQLSTATE via its code.
86
                // The exception code is always set to 7 here.
87
                // We have to match against the SQLSTATE in the error message in these cases.
88 2
                if (strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) {
89 2
                    return new Exception\ConnectionException($message, $exception);
90
                }
91
92
                break;
93
        }
94
95 2
        return new Exception\DriverException($message, $exception);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 4
    public function createDatabasePlatformForVersion($version)
102
    {
103 4
        if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
104 2
            throw DBALException::invalidPlatformVersionSpecified(
105 2
                $version,
106 2
                '<major_version>.<minor_version>.<patch_version>'
107
            );
108
        }
109
110 2
        $majorVersion = $versionParts['major'];
111 2
        $minorVersion = $versionParts['minor'] ?? 0;
112 2
        $patchVersion = $versionParts['patch'] ?? 0;
113 2
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
114
115
        switch(true) {
116 2
            case version_compare($version, '9.4', '>='):
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
117 2
                return new PostgreSQL94Platform();
118 2
            case version_compare($version, '9.2', '>='):
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
119 2
                return new PostgreSQL92Platform();
120 2
            case version_compare($version, '9.1', '>='):
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
121 2
                return new PostgreSQL91Platform();
122
            default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
123 2
                return new PostgreSqlPlatform();
124
        }
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 2 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...
131
    {
132 2
        $params = $conn->getParams();
133
134 2
        return (isset($params['dbname']))
135 2
            ? $params['dbname']
136 2
            : $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn();
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 2
    public function getDatabasePlatform()
143
    {
144 2
        return new PostgreSqlPlatform();
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 2
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
151
    {
152 2
        return new PostgreSqlSchemaManager($conn);
153
    }
154
}
155