Failed Conditions
Push — master ( 7bbed5...cf98cc )
by Sergei
84:08 queued 81:00
created

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

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver;
21
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver;
24
use Doctrine\DBAL\Exception;
25
use Doctrine\DBAL\Platforms\SQLAnywhere11Platform;
26
use Doctrine\DBAL\Platforms\SQLAnywhere12Platform;
27
use Doctrine\DBAL\Platforms\SQLAnywhere16Platform;
28
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
29
use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;
30
use Doctrine\DBAL\VersionAwarePlatformDriver;
31
use function preg_match;
32
use function version_compare;
33
34
/**
35
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SAP Sybase SQL Anywhere based drivers.
36
 *
37
 * @author Steve Müller <[email protected]>
38
 * @link   www.doctrine-project.org
39
 * @since  2.5
40
 */
41
abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
42
{
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html
47
     */
48 38
    public function convertException($message, DriverException $exception)
49
    {
50 38
        switch ($exception->getErrorCode()) {
51 38
            case '-306':
52 38
            case '-307':
53 38
            case '-684':
54 38
                return new Exception\DeadlockException($message, $exception);
55 38
            case '-210':
56 38
            case '-1175':
57 38
            case '-1281':
58 38
                return new Exception\LockWaitTimeoutException($message, $exception);
59 38
            case '-100':
60 38
            case '-103':
61 38
            case '-832':
62 38
                return new Exception\ConnectionException($message, $exception);
63 38
            case '-143':
64 38
                return new Exception\InvalidFieldNameException($message, $exception);
65 38
            case '-193':
66 38
            case '-196':
67 38
                return new Exception\UniqueConstraintViolationException($message, $exception);
68 38
            case '-194':
69 38
            case '-198':
70 38
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
71 38
            case '-144':
72 38
                return new Exception\NonUniqueFieldNameException($message, $exception);
73 38
            case '-184':
74 38
            case '-195':
75 38
                return new Exception\NotNullConstraintViolationException($message, $exception);
76 38
            case '-131':
77 38
                return new Exception\SyntaxErrorException($message, $exception);
78 38
            case '-110':
79 38
                return new Exception\TableExistsException($message, $exception);
80 38
            case '-141':
81 38
            case '-1041':
82 38
                return new Exception\TableNotFoundException($message, $exception);
83
        }
84
85 38
        return new Exception\DriverException($message, $exception);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 76
    public function createDatabasePlatformForVersion($version)
92
    {
93 76
        if ( ! preg_match(
94 76
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
95 76
            $version,
96 76
            $versionParts
97
        )) {
98 38
            throw DBALException::invalidPlatformVersionSpecified(
99 38
                $version,
100 38
                '<major_version>.<minor_version>.<patch_version>.<build_version>'
101
            );
102
        }
103
104 38
        $majorVersion = $versionParts['major'];
105 38
        $minorVersion = $versionParts['minor'] ?? 0;
106 38
        $patchVersion = $versionParts['patch'] ?? 0;
107 38
        $buildVersion = $versionParts['build'] ?? 0;
108 38
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;
109
110
        switch(true) {
111 38
            case version_compare($version, '16', '>='):
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
112 38
                return new SQLAnywhere16Platform();
113 38
            case version_compare($version, '12', '>='):
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114 38
                return new SQLAnywhere12Platform();
115 38
            case version_compare($version, '11', '>='):
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
116 38
                return new SQLAnywhere11Platform();
117
            default:
0 ignored issues
show
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
118 38
                return new SQLAnywherePlatform();
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 38
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
126
    {
127 38
        $params = $conn->getParams();
128
129 38
        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 boolean which is incompatible with the return type mandated by Doctrine\DBAL\Driver::getDatabase() of string.
Loading history...
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 38
    public function getDatabasePlatform()
136
    {
137 38
        return new SQLAnywhere12Platform();
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 38
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
144
    {
145 38
        return new SQLAnywhereSchemaManager($conn);
146
    }
147
}
148