AbstractSQLAnywhereDriver.php 3.71 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Driver;

5
use Doctrine\DBAL\Connection;
6 7
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
8
use Doctrine\DBAL\Platforms\SQLAnywhere16Platform;
9 10
use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
11
use function preg_match;
12 13 14 15

/**
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SAP Sybase SQL Anywhere based drivers.
 */
16
abstract class AbstractSQLAnywhereDriver implements ExceptionConverterDriver, VersionAwarePlatformDriver
17 18 19 20 21 22 23 24 25
{
    /**
     * {@inheritdoc}
     *
     * @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html
     */
    public function convertException($message, DriverException $exception)
    {
        switch ($exception->getErrorCode()) {
26
            case '-306':
27 28
            case '-307':
            case '-684':
29
                return new Exception\DeadlockException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
30

31 32 33 34
            case '-210':
            case '-1175':
            case '-1281':
                return new Exception\LockWaitTimeoutException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
35

36 37 38 39
            case '-100':
            case '-103':
            case '-832':
                return new Exception\ConnectionException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
40

41 42
            case '-143':
                return new Exception\InvalidFieldNameException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
43

44 45 46
            case '-193':
            case '-196':
                return new Exception\UniqueConstraintViolationException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
47

48
            case '-194':
49 50
            case '-198':
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
51

52 53
            case '-144':
                return new Exception\NonUniqueFieldNameException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
54

55 56 57
            case '-184':
            case '-195':
                return new Exception\NotNullConstraintViolationException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
58

59 60
            case '-131':
                return new Exception\SyntaxErrorException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
61

62 63
            case '-110':
                return new Exception\TableExistsException($message, $exception);
Sergei Morozov's avatar
Sergei Morozov committed
64

65 66 67 68 69 70 71 72 73 74 75 76 77
            case '-141':
            case '-1041':
                return new Exception\TableNotFoundException($message, $exception);
        }

        return new Exception\DriverException($message, $exception);
    }

    /**
     * {@inheritdoc}
     */
    public function createDatabasePlatformForVersion($version)
    {
78
        if (preg_match(
79 80 81
            '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
            $version,
            $versionParts
82
        ) === 0) {
83 84 85 86 87 88 89
            throw DBALException::invalidPlatformVersionSpecified(
                $version,
                '<major_version>.<minor_version>.<patch_version>.<build_version>'
            );
        }

        $majorVersion = $versionParts['major'];
90 91 92
        $minorVersion = $versionParts['minor'] ?? 0;
        $patchVersion = $versionParts['patch'] ?? 0;
        $buildVersion = $versionParts['build'] ?? 0;
93 94
        $version      = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;

95
        switch (true) {
96
            default:
97
                return new SQLAnywhere16Platform();
98 99 100 101 102 103
        }
    }

    /**
     * {@inheritdoc}
     */
104
    public function getDatabase(Connection $conn)
105 106 107
    {
        $params = $conn->getParams();

108
        return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchOne();
109 110 111 112 113 114 115
    }

    /**
     * {@inheritdoc}
     */
    public function getDatabasePlatform()
    {
116
        return new SQLAnywhere16Platform();
117 118 119 120 121
    }

    /**
     * {@inheritdoc}
     */
122
    public function getSchemaManager(Connection $conn)
123 124 125 126
    {
        return new SQLAnywhereSchemaManager($conn);
    }
}