Failed Conditions
Push — master ( 656579...2742cd )
by Marco
11:55
created

Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php (1 issue)

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', '>='):
110 2
                return new SQLAnywhere16Platform();
111 2
            case version_compare($version, '12', '>='):
112 2
                return new SQLAnywhere12Platform();
113 2
            case version_compare($version, '11', '>='):
114 2
                return new SQLAnywhere11Platform();
115
            default:
116 2
                return new SQLAnywherePlatform();
117
        }
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 2 View Code Duplication
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
124
    {
125 2
        $params = $conn->getParams();
126
127 2
        if (isset($params['dbname'])) {
128 2
            return $params['dbname'];
129
        }
130
131
        return $conn->query('SELECT DB_NAME()')->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $conn->query('SEL...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...
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 2
    public function getDatabasePlatform()
138
    {
139 2
        return new SQLAnywhere12Platform();
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 2
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
146
    {
147 2
        return new SQLAnywhereSchemaManager($conn);
148
    }
149
}
150