Completed
Pull Request — master (#3666)
by
unknown
61:37
created

createDatabasePlatformForVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 20
ccs 4
cts 4
cp 1
rs 9.8333
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
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...
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\Driver;
8
use Doctrine\DBAL\Driver\AbstractOracleDriver\EasyConnectString;
9
use Doctrine\DBAL\Exception;
10
use Doctrine\DBAL\Platforms\Oracle122Platform;
11
use Doctrine\DBAL\Platforms\OraclePlatform;
12
use Doctrine\DBAL\Schema\OracleSchemaManager;
13
use Doctrine\DBAL\VersionAwarePlatformDriver;
14
use function preg_match;
15
use function version_compare;
16
17
/**
18
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for Oracle based drivers.
19
 */
20 1622
abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
21
{
22 1622
    /**
23 1622
     * {@inheritdoc}
24 1612
     */
25 1606
    public function convertException($message, DriverException $exception)
26 1022
    {
27
        switch ($exception->getErrorCode()) {
28 1600
            case '1':
29 1183
            case '2299':
30
            case '38911':
31 1592
                return new Exception\UniqueConstraintViolationException($message, $exception);
32 1584
33 1164
            case '904':
34
                return new Exception\InvalidFieldNameException($message, $exception);
35 1578
36 1083
            case '918':
37
            case '960':
38 1570
                return new Exception\NonUniqueFieldNameException($message, $exception);
39 1155
40
            case '923':
41 1562
                return new Exception\SyntaxErrorException($message, $exception);
42 1099
43
            case '942':
44 1514
                return new Exception\TableNotFoundException($message, $exception);
45 1479
46 1268
            case '955':
47
                return new Exception\TableExistsException($message, $exception);
48 1446
49 1108
            case '1017':
50
            case '12545':
51 1438
                return new Exception\ConnectionException($message, $exception);
52 1438
53 1438
            case '1400':
54 1214
                return new Exception\NotNullConstraintViolationException($message, $exception);
55
56
            case '2266':
57 1157
            case '2291':
58
            case '2292':
59
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
60
        }
61
62
        return new Exception\DriverException($message, $exception);
63 1440
    }
64
65 1440
    /**
66
     * {@inheritdoc}
67 1440
     */
68
    public function createDatabasePlatformForVersion($version)
69
    {
70
        //Example: "Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production"
71
        if (! preg_match('/\s+((?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.(\d+)\.(\d+))\s+/', $version, $versionParts)) {
72
            throw DBALException::invalidPlatformVersionSpecified(
73 948
                $version,
74
                '<major_version>.<minor_version>.<patch_version>'
75 948
            );
76
        }
77
78
        $majorVersion = $versionParts['major'];
79
        $minorVersion = $versionParts['minor'] ?? 0;
80
        $patchVersion = $versionParts['patch'] ?? 0;
81 902
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
82
83 902
        switch (true) {
84
            case version_compare($version, '12.2', '>='):
85
                return new Oracle122Platform();
86
            default:
87
                return new OraclePlatform();
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93 64
     */
94
    public function getDatabase(Connection $conn)
95 64
    {
96
        $params = $conn->getParams();
97
98
        return $params['user'];
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getDatabasePlatform()
105
    {
106
        return new OraclePlatform();
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getSchemaManager(Connection $conn)
113
    {
114
        return new OracleSchemaManager($conn);
115
    }
116
117
    /**
118
     * Returns an appropriate Easy Connect String for the given parameters.
119
     *
120
     * @param mixed[] $params The connection parameters to return the Easy Connect String for.
121
     *
122
     * @return string
123
     */
124
    protected function getEasyConnectString(array $params)
125
    {
126
        return (string) EasyConnectString::fromConnectionParameters($params);
127
    }
128
}
129