Issues (201)

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