Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- New #318: Realize `ColumnBuilder` class (@Tigrov)
- Enh #320: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #322, #327: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Enh #323: Refactor `Dsn` class (@Tigrov)
- Enh #323, #373: Refactor `Dsn` class (@Tigrov)
- Enh #324: Set more specific result type in `Connection` methods `createCommand()` and `createTransaction()` (@vjik)
- Enh #326: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
- New #328: Override `QueryBuilder::prepareBinary()` method (@Tigrov)
Expand Down
41 changes: 23 additions & 18 deletions src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,51 @@

namespace Yiisoft\Db\Sqlite;

use Yiisoft\Db\Connection\AbstractDsn;
use Stringable;

/**
* Implement a Data Source Name (DSN) for an SQLite Server.
* Represents a Data Source Name (DSN) for a SQLite Server that's used to configure a {@see Driver} instance.
*
* To get DSN in string format, use the `(string)` type casting operator.
*
* @link https://www.php.net/manual/en/ref.pdo-sqlite.connection.php
*/
final class Dsn extends AbstractDsn
final class Dsn implements Stringable
{
public function __construct(string $driver = 'sqlite', string|null $databaseName = null)
{
parent::__construct($driver, '', $databaseName);
/**
* @param string $driver The database driver name.
* @param string $databaseName The database name to connect to. It can be
* - absolute file path for a file database;
* - 'memory' for a database in memory;
* - empty string for a temporary file database which is deleted when the connection is closed.
*/
public function __construct(
public readonly string $driver = 'sqlite',
public readonly string $databaseName = '',
) {
}

/**
* @return string The Data Source Name, or DSN, has the information required to connect to the database.
*
* Please refer to the [PHP manual](https://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
*
* The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
* `key=value` and concatenated by `;`. For example:
* The `driver` property is used as the driver prefix of the DSN. For example:
*
* ```php
* $dsn = new Dsn('sqlite', __DIR__ . '/data/test.sq3');
* $driver = new Driver($dsn->asString());
* $driver = new Driver($dsn);
* $db = new Connection($driver, $schemaCache);
* ```
*
* Will result in the DSN string `sqlite:/path/to/data/test.sq3`.
*/
public function asString(): string
public function __toString(): string
{
$driver = $this->getDriver();
$databaseName = $this->getDatabaseName();
if ($this->databaseName === 'memory') {
return "$this->driver::memory:";
}

return match ($databaseName) {
'' => "$driver:",
null => "$driver:",
'memory' => "$driver::memory:",
default => "$driver:$databaseName",
};
return "$this->driver:$this->databaseName";
}
}
35 changes: 16 additions & 19 deletions tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,33 @@

/**
* @group sqlite
*
* @psalm-suppress PropertyNotSetInConstructor
*/
final class DsnTest extends TestCase
{
public function testAsString(): void
public function testConstruct(): void
{
$this->assertSame(
'sqlite:' . __DIR__ . '/runtime/yiitest.sq3',
(new Dsn('sqlite', __DIR__ . '/runtime/yiitest.sq3'))->asString(),
);
}
$dsn = new Dsn('sqlite', __DIR__ . '/runtime/yiitest.sq3');

public function testAsStringWithDatabaseName(): void
{
$this->assertSame('sqlite:', (new Dsn('sqlite'))->asString());
$this->assertSame('sqlite', $dsn->driver);
$this->assertSame(__DIR__ . '/runtime/yiitest.sq3', $dsn->databaseName);
$this->assertSame('sqlite:' . __DIR__ . '/runtime/yiitest.sq3', (string) $dsn);
}

public function testAsStringWithDatabaseNameWithEmptyString(): void
public function testConstructDefaults(): void
{
$this->assertSame('sqlite:', (new Dsn('sqlite', ''))->asString());
}
$dsn = new Dsn();

public function testAsStringWithDatabaseNameWithNull(): void
{
$this->assertSame('sqlite:', (new Dsn('sqlite', null))->asString());
$this->assertSame('sqlite', $dsn->driver);
$this->assertSame('', $dsn->databaseName);
$this->assertSame('sqlite:', (string) $dsn);
}

public function testAsStringWithMemory(): void
public function testConstructWithMemory(): void
{
$this->assertSame('sqlite::memory:', (new Dsn('sqlite', 'memory'))->asString());
$dsn = new Dsn('sqlite', 'memory');

$this->assertSame('sqlite', $dsn->driver);
$this->assertSame('memory', $dsn->databaseName);
$this->assertSame('sqlite::memory:', (string) $dsn);
}
}
2 changes: 1 addition & 1 deletion tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function getConnection(bool $fixture = false): Connection
protected function getDsn(): string
{
if ($this->dsn === '') {
$this->dsn = (new Dsn('sqlite', 'memory'))->asString();
$this->dsn = (string) new Dsn('sqlite', 'memory');
}

return $this->dsn;
Expand Down