diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-08-05 17:41:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-05 17:41:04 +0200 |
commit | 9ef2e142c4614596f95772d9145b158ecab68e58 (patch) | |
tree | a6f9e9b68f86006d6b4eb2e4717abe146410165c /lib | |
parent | 6dc614cb3115244a9d1a889c4e92252f157aef05 (diff) | |
parent | 5654799cb43d1e39c05b4055f92e9cbffeff16a0 (diff) | |
download | nextcloud-server-9ef2e142c4614596f95772d9145b158ecab68e58.tar.gz nextcloud-server-9ef2e142c4614596f95772d9145b158ecab68e58.zip |
Merge pull request #46931 from nextcloud/connection-builder-additional-params
fix: fix passing additional db connection parameters in factory
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/DB/ConnectionFactory.php | 45 |
1 files changed, 20 insertions, 25 deletions
diff --git a/lib/private/DB/ConnectionFactory.php b/lib/private/DB/ConnectionFactory.php index 5e50d01538a..dd041f1e41d 100644 --- a/lib/private/DB/ConnectionFactory.php +++ b/lib/private/DB/ConnectionFactory.php @@ -54,16 +54,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'; } @@ -101,49 +95,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 ); @@ -176,7 +170,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), [ @@ -220,6 +214,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, [ |