| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Connectors;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Support\Arr;
|
|
|
6 |
use PDO;
|
|
|
7 |
|
|
|
8 |
class SqlServerConnector extends Connector implements ConnectorInterface
|
|
|
9 |
{
|
|
|
10 |
/**
|
|
|
11 |
* The PDO connection options.
|
|
|
12 |
*
|
|
|
13 |
* @var array
|
|
|
14 |
*/
|
|
|
15 |
protected $options = [
|
|
|
16 |
PDO::ATTR_CASE => PDO::CASE_NATURAL,
|
|
|
17 |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
|
18 |
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
|
|
|
19 |
PDO::ATTR_STRINGIFY_FETCHES => false,
|
|
|
20 |
];
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Establish a database connection.
|
|
|
24 |
*
|
|
|
25 |
* @param array $config
|
|
|
26 |
* @return \PDO
|
|
|
27 |
*/
|
|
|
28 |
public function connect(array $config)
|
|
|
29 |
{
|
|
|
30 |
$options = $this->getOptions($config);
|
|
|
31 |
|
|
|
32 |
$connection = $this->createConnection($this->getDsn($config), $config, $options);
|
|
|
33 |
|
|
|
34 |
$this->configureIsolationLevel($connection, $config);
|
|
|
35 |
|
|
|
36 |
return $connection;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Set the connection transaction isolation level.
|
|
|
41 |
*
|
|
|
42 |
* https://learn.microsoft.com/en-us/sql/t-sql/statements/set-transaction-isolation-level-transact-sql
|
|
|
43 |
*
|
|
|
44 |
* @param \PDO $connection
|
|
|
45 |
* @param array $config
|
|
|
46 |
* @return void
|
|
|
47 |
*/
|
|
|
48 |
protected function configureIsolationLevel($connection, array $config)
|
|
|
49 |
{
|
|
|
50 |
if (! isset($config['isolation_level'])) {
|
|
|
51 |
return;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
$connection->prepare(
|
|
|
55 |
"SET TRANSACTION ISOLATION LEVEL {$config['isolation_level']}"
|
|
|
56 |
)->execute();
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Create a DSN string from a configuration.
|
|
|
61 |
*
|
|
|
62 |
* @param array $config
|
|
|
63 |
* @return string
|
|
|
64 |
*/
|
|
|
65 |
protected function getDsn(array $config)
|
|
|
66 |
{
|
|
|
67 |
// First we will create the basic DSN setup as well as the port if it is in
|
|
|
68 |
// in the configuration options. This will give us the basic DSN we will
|
|
|
69 |
// need to establish the PDO connections and return them back for use.
|
|
|
70 |
if ($this->prefersOdbc($config)) {
|
|
|
71 |
return $this->getOdbcDsn($config);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
if (in_array('sqlsrv', $this->getAvailableDrivers())) {
|
|
|
75 |
return $this->getSqlSrvDsn($config);
|
|
|
76 |
} else {
|
|
|
77 |
return $this->getDblibDsn($config);
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Determine if the database configuration prefers ODBC.
|
|
|
83 |
*
|
|
|
84 |
* @param array $config
|
|
|
85 |
* @return bool
|
|
|
86 |
*/
|
|
|
87 |
protected function prefersOdbc(array $config)
|
|
|
88 |
{
|
|
|
89 |
return in_array('odbc', $this->getAvailableDrivers()) &&
|
|
|
90 |
($config['odbc'] ?? null) === true;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Get the DSN string for a DbLib connection.
|
|
|
95 |
*
|
|
|
96 |
* @param array $config
|
|
|
97 |
* @return string
|
|
|
98 |
*/
|
|
|
99 |
protected function getDblibDsn(array $config)
|
|
|
100 |
{
|
|
|
101 |
return $this->buildConnectString('dblib', array_merge([
|
|
|
102 |
'host' => $this->buildHostString($config, ':'),
|
|
|
103 |
'dbname' => $config['database'],
|
|
|
104 |
], Arr::only($config, ['appname', 'charset', 'version'])));
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Get the DSN string for an ODBC connection.
|
|
|
109 |
*
|
|
|
110 |
* @param array $config
|
|
|
111 |
* @return string
|
|
|
112 |
*/
|
|
|
113 |
protected function getOdbcDsn(array $config)
|
|
|
114 |
{
|
|
|
115 |
return isset($config['odbc_datasource_name'])
|
|
|
116 |
? 'odbc:'.$config['odbc_datasource_name'] : '';
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Get the DSN string for a SqlSrv connection.
|
|
|
121 |
*
|
|
|
122 |
* @param array $config
|
|
|
123 |
* @return string
|
|
|
124 |
*/
|
|
|
125 |
protected function getSqlSrvDsn(array $config)
|
|
|
126 |
{
|
|
|
127 |
$arguments = [
|
|
|
128 |
'Server' => $this->buildHostString($config, ','),
|
|
|
129 |
];
|
|
|
130 |
|
|
|
131 |
if (isset($config['database'])) {
|
|
|
132 |
$arguments['Database'] = $config['database'];
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
if (isset($config['readonly'])) {
|
|
|
136 |
$arguments['ApplicationIntent'] = 'ReadOnly';
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
if (isset($config['pooling']) && $config['pooling'] === false) {
|
|
|
140 |
$arguments['ConnectionPooling'] = '0';
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
if (isset($config['appname'])) {
|
|
|
144 |
$arguments['APP'] = $config['appname'];
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
if (isset($config['encrypt'])) {
|
|
|
148 |
$arguments['Encrypt'] = $config['encrypt'];
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
if (isset($config['trust_server_certificate'])) {
|
|
|
152 |
$arguments['TrustServerCertificate'] = $config['trust_server_certificate'];
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if (isset($config['multiple_active_result_sets']) && $config['multiple_active_result_sets'] === false) {
|
|
|
156 |
$arguments['MultipleActiveResultSets'] = 'false';
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
if (isset($config['transaction_isolation'])) {
|
|
|
160 |
$arguments['TransactionIsolation'] = $config['transaction_isolation'];
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
if (isset($config['multi_subnet_failover'])) {
|
|
|
164 |
$arguments['MultiSubnetFailover'] = $config['multi_subnet_failover'];
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
if (isset($config['column_encryption'])) {
|
|
|
168 |
$arguments['ColumnEncryption'] = $config['column_encryption'];
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
if (isset($config['key_store_authentication'])) {
|
|
|
172 |
$arguments['KeyStoreAuthentication'] = $config['key_store_authentication'];
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
if (isset($config['key_store_principal_id'])) {
|
|
|
176 |
$arguments['KeyStorePrincipalId'] = $config['key_store_principal_id'];
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
if (isset($config['key_store_secret'])) {
|
|
|
180 |
$arguments['KeyStoreSecret'] = $config['key_store_secret'];
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
if (isset($config['login_timeout'])) {
|
|
|
184 |
$arguments['LoginTimeout'] = $config['login_timeout'];
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
if (isset($config['authentication'])) {
|
|
|
188 |
$arguments['Authentication'] = $config['authentication'];
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
return $this->buildConnectString('sqlsrv', $arguments);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Build a connection string from the given arguments.
|
|
|
196 |
*
|
|
|
197 |
* @param string $driver
|
|
|
198 |
* @param array $arguments
|
|
|
199 |
* @return string
|
|
|
200 |
*/
|
|
|
201 |
protected function buildConnectString($driver, array $arguments)
|
|
|
202 |
{
|
|
|
203 |
return $driver.':'.implode(';', array_map(function ($key) use ($arguments) {
|
|
|
204 |
return sprintf('%s=%s', $key, $arguments[$key]);
|
|
|
205 |
}, array_keys($arguments)));
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Build a host string from the given configuration.
|
|
|
210 |
*
|
|
|
211 |
* @param array $config
|
|
|
212 |
* @param string $separator
|
|
|
213 |
* @return string
|
|
|
214 |
*/
|
|
|
215 |
protected function buildHostString(array $config, $separator)
|
|
|
216 |
{
|
|
|
217 |
if (empty($config['port'])) {
|
|
|
218 |
return $config['host'];
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
return $config['host'].$separator.$config['port'];
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Get the available PDO drivers.
|
|
|
226 |
*
|
|
|
227 |
* @return array
|
|
|
228 |
*/
|
|
|
229 |
protected function getAvailableDrivers()
|
|
|
230 |
{
|
|
|
231 |
return PDO::getAvailableDrivers();
|
|
|
232 |
}
|
|
|
233 |
}
|