-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3297 from morozov/issues/3296
Do not generate SID or SERVICE_NAME when dbname or service name is not specified
- Loading branch information
Showing
3 changed files
with
184 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
lib/Doctrine/DBAL/Driver/AbstractOracleDriver/EasyConnectString.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Driver\AbstractOracleDriver; | ||
|
||
use function implode; | ||
use function is_array; | ||
use function sprintf; | ||
|
||
/** | ||
* Represents an Oracle Easy Connect string | ||
* | ||
* @link https://docs.oracle.com/database/121/NETAG/naming.htm | ||
*/ | ||
final class EasyConnectString | ||
{ | ||
/** @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 mixed[] $params | ||
*/ | ||
public static function fromArray(array $params) : self | ||
{ | ||
return new self(self::renderParams($params)); | ||
} | ||
|
||
/** | ||
* Creates the object from the given DBAL connection parameters. | ||
* | ||
* @param mixed[] $params | ||
*/ | ||
public static function fromConnectionParameters(array $params) : self | ||
{ | ||
if (! empty($params['connectstring'])) { | ||
return new self($params['connectstring']); | ||
} | ||
|
||
if (empty($params['host'])) { | ||
return new self($params['dbname'] ?? ''); | ||
} | ||
|
||
$connectData = []; | ||
|
||
if (isset($params['servicename']) || isset($params['dbname'])) { | ||
$serviceKey = 'SID'; | ||
|
||
if (! empty($params['service'])) { | ||
$serviceKey = 'SERVICE_NAME'; | ||
} | ||
|
||
$serviceName = $params['servicename'] ?? $params['dbname']; | ||
|
||
$connectData[$serviceKey] = $serviceName; | ||
} | ||
|
||
if (! empty($params['instancename'])) { | ||
$connectData['INSTANCE_NAME'] = $params['instancename']; | ||
} | ||
|
||
if (! empty($params['pooled'])) { | ||
$connectData['SERVER'] = 'POOLED'; | ||
} | ||
|
||
return self::fromArray([ | ||
'DESCRIPTION' => [ | ||
'ADDRESS' => [ | ||
'PROTOCOL' => 'TCP', | ||
'HOST' => $params['host'], | ||
'PORT' => $params['port'] ?? 1521, | ||
], | ||
'CONNECT_DATA' => $connectData, | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @param mixed[] $params | ||
*/ | ||
private static function renderParams(array $params) : string | ||
{ | ||
$chunks = []; | ||
|
||
foreach ($params as $key => $value) { | ||
$string = self::renderValue($value); | ||
|
||
if ($string === '') { | ||
continue; | ||
} | ||
|
||
$chunks[] = sprintf('(%s=%s)', $key, $string); | ||
} | ||
|
||
return implode('', $chunks); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
private static function renderValue($value) : string | ||
{ | ||
if (is_array($value)) { | ||
return self::renderParams($value); | ||
} | ||
|
||
return (string) $value; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriver/EasyConnectStringTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\DBAL\Driver\AbstractOracleDriver; | ||
|
||
use Doctrine\DBAL\Driver\AbstractOracleDriver\EasyConnectString; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class EasyConnectStringTest extends TestCase | ||
{ | ||
/** | ||
* @param mixed[] $params | ||
* @dataProvider connectionParametersProvider | ||
*/ | ||
public function testFromConnectionParameters(array $params, string $expected) : void | ||
{ | ||
$string = EasyConnectString::fromConnectionParameters($params); | ||
|
||
$this->assertSame($expected, (string) $string); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public static function connectionParametersProvider() : iterable | ||
{ | ||
return [ | ||
'empty-params' => [[],''], | ||
'common-params' => [ | ||
[ | ||
'host' => 'oracle.example.com', | ||
'port' => 1521, | ||
'dbname' => 'XE', | ||
], | ||
'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle.example.com)(PORT=1521))(CONNECT_DATA=(SID=XE)))', | ||
], | ||
'no-db-name' => [ | ||
['host' => 'localhost'], | ||
'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))', | ||
], | ||
'service' => [ | ||
[ | ||
'host' => 'localhost', | ||
'port' => 1521, | ||
'service' => true, | ||
'servicename' => 'BILLING', | ||
], | ||
'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=BILLING)))', | ||
], | ||
'advanced-params' => [ | ||
[ | ||
'host' => 'localhost', | ||
'port' => 41521, | ||
'dbname' => 'XE', | ||
'instancename' => 'SALES', | ||
'pooled' => true, | ||
], | ||
'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=41521))(CONNECT_DATA=(SID=XE)(INSTANCE_NAME=SALES)(SERVER=POOLED)))', | ||
], | ||
]; | ||
} | ||
} |