Driver.php 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?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;

22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver\AbstractSQLAnywhereDriver;
24 25 26
use function array_keys;
use function array_map;
use function implode;
27 28 29 30 31 32 33 34

/**
 * 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 extends AbstractSQLAnywhereDriver
36 37 38 39
{
    /**
     * {@inheritdoc}
     *
40
     * @throws \Doctrine\DBAL\DBALException if there was a problem establishing the connection.
41
     */
42
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
43
    {
44 45 46
        try {
            return new SQLAnywhereConnection(
                $this->buildDsn(
47 48 49 50
                    $params['host'] ?? null,
                    $params['port'] ?? null,
                    $params['server'] ?? null,
                    $params['dbname'] ?? null,
51 52 53 54
                    $username,
                    $password,
                    $driverOptions
                ),
55
                $params['persistent'] ?? false
56 57 58 59
            );
        } catch (SQLAnywhereException $e) {
            throw DBALException::driverException($this, $e);
        }
60 61 62 63 64 65 66 67 68 69
    }

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

70 71 72
    /**
     * Build the connection string for given connection parameters and driver options.
     *
73 74 75 76 77 78 79 80 81
     * @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 array  $driverOptions Additional parameters to use for the connection.
82 83 84
     *
     * @return string
     */
85
    private function buildDsn($host, $port, $server, $dbname, $username = null, $password = null, array $driverOptions = [])
86
    {
87
        $host = $host ?: 'localhost';
88
        $port = $port ?: 2638;
89

90 91 92 93
        if (! empty($server)) {
            $server = ';ServerName=' . $server;
        }

94
        return
95 96
            'HOST=' . $host . ':' . $port .
            $server .
97 98 99 100 101 102 103 104 105 106
            ';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
107
}