diff options
Diffstat (limited to 'lib/private/DB/ConnectionFactory.php')
-rw-r--r-- | lib/private/DB/ConnectionFactory.php | 137 |
1 files changed, 74 insertions, 63 deletions
diff --git a/lib/private/DB/ConnectionFactory.php b/lib/private/DB/ConnectionFactory.php index e868f18ec34..d9b80b81992 100644 --- a/lib/private/DB/ConnectionFactory.php +++ b/lib/private/DB/ConnectionFactory.php @@ -1,30 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Andreas Fischer <bantu@owncloud.com> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\DB; @@ -32,7 +11,11 @@ use Doctrine\Common\EventManager; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Event\Listeners\OracleSessionInit; +use OC\DB\QueryBuilder\Sharded\AutoIncrementHandler; +use OC\DB\QueryBuilder\Sharded\ShardConnectionManager; use OC\SystemConfig; +use OCP\ICacheFactory; +use OCP\Server; /** * Takes care of creating and configuring Doctrine connections. @@ -75,16 +58,13 @@ class ConnectionFactory { ], ]; - /** @var SystemConfig */ - private $config; + private ShardConnectionManager $shardConnectionManager; + private ICacheFactory $cacheFactory; - /** - * ConnectionFactory constructor. - * - * @param SystemConfig $systemConfig - */ - public function __construct(SystemConfig $systemConfig) { - $this->config = $systemConfig; + public function __construct( + private SystemConfig $config, + ?ICacheFactory $cacheFactory = null, + ) { if ($this->config->getValue('mysql.utf8mb4', false)) { $this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4'; } @@ -92,6 +72,8 @@ class ConnectionFactory { if ($collationOverride) { $this->defaultConnectionParams['mysql']['collation'] = $collationOverride; } + $this->shardConnectionManager = new ShardConnectionManager($this->config, $this); + $this->cacheFactory = $cacheFactory ?? Server::get(ICacheFactory::class); } /** @@ -122,40 +104,37 @@ 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, $type); switch ($normalizedType) { - 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']); + case 'pgsql': + // pg_connect used by Doctrine DBAL does not support URI notation (enclosed in brackets) + $matches = []; + if (preg_match('/^\[([^\]]+)\]$/', $connectionParams['host'], $matches)) { + // Host variable carries a port or socket. + $connectionParams['host'] = $matches[1]; } - $host = $additionalConnectionParams['host']; - $port = $additionalConnectionParams['port'] ?? null; - $dbName = $additionalConnectionParams['dbname']; + break; - // 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 - } else { - $additionalConnectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName; - } - unset($additionalConnectionParams['host']); + case 'oci': + $eventManager->addEventSubscriber(new OracleSessionInit); + $connectionParams = $this->forceConnectionStringOracle($connectionParams); + $connectionParams['primary'] = $this->forceConnectionStringOracle($connectionParams['primary']); + $connectionParams['replica'] = array_map([$this, 'forceConnectionStringOracle'], $connectionParams['replica']); 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 ); @@ -184,12 +163,10 @@ class ConnectionFactory { /** * Create the connection parameters for the config - * - * @param string $configPrefix - * @return array */ - public function createConnectionParams(string $configPrefix = '') { - $type = $this->config->getValue('dbtype', 'sqlite'); + public function createConnectionParams(string $configPrefix = '', array $additionalConnectionParams = [], ?string $type = null) { + // use provided type or if null use type from config + $type = $type ?? $this->config->getValue('dbtype', 'sqlite'); $connectionParams = array_merge($this->getDefaultConnectionParams($type), [ 'user' => $this->config->getValue($configPrefix . 'dbuser', $this->config->getValue('dbuser', '')), @@ -198,7 +175,7 @@ class ConnectionFactory { $name = $this->config->getValue($configPrefix . 'dbname', $this->config->getValue('dbname', self::DEFAULT_DBNAME)); if ($this->normalizeType($type) === 'sqlite3') { - $dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data'); + $dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); $connectionParams['path'] = $dataDir . '/' . $name . '.db'; } else { $host = $this->config->getValue($configPrefix . 'dbhost', $this->config->getValue('dbhost', '')); @@ -221,7 +198,7 @@ class ConnectionFactory { 'tablePrefix' => $connectionParams['tablePrefix'] ]; - if ($this->config->getValue('mysql.utf8mb4', false)) { + if ($type === 'mysql' && $this->config->getValue('mysql.utf8mb4', false)) { $connectionParams['defaultTableOptions'] = [ 'collate' => 'utf8mb4_bin', 'charset' => 'utf8mb4', @@ -233,7 +210,21 @@ class ConnectionFactory { $connectionParams['persistent'] = true; } - $replica = $this->config->getValue('dbreplica', []) ?: [$connectionParams]; + $connectionParams['sharding'] = $this->config->getValue('dbsharding', []); + if (!empty($connectionParams['sharding'])) { + $connectionParams['shard_connection_manager'] = $this->shardConnectionManager; + $connectionParams['auto_increment_handler'] = new AutoIncrementHandler( + $this->cacheFactory, + $this->shardConnectionManager, + ); + } else { + // just in case only the presence could lead to funny behaviour + unset($connectionParams['sharding']); + } + + $connectionParams = array_merge($connectionParams, $additionalConnectionParams); + + $replica = $this->config->getValue($configPrefix . 'dbreplica', $this->config->getValue('dbreplica', [])) ?: [$connectionParams]; return array_merge($connectionParams, [ 'primary' => $connectionParams, 'replica' => $replica, @@ -254,7 +245,7 @@ class ConnectionFactory { // Host variable carries a port or socket. $params['host'] = $matches[1]; if (is_numeric($matches[2])) { - $params['port'] = (int) $matches[2]; + $params['port'] = (int)$matches[2]; } else { $params['unix_socket'] = $matches[2]; } @@ -262,4 +253,24 @@ class ConnectionFactory { return $params; } + + protected function forceConnectionStringOracle(array $connectionParams): array { + // the driverOptions are unused in dbal and need to be mapped to the parameters + if (isset($connectionParams['driverOptions'])) { + $connectionParams = array_merge($connectionParams, $connectionParams['driverOptions']); + } + $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 === '') { + $connectionParams['dbname'] = $dbName; // use dbname as easy connect name + } else { + $connectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : '') . '/' . $dbName; + } + unset($connectionParams['host']); + + return $connectionParams; + } } |