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\PostgreSQL100Platform; |
||
26 | use Doctrine\DBAL\Platforms\PostgreSQL91Platform; |
||
27 | use Doctrine\DBAL\Platforms\PostgreSQL92Platform; |
||
28 | use Doctrine\DBAL\Platforms\PostgreSQL94Platform; |
||
29 | use Doctrine\DBAL\Platforms\PostgreSqlPlatform; |
||
30 | use Doctrine\DBAL\Schema\PostgreSqlSchemaManager; |
||
31 | use Doctrine\DBAL\VersionAwarePlatformDriver; |
||
32 | use function preg_match; |
||
33 | use function strpos; |
||
34 | use function version_compare; |
||
35 | |||
36 | /** |
||
37 | * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for PostgreSQL based drivers. |
||
38 | * |
||
39 | * @author Steve Müller <[email protected]> |
||
40 | * @link www.doctrine-project.org |
||
41 | * @since 2.5 |
||
42 | */ |
||
43 | abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver |
||
44 | { |
||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | * |
||
48 | * @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html |
||
49 | */ |
||
50 | 898 | public function convertException($message, DriverException $exception) |
|
51 | { |
||
52 | 898 | switch ($exception->getSQLState()) { |
|
53 | 898 | case '40001': |
|
54 | 898 | case '40P01': |
|
55 | 38 | return new Exception\DeadlockException($message, $exception); |
|
56 | 898 | case '0A000': |
|
57 | // Foreign key constraint violations during a TRUNCATE operation |
||
58 | // are considered "feature not supported" in PostgreSQL. |
||
59 | 6 | if (strpos($exception->getMessage(), 'truncate') !== false) { |
|
60 | 6 | return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
|
61 | } |
||
62 | |||
63 | break; |
||
64 | 892 | case '23502': |
|
65 | 44 | return new Exception\NotNullConstraintViolationException($message, $exception); |
|
66 | |||
67 | 886 | case '23503': |
|
68 | 56 | return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
|
69 | |||
70 | 868 | case '23505': |
|
71 | 50 | return new Exception\UniqueConstraintViolationException($message, $exception); |
|
72 | |||
73 | 856 | case '42601': |
|
74 | 44 | return new Exception\SyntaxErrorException($message, $exception); |
|
75 | |||
76 | 850 | case '42702': |
|
77 | 44 | return new Exception\NonUniqueFieldNameException($message, $exception); |
|
78 | |||
79 | 844 | case '42703': |
|
80 | 44 | return new Exception\InvalidFieldNameException($message, $exception); |
|
81 | |||
82 | 838 | case '42P01': |
|
83 | 308 | return new Exception\TableNotFoundException($message, $exception); |
|
84 | |||
85 | 790 | case '42P07': |
|
86 | 302 | return new Exception\TableExistsException($message, $exception); |
|
87 | |||
88 | 526 | case '7': |
|
89 | // In some case (mainly connection errors) the PDO exception does not provide a SQLSTATE via its code. |
||
90 | // The exception code is always set to 7 here. |
||
91 | // We have to match against the SQLSTATE in the error message in these cases. |
||
92 | 56 | if (strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { |
|
93 | 56 | return new Exception\ConnectionException($message, $exception); |
|
94 | } |
||
95 | |||
96 | break; |
||
97 | } |
||
98 | |||
99 | 508 | return new Exception\DriverException($message, $exception); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 238 | public function createDatabasePlatformForVersion($version) |
|
106 | { |
||
107 | 238 | if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { |
|
108 | 38 | throw DBALException::invalidPlatformVersionSpecified( |
|
109 | 38 | $version, |
|
110 | 38 | '<major_version>.<minor_version>.<patch_version>' |
|
111 | ); |
||
112 | } |
||
113 | |||
114 | 200 | $majorVersion = $versionParts['major']; |
|
115 | 200 | $minorVersion = $versionParts['minor'] ?? 0; |
|
116 | 200 | $patchVersion = $versionParts['patch'] ?? 0; |
|
117 | 200 | $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
|
118 | |||
119 | switch(true) { |
||
120 | 200 | case version_compare($version, '10.0', '>='): |
|
0 ignored issues
–
show
|
|||
121 | 65 | return new PostgreSQL100Platform(); |
|
122 | 173 | case version_compare($version, '9.4', '>='): |
|
0 ignored issues
–
show
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 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...
|
|||
123 | 119 | return new PostgreSQL94Platform(); |
|
124 | 92 | case version_compare($version, '9.2', '>='): |
|
0 ignored issues
–
show
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 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...
|
|||
125 | 92 | return new PostgreSQL92Platform(); |
|
126 | 38 | case version_compare($version, '9.1', '>='): |
|
0 ignored issues
–
show
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 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...
|
|||
127 | 38 | return new PostgreSQL91Platform(); |
|
128 | default: |
||
129 | 38 | return new PostgreSqlPlatform(); |
|
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 478 | public function getDatabase(\Doctrine\DBAL\Connection $conn) |
|
137 | { |
||
138 | 478 | $params = $conn->getParams(); |
|
139 | |||
140 | 478 | return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn(); |
|
0 ignored issues
–
show
|
|||
141 | } |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 38 | public function getDatabasePlatform() |
|
147 | { |
||
148 | 38 | return new PostgreSqlPlatform(); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 86 | public function getSchemaManager(\Doctrine\DBAL\Connection $conn) |
|
155 | { |
||
156 | 86 | return new PostgreSqlSchemaManager($conn); |
|
157 | } |
||
158 | } |
||
159 |
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.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.