1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle Database package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Config\SQLServer; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Config\ProvidesSourceString; |
15
|
|
|
use Cycle\Database\Config\PDOConnectionConfig; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @psalm-type IsolationLevelType = \PDO::SQLSRV_TXN_* |
19
|
|
|
* @psalm-import-type PDOFlag from PDOConnectionConfig |
20
|
|
|
*/ |
21
|
|
|
class TcpConnectionConfig extends ConnectionConfig implements ProvidesSourceString |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param non-empty-string $database The name of the database. |
|
|
|
|
25
|
|
|
* @param non-empty-string $host Database connection host. |
26
|
|
|
* @param positive-int|null $port Database connection port. |
27
|
|
|
* @param non-empty-string|null $app The application name used in tracing. |
28
|
|
|
* @param bool|null $pooling Specifies whether the connection is assigned from a |
29
|
|
|
* connection pool ({@see true}) or not ({@see false}). |
30
|
|
|
* @param bool|null $encrypt Specifies whether the communication with SQL Server |
31
|
|
|
* is encrypted ({@see true}) or unencrypted ({@see false}). |
32
|
|
|
* @param non-empty-string|null $failover Specifies the server and instance of the database's |
33
|
|
|
* mirror (if enabled and configured) to use when the primary server is unavailable. |
34
|
|
|
* @param int|null $timeout Specifies the number of seconds to wait before failing the connection attempt. |
35
|
|
|
* @param bool|null $mars Disables or explicitly enables support for Multiple Active Result Sets (MARS). |
36
|
|
|
* @param bool|null $quoted Specifies whether to use SQL-92 rules for quoted |
37
|
|
|
* identifiers ({@see true}) or to use legacy Transact-SQL rules ({@see false}). |
38
|
|
|
* @param non-empty-string|null $traceFile Specifies the path for the file used for trace data. |
39
|
|
|
* @param bool|null $trace Specifies whether ODBC tracing is enabled ({@see true}) or |
40
|
|
|
* disabled ({@see false}) for the connection being established. |
41
|
|
|
* @param IsolationLevelType|null $isolation Specifies the transaction isolation level. |
42
|
|
|
* The accepted values for this option are: |
43
|
|
|
* - {@see \PDO::SQLSRV_TXN_READ_UNCOMMITTED} |
44
|
|
|
* - {@see \PDO::SQLSRV_TXN_READ_COMMITTED} |
45
|
|
|
* - {@see \PDO::SQLSRV_TXN_REPEATABLE_READ} |
46
|
|
|
* - {@see \PDO::SQLSRV_TXN_SNAPSHOT} |
47
|
|
|
* - {@see \PDO::SQLSRV_TXN_SERIALIZABLE} |
48
|
|
|
* @param bool|null $trustServerCertificate Specifies whether the client should |
49
|
|
|
* trust ({@see true}) or reject ({@see false}) a self-signed server certificate. |
50
|
|
|
* @param non-empty-string|null $wsid Specifies the name of the computer for tracing. |
51
|
|
|
* @param non-empty-string|null $user |
52
|
|
|
* @param non-empty-string|null $password |
53
|
|
|
* @param array $options |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
public string $database, |
57
|
|
|
public string $host = 'localhost', |
58
|
|
|
public ?int $port = null, |
59
|
|
|
public ?string $app = null, |
60
|
|
|
public ?bool $pooling = null, |
61
|
|
|
public ?bool $encrypt = null, |
62
|
|
|
public ?string $failover = null, |
63
|
|
|
public ?int $timeout = null, |
64
|
|
|
public ?bool $mars = null, |
65
|
|
|
public ?bool $quoted = null, |
66
|
|
|
public ?string $traceFile = null, |
67
|
|
|
public ?bool $trace = null, |
68
|
|
|
public ?int $isolation = null, |
69
|
|
|
public ?bool $trustServerCertificate = null, |
70
|
|
|
public ?string $wsid = null, |
71
|
|
|
?string $user = null, |
72
|
|
|
?string $password = null, |
73
|
|
|
array $options = [] |
74
|
|
|
) { |
75
|
|
|
parent::__construct($user, $password, $options); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
2 |
|
public function getSourceString(): string |
82
|
|
|
{ |
83
|
2 |
|
return $this->database; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the SQL Server specific PDO DataSourceName, that looks like: |
88
|
|
|
* <code> |
89
|
|
|
* sqlsrv:Server=localhost,1521;Database=dbname |
90
|
|
|
* </code> |
91
|
|
|
* |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
*/ |
94
|
6 |
|
public function getDsn(): string |
95
|
|
|
{ |
96
|
6 |
|
$config = [ |
97
|
6 |
|
'APP' => $this->app, |
98
|
6 |
|
'ConnectionPooling' => $this->pooling, |
99
|
6 |
|
'Database' => $this->database, |
100
|
6 |
|
'Encrypt' => $this->encrypt, |
101
|
6 |
|
'Failover_Partner' => $this->failover, |
102
|
6 |
|
'LoginTimeout' => $this->timeout, |
103
|
6 |
|
'MultipleActiveResultSets' => $this->mars, |
104
|
6 |
|
'QuotedId' => $this->quoted, |
105
|
6 |
|
'Server' => \implode(',', [$this->host, $this->port]), |
106
|
6 |
|
'TraceFile' => $this->traceFile, |
107
|
6 |
|
'TraceOn' => $this->trace, |
108
|
6 |
|
'TransactionIsolation' => $this->isolation, |
109
|
6 |
|
'TrustServerCertificate' => $this->trustServerCertificate, |
110
|
6 |
|
'WSID' => $this->wsid, |
111
|
|
|
]; |
112
|
|
|
|
113
|
6 |
|
return \sprintf('%s:%s', $this->getName(), $this->dsn($config)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function __set_state(array $state): self |
117
|
|
|
{ |
118
|
|
|
return new self(...$state); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|