Unverified Commit 78de4498 authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #4074 from morozov/issues/4073

Fix handling host and port configuration in sqlsrv and pdo_sqlsrv drivers
parents e5e3abfe b8f58f2a
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\AbstractDriverException;
/**
* @internal
*
* @psalm-immutable
*/
final class PortWithoutHost extends AbstractDriverException
{
public static function new() : self
{
return new self('Connection port specified without the host');
}
}
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver\PortWithoutHost;
use function is_int;
use function sprintf;
......@@ -48,11 +49,13 @@ class Driver extends AbstractSQLServerDriver
if (isset($params['host'])) {
$dsn .= $params['host'];
}
if (isset($params['port']) && ! empty($params['port'])) {
if (isset($params['port'])) {
$dsn .= ',' . $params['port'];
}
} elseif (isset($params['port'])) {
throw PortWithoutHost::new();
}
if (isset($params['dbname'])) {
$connectionOptions['Database'] = $params['dbname'];
......
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Driver\SQLSrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver\PortWithoutHost;
/**
* Driver for ext/sqlsrv.
......@@ -14,13 +15,16 @@ class Driver extends AbstractSQLServerDriver
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
if (! isset($params['host'])) {
throw new SQLSrvException("Missing 'host' in configuration for sqlsrv driver.");
}
$serverName = '';
if (isset($params['host'])) {
$serverName = $params['host'];
if (isset($params['port'])) {
$serverName .= ', ' . $params['port'];
$serverName .= ',' . $params['port'];
}
} elseif (isset($params['port'])) {
throw PortWithoutHost::new();
}
if (isset($params['dbname'])) {
......
......@@ -3,8 +3,7 @@
namespace Doctrine\Tests\DBAL\Driver;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver\PortWithoutHost;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLServer2005Platform;
use Doctrine\DBAL\Platforms\SQLServer2008Platform;
......@@ -13,13 +12,8 @@ use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\SQLServerSchemaManager;
class AbstractSQLServerDriverTest extends AbstractDriverTest
abstract class AbstractSQLServerDriverTest extends AbstractDriverTest
{
protected function createDriver() : Driver
{
return $this->getMockForAbstractClass(AbstractSQLServerDriver::class);
}
protected function createPlatform() : AbstractPlatform
{
return new SQLServer2008Platform();
......@@ -64,4 +58,10 @@ class AbstractSQLServerDriverTest extends AbstractDriverTest
['12', SQLServer2012Platform::class],
];
}
public function testPortWithoutHost() : void
{
$this->expectException(PortWithoutHost::class);
$this->driver->connect(['port' => 1433]);
}
}
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