1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Driver\PDOSqlsrv; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Driver\AbstractSQLServerDriver; |
23
|
|
|
use function is_int; |
24
|
|
|
use function sprintf; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The PDO-based Sqlsrv driver. |
28
|
|
|
* |
29
|
|
|
* @since 2.0 |
30
|
|
|
*/ |
31
|
|
|
class Driver extends AbstractSQLServerDriver |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
31 |
|
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) |
37
|
|
|
{ |
38
|
31 |
|
[$driverOptions, $connectionOptions] = $this->splitOptions($driverOptions); |
39
|
|
|
|
40
|
31 |
|
return new Connection( |
41
|
31 |
|
$this->_constructPdoDsn($params, $connectionOptions), |
42
|
31 |
|
$username, |
43
|
31 |
|
$password, |
44
|
31 |
|
$driverOptions |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructs the Sqlsrv PDO DSN. |
50
|
|
|
* |
51
|
|
|
* @param array $params |
52
|
|
|
* @param string[] $connectionOptions |
53
|
|
|
* |
54
|
|
|
* @return string The DSN. |
55
|
|
|
*/ |
56
|
31 |
|
private function _constructPdoDsn(array $params, array $connectionOptions) |
57
|
|
|
{ |
58
|
31 |
|
$dsn = 'sqlsrv:server='; |
59
|
|
|
|
60
|
31 |
|
if (isset($params['host'])) { |
61
|
31 |
|
$dsn .= $params['host']; |
62
|
|
|
} |
63
|
|
|
|
64
|
31 |
|
if (isset($params['port']) && !empty($params['port'])) { |
65
|
31 |
|
$dsn .= ',' . $params['port']; |
66
|
|
|
} |
67
|
|
|
|
68
|
31 |
|
if (isset($params['dbname'])) { |
69
|
27 |
|
$connectionOptions['Database'] = $params['dbname']; |
70
|
|
|
} |
71
|
|
|
|
72
|
31 |
|
if (isset($params['MultipleActiveResultSets'])) { |
73
|
|
|
$connectionOptions['MultipleActiveResultSets'] = $params['MultipleActiveResultSets'] ? 'true' : 'false'; |
74
|
|
|
} |
75
|
|
|
|
76
|
31 |
|
$dsn .= $this->getConnectionOptionsDsn($connectionOptions); |
77
|
|
|
|
78
|
31 |
|
return $dsn; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Separates a connection options from a driver options |
83
|
|
|
* |
84
|
|
|
* @param int[]|string[] $options |
85
|
|
|
* @return int[][]|string[][] |
86
|
|
|
*/ |
87
|
31 |
|
private function splitOptions(array $options) : array |
88
|
|
|
{ |
89
|
31 |
|
$driverOptions = []; |
90
|
31 |
|
$connectionOptions = []; |
91
|
|
|
|
92
|
31 |
|
foreach ($options as $optionKey => $optionValue) { |
93
|
2 |
|
if (is_int($optionKey)) { |
94
|
1 |
|
$driverOptions[$optionKey] = $optionValue; |
95
|
|
|
} else { |
96
|
2 |
|
$connectionOptions[$optionKey] = $optionValue; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
31 |
|
return [$driverOptions, $connectionOptions]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Converts a connection options array to the DSN |
105
|
|
|
* |
106
|
|
|
* @param string[] $connectionOptions |
107
|
|
|
*/ |
108
|
31 |
|
private function getConnectionOptionsDsn(array $connectionOptions) : string |
109
|
|
|
{ |
110
|
31 |
|
$connectionOptionsDsn = ''; |
111
|
|
|
|
112
|
31 |
|
foreach ($connectionOptions as $paramName => $paramValue) { |
113
|
28 |
|
$connectionOptionsDsn .= sprintf(';%s=%s', $paramName, $paramValue); |
114
|
|
|
} |
115
|
|
|
|
116
|
31 |
|
return $connectionOptionsDsn; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
22 |
|
public function getName() |
123
|
|
|
{ |
124
|
22 |
|
return 'pdo_sqlsrv'; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|