Unverified Commit e5e3abfe authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #4066 from morozov/issues/4065

Fix DSN detection in the IBM DB2 driver
parents 3745877c 747b0420
......@@ -14,27 +14,16 @@ class DB2Driver extends AbstractDB2Driver
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
if (! isset($params['protocol'])) {
$params['protocol'] = 'TCPIP';
}
if ($params['host'] !== 'localhost' && $params['host'] !== '127.0.0.1') {
// if the host isn't localhost, use extended connection params
$params['dbname'] = 'DRIVER={IBM DB2 ODBC DRIVER}' .
';DATABASE=' . $params['dbname'] .
';HOSTNAME=' . $params['host'] .
';PROTOCOL=' . $params['protocol'] .
';UID=' . $username .
';PWD=' . $password . ';';
if (isset($params['port'])) {
$params['dbname'] .= 'PORT=' . $params['port'];
}
$username = null;
$password = null;
}
$params['user'] = $username;
$params['password'] = $password;
$params['dbname'] = DataSourceName::fromConnectionParameters($params)->toString();
return new DB2Connection($params, (string) $username, (string) $password, $driverOptions);
return new DB2Connection(
$params,
(string) $username,
(string) $password,
$driverOptions
);
}
/**
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\IBMDB2;
use function implode;
use function sprintf;
use function strpos;
/**
* IBM DB2 DSN
*/
final class DataSourceName
{
/** @var string */
private $string;
private function __construct(string $string)
{
$this->string = $string;
}
public function toString() : string
{
return $this->string;
}
/**
* Creates the object from an array representation
*
* @param array<string,mixed> $params
*/
public static function fromArray(array $params) : self
{
$chunks = [];
foreach ($params as $key => $value) {
$chunks[] = sprintf('%s=%s', $key, $value);
}
return new self(implode(';', $chunks));
}
/**
* Creates the object from the given DBAL connection parameters.
*
* @param array<string,mixed> $params
*/
public static function fromConnectionParameters(array $params) : self
{
if (isset($params['dbname']) && strpos($params['dbname'], '=') !== false) {
return new self($params['dbname']);
}
$dsnParams = [];
foreach ([
'host' => 'HOSTNAME',
'port' => 'PORT',
'protocol' => 'PROTOCOL',
'dbname' => 'DATABASE',
'user' => 'UID',
'password' => 'PWD',
] as $dbalParam => $dsnParam) {
if (! isset($params[$dbalParam])) {
continue;
}
$dsnParams[$dsnParam] = $params[$dbalParam];
}
return self::fromArray($dsnParams);
}
}
<?php
namespace Doctrine\Tests\DBAL\Driver\IBMDB2;
use Doctrine\DBAL\Driver\IBMDB2\DataSourceName;
use PHPUnit\Framework\TestCase;
class DataSourceNameTest extends TestCase
{
/**
* @param mixed[] $params
*
* @dataProvider connectionParametersProvider
*/
public function testFromConnectionParameters(array $params, string $expected) : void
{
$dsn = DataSourceName::fromConnectionParameters($params);
$this->assertSame($expected, $dsn->toString());
}
/**
* @return iterable<string,array<int,mixed>>
*/
public static function connectionParametersProvider() : iterable
{
yield 'empty-params' => [[],''];
yield 'cataloged-database' => [
[
'host' => 'localhost',
'port' => 50000,
'dbname' => 'doctrine',
'user' => 'db2inst1',
'password' => 'Passw0rd',
],
'HOSTNAME=localhost;PORT=50000;DATABASE=doctrine;UID=db2inst1;PWD=Passw0rd',
];
yield 'uncataloged-database' => [
['dbname' => 'HOSTNAME=localhost;PORT=50000;DATABASE=doctrine;UID=db2inst1;PWD=Passw0rd'],
'HOSTNAME=localhost;PORT=50000;DATABASE=doctrine;UID=db2inst1;PWD=Passw0rd',
];
}
}
......@@ -14,7 +14,7 @@
<var name="db_host" value="127.0.0.1"/>
<var name="db_user" value="db2inst1"/>
<var name="db_password" value="Doctrine2018"/>
<var name="db_dbname" value="HOSTNAME=127.0.0.1;UID=db2inst1;PWD=Doctrine2018;DATABASE=doctrine"/>
<var name="db_dbname" value="doctrine"/>
</php>
<testsuites>
......
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