Skip to content

Commit 57d61ed

Browse files
committed
Merge branch 'refs/heads/master' into fix-insertBatch
2 parents a0543d1 + b6d0334 commit 57d61ed

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov)
1818
- New #282: Add `ColumnDefinitionBuilder` class (@Tigrov)
1919
- Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov)
20+
- Enh #283: Refactor `Dsn` class (@Tigrov)
2021

2122
## 1.3.0 March 21, 2024
2223

src/Dsn.php

+16-16
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
final class Dsn extends AbstractDsn
1515
{
1616
/**
17-
* @psalm-param string[] $options
17+
* @psalm-param array<string,string> $options
1818
*/
1919
public function __construct(
20-
private string $driver,
21-
private string $host,
22-
private string|null $databaseName = null,
23-
private string $port = '1521',
24-
private array $options = []
20+
string $driver = 'oci',
21+
string $host = '127.0.0.1',
22+
string|null $databaseName = null,
23+
string $port = '1521',
24+
array $options = []
2525
) {
2626
parent::__construct($driver, $host, $databaseName, $port, $options);
2727
}
@@ -43,20 +43,20 @@ public function __construct(
4343
*/
4444
public function asString(): string
4545
{
46-
if (!empty($this->databaseName)) {
47-
$dsn = "$this->driver:" . "dbname=$this->host:$this->port/$this->databaseName";
48-
} else {
49-
$dsn = "$this->driver:" . "dbname=$this->host:$this->port";
50-
}
46+
$driver = $this->getDriver();
47+
$host = $this->getHost();
48+
$databaseName = $this->getDatabaseName();
49+
$port = $this->getPort();
50+
$options = $this->getOptions();
5151

52-
$parts = [];
52+
$dsn = "$driver:dbname=$host:$port";
5353

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

58-
if (!empty($parts)) {
59-
$dsn .= ';' . implode(';', $parts);
58+
foreach ($options as $key => $value) {
59+
$dsn .= ";$key=$value";
6060
}
6161

6262
return $dsn;

tests/Support/TestTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface
3333

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

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

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

4747
return $this->dsn;

0 commit comments

Comments
 (0)