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
|
876 |
|
public function convertException($message, DriverException $exception) |
51
|
|
|
{ |
52
|
876 |
|
switch ($exception->getSQLState()) { |
53
|
876 |
|
case '40001': |
54
|
876 |
|
case '40P01': |
55
|
34 |
|
return new Exception\DeadlockException($message, $exception); |
56
|
876 |
|
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
|
870 |
|
case '23502': |
65
|
40 |
|
return new Exception\NotNullConstraintViolationException($message, $exception); |
66
|
|
|
|
67
|
864 |
|
case '23503': |
68
|
52 |
|
return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
69
|
|
|
|
70
|
846 |
|
case '23505': |
71
|
46 |
|
return new Exception\UniqueConstraintViolationException($message, $exception); |
72
|
|
|
|
73
|
834 |
|
case '42601': |
74
|
40 |
|
return new Exception\SyntaxErrorException($message, $exception); |
75
|
|
|
|
76
|
828 |
|
case '42702': |
77
|
40 |
|
return new Exception\NonUniqueFieldNameException($message, $exception); |
78
|
|
|
|
79
|
822 |
|
case '42703': |
80
|
40 |
|
return new Exception\InvalidFieldNameException($message, $exception); |
81
|
|
|
|
82
|
816 |
|
case '42P01': |
83
|
274 |
|
return new Exception\TableNotFoundException($message, $exception); |
84
|
|
|
|
85
|
786 |
|
case '42P07': |
86
|
310 |
|
return new Exception\TableExistsException($message, $exception); |
87
|
|
|
|
88
|
510 |
|
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
|
52 |
|
if (strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { |
93
|
52 |
|
return new Exception\ConnectionException($message, $exception); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
492 |
|
return new Exception\DriverException($message, $exception); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
230 |
|
public function createDatabasePlatformForVersion($version) |
106
|
|
|
{ |
107
|
230 |
|
if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { |
108
|
34 |
|
throw DBALException::invalidPlatformVersionSpecified( |
109
|
34 |
|
$version, |
110
|
34 |
|
'<major_version>.<minor_version>.<patch_version>' |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
196 |
|
$majorVersion = $versionParts['major']; |
115
|
196 |
|
$minorVersion = $versionParts['minor'] ?? 0; |
116
|
196 |
|
$patchVersion = $versionParts['patch'] ?? 0; |
117
|
196 |
|
$version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
118
|
|
|
|
119
|
|
|
switch(true) { |
120
|
196 |
|
case version_compare($version, '10.0', '>='): |
121
|
61 |
|
return new PostgreSQL100Platform(); |
122
|
169 |
|
case version_compare($version, '9.4', '>='): |
123
|
115 |
|
return new PostgreSQL94Platform(); |
124
|
88 |
|
case version_compare($version, '9.2', '>='): |
125
|
88 |
|
return new PostgreSQL92Platform(); |
126
|
34 |
|
case version_compare($version, '9.1', '>='): |
127
|
34 |
|
return new PostgreSQL91Platform(); |
128
|
|
|
default: |
129
|
34 |
|
return new PostgreSqlPlatform(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
468 |
|
public function getDatabase(\Doctrine\DBAL\Connection $conn) |
137
|
|
|
{ |
138
|
468 |
|
$params = $conn->getParams(); |
139
|
|
|
|
140
|
468 |
|
return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn(); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
34 |
|
public function getDatabasePlatform() |
147
|
|
|
{ |
148
|
34 |
|
return new PostgreSqlPlatform(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
82 |
|
public function getSchemaManager(\Doctrine\DBAL\Connection $conn) |
155
|
|
|
{ |
156
|
82 |
|
return new PostgreSqlSchemaManager($conn); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|