Completed
Push — master ( 8575c2...a53269 )
by Marco
02:31 queued 01:15
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
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
32
/**
33
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SAP Sybase SQL Anywhere based drivers.
34
 *
35
 * @author Steve Müller <[email protected]>
36
 * @link   www.doctrine-project.org
37
 * @since  2.5
38
 */
39
abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
40
{
41
    /**
42
     * {@inheritdoc}
43
     *
44
     * @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html
45
     */
46 2
    public function convertException($message, DriverException $exception)
47
    {
48 2
        switch ($exception->getErrorCode()) {
49 2
            case '-306':
50 2
            case '-307':
51 2
            case '-684':
52 2
                return new Exception\DeadlockException($message, $exception);
53 2
            case '-210':
54 2
            case '-1175':
55 2
            case '-1281':
56 2
                return new Exception\LockWaitTimeoutException($message, $exception);
57 2
            case '-100':
58 2
            case '-103':
59 2
            case '-832':
60 2
                return new Exception\ConnectionException($message, $exception);
61 2
            case '-143':
62 2
                return new Exception\InvalidFieldNameException($message, $exception);
63 2
            case '-193':
64 2
            case '-196':
65 2
                return new Exception\UniqueConstraintViolationException($message, $exception);
66 2
            case '-194':
67 2
            case '-198':
68 2
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
69 2
            case '-144':
70 2
                return new Exception\NonUniqueFieldNameException($message, $exception);
71 2
            case '-184':
72 2
            case '-195':
73 2
                return new Exception\NotNullConstraintViolationException($message, $exception);
74 2
            case '-131':
75 2
                return new Exception\SyntaxErrorException($message, $exception);
76 2
            case '-110':
77 2
                return new Exception\TableExistsException($message, $exception);
78 2
            case '-141':
79 2
            case '-1041':
80 2
                return new Exception\TableNotFoundException($message, $exception);
81
        }
82
83 2
        return new Exception\DriverException($message, $exception);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 4
    public function createDatabasePlatformForVersion($version)
90
    {
91 4
        if ( ! preg_match(
92 4
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
93 4
            $version,
94 4
            $versionParts
95
        )) {
96 2
            throw DBALException::invalidPlatformVersionSpecified(
97 2
                $version,
98 2
                '<major_version>.<minor_version>.<patch_version>.<build_version>'
99
            );
100
        }
101
102 2
        $majorVersion = $versionParts['major'];
103 2
        $minorVersion = $versionParts['minor'] ?? 0;
104 2
        $patchVersion = $versionParts['patch'] ?? 0;
105 2
        $buildVersion = $versionParts['build'] ?? 0;
106 2
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;
107
108
        switch(true) {
109 2
            case version_compare($version, '16', '>='):
0 ignored issues
show
Coding Style introduced by
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...
110 2
                return new SQLAnywhere16Platform();
111 2
            case version_compare($version, '12', '>='):
0 ignored issues
show
Coding Style introduced by
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 2
                return new SQLAnywhere12Platform();
113 2
            case version_compare($version, '11', '>='):
0 ignored issues
show
Coding Style introduced by
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 2
                return new SQLAnywhere11Platform();
115
            default:
0 ignored issues
show
Coding Style introduced by
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...
116 2
                return new SQLAnywherePlatform();
117
        }
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 2
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
124
    {
125 2
        $params = $conn->getParams();
126
127 2
        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...
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 2
    public function getDatabasePlatform()
134
    {
135 2
        return new SQLAnywhere12Platform();
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 2
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
142
    {
143 2
        return new SQLAnywhereSchemaManager($conn);
144
    }
145
}
146