Issues (201)

src/Driver/AbstractPostgreSQLDriver.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver;
6
7
use Doctrine\DBAL\Connection;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Doctrine\DBAL\Driver\Connection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
9
use Doctrine\DBAL\Exception;
10
use Doctrine\DBAL\Exception\DriverException;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Doctrine\DBAL\Driver\DriverException. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Doctrine\DBAL\Platforms\AbstractPlatform;
12
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
13
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
14
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
15
use Doctrine\DBAL\Schema\AbstractSchemaManager;
16
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
17
use Doctrine\DBAL\VersionAwarePlatformDriver;
18
use function preg_match;
19
use function strpos;
20
use function version_compare;
21
22
/**
23
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for PostgreSQL based drivers.
24
 */
25
abstract class AbstractPostgreSQLDriver implements ExceptionConverterDriver, VersionAwarePlatformDriver
26
{
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @link http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
31
     */
32 844
    public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
33
    {
34 844
        switch ($exception->getSQLState()) {
35 844
            case '40001':
36 817
            case '40P01':
37 54
                return new Exception\DeadlockException($message, $exception);
38
39 790
            case '0A000':
40
                // Foreign key constraint violations during a TRUNCATE operation
41
                // are considered "feature not supported" in PostgreSQL.
42 5
                if (strpos($exception->getMessage(), 'truncate') !== false) {
43 5
                    return new Exception\ForeignKeyConstraintViolationException($message, $exception);
44
                }
45
46
                break;
47
48 785
            case '23502':
49 32
                return new Exception\NotNullConstraintViolationException($message, $exception);
50
51 753
            case '23503':
52 42
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
53
54 711
            case '23505':
55 37
                return new Exception\UniqueConstraintViolationException($message, $exception);
56
57 674
            case '42601':
58 32
                return new Exception\SyntaxErrorException($message, $exception);
59
60 642
            case '42702':
61 32
                return new Exception\NonUniqueFieldNameException($message, $exception);
62
63 610
            case '42703':
64 32
                return new Exception\InvalidFieldNameException($message, $exception);
65
66 578
            case '42P01':
67 252
                return new Exception\TableNotFoundException($message, $exception);
68
69 471
            case '42P07':
70 82
                return new Exception\TableExistsException($message, $exception);
71
        }
72
73
        // In some case (mainly connection errors) the PDO exception does not provide a SQLSTATE via its code.
74
        // The exception code is always set to 7 here.
75
        // We have to match against the SQLSTATE in the error message in these cases.
76 389
        if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) {
77 37
            return new Exception\ConnectionException($message, $exception);
78
        }
79
80 352
        return new DriverException($message, $exception);
81
    }
82
83 174
    public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
84
    {
85 174
        if (preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts) === 0) {
86 27
            throw InvalidPlatformVersion::new(
87 27
                $version,
88 27
                '<major_version>.<minor_version>.<patch_version>'
89
            );
90
        }
91
92 147
        $majorVersion = $versionParts['major'];
93 147
        $minorVersion = $versionParts['minor'] ?? 0;
94 147
        $patchVersion = $versionParts['patch'] ?? 0;
95 147
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
96
97 147
        if (version_compare($version, '10.0', '>=')) {
98 75
            return new PostgreSQL100Platform();
99
        }
100
101 99
        return new PostgreSQL94Platform();
102
    }
103
104 27
    public function getDatabasePlatform() : AbstractPlatform
105
    {
106 27
        return new PostgreSQL94Platform();
107
    }
108
109 67
    public function getSchemaManager(Connection $conn) : AbstractSchemaManager
110
    {
111 67
        return new PostgreSqlSchemaManager($conn);
112
    }
113
}
114