Driver.php 2.89 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Driver\SQLAnywhere;

5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Driver\AbstractSQLAnywhereDriver;
7

8 9 10
use function array_keys;
use function array_map;
use function implode;
11 12 13 14

/**
 * A Doctrine DBAL driver for the SAP Sybase SQL Anywhere PHP extension.
 */
15
class Driver extends AbstractSQLAnywhereDriver
16 17 18 19
{
    /**
     * {@inheritdoc}
     *
20
     * @throws DBALException If there was a problem establishing the connection.
21
     */
22
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
23
    {
24 25 26
        try {
            return new SQLAnywhereConnection(
                $this->buildDsn(
27 28 29 30
                    $params['host'] ?? null,
                    $params['port'] ?? null,
                    $params['server'] ?? null,
                    $params['dbname'] ?? null,
31 32 33 34
                    $username,
                    $password,
                    $driverOptions
                ),
35
                $params['persistent'] ?? false
36 37 38 39
            );
        } catch (SQLAnywhereException $e) {
            throw DBALException::driverException($this, $e);
        }
40 41 42 43
    }

    /**
     * {@inheritdoc}
44 45
     *
     * @deprecated
46 47 48 49 50 51
     */
    public function getName()
    {
        return 'sqlanywhere';
    }

52 53 54
    /**
     * Build the connection string for given connection parameters and driver options.
     *
55 56 57 58 59 60 61 62 63
     * @param string  $host          Host address to connect to.
     * @param int     $port          Port to use for the connection (default to SQL Anywhere standard port 2638).
     * @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 mixed[] $driverOptions Additional parameters to use for the connection.
64 65 66
     *
     * @return string
     */
67
    private function buildDsn($host, $port, $server, $dbname, $username = null, $password = null, array $driverOptions = [])
68
    {
69
        $host = $host ?: 'localhost';
70
        $port = $port ?: 2638;
71

72 73 74 75
        if (! empty($server)) {
            $server = ';ServerName=' . $server;
        }

76
        return 'HOST=' . $host . ':' . $port .
77
            $server .
78 79 80 81 82
            ';DBN=' . $dbname .
            ';UID=' . $username .
            ';PWD=' . $password .
            ';' . implode(
                ';',
83
                array_map(static function ($key, $value) {
84 85 86 87
                    return $key . '=' . $value;
                }, array_keys($driverOptions), $driverOptions)
            );
    }
Steve Müller's avatar
Steve Müller committed
88
}