Completed
Push — master ( 5dd66e...5b5c2c )
by Marco
114:19 queued 111:15
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 304
    public function convertException($message, DriverException $exception)
29
    {
30 304
        switch ($exception->getErrorCode()) {
31 304
            case '-306':
32 304
            case '-307':
33 304
            case '-684':
34 304
                return new Exception\DeadlockException($message, $exception);
35 304
            case '-210':
36 304
            case '-1175':
37 304
            case '-1281':
38 304
                return new Exception\LockWaitTimeoutException($message, $exception);
39 304
            case '-100':
40 304
            case '-103':
41 304
            case '-832':
42 304
                return new Exception\ConnectionException($message, $exception);
43 304
            case '-143':
44 304
                return new Exception\InvalidFieldNameException($message, $exception);
45 304
            case '-193':
46 304
            case '-196':
47 304
                return new Exception\UniqueConstraintViolationException($message, $exception);
48 304
            case '-194':
49 304
            case '-198':
50 304
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
51 304
            case '-144':
52 304
                return new Exception\NonUniqueFieldNameException($message, $exception);
53 304
            case '-184':
54 304
            case '-195':
55 304
                return new Exception\NotNullConstraintViolationException($message, $exception);
56 304
            case '-131':
57 304
                return new Exception\SyntaxErrorException($message, $exception);
58 304
            case '-110':
59 304
                return new Exception\TableExistsException($message, $exception);
60 304
            case '-141':
61 304
            case '-1041':
62 304
                return new Exception\TableNotFoundException($message, $exception);
63
        }
64
65 304
        return new Exception\DriverException($message, $exception);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 283
    public function createDatabasePlatformForVersion($version)
72
    {
73 283
        if (! preg_match(
74 8
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
75 283
            $version,
76 283
            $versionParts
77
        )) {
78 254
            throw DBALException::invalidPlatformVersionSpecified(
79 254
                $version,
80 254
                '<major_version>.<minor_version>.<patch_version>.<build_version>'
81
            );
82
        }
83
84 279
        $majorVersion = $versionParts['major'];
85 279
        $minorVersion = $versionParts['minor'] ?? 0;
86 279
        $patchVersion = $versionParts['patch'] ?? 0;
87 279
        $buildVersion = $versionParts['build'] ?? 0;
88 279
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;
89
90
        switch (true) {
91 279
            case version_compare($version, '16', '>='):
92 279
                return new SQLAnywhere16Platform();
93 279
            case version_compare($version, '12', '>='):
94 279
                return new SQLAnywhere12Platform();
95 279
            case version_compare($version, '11', '>='):
96 279
                return new SQLAnywhere11Platform();
97
            default:
98 279
                return new SQLAnywherePlatform();
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 229
    public function getDatabase(Connection $conn)
106
    {
107 229
        $params = $conn->getParams();
108
109 229
        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 204
    public function getDatabasePlatform()
116
    {
117 204
        return new SQLAnywhere12Platform();
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 179
    public function getSchemaManager(Connection $conn)
124
    {
125 179
        return new SQLAnywhereSchemaManager($conn);
126
    }
127
}
128