-
Notifications
You must be signed in to change notification settings - Fork 638
/
ConnectionPool.php
176 lines (155 loc) · 5.81 KB
/
ConnectionPool.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Core\Database;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Doctrine\DBAL\DriverManager;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
/**
* Manager that handles opening/retrieving database connections.
*
* It's a facade to the actual Doctrine DBAL DriverManager that implements TYPO3
* specific functionality like mapping individual tables to different database
* connections.
*
* getConnectionForTable() is the only supported way to get a connection that
* honors the table mapping configuration.
*/
class ConnectionPool
{
/**
* @var string
*/
const DEFAULT_CONNECTION_NAME = 'Default';
/**
* @var Connection[]
*/
protected static $connections = [];
/**
* Creates a connection object based on the specified table name.
*
* This is the official entry point to get a database connection to ensure
* that the mapping of table names to database connections is honored.
*
* @param string $tableName
* @return Connection
*/
public function getConnectionForTable(string $tableName): Connection
{
if (empty($tableName)) {
throw new \UnexpectedValueException(
'ConnectionPool->getConnectionForTable() requires a table name to be provided.',
1459421719
);
}
$connectionName = self::DEFAULT_CONNECTION_NAME;
if (!empty($GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'][$tableName])) {
$connectionName = (string)$GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'][$tableName];
}
return $this->getConnectionByName($connectionName);
}
/**
* Creates a connection object based on the specified identifier.
*
* This method should only be used in edge cases. Use getConnectionForTable() so
* that the tablename<>databaseConnection mapping will be taken into account.
*
* @param string $connectionName
* @return Connection
* @throws \Doctrine\DBAL\DBALException
*/
public function getConnectionByName(string $connectionName): Connection
{
if (empty($connectionName)) {
throw new \UnexpectedValueException(
'ConnectionPool->getConnectionByName() requires a connection name to be provided.',
1459422125
);
}
if (isset(static::$connections[$connectionName])) {
return static::$connections[$connectionName];
}
$connectionParams = $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections'][$connectionName] ?? [];
if (empty($connectionParams)) {
throw new \RuntimeException(
'The requested database connection named "' . $connectionName . '" has not been configured.',
1459422492
);
}
if (empty($connectionParams['wrapperClass'])) {
$connectionParams['wrapperClass'] = Connection::class;
}
if (!is_a($connectionParams['wrapperClass'], Connection::class, true)) {
throw new \UnexpectedValueException(
'The "wrapperClass" for the connection name "' . $connectionName .
'" needs to be a subclass of "' . Connection::class . '".',
1459422968
);
}
static::$connections[$connectionName] = $this->getDatabaseConnection($connectionParams);
return static::$connections[$connectionName];
}
/**
* Creates a connection object based on the specified parameters
*
* @param array $connectionParams
* @return Connection
*/
protected function getDatabaseConnection(array $connectionParams): Connection
{
// Default to UTF-8 connection charset
if (empty($connectionParams['charset'])) {
$connectionParams['charset'] = 'utf8';
}
// Force consistent handling of binary objects across datbase platforms
// MySQL returns strings by default, PostgreSQL streams.
if (strpos($connectionParams['driver'], 'pdo_') === 0) {
$connectionParams['driverOptions'][\PDO::ATTR_STRINGIFY_FETCHES] = true;
}
/** @var Connection $conn */
$conn = DriverManager::getConnection($connectionParams);
$conn->setFetchMode(\PDO::FETCH_ASSOC);
$conn->prepareConnection($connectionParams['initCommands'] ?? '');
return $conn;
}
/**
* Returns the connection specific query builder object that can be used to build
* complex SQL queries using and object oriented approach.
*
* @param string $tableName
* @return QueryBuilder
*/
public function getQueryBuilderForTable(string $tableName): QueryBuilder
{
if (empty($tableName)) {
throw new \UnexpectedValueException(
'ConnectionPool->getQueryBuilderForTable() requires a connection name to be provided.',
1459423448
);
}
return $this->getConnectionForTable($tableName)->createQueryBuilder();
}
/**
* Returns an array containing the names of all currently configured connections.
*
* This method should only be used in edge cases. Use getConnectionForTable() so
* that the tablename<>databaseConnection mapping will be taken into account.
*
* @internal
* @return array
*/
public function getConnectionNames(): array
{
return array_keys($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']);
}
}