Failed Conditions
Push — master ( 656579...2742cd )
by Marco
11:55
created

Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php (1 issue)

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', '>='):
117 2
                return new PostgreSQL94Platform();
118 2
            case version_compare($version, '9.2', '>='):
119 2
                return new PostgreSQL92Platform();
120 2
            case version_compare($version, '9.1', '>='):
121 2
                return new PostgreSQL91Platform();
122
            default:
123 2
                return new PostgreSqlPlatform();
124
        }
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 2 View Code Duplication
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
131
    {
132 2
        $params = $conn->getParams();
133
134 2
        return (isset($params['dbname']))
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? $para...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...
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