‪TYPO3CMS  9.5
ConnectionPool.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Doctrine\DBAL\DriverManager;
19 use Doctrine\DBAL\Events;
20 use Doctrine\DBAL\Types\Type;
32 
44 {
48  const ‪DEFAULT_CONNECTION_NAME = 'Default';
49 
53  protected static ‪$connections = [];
54 
58  protected ‪$customDoctrineTypes = [
59  ‪EnumType::TYPE => EnumType::class,
60  ‪SetType::TYPE => SetType::class,
61  ];
62 
68  protected static ‪$driverMap = [
69  'pdo_mysql' => PDOMySqlDriver::class,
70  'pdo_sqlite' => PDOSqliteDriver::class,
71  'pdo_pgsql' => PDOPgSqlDriver::class,
72  'pdo_sqlsrv' => PDOSqlsrvDriver::class,
73  // TODO: not supported yet, need to be checked later
74 // 'pdo_oci' => PDOOCIDriver::class,
75 // 'drizzle_pdo_mysql' => DrizzlePDOMySQLDriver::class,
76  ];
77 
87  public function ‪getConnectionForTable(string $tableName): ‪Connection
88  {
89  if (empty($tableName)) {
90  throw new \UnexpectedValueException(
91  'ConnectionPool->getConnectionForTable() requires a table name to be provided.',
92  1459421719
93  );
94  }
95 
96  $connectionName = ‪self::DEFAULT_CONNECTION_NAME;
97  if (!empty(‪$GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'][$tableName])) {
98  $connectionName = (string)‪$GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'][$tableName];
99  }
100 
101  return $this->‪getConnectionByName($connectionName);
102  }
103 
114  public function ‪getConnectionByName(string $connectionName): ‪Connection
115  {
116  if (empty($connectionName)) {
117  throw new \UnexpectedValueException(
118  'ConnectionPool->getConnectionByName() requires a connection name to be provided.',
119  1459422125
120  );
121  }
122 
123  if (isset(static::$connections[$connectionName])) {
124  return static::$connections[$connectionName];
125  }
126 
127  $connectionParams = ‪$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections'][$connectionName] ?? [];
128  if (empty($connectionParams)) {
129  throw new \RuntimeException(
130  'The requested database connection named "' . $connectionName . '" has not been configured.',
131  1459422492
132  );
133  }
134 
135  if (empty($connectionParams['wrapperClass'])) {
136  $connectionParams['wrapperClass'] = Connection::class;
137  }
138 
139  if (!is_a($connectionParams['wrapperClass'], Connection::class, true)) {
140  throw new \UnexpectedValueException(
141  'The "wrapperClass" for the connection name "' . $connectionName .
142  '" needs to be a subclass of "' . Connection::class . '".',
143  1459422968
144  );
145  }
146 
147  static::$connections[$connectionName] = $this->‪getDatabaseConnection($connectionParams);
148 
149  return static::$connections[$connectionName];
150  }
151 
158  protected function ‪mapCustomDriver(array $connectionParams): array
159  {
160  // if no custom driver is provided, map TYPO3 specific drivers
161  if (!isset($connectionParams['driverClass']) && isset(static::$driverMap[$connectionParams['driver']])) {
162  $connectionParams['driverClass'] = static::$driverMap[$connectionParams['driver']];
163  }
164 
165  return $connectionParams;
166  }
167 
174  protected function ‪getDatabaseConnection(array $connectionParams): Connection
175  {
176  // Default to UTF-8 connection charset
177  if (empty($connectionParams['charset'])) {
178  $connectionParams['charset'] = 'utf8';
179  }
180 
181  $connectionParams = $this->‪mapCustomDriver($connectionParams);
182 
184  $conn = DriverManager::getConnection($connectionParams);
185  $conn->setFetchMode(\PDO::FETCH_ASSOC);
186  $conn->prepareConnection($connectionParams['initCommands'] ?? '');
187 
188  // Register custom data types
189  foreach ($this->customDoctrineTypes as $type => $className) {
190  if (!Type::hasType($type)) {
191  Type::addType($type, $className);
192  }
193  }
194 
195  // Register all custom data types in the type mapping
196  foreach ($this->customDoctrineTypes as $type => $className) {
197  $conn->getDatabasePlatform()->registerDoctrineTypeMapping($type, $type);
198  }
199 
200  // Handler for building custom data type column definitions
201  // in the SchemaManager
202  $conn->getDatabasePlatform()->getEventManager()->addEventListener(
203  Events::onSchemaColumnDefinition,
204  GeneralUtility::makeInstance(SchemaColumnDefinitionListener::class)
205  );
206 
207  // Handler for enhanced index definitions in the SchemaManager
208  $conn->getDatabasePlatform()->getEventManager()->addEventListener(
209  Events::onSchemaIndexDefinition,
210  GeneralUtility::makeInstance(SchemaIndexDefinitionListener::class)
211  );
212 
213  // Handler for adding custom database platform options to ALTER TABLE
214  // requests in the SchemaManager
215  $conn->getDatabasePlatform()->getEventManager()->addEventListener(
216  Events::onSchemaAlterTable,
217  GeneralUtility::makeInstance(SchemaAlterTableListener::class)
218  );
219 
220  return $conn;
221  }
222 
230  public function ‪getQueryBuilderForTable(string $tableName): QueryBuilder
231  {
232  if (empty($tableName)) {
233  throw new \UnexpectedValueException(
234  'ConnectionPool->getQueryBuilderForTable() requires a connection name to be provided.',
235  1459423448
236  );
237  }
238 
239  return $this->‪getConnectionForTable($tableName)->‪createQueryBuilder();
240  }
241 
251  public function ‪getConnectionNames(): array
252  {
253  return array_keys(‪$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']);
254  }
255 
265  public function ‪getCustomDoctrineTypes(): array
266  {
268  }
269 
275  public function ‪resetConnections(): void
276  {
277  static::$connections = [];
278  }
279 }
‪TYPO3\CMS\Core\Database\Driver\PDOPgSql\Driver
Definition: Driver.php:30
‪TYPO3\CMS\Core\Database\Driver\PDOMySql\Driver
Definition: Driver.php:29
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaIndexDefinitionListener
Definition: SchemaIndexDefinitionListener.php:28
‪TYPO3\CMS\Core\Database\Schema\Types\SetType
Definition: SetType.php:25
‪TYPO3\CMS\Core\Database\Driver\PDOSqlsrv\Driver
Definition: Driver.php:24
‪TYPO3\CMS\Core\Database\Schema\Types\EnumType
Definition: EnumType.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool\$driverMap
‪static string[] $driverMap
Definition: ConnectionPool.php:65
‪TYPO3\CMS\Core\Database\Driver\PDOSqlite\Driver
Definition: Driver.php:29
‪TYPO3\CMS\Core\Database\Schema\Types\EnumType\TYPE
‪const TYPE
Definition: EnumType.php:26
‪TYPO3\CMS\Core\Database\ConnectionPool\DEFAULT_CONNECTION_NAME
‪const DEFAULT_CONNECTION_NAME
Definition: ConnectionPool.php:48
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Core\Database\ConnectionPool\getCustomDoctrineTypes
‪array getCustomDoctrineTypes()
Definition: ConnectionPool.php:262
‪TYPO3\CMS\Core\Database\ConnectionPool\$customDoctrineTypes
‪array $customDoctrineTypes
Definition: ConnectionPool.php:56
‪TYPO3\CMS\Core\Database\ConnectionPool\mapCustomDriver
‪array mapCustomDriver(array $connectionParams)
Definition: ConnectionPool.php:155
‪TYPO3\CMS\Core\Database\ConnectionPool\getConnectionForTable
‪Connection getConnectionForTable(string $tableName)
Definition: ConnectionPool.php:84
‪TYPO3\CMS\Core\Database\ConnectionPool\$connections
‪static Connection[] $connections
Definition: ConnectionPool.php:52
‪TYPO3\CMS\Core\Database\ConnectionPool\getConnectionByName
‪Connection getConnectionByName(string $connectionName)
Definition: ConnectionPool.php:111
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaAlterTableListener
Definition: SchemaAlterTableListener.php:27
‪TYPO3\CMS\Core\Database\ConnectionPool\getQueryBuilderForTable
‪QueryBuilder getQueryBuilderForTable(string $tableName)
Definition: ConnectionPool.php:227
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Database\Connection\createQueryBuilder
‪TYPO3 CMS Core Database Query QueryBuilder createQueryBuilder()
Definition: Connection.php:110
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Database\Schema\Types\SetType\TYPE
‪const TYPE
Definition: SetType.php:26
‪TYPO3\CMS\Core\Database\ConnectionPool\resetConnections
‪resetConnections()
Definition: ConnectionPool.php:272
‪TYPO3\CMS\Core\Database\ConnectionPool\getDatabaseConnection
‪Connection getDatabaseConnection(array $connectionParams)
Definition: ConnectionPool.php:171
‪TYPO3\CMS\Core\Database
Definition: Connection.php:3
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener
Definition: SchemaColumnDefinitionListener.php:29
‪TYPO3\CMS\Core\Database\ConnectionPool\getConnectionNames
‪array getConnectionNames()
Definition: ConnectionPool.php:248