|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mssql; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Connection\AbstractDsn; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The Dsn class is typically used to parse a DSN string, which is a string that contains all the necessary information |
|
11
|
|
|
* to connect to a database SQL Server, such as the database driver, host, database name, port. |
|
12
|
|
|
* |
|
13
|
|
|
* It also allows you to access individual components of the DSN, such as the driver, host, database name or port. |
|
14
|
|
|
* |
|
15
|
|
|
* @link https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php |
|
16
|
|
|
*/ |
|
17
|
|
|
final class Dsn extends AbstractDsn |
|
18
|
|
|
{ |
|
19
|
617 |
|
public function __construct( |
|
20
|
|
|
private string $driver, |
|
21
|
|
|
private string $host, |
|
22
|
|
|
private string $databaseName, |
|
23
|
|
|
private string $port = '1433' |
|
24
|
|
|
) { |
|
25
|
617 |
|
parent::__construct($driver, $host, $databaseName, $port); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return string the Data Source Name, or DSN, contains the information required to connect to the database. |
|
30
|
|
|
* Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string. |
|
31
|
|
|
* |
|
32
|
|
|
* The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as |
|
33
|
|
|
* `key=value` and concatenated by `;`. For example: |
|
34
|
|
|
* |
|
35
|
|
|
* ```php |
|
36
|
|
|
* $dsn = new Dsn('sqlsrv', 'localhost', 'yiitest', '1433'); |
|
37
|
|
|
* $db = new ConnectionPDO(new PDODriver($dsn->asString(), 'username', 'password'), $queryCache, $schemaCache); |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* Will result in the DSN string `sqlsrv:Server=localhost,1433;Database=yiitest`. |
|
41
|
|
|
*/ |
|
42
|
617 |
|
public function asString(): string |
|
43
|
|
|
{ |
|
44
|
617 |
|
return match ($this->port) { |
|
45
|
617 |
|
'' => "$this->driver:" . "Server=$this->host;" . "Database=$this->databaseName", |
|
46
|
617 |
|
default => "$this->driver:" . "Server=$this->host,$this->port;" . "Database=$this->databaseName", |
|
47
|
617 |
|
}; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|