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
|
1065 |
|
public function convertException($message, DriverException $exception) |
31
|
|
|
{ |
32
|
1065 |
|
switch ($exception->getSQLState()) { |
33
|
1065 |
|
case '40001': |
34
|
1065 |
|
case '40P01': |
35
|
54 |
|
return new Exception\DeadlockException($message, $exception); |
36
|
1065 |
|
case '0A000': |
37
|
|
|
// Foreign key constraint violations during a TRUNCATE operation |
38
|
|
|
// are considered "feature not supported" in PostgreSQL. |
39
|
7 |
|
if (strpos($exception->getMessage(), 'truncate') !== false) { |
40
|
7 |
|
return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
break; |
44
|
1058 |
|
case '23502': |
45
|
61 |
|
return new Exception\NotNullConstraintViolationException($message, $exception); |
46
|
|
|
|
47
|
1051 |
|
case '23503': |
48
|
75 |
|
return new Exception\ForeignKeyConstraintViolationException($message, $exception); |
49
|
|
|
|
50
|
1030 |
|
case '23505': |
51
|
68 |
|
return new Exception\UniqueConstraintViolationException($message, $exception); |
52
|
|
|
|
53
|
1016 |
|
case '42601': |
54
|
61 |
|
return new Exception\SyntaxErrorException($message, $exception); |
55
|
|
|
|
56
|
1009 |
|
case '42702': |
57
|
61 |
|
return new Exception\NonUniqueFieldNameException($message, $exception); |
58
|
|
|
|
59
|
1002 |
|
case '42703': |
60
|
61 |
|
return new Exception\InvalidFieldNameException($message, $exception); |
61
|
|
|
|
62
|
995 |
|
case '42P01': |
63
|
368 |
|
return new Exception\TableNotFoundException($message, $exception); |
64
|
|
|
|
65
|
932 |
|
case '42P07': |
66
|
362 |
|
return new Exception\TableExistsException($message, $exception); |
67
|
|
|
|
68
|
624 |
|
case '7': |
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
|
75 |
|
if (strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { |
73
|
75 |
|
return new Exception\ConnectionException($message, $exception); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
|
79
|
603 |
|
return new Exception\DriverException($message, $exception); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
297 |
|
public function createDatabasePlatformForVersion($version) |
86
|
|
|
{ |
87
|
297 |
|
if (! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { |
88
|
54 |
|
throw DBALException::invalidPlatformVersionSpecified( |
89
|
54 |
|
$version, |
90
|
54 |
|
'<major_version>.<minor_version>.<patch_version>' |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
243 |
|
$majorVersion = $versionParts['major']; |
95
|
243 |
|
$minorVersion = $versionParts['minor'] ?? 0; |
96
|
243 |
|
$patchVersion = $versionParts['patch'] ?? 0; |
97
|
243 |
|
$version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
98
|
|
|
|
99
|
|
|
switch (true) { |
100
|
243 |
|
case version_compare($version, '10.0', '>='): |
101
|
108 |
|
return new PostgreSQL100Platform(); |
102
|
189 |
|
case version_compare($version, '9.4', '>='): |
103
|
135 |
|
return new PostgreSQL94Platform(); |
104
|
108 |
|
case version_compare($version, '9.2', '>='): |
105
|
108 |
|
return new PostgreSQL92Platform(); |
106
|
54 |
|
case version_compare($version, '9.1', '>='): |
107
|
54 |
|
return new PostgreSQL91Platform(); |
108
|
|
|
default: |
109
|
54 |
|
return new PostgreSqlPlatform(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
582 |
|
public function getDatabase(Connection $conn) |
117
|
|
|
{ |
118
|
582 |
|
$params = $conn->getParams(); |
119
|
|
|
|
120
|
582 |
|
return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn(); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
54 |
|
public function getDatabasePlatform() |
127
|
|
|
{ |
128
|
54 |
|
return new PostgreSqlPlatform(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
110 |
|
public function getSchemaManager(Connection $conn) |
135
|
|
|
{ |
136
|
110 |
|
return new PostgreSqlSchemaManager($conn); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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: