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
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
    }

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

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

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

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