1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
|
|
|
8
|
|
|
use Doctrine\DBAL\DBALException; |
9
|
|
|
use Doctrine\DBAL\Driver; |
10
|
|
|
use Doctrine\DBAL\Exception; |
11
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQL100Platform; |
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
|
1098 |
|
public function convertException($message, DriverException $exception) |
31
|
|
|
{ |
32
|
1098 |
|
switch ($exception->getSQLState()) { |
33
|
1098 |
|
case '40001': |
34
|
1098 |
|
case '40P01': |
35
|
1098 |
|
return new Exception\DeadlockException($message, $exception); |
36
|
1098 |
|
case '0A000': |
37
|
|
|
// Foreign key constraint violations during a TRUNCATE operation |
38
|
|
|
// are considered "feature not supported" in PostgreSQL. |
39
|
714 |
|
if (strpos($exception->getMessage(), 'truncate') !== false) { |
40
|
714 |
|
return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
break; |
44
|
1098 |
|
case '23502': |
45
|
1098 |
|
return new Exception\NotNullConstraintViolationException($message, $exception); |
46
|
|
|
|
47
|
1098 |
|
case '23503': |
48
|
1098 |
|
return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
49
|
|
|
|
50
|
1098 |
|
case '23505': |
51
|
1098 |
|
return new Exception\UniqueConstraintViolationException($message, $exception); |
52
|
|
|
|
53
|
1098 |
|
case '42601': |
54
|
1098 |
|
return new Exception\SyntaxErrorException($message, $exception); |
55
|
|
|
|
56
|
1098 |
|
case '42702': |
57
|
1098 |
|
return new Exception\NonUniqueFieldNameException($message, $exception); |
58
|
|
|
|
59
|
1098 |
|
case '42703': |
60
|
1098 |
|
return new Exception\InvalidFieldNameException($message, $exception); |
61
|
|
|
|
62
|
1098 |
|
case '42P01': |
63
|
1098 |
|
return new Exception\TableNotFoundException($message, $exception); |
64
|
|
|
|
65
|
1098 |
|
case '42P07': |
66
|
1098 |
|
return new Exception\TableExistsException($message, $exception); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// In some case (mainly connection errors) the PDO exception does not provide a SQLSTATE via its code. |
70
|
|
|
// The exception code is always set to 7 here. |
71
|
|
|
// We have to match against the SQLSTATE in the error message in these cases. |
72
|
1098 |
|
if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { |
73
|
1098 |
|
return new Exception\ConnectionException($message, $exception); |
74
|
|
|
} |
75
|
|
|
|
76
|
1098 |
|
return new Exception\DriverException($message, $exception); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
1074 |
|
public function createDatabasePlatformForVersion($version) |
83
|
|
|
{ |
84
|
1074 |
|
if (! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { |
85
|
1050 |
|
throw DBALException::invalidPlatformVersionSpecified( |
86
|
1050 |
|
$version, |
87
|
1050 |
|
'<major_version>.<minor_version>.<patch_version>' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
1074 |
|
$majorVersion = $versionParts['major']; |
92
|
1074 |
|
$minorVersion = $versionParts['minor'] ?? 0; |
93
|
1074 |
|
$patchVersion = $versionParts['patch'] ?? 0; |
94
|
1074 |
|
$version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
95
|
|
|
|
96
|
|
|
switch (true) { |
97
|
1074 |
|
case version_compare($version, '10.0', '>='): |
98
|
1074 |
|
return new PostgreSQL100Platform(); |
99
|
1074 |
|
case version_compare($version, '9.4', '>='): |
100
|
1074 |
|
return new PostgreSQL94Platform(); |
101
|
|
|
default: |
102
|
1074 |
|
return new PostgreSqlPlatform(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
1122 |
|
public function getDatabase(Connection $conn) |
110
|
|
|
{ |
111
|
1122 |
|
$params = $conn->getParams(); |
112
|
|
|
|
113
|
1122 |
|
return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn(); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1026 |
|
public function getDatabasePlatform() |
120
|
|
|
{ |
121
|
1026 |
|
return new PostgreSqlPlatform(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
1002 |
|
public function getSchemaManager(Connection $conn) |
128
|
|
|
{ |
129
|
1002 |
|
return new PostgreSqlSchemaManager($conn); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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: