diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Driver.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Driver.php | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Driver.php b/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Driver.php new file mode 100644 index 0000000..a3cae97 --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/PDO/SQLSrv/Driver.php | |||
@@ -0,0 +1,108 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Driver\PDO\SQLSrv; | ||
6 | |||
7 | use Doctrine\DBAL\Driver\AbstractSQLServerDriver; | ||
8 | use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost; | ||
9 | use Doctrine\DBAL\Driver\Exception; | ||
10 | use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection; | ||
11 | use Doctrine\DBAL\Driver\PDO\Exception as PDOException; | ||
12 | use PDO; | ||
13 | use SensitiveParameter; | ||
14 | |||
15 | use function is_int; | ||
16 | use function sprintf; | ||
17 | |||
18 | final class Driver extends AbstractSQLServerDriver | ||
19 | { | ||
20 | /** | ||
21 | * {@inheritDoc} | ||
22 | */ | ||
23 | public function connect( | ||
24 | #[SensitiveParameter] | ||
25 | array $params, | ||
26 | ): Connection { | ||
27 | $driverOptions = $dsnOptions = []; | ||
28 | |||
29 | if (isset($params['driverOptions'])) { | ||
30 | foreach ($params['driverOptions'] as $option => $value) { | ||
31 | if (is_int($option)) { | ||
32 | $driverOptions[$option] = $value; | ||
33 | } else { | ||
34 | $dsnOptions[$option] = $value; | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | |||
39 | if (! empty($params['persistent'])) { | ||
40 | $driverOptions[PDO::ATTR_PERSISTENT] = true; | ||
41 | } | ||
42 | |||
43 | $safeParams = $params; | ||
44 | unset($safeParams['password']); | ||
45 | |||
46 | try { | ||
47 | $pdo = new PDO( | ||
48 | $this->constructDsn($safeParams, $dsnOptions), | ||
49 | $params['user'] ?? '', | ||
50 | $params['password'] ?? '', | ||
51 | $driverOptions, | ||
52 | ); | ||
53 | } catch (\PDOException $exception) { | ||
54 | throw PDOException::new($exception); | ||
55 | } | ||
56 | |||
57 | return new Connection(new PDOConnection($pdo)); | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Constructs the Sqlsrv PDO DSN. | ||
62 | * | ||
63 | * @param mixed[] $params | ||
64 | * @param string[] $connectionOptions | ||
65 | * | ||
66 | * @throws Exception | ||
67 | */ | ||
68 | private function constructDsn(array $params, array $connectionOptions): string | ||
69 | { | ||
70 | $dsn = 'sqlsrv:server='; | ||
71 | |||
72 | if (isset($params['host'])) { | ||
73 | $dsn .= $params['host']; | ||
74 | |||
75 | if (isset($params['port'])) { | ||
76 | $dsn .= ',' . $params['port']; | ||
77 | } | ||
78 | } elseif (isset($params['port'])) { | ||
79 | throw PortWithoutHost::new(); | ||
80 | } | ||
81 | |||
82 | if (isset($params['dbname'])) { | ||
83 | $connectionOptions['Database'] = $params['dbname']; | ||
84 | } | ||
85 | |||
86 | if (isset($params['MultipleActiveResultSets'])) { | ||
87 | $connectionOptions['MultipleActiveResultSets'] = $params['MultipleActiveResultSets'] ? 'true' : 'false'; | ||
88 | } | ||
89 | |||
90 | return $dsn . $this->getConnectionOptionsDsn($connectionOptions); | ||
91 | } | ||
92 | |||
93 | /** | ||
94 | * Converts a connection options array to the DSN | ||
95 | * | ||
96 | * @param string[] $connectionOptions | ||
97 | */ | ||
98 | private function getConnectionOptionsDsn(array $connectionOptions): string | ||
99 | { | ||
100 | $connectionOptionsDsn = ''; | ||
101 | |||
102 | foreach ($connectionOptions as $paramName => $paramValue) { | ||
103 | $connectionOptionsDsn .= sprintf(';%s=%s', $paramName, $paramValue); | ||
104 | } | ||
105 | |||
106 | return $connectionOptionsDsn; | ||
107 | } | ||
108 | } | ||