1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Driver; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
|
|
|
6
|
|
|
use Doctrine\DBAL\DBALException; |
7
|
|
|
use Doctrine\DBAL\Driver; |
8
|
|
|
use Doctrine\DBAL\Exception; |
9
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQL100Platform; |
10
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQL91Platform; |
11
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQL92Platform; |
12
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQL94Platform; |
13
|
|
|
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; |
14
|
|
|
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager; |
15
|
|
|
use Doctrine\DBAL\VersionAwarePlatformDriver; |
16
|
|
|
use function preg_match; |
17
|
|
|
use function strpos; |
18
|
|
|
use function version_compare; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for PostgreSQL based drivers. |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
* |
28
|
|
|
* @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html |
29
|
|
|
*/ |
30
|
1847 |
|
public function convertException($message, DriverException $exception) |
31
|
|
|
{ |
32
|
1847 |
|
switch ($exception->getSQLState()) { |
33
|
1847 |
|
case '40001': |
34
|
1845 |
|
case '40P01': |
35
|
1620 |
|
return new Exception\DeadlockException($message, $exception); |
36
|
|
|
|
37
|
1843 |
|
case '0A000': |
38
|
|
|
// Foreign key constraint violations during a TRUNCATE operation |
39
|
|
|
// are considered "feature not supported" in PostgreSQL. |
40
|
910 |
|
if (strpos($exception->getMessage(), 'truncate') !== false) { |
41
|
910 |
|
return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
break; |
45
|
|
|
|
46
|
1843 |
|
case '23502': |
47
|
1733 |
|
return new Exception\NotNullConstraintViolationException($message, $exception); |
48
|
|
|
|
49
|
1841 |
|
case '23503': |
50
|
1802 |
|
return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
51
|
|
|
|
52
|
1839 |
|
case '23505': |
53
|
1641 |
|
return new Exception\UniqueConstraintViolationException($message, $exception); |
54
|
|
|
|
55
|
1837 |
|
case '42601': |
56
|
1710 |
|
return new Exception\SyntaxErrorException($message, $exception); |
57
|
|
|
|
58
|
1835 |
|
case '42702': |
59
|
1756 |
|
return new Exception\NonUniqueFieldNameException($message, $exception); |
60
|
|
|
|
61
|
1833 |
|
case '42703': |
62
|
1779 |
|
return new Exception\InvalidFieldNameException($message, $exception); |
63
|
|
|
|
64
|
1831 |
|
case '42P01': |
65
|
1664 |
|
return new Exception\TableNotFoundException($message, $exception); |
66
|
|
|
|
67
|
1829 |
|
case '42P07': |
68
|
1687 |
|
return new Exception\TableExistsException($message, $exception); |
69
|
|
|
|
70
|
1827 |
|
case '7': |
71
|
|
|
// In some case (mainly connection errors) the PDO exception does not provide a SQLSTATE via its code. |
72
|
|
|
// The exception code is always set to 7 here. |
73
|
|
|
// We have to match against the SQLSTATE in the error message in these cases. |
74
|
1825 |
|
if (strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { |
75
|
1825 |
|
return new Exception\ConnectionException($message, $exception); |
76
|
|
|
} |
77
|
|
|
|
78
|
1570 |
|
break; |
79
|
|
|
} |
80
|
|
|
|
81
|
1572 |
|
return new Exception\DriverException($message, $exception); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1551 |
|
public function createDatabasePlatformForVersion($version) |
88
|
|
|
{ |
89
|
1551 |
|
if (! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { |
90
|
1526 |
|
throw DBALException::invalidPlatformVersionSpecified( |
91
|
1526 |
|
$version, |
92
|
1526 |
|
'<major_version>.<minor_version>.<patch_version>' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
1549 |
|
$majorVersion = $versionParts['major']; |
97
|
1549 |
|
$minorVersion = $versionParts['minor'] ?? 0; |
98
|
1549 |
|
$patchVersion = $versionParts['patch'] ?? 0; |
99
|
1549 |
|
$version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
100
|
|
|
|
101
|
|
|
switch (true) { |
102
|
1549 |
|
case version_compare($version, '10.0', '>='): |
103
|
1549 |
|
return new PostgreSQL100Platform(); |
104
|
1549 |
|
case version_compare($version, '9.4', '>='): |
105
|
1549 |
|
return new PostgreSQL94Platform(); |
106
|
1549 |
|
case version_compare($version, '9.2', '>='): |
107
|
1549 |
|
return new PostgreSQL92Platform(); |
108
|
1549 |
|
case version_compare($version, '9.1', '>='): |
109
|
1549 |
|
return new PostgreSQL91Platform(); |
110
|
|
|
default: |
111
|
1549 |
|
return new PostgreSqlPlatform(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1848 |
|
public function getDatabase(Connection $conn) |
119
|
|
|
{ |
120
|
1848 |
|
$params = $conn->getParams(); |
121
|
|
|
|
122
|
1848 |
|
return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
1503 |
|
public function getDatabasePlatform() |
129
|
|
|
{ |
130
|
1503 |
|
return new PostgreSqlPlatform(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
1480 |
|
public function getSchemaManager(Connection $conn) |
137
|
|
|
{ |
138
|
1480 |
|
return new PostgreSqlSchemaManager($conn); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: