Completed
Push — master ( 9b88bf...94cec7 )
by Marco
31s queued 15s
created

Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php (2 issues)

1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
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...
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\Driver;
8
use Doctrine\DBAL\Exception;
9
use Doctrine\DBAL\Platforms\SQLAnywhere11Platform;
10
use Doctrine\DBAL\Platforms\SQLAnywhere12Platform;
11
use Doctrine\DBAL\Platforms\SQLAnywhere16Platform;
12
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
13
use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;
14
use Doctrine\DBAL\VersionAwarePlatformDriver;
15
use function preg_match;
16
use function version_compare;
17
18
/**
19
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SAP Sybase SQL Anywhere based drivers.
20
 */
21
abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
22
{
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html
27
     */
28 54
    public function convertException($message, DriverException $exception)
29
    {
30 54
        switch ($exception->getErrorCode()) {
31 54
            case '-306':
32 54
            case '-307':
33 54
            case '-684':
34 54
                return new Exception\DeadlockException($message, $exception);
35 54
            case '-210':
36 54
            case '-1175':
37 54
            case '-1281':
38 54
                return new Exception\LockWaitTimeoutException($message, $exception);
39 54
            case '-100':
40 54
            case '-103':
41 54
            case '-832':
42 54
                return new Exception\ConnectionException($message, $exception);
43 54
            case '-143':
44 54
                return new Exception\InvalidFieldNameException($message, $exception);
45 54
            case '-193':
46 54
            case '-196':
47 54
                return new Exception\UniqueConstraintViolationException($message, $exception);
48 54
            case '-194':
49 54
            case '-198':
50 54
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
51 54
            case '-144':
52 54
                return new Exception\NonUniqueFieldNameException($message, $exception);
53 54
            case '-184':
54 54
            case '-195':
55 54
                return new Exception\NotNullConstraintViolationException($message, $exception);
56 54
            case '-131':
57 54
                return new Exception\SyntaxErrorException($message, $exception);
58 54
            case '-110':
59 54
                return new Exception\TableExistsException($message, $exception);
60 54
            case '-141':
61 54
            case '-1041':
62 54
                return new Exception\TableNotFoundException($message, $exception);
63
        }
64
65 54
        return new Exception\DriverException($message, $exception);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 108
    public function createDatabasePlatformForVersion($version)
72
    {
73 108
        if (! preg_match(
74 108
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
75 108
            $version,
76 108
            $versionParts
77
        )) {
78 54
            throw DBALException::invalidPlatformVersionSpecified(
79 54
                $version,
80 54
                '<major_version>.<minor_version>.<patch_version>.<build_version>'
81
            );
82
        }
83
84 54
        $majorVersion = $versionParts['major'];
85 54
        $minorVersion = $versionParts['minor'] ?? 0;
86 54
        $patchVersion = $versionParts['patch'] ?? 0;
87 54
        $buildVersion = $versionParts['build'] ?? 0;
88 54
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;
89
90
        switch (true) {
91 54
            case version_compare($version, '16', '>='):
92 54
                return new SQLAnywhere16Platform();
93 54
            case version_compare($version, '12', '>='):
94 54
                return new SQLAnywhere12Platform();
95 54
            case version_compare($version, '11', '>='):
96 54
                return new SQLAnywhere11Platform();
97
            default:
98 54
                return new SQLAnywherePlatform();
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 54
    public function getDatabase(Connection $conn)
106
    {
107 54
        $params = $conn->getParams();
108
109 54
        return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $params['dbname']...NAME()')->fetchColumn() also could return the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver::getDatabase() of string.
Loading history...
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 54
    public function getDatabasePlatform()
116
    {
117 54
        return new SQLAnywhere12Platform();
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 54
    public function getSchemaManager(Connection $conn)
124
    {
125 54
        return new SQLAnywhereSchemaManager($conn);
126
    }
127
}
128