‪TYPO3CMS  11.5
Driver.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
21 use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost;
22 
27 class ‪Driver extends AbstractSQLServerDriver
28 {
32  public function ‪connect(array $params, $username = null, $password = null, array $driverOptions = [])
33  {
34  [$driverOptions, $connectionOptions] = $this->‪splitOptions($driverOptions);
35 
36  return new ‪Connection(
37  $this->‪_constructPdoDsn($params, $connectionOptions),
38  $username,
39  $password,
40  $driverOptions
41  );
42  }
43 
52  private function ‪_constructPdoDsn(array $params, array $connectionOptions)
53  {
54  $dsn = 'sqlsrv:server=';
55 
56  if (isset($params['host'])) {
57  $dsn .= $params['host'];
58 
59  if (isset($params['port'])) {
60  $dsn .= ',' . $params['port'];
61  }
62  } elseif (isset($params['port'])) {
63  throw PortWithoutHost::new();
64  }
65 
66  if (isset($params['dbname'])) {
67  $connectionOptions['Database'] = $params['dbname'];
68  }
69 
70  if (isset($params['MultipleActiveResultSets'])) {
71  $connectionOptions['MultipleActiveResultSets'] = $params['MultipleActiveResultSets'] ? 'true' : 'false';
72  }
73 
74  return $dsn . $this->‪getConnectionOptionsDsn($connectionOptions);
75  }
76 
77  private function ‪splitOptions(array $options): array
78  {
79  $driverOptions = [];
80  $connectionOptions = [];
81 
82  foreach ($options as $optionKey => $optionValue) {
83  if (is_int($optionKey)) {
84  $driverOptions[$optionKey] = $optionValue;
85  } else {
86  $connectionOptions[$optionKey] = $optionValue;
87  }
88  }
89 
90  return [$driverOptions, $connectionOptions];
91  }
92 
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 
114  public function ‪getName()
115  {
116  return 'pdo_sqlsrv';
117  }
118 }
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv\Driver\getName
‪getName()
Definition: Driver.php:114
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv\Driver
Definition: Driver.php:28
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv\Connection
Definition: Connection.php:28
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv\Driver\getConnectionOptionsDsn
‪getConnectionOptionsDsn(array $connectionOptions)
Definition: Driver.php:98
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv\Driver\connect
‪connect(array $params, $username=null, $password=null, array $driverOptions=[])
Definition: Driver.php:32
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv\Driver\splitOptions
‪splitOptions(array $options)
Definition: Driver.php:77
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv\Driver\_constructPdoDsn
‪string _constructPdoDsn(array $params, array $connectionOptions)
Definition: Driver.php:52
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv
Definition: Connection.php:18