Skip to content

Commit

Permalink
Refactor Dsn class (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 18, 2024
1 parent 226d840 commit b6d0334
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- New #280: Realize `ColumnBuilder` class (@Tigrov)
- Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #282: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Enh #283: Refactor `Dsn` class (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
32 changes: 16 additions & 16 deletions src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
final class Dsn extends AbstractDsn
{
/**
* @psalm-param string[] $options
* @psalm-param array<string,string> $options
*/
public function __construct(
private string $driver,
private string $host,
private string|null $databaseName = null,
private string $port = '1521',
private array $options = []
string $driver = 'oci',
string $host = '127.0.0.1',
string|null $databaseName = null,
string $port = '1521',
array $options = []
) {
parent::__construct($driver, $host, $databaseName, $port, $options);
}
Expand All @@ -43,20 +43,20 @@ public function __construct(
*/
public function asString(): string
{
if (!empty($this->databaseName)) {
$dsn = "$this->driver:" . "dbname=$this->host:$this->port/$this->databaseName";
} else {
$dsn = "$this->driver:" . "dbname=$this->host:$this->port";
}
$driver = $this->getDriver();
$host = $this->getHost();
$databaseName = $this->getDatabaseName();
$port = $this->getPort();
$options = $this->getOptions();

$parts = [];
$dsn = "$driver:dbname=$host:$port";

foreach ($this->options as $key => $value) {
$parts[] = "$key=$value";
if (!empty($databaseName)) {
$dsn .= "/$databaseName";
}

if (!empty($parts)) {
$dsn .= ';' . implode(';', $parts);
foreach ($options as $key => $value) {
$dsn .= ";$key=$value";
}

return $dsn;
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface

protected static function getDb(): PdoConnectionInterface
{
$dsn = (new Dsn('oci', 'localhost', 'XE', '1521', ['charset' => 'AL32UTF8']))->asString();
$dsn = (new Dsn(databaseName: 'XE', options: ['charset' => 'AL32UTF8']))->asString();

return new Connection(new Driver($dsn, 'system', 'root'), DbHelper::getSchemaCache());
}

protected function getDsn(): string
{
if ($this->dsn === '') {
$this->dsn = (new Dsn('oci', 'localhost', 'XE', '1521', ['charset' => 'AL32UTF8']))->asString();
$this->dsn = (new Dsn(databaseName: 'XE', options: ['charset' => 'AL32UTF8']))->asString();
}

return $this->dsn;
Expand Down

0 comments on commit b6d0334

Please sign in to comment.