From e13e5a541d3e0baffc46c2c80915f5d5e20b00bf Mon Sep 17 00:00:00 2001 From: Andrey Kolchenko Date: Mon, 22 May 2017 19:57:14 +0300 Subject: [PATCH] Fix DSN --- src/Che/DBAL/Vertica/VerticaDriver.php | 64 +++++++++++++++----------- tests/VerticaDriverTest.php | 26 +++++++++-- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/Che/DBAL/Vertica/VerticaDriver.php b/src/Che/DBAL/Vertica/VerticaDriver.php index 1ef19de..240ee4f 100644 --- a/src/Che/DBAL/Vertica/VerticaDriver.php +++ b/src/Che/DBAL/Vertica/VerticaDriver.php @@ -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()); } /** @@ -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); } /** @@ -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', @@ -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'] . ';'; @@ -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'])) { diff --git a/tests/VerticaDriverTest.php b/tests/VerticaDriverTest.php index 17f81ab..600786c 100644 --- a/tests/VerticaDriverTest.php +++ b/tests/VerticaDriverTest.php @@ -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)); } }