Driver.php 6.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Driver\SQLAnywhere;

use Doctrine\DBAL\Connection;
23
use Doctrine\DBAL\DBALException;
Steve Müller's avatar
Steve Müller committed
24
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
25 26 27 28 29 30 31 32 33 34
use Doctrine\DBAL\Platforms\SQLAnywhere12Platform;
use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;

/**
 * A Doctrine DBAL driver for the SAP Sybase SQL Anywhere PHP extension.
 *
 * @author Steve Müller <st.mueller@dzh-online.de>
 * @link   www.doctrine-project.org
 * @since  2.5
 */
35
class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
36 37 38 39
{
    /**
     * {@inheritdoc}
     *
40 41
     * @throws \Doctrine\DBAL\DBALException if there was a problem establishing the connection.
     * @throws SQLAnywhereException         if a mandatory connection parameter is missing.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
     */
    public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
    {
        if ( ! isset($params['host'])) {
            throw new SQLAnywhereException("Missing 'host' in configuration for sqlanywhere driver.");
        }

        if ( ! isset($params['server'])) {
            throw new SQLAnywhereException("Missing 'server' in configuration for sqlanywhere driver.");
        }

        if ( ! isset($params['dbname'])) {
            throw new SQLAnywhereException("Missing 'dbname' in configuration for sqlanywhere driver.");
        }

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        try {
            return new SQLAnywhereConnection(
                $this->buildDsn(
                    $params['host'],
                    isset($params['port']) ? $params['port'] : null,
                    $params['server'],
                    $params['dbname'],
                    $username,
                    $password,
                    $driverOptions
                ),
                isset($params['persistent']) ? $params['persistent'] : false
            );
        } catch (SQLAnywhereException $e) {
            throw DBALException::driverException($this, $e);
        }
73 74
    }

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    /**
     * {@inheritdoc}
     */
    public function convertExceptionCode(\Exception $exception)
    {
        switch ($exception->getCode()) {
            case '-100':
            case '-103':
            case '-832':
                return DBALException::ERROR_ACCESS_DENIED;
            case '-143':
                return DBALException::ERROR_BAD_FIELD_NAME;
            case '-193':
            case '-196':
                return DBALException::ERROR_DUPLICATE_KEY;
            case '-198':
                return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
            case '-144':
                return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
            case '-184':
            case '-195':
                return DBALException::ERROR_NOT_NULL;
            case '-131':
                return DBALException::ERROR_SYNTAX;
            case '-110':
                return DBALException::ERROR_TABLE_ALREADY_EXISTS;
            case '-141':
            case '-1041':
                return DBALException::ERROR_UNKNOWN_TABLE;
        }

106
        return 0;
107 108
    }

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    /**
     * {@inheritdoc}
     */
    public function getDatabase(Connection $conn)
    {
        $params = $conn->getParams();

        return $params['dbname'];
    }

    /**
     * {@inheritdoc}
     */
    public function getDatabasePlatform()
    {
        return new SQLAnywhere12Platform();
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'sqlanywhere';
    }

    /**
     * {@inheritdoc}
     */
    public function getSchemaManager(Connection $conn)
    {
        return new SQLAnywhereSchemaManager($conn);
    }
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

    /**
     * Build the connection string for given connection parameters and driver options.
     *
     * @param string  $host          Host address to connect to.
     * @param integer $port          Port to use for the connection (default to SQL Anywhere standard port 2683).
     * @param string  $server        Database server name on the host to connect to.
     *                               SQL Anywhere allows multiple database server instances on the same host,
     *                               therefore specifying the server instance name to use is mandatory.
     * @param string  $dbname        Name of the database on the server instance to connect to.
     * @param string  $username      User name to use for connection authentication.
     * @param string  $password      Password to use for connection authentication.
     * @param array   $driverOptions Additional parameters to use for the connection.
     *
     * @return string
     */
    private function buildDsn($host, $port, $server, $dbname, $username = null, $password = null, array $driverOptions = array())
    {
        $port = $port ?: 2683;

        return
            'LINKS=tcpip(HOST=' . $host . ';PORT=' . $port . ';DoBroadcast=Direct)' .
            ';ServerName=' . $server .
            ';DBN=' . $dbname .
            ';UID=' . $username .
            ';PWD=' . $password .
            ';' . implode(
                ';',
                array_map(function ($key, $value) {
                    return $key . '=' . $value;
                }, array_keys($driverOptions), $driverOptions)
            );
    }
Steve Müller's avatar
Steve Müller committed
175
}