Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
Fix DSN
Browse files Browse the repository at this point in the history
  • Loading branch information
komex committed May 22, 2017
1 parent e812e59 commit e13e5a5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
64 changes: 37 additions & 27 deletions src/Che/DBAL/Vertica/VerticaDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ class VerticaDriver implements Driver
* @var OptionsResolver
*/
private $resolver;
/**
* @var OptionsResolver
*/
private $driverResolver;

/**
* VerticaDriver constructor.
*/
public function __construct()
{
$this->resolver = new OptionsResolver();
$this->configureResolver($this->resolver);
$this->resolver = $this->configureResolver(new OptionsResolver());
$this->driverResolver = $this->configureDriverResolver(new OptionsResolver());
}

/**
Expand All @@ -53,7 +57,7 @@ public function __construct()
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
return new ODBCConnection($this->constructDsn($params), $username, $password);
return new ODBCConnection($this->constructDsn($params, $driverOptions), $username, $password);
}

/**
Expand Down Expand Up @@ -96,14 +100,33 @@ public function getDatabase(Connection $conn)

/**
* @param OptionsResolver $resolver
*
* @return OptionsResolver
*/
protected function configureResolver(OptionsResolver $resolver)
{
$resolver
->setDefined(['dsn'])
->setDefaults(['host' => 'localhost', 'port' => 5433, 'dbname' => 'vmartdb'])
->setAllowedTypes('dsn', 'string')
->setAllowedTypes('host', 'string')
->setAllowedTypes('port', 'integer')
->setAllowedTypes('dbname', 'string');

return $resolver;
}

/**
* @param OptionsResolver $resolver
*
* @return OptionsResolver
*/
protected function configureDriverResolver(OptionsResolver $resolver)
{
$booleanNormalizer = function (Options $options, $value) {
return ($value === true) ? 'true' : 'false';
};
$optionsResolver = new OptionsResolver();
$optionsResolver
$resolver
->setDefined(
[
'odbc_driver',
Expand Down Expand Up @@ -134,38 +157,25 @@ protected function configureResolver(OptionsResolver $resolver)
->setNormalizer('audo_commit', $booleanNormalizer)
->setNormalizer('direct_batch_insert', $booleanNormalizer)
->setNormalizer('read_only', $booleanNormalizer);
$resolver
->setDefined(['dsn', 'driverOptions'])
->setDefaults(['host' => 'localhost', 'port' => 5433, 'dbname' => 'vmartdb', 'driverOptions' => []])
->setAllowedTypes('dsn', 'string')
->setAllowedTypes('host', 'string')
->setAllowedTypes('port', 'integer')
->setAllowedTypes('dbname', 'string')
->setAllowedTypes('driverOptions', 'array')
->setNormalizer(
'driverOptions',
function (Options $options, array $value) use ($optionsResolver) {
if (!empty($options['dsn'])) {
return [];
}

return $optionsResolver->resolve($value);
}
);

return $resolver;
}

/**
* @param array $params
* @param array $driverOptions
*
* @return string
*/
protected function constructDsn(array $params)
protected function constructDsn(array $params, array $driverOptions)
{
$params = $this->resolver->resolve($params);
$params = $this->resolver->resolve(
array_intersect_key($params, array_flip($this->resolver->getDefinedOptions()))
);
if (!empty($params['dsn'])) {
return $params['dsn'];
}
$driverOptions = $params['driverOptions'];
$driverOptions = $this->driverResolver->resolve($driverOptions);
$dsn = 'Driver=' . $driverOptions['odbc_driver'] . ';';

$dsn .= 'Servername=' . $params['host'] . ';';
Expand Down Expand Up @@ -196,7 +206,7 @@ protected function constructDsn(array $params)
}

if (!empty($driverOptions['label'])) {
$dsn .= 'Label=' . $driverOptions['label'];
$dsn .= 'Label=' . $driverOptions['label'] . ';';
}

if (isset($driverOptions['audo_commit'])) {
Expand Down
26 changes: 22 additions & 4 deletions tests/VerticaDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,39 @@ class VerticaDriverTest extends \PHPUnit_Framework_TestCase
public function dpConstructDsn()
{
return [
[[], 'Driver=Vertica;Servername=localhost;Port=5433;Database=vmartdb;'],
[
[],
[],
'Driver=Vertica;Servername=localhost;Port=5433;Database=vmartdb;',
],
[
[
'odbc_driver' => 'VerticaDSN',
'dbname' => 'testdb',
'user' => 'dbadmin',
'password' => 'pass',
'host' => '127.0.0.1',
'driverClass' => 'Che\\DBAL\\Vertica\\VerticaDriver',
'driverOptions' => ['label' => 'DEV', 'result_buffer_size' => 0],
],
['label' => 'DEV', 'result_buffer_size' => 0, 'odbc_driver' => 'VerticaDSN'],
'Driver=VerticaDSN;Servername=127.0.0.1;Port=5433;Database=testdb;Label=DEV;ResultBufferSize=0;',
],
];
}

/**
* @param array $params
* @param $expected
* @param array $driverOptions
* @param string $expected
*
* @dataProvider dpConstructDsn
*/
public function testConstructDsn(array $params, $expected)
public function testConstructDsn(array $params, array $driverOptions, $expected)
{
$driver = new VerticaDriver();
$method = new \ReflectionMethod($driver, 'constructDsn');
$method->setAccessible(true);
self::assertSame($expected, $method->invoke($driver, $params));
self::assertSame($expected, $method->invoke($driver, $params, $driverOptions));
}
}

0 comments on commit e13e5a5

Please sign in to comment.