Unverified Commit 62fe3fe4 authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3033 from aashmelev/aashmelev-feature-dsn

Added support for available DSN parameters for the PDOSqlsrv driver
parents 041d444f 931d9a56
......@@ -20,6 +20,8 @@
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use function is_int;
use function sprintf;
/**
* The PDO-based Sqlsrv driver.
......@@ -33,8 +35,10 @@ class Driver extends AbstractSQLServerDriver
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
[$driverOptions, $connectionOptions] = $this->splitOptions($driverOptions);
return new Connection(
$this->_constructPdoDsn($params),
$this->_constructPdoDsn($params, $connectionOptions),
$username,
$password,
$driverOptions
......@@ -45,10 +49,11 @@ class Driver extends AbstractSQLServerDriver
* Constructs the Sqlsrv PDO DSN.
*
* @param array $params
* @param string[] $connectionOptions
*
* @return string The DSN.
*/
private function _constructPdoDsn(array $params)
private function _constructPdoDsn(array $params, array $connectionOptions)
{
$dsn = 'sqlsrv:server=';
......@@ -61,16 +66,56 @@ class Driver extends AbstractSQLServerDriver
}
if (isset($params['dbname'])) {
$dsn .= ';Database=' . $params['dbname'];
$connectionOptions['Database'] = $params['dbname'];
}
if (isset($params['MultipleActiveResultSets'])) {
$dsn .= '; MultipleActiveResultSets=' . ($params['MultipleActiveResultSets'] ? 'true' : 'false');
$connectionOptions['MultipleActiveResultSets'] = $params['MultipleActiveResultSets'] ? 'true' : 'false';
}
$dsn .= $this->getConnectionOptionsDsn($connectionOptions);
return $dsn;
}
/**
* Separates a connection options from a driver options
*
* @param int[]|string[] $options
* @return int[][]|string[][]
*/
private function splitOptions(array $options) : array
{
$driverOptions = [];
$connectionOptions = [];
foreach ($options as $optionKey => $optionValue) {
if (is_int($optionKey)) {
$driverOptions[$optionKey] = $optionValue;
} else {
$connectionOptions[$optionKey] = $optionValue;
}
}
return [$driverOptions, $connectionOptions];
}
/**
* Converts a connection options array to the DSN
*
* @param string[] $connectionOptions
*/
private function getConnectionOptionsDsn(array $connectionOptions) : string
{
$connectionOptionsDsn = '';
foreach ($connectionOptions as $paramName => $paramValue) {
$connectionOptionsDsn .= sprintf(';%s=%s', $paramName, $paramValue);
}
return $connectionOptionsDsn;
}
/**
* {@inheritdoc}
*/
......
......@@ -2,8 +2,10 @@
namespace Doctrine\Tests\DBAL\Functional\Driver\PDOSqlsrv;
use Doctrine\DBAL\Driver\Connection as Connection;
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver;
use Doctrine\Tests\DBAL\Functional\Driver\AbstractDriverTest;
use PDO;
use function extension_loaded;
class DriverTest extends AbstractDriverTest
......@@ -16,9 +18,11 @@ class DriverTest extends AbstractDriverTest
parent::setUp();
if (! $this->_conn->getDriver() instanceof Driver) {
$this->markTestSkipped('pdo_sqlsrv only test.');
if ($this->_conn->getDriver() instanceof Driver) {
return;
}
$this->markTestSkipped('pdo_sqlsrv only test.');
}
/**
......@@ -36,4 +40,35 @@ class DriverTest extends AbstractDriverTest
{
return 'master';
}
/**
* @param int[]|string[] $driverOptions
*/
protected function getConnection(array $driverOptions) : Connection
{
return $this->_conn->getDriver()->connect(
[
'host' => $GLOBALS['db_host'],
'port' => $GLOBALS['db_port'],
],
$GLOBALS['db_username'],
$GLOBALS['db_password'],
$driverOptions
);
}
public function testConnectionOptions() : void
{
$connection = $this->getConnection(['APP' => 'APP_NAME']);
$result = $connection->query('SELECT APP_NAME()')->fetchColumn();
self::assertSame('APP_NAME', $result);
}
public function testDriverOptions() : void
{
$connection = $this->getConnection([PDO::ATTR_CASE => PDO::CASE_UPPER]);
self::assertSame(PDO::CASE_UPPER, $connection->getAttribute(PDO::ATTR_CASE));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment