|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Oracle; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Connection\AbstractDsn; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Implement a Data Source Name (DSN) for an Oracle Server. |
|
11
|
|
|
* |
|
12
|
|
|
* @link https://www.php.net/manual/en/ref.pdo-oci.connection.php |
|
13
|
|
|
*/ |
|
14
|
|
|
final class Dsn extends AbstractDsn |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @psalm-param string[] $options |
|
18
|
|
|
*/ |
|
19
|
6 |
|
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 = [] |
|
25
|
|
|
) { |
|
26
|
6 |
|
parent::__construct($driver, $host, $databaseName, $port, $options); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return string The Data Source Name, or DSN, contains the information required to connect to the database. |
|
31
|
|
|
* |
|
32
|
|
|
* Please refer to the [PHP manual](https://php.net/manual/en/pdo.construct.php) on the format of the DSN string. |
|
33
|
|
|
* |
|
34
|
|
|
* The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as |
|
35
|
|
|
* `key=value` and concatenated by `;`. For example: |
|
36
|
|
|
* |
|
37
|
|
|
* ```php |
|
38
|
|
|
* $dsn = new Dsn('oci', 'localhost', 'yiitest', '1521', ['charset' => 'AL32UTF8']); |
|
39
|
|
|
* $connection = new Connection($dsn->asString(), 'system', 'root'); |
|
40
|
|
|
* ``` |
|
41
|
|
|
* |
|
42
|
|
|
* Will result in the DSN string `oci:dbname=localhost:1521/yiitest;charset=AL32UTF8`. |
|
43
|
|
|
*/ |
|
44
|
6 |
|
public function asString(): string |
|
45
|
|
|
{ |
|
46
|
6 |
|
if (!empty($this->databaseName)) { |
|
47
|
2 |
|
$dsn = "$this->driver:" . "dbname=$this->host:$this->port/$this->databaseName"; |
|
48
|
|
|
} else { |
|
49
|
4 |
|
$dsn = "$this->driver:" . "dbname=$this->host:$this->port"; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
$parts = []; |
|
53
|
|
|
|
|
54
|
6 |
|
foreach ($this->options as $key => $value) { |
|
55
|
5 |
|
$parts[] = "$key=$value"; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
6 |
|
if (!empty($parts)) { |
|
59
|
5 |
|
$dsn .= ';' . implode(';', $parts); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
6 |
|
return $dsn; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|