1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Sqlite; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Connection\AbstractDsn; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Implement a Data Source Name (DSN) for an SQLite Server. |
11
|
|
|
* |
12
|
|
|
* @link https://www.php.net/manual/en/ref.pdo-sqlite.connection.php |
13
|
|
|
*/ |
14
|
|
|
final class Dsn extends AbstractDsn |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @psalm-param string[] $options |
18
|
|
|
*/ |
19
|
701 |
|
public function __construct(private string $driver, private string|null $databaseName = null) |
20
|
|
|
{ |
21
|
701 |
|
parent::__construct($driver, '', $databaseName); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string The Data Source Name, or DSN, has the information required to connect to the database. |
26
|
|
|
* |
27
|
|
|
* Please refer to the [PHP manual](https://php.net/manual/en/pdo.construct.php) on the format of the DSN string. |
28
|
|
|
* |
29
|
|
|
* The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as |
30
|
|
|
* `key=value` and concatenated by `;`. For example: |
31
|
|
|
* |
32
|
|
|
* ```php |
33
|
|
|
* $dsn = new Dsn('sqlite', __DIR__ . '/data/test.sq3'); |
34
|
|
|
* $driver = new Driver($dsn->asString()); |
35
|
|
|
* $db = new Connection($driver, $schemaCache); |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* Will result in the DSN string `sqlite:/path/to/data/test.sq3`. |
39
|
|
|
*/ |
40
|
701 |
|
public function asString(): string |
41
|
|
|
{ |
42
|
701 |
|
return match ($this->databaseName) { |
43
|
701 |
|
'' => $this->driver . ':', |
44
|
701 |
|
'memory' => $this->driver . '::memory:', |
45
|
701 |
|
null => $this->driver . ':', |
46
|
701 |
|
default => $this->driver . ':' . $this->databaseName, |
47
|
701 |
|
}; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|