Completed
Push — develop ( dcb0ff...425513 )
by Sergei
23s queued 13s
created

AbstractSQLAnywhereDriver::getDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver;
6
7
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...
8
use Doctrine\DBAL\Driver;
9
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
10
use Doctrine\DBAL\Exception;
11
use Doctrine\DBAL\Exception\DriverException;
0 ignored issues
show
Bug introduced by
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...
12
use Doctrine\DBAL\Platforms\AbstractPlatform;
13
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
14
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
15
use Doctrine\DBAL\Schema\AbstractSchemaManager;
16
use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;
17
use Doctrine\DBAL\VersionAwarePlatformDriver;
18
use function preg_match;
19
20
/**
21
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SAP Sybase SQL Anywhere based drivers.
22
 */
23
abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
24
{
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html
29
     */
30 196
    public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
31
    {
32 196
        switch ($exception->getCode()) {
33
            case -306:
34
            case -307:
35
            case -684:
36 3
                return new Exception\DeadlockException($message, $exception);
37
            case -210:
38
            case -1175:
39
            case -1281:
40 3
                return new Exception\LockWaitTimeoutException($message, $exception);
41
            case -100:
42
            case -103:
43
            case -832:
44 178
                return new Exception\ConnectionException($message, $exception);
45
            case -143:
46 1
                return new Exception\InvalidFieldNameException($message, $exception);
47
            case -193:
48
            case -196:
49 2
                return new Exception\UniqueConstraintViolationException($message, $exception);
50
            case -194:
51
            case -198:
52 1
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
53
            case -144:
54 1
                return new Exception\NonUniqueFieldNameException($message, $exception);
55
            case -184:
56
            case -195:
57 2
                return new Exception\NotNullConstraintViolationException($message, $exception);
58
            case -131:
59 1
                return new Exception\SyntaxErrorException($message, $exception);
60
            case -110:
61 1
                return new Exception\TableExistsException($message, $exception);
62
            case -141:
63
            case -1041:
64 2
                return new Exception\TableNotFoundException($message, $exception);
65
        }
66
67 1
        return new DriverException($message, $exception);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 127
    public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
74
    {
75 127
        if (! preg_match(
76 2
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
77 127
            $version,
78 127
            $versionParts
79
        )) {
80 101
            throw InvalidPlatformVersion::new(
81 101
                $version,
82 101
                '<major_version>.<minor_version>.<patch_version>.<build_version>'
83
            );
84
        }
85
86 126
        switch (true) {
87
            default:
88 126
                return new SQLAnywherePlatform();
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 76
    public function getDatabasePlatform() : AbstractPlatform
96
    {
97 76
        return new SQLAnywherePlatform();
98
    }
99 76
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getSchemaManager(Connection $conn) : AbstractSchemaManager
104
    {
105 51
        return new SQLAnywhereSchemaManager($conn);
106
    }
107
}
108