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