Skip to content

Commit c64276f

Browse files
authored
Make Database name optional in Socket Dsn (#652)
1 parent bb6a21b commit c64276f

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/Connection/AbstractDsnSocket.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ abstract class AbstractDsnSocket implements DsnInterface, Stringable
2222
public function __construct(
2323
private string $driver,
2424
private string $unixSocket,
25-
private string $databaseName,
25+
private string|null $databaseName = null,
2626
private array $options = []
2727
) {
2828
}
2929

3030
public function asString(): string
3131
{
32-
$dsn = "$this->driver:" . "unix_socket=$this->unixSocket" . ';' . "dbname=$this->databaseName";
32+
$dsn = "$this->driver:" . "unix_socket=$this->unixSocket";
33+
34+
if ($this->databaseName !== null && $this->databaseName !== '') {
35+
$dsn .= ';' . "dbname=$this->databaseName";
36+
}
3337

3438
$parts = [];
3539

@@ -53,9 +57,9 @@ public function __toString(): string
5357
}
5458

5559
/**
56-
* @return string The database name to connect to.
60+
* @return string|null The database name to connect to.
5761
*/
58-
public function getDatabaseName(): string
62+
public function getDatabaseName(): string|null
5963
{
6064
return $this->databaseName;
6165
}

tests/Db/Connection/DsnSocketTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ public function testGetDatabaseName(): void
3131
$this->assertSame('yiitest', $dsn->getDatabaseName());
3232
}
3333

34+
public function testGetDsnWithoutDatabaseName(): void
35+
{
36+
$dsn = new DsnSocket('mysql', '/var/run/mysqld/mysqld.sock', '', ['charset' => 'utf8']);
37+
38+
$this->assertSame('mysql:unix_socket=/var/run/mysqld/mysqld.sock;charset=utf8', $dsn->asString());
39+
$this->assertSame('mysql:unix_socket=/var/run/mysqld/mysqld.sock;charset=utf8', $dsn->__toString());
40+
$this->assertEmpty($dsn->getDatabaseName());
41+
42+
$dsn = new DsnSocket('mysql', '/var/run/mysqld/mysqld.sock', null, ['charset' => 'utf8']);
43+
44+
$this->assertSame('mysql:unix_socket=/var/run/mysqld/mysqld.sock;charset=utf8', $dsn->asString());
45+
$this->assertSame('mysql:unix_socket=/var/run/mysqld/mysqld.sock;charset=utf8', $dsn->__toString());
46+
$this->assertNull($dsn->getDatabaseName());
47+
}
48+
3449
public function testGetDriver(): void
3550
{
3651
$dsn = new DsnSocket('mysql', '/var/run/mysqld/mysqld.sock', 'yiitest', ['charset' => 'utf8']);

tests/Db/Connection/DsnTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public function testGetDsnWithoutDatabaseName(): void
5353

5454
$this->assertSame('mysql:host=localhost;port=3306;charset=utf8', $dsn->asString());
5555
$this->assertSame('mysql:host=localhost;port=3306;charset=utf8', $dsn->__toString());
56+
$this->assertEmpty($dsn->getDatabaseName());
57+
58+
$dsn = new Dsn('mysql', 'localhost', null, '3306', ['charset' => 'utf8']);
59+
60+
$this->assertSame('mysql:host=localhost;port=3306;charset=utf8', $dsn->asString());
61+
$this->assertSame('mysql:host=localhost;port=3306;charset=utf8', $dsn->__toString());
62+
$this->assertNull($dsn->getDatabaseName());
5663
}
5764

5865
public function testGetHost(): void

0 commit comments

Comments
 (0)