Driver.php 2.86 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
use function array_keys;
use function array_map;
use function implode;
10 11 12 13

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

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

49 50 51
    /**
     * Build the connection string for given connection parameters and driver options.
     *
52 53 54 55 56 57 58 59 60
     * @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.
61 62 63
     *
     * @return string
     */
64
    private function buildDsn($host, $port, $server, $dbname, $username = null, $password = null, array $driverOptions = [])
65
    {
66
        $host = $host ?: 'localhost';
67
        $port = $port ?: 2638;
68

69 70 71 72
        if (! empty($server)) {
            $server = ';ServerName=' . $server;
        }

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