aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2024-08-06 23:40:35 +0200
committerGitHub <noreply@github.com>2024-08-06 23:40:35 +0200
commit89f82fd5378e2f4e53966ce25fbc8a4f02386de6 (patch)
tree0e73df8f856122a230f8eb119906e2a5d48ada21
parent571b5dbc4905430d67035cc5f6f85e2ec7f385e0 (diff)
parentcf12dd03f954939da3b75680566c569bf7504daa (diff)
downloadnextcloud-server-89f82fd5378e2f4e53966ce25fbc8a4f02386de6.tar.gz
nextcloud-server-89f82fd5378e2f4e53966ce25fbc8a4f02386de6.zip
Merge pull request #47046 from nextcloud/backport/46931/stable29
[stable29] fix: fix passing additional db connection parameters in factory
-rw-r--r--lib/private/DB/ConnectionFactory.php45
1 files changed, 20 insertions, 25 deletions
diff --git a/lib/private/DB/ConnectionFactory.php b/lib/private/DB/ConnectionFactory.php
index e34bd136d6d..34c628ce320 100644
--- a/lib/private/DB/ConnectionFactory.php
+++ b/lib/private/DB/ConnectionFactory.php
@@ -75,16 +75,10 @@ class ConnectionFactory {
],
];
- /** @var SystemConfig */
- private $config;
- /**
- * ConnectionFactory constructor.
- *
- * @param SystemConfig $systemConfig
- */
- public function __construct(SystemConfig $systemConfig) {
- $this->config = $systemConfig;
+ public function __construct(
+ private SystemConfig $config
+ ) {
if ($this->config->getValue('mysql.utf8mb4', false)) {
$this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4';
}
@@ -122,49 +116,49 @@ class ConnectionFactory {
* @param array $additionalConnectionParams Additional connection parameters
* @return \OC\DB\Connection
*/
- public function getConnection($type, $additionalConnectionParams) {
+ public function getConnection(string $type, array $additionalConnectionParams): Connection {
$normalizedType = $this->normalizeType($type);
$eventManager = new EventManager();
$eventManager->addEventSubscriber(new SetTransactionIsolationLevel());
- $additionalConnectionParams = array_merge($this->createConnectionParams(), $additionalConnectionParams);
+ $connectionParams = $this->createConnectionParams('', $additionalConnectionParams);
switch ($normalizedType) {
case 'pgsql':
// pg_connect used by Doctrine DBAL does not support URI notation (enclosed in brackets)
$matches = [];
- if (preg_match('/^\[([^\]]+)\]$/', $additionalConnectionParams['host'], $matches)) {
+ if (preg_match('/^\[([^\]]+)\]$/', $connectionParams['host'], $matches)) {
// Host variable carries a port or socket.
- $additionalConnectionParams['host'] = $matches[1];
+ $connectionParams['host'] = $matches[1];
}
break;
case 'oci':
$eventManager->addEventSubscriber(new OracleSessionInit);
// the driverOptions are unused in dbal and need to be mapped to the parameters
- if (isset($additionalConnectionParams['driverOptions'])) {
- $additionalConnectionParams = array_merge($additionalConnectionParams, $additionalConnectionParams['driverOptions']);
+ if (isset($connectionParams['driverOptions'])) {
+ $connectionParams = array_merge($connectionParams, $connectionParams['driverOptions']);
}
- $host = $additionalConnectionParams['host'];
- $port = $additionalConnectionParams['port'] ?? null;
- $dbName = $additionalConnectionParams['dbname'];
+ $host = $connectionParams['host'];
+ $port = $connectionParams['port'] ?? null;
+ $dbName = $connectionParams['dbname'];
// we set the connect string as dbname and unset the host to coerce doctrine into using it as connect string
if ($host === '') {
- $additionalConnectionParams['dbname'] = $dbName; // use dbname as easy connect name
+ $connectionParams['dbname'] = $dbName; // use dbname as easy connect name
} else {
- $additionalConnectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName;
+ $connectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName;
}
- unset($additionalConnectionParams['host']);
+ unset($connectionParams['host']);
break;
case 'sqlite3':
- $journalMode = $additionalConnectionParams['sqlite.journal_mode'];
- $additionalConnectionParams['platform'] = new OCSqlitePlatform();
+ $journalMode = $connectionParams['sqlite.journal_mode'];
+ $connectionParams['platform'] = new OCSqlitePlatform();
$eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode));
break;
}
/** @var Connection $connection */
$connection = DriverManager::getConnection(
- $additionalConnectionParams,
+ $connectionParams,
new Configuration(),
$eventManager
);
@@ -197,7 +191,7 @@ class ConnectionFactory {
* @param string $configPrefix
* @return array
*/
- public function createConnectionParams(string $configPrefix = '') {
+ public function createConnectionParams(string $configPrefix = '', array $additionalConnectionParams = []) {
$type = $this->config->getValue('dbtype', 'sqlite');
$connectionParams = array_merge($this->getDefaultConnectionParams($type), [
@@ -241,6 +235,7 @@ class ConnectionFactory {
if ($this->config->getValue('dbpersistent', false)) {
$connectionParams['persistent'] = true;
}
+ $connectionParams = array_merge($connectionParams, $additionalConnectionParams);
$replica = $this->config->getValue($configPrefix . 'dbreplica', $this->config->getValue('dbreplica', [])) ?: [$connectionParams];
return array_merge($connectionParams, [