Introduce the IBM DB2 DSN value object

parent 27e57e41
......@@ -4,8 +4,6 @@ namespace Doctrine\DBAL\Driver\IBMDB2;
use Doctrine\DBAL\Driver\AbstractDB2Driver;
use function strpos;
/**
* IBM DB2 Driver.
*/
......@@ -16,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 (strpos($params['dbname'], '=') === false) {
// 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;
}
return new DB2Connection($params, (string) $username, (string) $password, $driverOptions);
$params['user'] = $username;
$params['password'] = $password;
$params['dbname'] = DataSourceName::fromConnectionParameters($params)->toString();
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',
];
}
}
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