aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/DB/Connection.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/DB/Connection.php')
-rw-r--r--lib/private/DB/Connection.php579
1 files changed, 471 insertions, 108 deletions
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php
index ceaffbcfa9a..f86cbc341a4 100644
--- a/lib/private/DB/Connection.php
+++ b/lib/private/DB/Connection.php
@@ -1,64 +1,55 @@
<?php
declare(strict_types=1);
-
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bart Visscher <bartv@thisnet.nl>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Ole Ostergaard <ole.c.ostergaard@gmail.com>
- * @author Ole Ostergaard <ole.ostergaard@knime.com>
- * @author Philipp Schaffrath <github@philipp.schaffrath.email>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Robin McCorkell <robin@mccorkell.me.uk>
- * @author Roeland Jago Douma <roeland@famdouma.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;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Configuration;
+use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Driver;
+use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Exception;
-use Doctrine\DBAL\Exception\ConstraintViolationException;
-use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
+use Doctrine\DBAL\Exception\ConnectionLost;
+use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
-use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
+use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Statement;
+use OC\DB\QueryBuilder\Partitioned\PartitionedQueryBuilder;
+use OC\DB\QueryBuilder\Partitioned\PartitionSplit;
+use OC\DB\QueryBuilder\QueryBuilder;
+use OC\DB\QueryBuilder\Sharded\AutoIncrementHandler;
+use OC\DB\QueryBuilder\Sharded\CrossShardMoveHelper;
+use OC\DB\QueryBuilder\Sharded\RoundRobinShardMapper;
+use OC\DB\QueryBuilder\Sharded\ShardConnectionManager;
+use OC\DB\QueryBuilder\Sharded\ShardDefinition;
+use OC\SystemConfig;
use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\DB\QueryBuilder\Sharded\IShardMapper;
use OCP\Diagnostics\IEventLogger;
+use OCP\ICacheFactory;
+use OCP\IDBConnection;
+use OCP\ILogger;
use OCP\IRequestId;
use OCP\PreConditionNotMetException;
use OCP\Profiler\IProfiler;
-use OC\DB\QueryBuilder\QueryBuilder;
-use OC\SystemConfig;
+use OCP\Security\ISecureRandom;
+use OCP\Server;
+use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
+use function count;
+use function in_array;
-class Connection extends \Doctrine\DBAL\Connection {
+class Connection extends PrimaryReadReplicaConnection {
/** @var string */
protected $tablePrefix;
@@ -68,6 +59,8 @@ class Connection extends \Doctrine\DBAL\Connection {
/** @var SystemConfig */
private $systemConfig;
+ private ClockInterface $clock;
+
private LoggerInterface $logger;
protected $lockedTable = null;
@@ -80,6 +73,41 @@ class Connection extends \Doctrine\DBAL\Connection {
/** @var DbDataCollector|null */
protected $dbDataCollector = null;
+ private array $lastConnectionCheck = [];
+
+ protected ?float $transactionActiveSince = null;
+
+ /** @var array<string, int> */
+ protected $tableDirtyWrites = [];
+
+ protected bool $logDbException = false;
+ private ?array $transactionBacktrace = null;
+
+ protected bool $logRequestId;
+ protected string $requestId;
+
+ /** @var array<string, list<string>> */
+ protected array $partitions;
+ /** @var ShardDefinition[] */
+ protected array $shards = [];
+ protected ShardConnectionManager $shardConnectionManager;
+ protected AutoIncrementHandler $autoIncrementHandler;
+ protected bool $isShardingEnabled;
+
+ public const SHARD_PRESETS = [
+ 'filecache' => [
+ 'companion_keys' => [
+ 'file_id',
+ ],
+ 'companion_tables' => [
+ 'filecache_extended',
+ 'files_metadata',
+ ],
+ 'primary_key' => 'fileid',
+ 'shard_key' => 'storage',
+ 'table' => 'filecache',
+ ],
+ ];
/**
* Initializes a new instance of the Connection class.
@@ -87,10 +115,10 @@ class Connection extends \Doctrine\DBAL\Connection {
* @throws \Exception
*/
public function __construct(
- array $params,
+ private array $params,
Driver $driver,
?Configuration $config = null,
- ?EventManager $eventManager = null
+ ?EventManager $eventManager = null,
) {
if (!isset($params['adapter'])) {
throw new \Exception('adapter not set');
@@ -104,12 +132,27 @@ class Connection extends \Doctrine\DBAL\Connection {
parent::__construct($params, $driver, $config, $eventManager);
$this->adapter = new $params['adapter']($this);
$this->tablePrefix = $params['tablePrefix'];
-
+ $this->isShardingEnabled = isset($this->params['sharding']) && !empty($this->params['sharding']);
+
+ if ($this->isShardingEnabled) {
+ /** @psalm-suppress InvalidArrayOffset */
+ $this->shardConnectionManager = $this->params['shard_connection_manager'] ?? Server::get(ShardConnectionManager::class);
+ /** @psalm-suppress InvalidArrayOffset */
+ $this->autoIncrementHandler = $this->params['auto_increment_handler'] ?? new AutoIncrementHandler(
+ Server::get(ICacheFactory::class),
+ $this->shardConnectionManager,
+ );
+ }
$this->systemConfig = \OC::$server->getSystemConfig();
- $this->logger = \OC::$server->get(LoggerInterface::class);
+ $this->clock = Server::get(ClockInterface::class);
+ $this->logger = Server::get(LoggerInterface::class);
+
+ $this->logRequestId = $this->systemConfig->getValue('db.log_request_id', false);
+ $this->logDbException = $this->systemConfig->getValue('db.log_exceptions', false);
+ $this->requestId = Server::get(IRequestId::class)->getId();
/** @var \OCP\Profiler\IProfiler */
- $profiler = \OC::$server->get(IProfiler::class);
+ $profiler = Server::get(IProfiler::class);
if ($profiler->isEnabled()) {
$this->dbDataCollector = new DbDataCollector($this);
$profiler->add($this->dbDataCollector);
@@ -117,25 +160,78 @@ class Connection extends \Doctrine\DBAL\Connection {
$this->dbDataCollector->setDebugStack($debugStack);
$this->_config->setSQLLogger($debugStack);
}
+
+ /** @var array<string, array{shards: array[], mapper: ?string, from_primary_key: ?int, from_shard_key: ?int}> $shardConfig */
+ $shardConfig = $this->params['sharding'] ?? [];
+ $shardNames = array_keys($shardConfig);
+ $this->shards = array_map(function (array $config, string $name) {
+ if (!isset(self::SHARD_PRESETS[$name])) {
+ throw new \Exception("Shard preset $name not found");
+ }
+
+ $shardMapperClass = $config['mapper'] ?? RoundRobinShardMapper::class;
+ $shardMapper = Server::get($shardMapperClass);
+ if (!$shardMapper instanceof IShardMapper) {
+ throw new \Exception("Invalid shard mapper: $shardMapperClass");
+ }
+ return new ShardDefinition(
+ self::SHARD_PRESETS[$name]['table'],
+ self::SHARD_PRESETS[$name]['primary_key'],
+ self::SHARD_PRESETS[$name]['companion_keys'],
+ self::SHARD_PRESETS[$name]['shard_key'],
+ $shardMapper,
+ self::SHARD_PRESETS[$name]['companion_tables'],
+ $config['shards'],
+ $config['from_primary_key'] ?? 0,
+ $config['from_shard_key'] ?? 0,
+ );
+ }, $shardConfig, $shardNames);
+ $this->shards = array_combine($shardNames, $this->shards);
+ $this->partitions = array_map(function (ShardDefinition $shard) {
+ return array_merge([$shard->table], $shard->companionTables);
+ }, $this->shards);
+
+ $this->setNestTransactionsWithSavepoints(true);
+ }
+
+ /**
+ * @return IDBConnection[]
+ */
+ public function getShardConnections(): array {
+ $connections = [];
+ if ($this->isShardingEnabled) {
+ foreach ($this->shards as $shardDefinition) {
+ foreach ($shardDefinition->getAllShards() as $shard) {
+ if ($shard !== ShardDefinition::MIGRATION_SHARD) {
+ /** @var ConnectionAdapter $connection */
+ $connections[] = $this->shardConnectionManager->getConnection($shardDefinition, $shard);
+ }
+ }
+ }
+ }
+ return $connections;
}
/**
* @throws Exception
*/
- public function connect() {
+ public function connect($connectionName = null) {
try {
if ($this->_conn) {
+ $this->reconnectIfNeeded();
/** @psalm-suppress InternalMethod */
return parent::connect();
}
// Only trigger the event logger for the initial connect call
- $eventLogger = \OC::$server->get(IEventLogger::class);
+ $eventLogger = Server::get(IEventLogger::class);
$eventLogger->start('connect:db', 'db connection opened');
/** @psalm-suppress InternalMethod */
$status = parent::connect();
$eventLogger->end('connect:db');
+ $this->lastConnectionCheck[$this->getConnectionName()] = time();
+
return $status;
} catch (Exception $e) {
// throw a new exception to prevent leaking info from the stacktrace
@@ -143,6 +239,15 @@ class Connection extends \Doctrine\DBAL\Connection {
}
}
+ protected function performConnect(?string $connectionName = null): bool {
+ if (($connectionName ?? 'replica') === 'replica'
+ && count($this->params['replica']) === 1
+ && $this->params['primary'] === $this->params['replica'][0]) {
+ return parent::performConnect('primary');
+ }
+ return parent::performConnect($connectionName);
+ }
+
public function getStats(): array {
return [
'built' => $this->queriesBuilt,
@@ -155,18 +260,34 @@ class Connection extends \Doctrine\DBAL\Connection {
*/
public function getQueryBuilder(): IQueryBuilder {
$this->queriesBuilt++;
- return new QueryBuilder(
+
+ $builder = new QueryBuilder(
new ConnectionAdapter($this),
$this->systemConfig,
$this->logger
);
+ if ($this->isShardingEnabled && count($this->partitions) > 0) {
+ $builder = new PartitionedQueryBuilder(
+ $builder,
+ $this->shards,
+ $this->shardConnectionManager,
+ $this->autoIncrementHandler,
+ );
+ foreach ($this->partitions as $name => $tables) {
+ $partition = new PartitionSplit($name, $tables);
+ $builder->addPartition($partition);
+ }
+ return $builder;
+ } else {
+ return $builder;
+ }
}
/**
* Gets the QueryBuilder for the connection.
*
* @return \Doctrine\DBAL\Query\QueryBuilder
- * @deprecated please use $this->getQueryBuilder() instead
+ * @deprecated 8.0.0 please use $this->getQueryBuilder() instead
*/
public function createQueryBuilder() {
$backtrace = $this->getCallerBacktrace();
@@ -179,7 +300,7 @@ class Connection extends \Doctrine\DBAL\Connection {
* Gets the ExpressionBuilder for the connection.
*
* @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder
- * @deprecated please use $this->getQueryBuilder()->expr() instead
+ * @deprecated 8.0.0 please use $this->getQueryBuilder()->expr() instead
*/
public function getExpressionBuilder() {
$backtrace = $this->getCallerBacktrace();
@@ -222,21 +343,20 @@ class Connection extends \Doctrine\DBAL\Connection {
* @return Statement The prepared statement.
* @throws Exception
*/
- public function prepare($statement, $limit = null, $offset = null): Statement {
+ public function prepare($sql, $limit = null, $offset = null): Statement {
if ($limit === -1 || $limit === null) {
$limit = null;
} else {
- $limit = (int) $limit;
+ $limit = (int)$limit;
}
if ($offset !== null) {
- $offset = (int) $offset;
+ $offset = (int)$offset;
}
if (!is_null($limit)) {
$platform = $this->getDatabasePlatform();
- $statement = $platform->modifyLimitQuery($statement, $limit, $offset);
+ $sql = $platform->modifyLimitQuery($sql, $limit, $offset);
}
- $statement = $this->replaceTablePrefix($statement);
- $statement = $this->adapter->fixupStatement($statement);
+ $statement = $this->finishQuery($sql);
return parent::prepare($statement);
}
@@ -247,32 +367,78 @@ class Connection extends \Doctrine\DBAL\Connection {
* If the query is parametrized, a prepared statement is used.
* If an SQLLogger is configured, the execution is logged.
*
- * @param string $sql The SQL query to execute.
- * @param array $params The parameters to bind to the query, if any.
- * @param array $types The types the previous parameters are in.
- * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
+ * @param string $sql The SQL query to execute.
+ * @param array $params The parameters to bind to the query, if any.
+ * @param array $types The types the previous parameters are in.
+ * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
*
* @return Result The executed statement.
*
* @throws \Doctrine\DBAL\Exception
*/
- public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile $qcp = null): Result {
- $sql = $this->replaceTablePrefix($sql);
- $sql = $this->adapter->fixupStatement($sql);
+ public function executeQuery(string $sql, array $params = [], $types = [], ?QueryCacheProfile $qcp = null): Result {
+ $tables = $this->getQueriedTables($sql);
+ $now = $this->clock->now()->getTimestamp();
+ $dirtyTableWrites = [];
+ foreach ($tables as $table) {
+ $lastAccess = $this->tableDirtyWrites[$table] ?? 0;
+ // Only very recent writes are considered dirty
+ if ($lastAccess >= ($now - 3)) {
+ $dirtyTableWrites[] = $table;
+ }
+ }
+ if ($this->isTransactionActive()) {
+ // Transacted queries go to the primary. The consistency of the primary guarantees that we can not run
+ // into a dirty read.
+ } elseif (count($dirtyTableWrites) === 0) {
+ // No tables read that could have been written already in the same request and no transaction active
+ // so we can switch back to the replica for reading as long as no writes happen that switch back to the primary
+ // We cannot log here as this would log too early in the server boot process
+ $this->ensureConnectedToReplica();
+ } else {
+ // Read to a table that has been written to previously
+ // While this might not necessarily mean that we did a read after write it is an indication for a code path to check
+ $this->logger->log(
+ (int)($this->systemConfig->getValue('loglevel_dirty_database_queries', null) ?? 0),
+ 'dirty table reads: ' . $sql,
+ [
+ 'tables' => array_keys($this->tableDirtyWrites),
+ 'reads' => $tables,
+ 'exception' => new \Exception('dirty table reads: ' . $sql),
+ ],
+ );
+ // To prevent a dirty read on a replica that is slightly out of sync, we
+ // switch back to the primary. This is detrimental for performance but
+ // safer for consistency.
+ $this->ensureConnectedToPrimary();
+ }
+
+ $sql = $this->finishQuery($sql);
$this->queriesExecuted++;
- $this->logQueryToFile($sql);
- return parent::executeQuery($sql, $params, $types, $qcp);
+ $this->logQueryToFile($sql, $params);
+ try {
+ return parent::executeQuery($sql, $params, $types, $qcp);
+ } catch (\Exception $e) {
+ $this->logDatabaseException($e);
+ throw $e;
+ }
+ }
+
+ /**
+ * Helper function to get the list of tables affected by a given query
+ * used to track dirty tables that received a write with the current request
+ */
+ private function getQueriedTables(string $sql): array {
+ $re = '/(\*PREFIX\*\w+)/mi';
+ preg_match_all($re, $sql, $matches);
+ return array_map([$this, 'replaceTablePrefix'], $matches[0] ?? []);
}
/**
* @throws Exception
*/
public function executeUpdate(string $sql, array $params = [], array $types = []): int {
- $sql = $this->replaceTablePrefix($sql);
- $sql = $this->adapter->fixupStatement($sql);
- $this->queriesExecuted++;
- $this->logQueryToFile($sql);
- return parent::executeUpdate($sql, $params, $types);
+ return $this->executeStatement($sql, $params, $types);
}
/**
@@ -281,33 +447,57 @@ class Connection extends \Doctrine\DBAL\Connection {
*
* This method supports PDO binding types as well as DBAL mapping types.
*
- * @param string $sql The SQL query.
- * @param array $params The query parameters.
- * @param array $types The parameter types.
+ * @param string $sql The SQL query.
+ * @param array $params The query parameters.
+ * @param array $types The parameter types.
*
* @return int The number of affected rows.
*
* @throws \Doctrine\DBAL\Exception
*/
public function executeStatement($sql, array $params = [], array $types = []): int {
- $sql = $this->replaceTablePrefix($sql);
- $sql = $this->adapter->fixupStatement($sql);
+ $tables = $this->getQueriedTables($sql);
+ foreach ($tables as $table) {
+ $this->tableDirtyWrites[$table] = $this->clock->now()->getTimestamp();
+ }
+ $sql = $this->finishQuery($sql);
$this->queriesExecuted++;
- $this->logQueryToFile($sql);
- return (int)parent::executeStatement($sql, $params, $types);
+ $this->logQueryToFile($sql, $params);
+ try {
+ return (int)parent::executeStatement($sql, $params, $types);
+ } catch (\Exception $e) {
+ $this->logDatabaseException($e);
+ throw $e;
+ }
}
- protected function logQueryToFile(string $sql): void {
+ protected function logQueryToFile(string $sql, array $params): void {
$logFile = $this->systemConfig->getValue('query_log_file');
if ($logFile !== '' && is_writable(dirname($logFile)) && (!file_exists($logFile) || is_writable($logFile))) {
$prefix = '';
if ($this->systemConfig->getValue('query_log_file_requestid') === 'yes') {
- $prefix .= \OC::$server->get(IRequestId::class)->getId() . "\t";
+ $prefix .= Server::get(IRequestId::class)->getId() . "\t";
+ }
+
+ $postfix = '';
+ if ($this->systemConfig->getValue('query_log_file_parameters') === 'yes') {
+ $postfix .= '; ' . json_encode($params);
}
+ if ($this->systemConfig->getValue('query_log_file_backtrace') === 'yes') {
+ $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+ array_pop($trace);
+ $postfix .= '; ' . json_encode($trace);
+ }
+
+ // FIXME: Improve to log the actual target db host
+ $isPrimary = $this->connections['primary'] === $this->_conn;
+ $prefix .= ' ' . ($isPrimary === true ? 'primary' : 'replica') . ' ';
+ $prefix .= ' ' . $this->getTransactionNestingLevel() . ' ';
+
file_put_contents(
$this->systemConfig->getValue('query_log_file', ''),
- $prefix . $sql . "\n",
+ $prefix . $sql . $postfix . "\n",
FILE_APPEND
);
}
@@ -323,14 +513,14 @@ class Connection extends \Doctrine\DBAL\Connection {
*
* @param string $seqName Name of the sequence object from which the ID should be returned.
*
- * @return string the last inserted ID.
+ * @return int the last inserted ID.
* @throws Exception
*/
- public function lastInsertId($seqName = null) {
- if ($seqName) {
- $seqName = $this->replaceTablePrefix($seqName);
+ public function lastInsertId($name = null): int {
+ if ($name) {
+ $name = $this->replaceTablePrefix($name);
}
- return $this->adapter->lastInsertId($seqName);
+ return $this->adapter->lastInsertId($name);
}
/**
@@ -349,18 +539,28 @@ class Connection extends \Doctrine\DBAL\Connection {
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
* @param array $input data that should be inserted into the table (column name => value)
* @param array|null $compare List of values that should be checked for "if not exists"
- * If this is null or an empty array, all keys of $input will be compared
- * Please note: text fields (clob) must not be used in the compare array
+ * If this is null or an empty array, all keys of $input will be compared
+ * Please note: text fields (clob) must not be used in the compare array
* @return int number of inserted rows
* @throws \Doctrine\DBAL\Exception
* @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371
*/
- public function insertIfNotExist($table, $input, array $compare = null) {
- return $this->adapter->insertIfNotExist($table, $input, $compare);
+ public function insertIfNotExist($table, $input, ?array $compare = null) {
+ try {
+ return $this->adapter->insertIfNotExist($table, $input, $compare);
+ } catch (\Exception $e) {
+ $this->logDatabaseException($e);
+ throw $e;
+ }
}
public function insertIgnoreConflict(string $table, array $values) : int {
- return $this->adapter->insertIgnoreConflict($table, $values);
+ try {
+ return $this->adapter->insertIgnoreConflict($table, $values);
+ } catch (\Exception $e) {
+ $this->logDatabaseException($e);
+ throw $e;
+ }
}
private function getType($value) {
@@ -381,10 +581,10 @@ class Connection extends \Doctrine\DBAL\Connection {
* @param array $values (column name => value)
* @param array $updatePreconditionValues ensure values match preconditions (column name => value)
* @return int number of new rows
- * @throws \Doctrine\DBAL\Exception
+ * @throws \OCP\DB\Exception
* @throws PreConditionNotMetException
*/
- public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) {
+ public function setValues(string $table, array $keys, array $values, array $updatePreconditionValues = []): int {
try {
$insertQb = $this->getQueryBuilder();
$insertQb->insert($table)
@@ -394,31 +594,37 @@ class Connection extends \Doctrine\DBAL\Connection {
}, array_merge($keys, $values))
);
return $insertQb->executeStatement();
- } catch (NotNullConstraintViolationException $e) {
- throw $e;
- } catch (ConstraintViolationException $e) {
+ } catch (\OCP\DB\Exception $e) {
+ if (!in_array($e->getReason(), [
+ \OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION,
+ \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION,
+ ])
+ ) {
+ throw $e;
+ }
+
// value already exists, try update
$updateQb = $this->getQueryBuilder();
$updateQb->update($table);
foreach ($values as $name => $value) {
$updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value)));
}
- $where = $updateQb->expr()->andX();
+ $where = [];
$whereValues = array_merge($keys, $updatePreconditionValues);
foreach ($whereValues as $name => $value) {
if ($value === '') {
- $where->add($updateQb->expr()->emptyString(
+ $where[] = $updateQb->expr()->emptyString(
$name
- ));
+ );
} else {
- $where->add($updateQb->expr()->eq(
+ $where[] = $updateQb->expr()->eq(
$name,
$updateQb->createNamedParameter($value, $this->getType($value)),
$this->getType($value)
- ));
+ );
}
}
- $updateQb->where($where);
+ $updateQb->where($updateQb->expr()->andX(...$where));
$affected = $updateQb->executeStatement();
if ($affected === 0 && !empty($updatePreconditionValues)) {
@@ -468,9 +674,9 @@ class Connection extends \Doctrine\DBAL\Connection {
$msg = $this->errorCode() . ': ';
$errorInfo = $this->errorInfo();
if (!empty($errorInfo)) {
- $msg .= 'SQLSTATE = '.$errorInfo[0] . ', ';
- $msg .= 'Driver Code = '.$errorInfo[1] . ', ';
- $msg .= 'Driver Message = '.$errorInfo[2];
+ $msg .= 'SQLSTATE = ' . $errorInfo[0] . ', ';
+ $msg .= 'Driver Code = ' . $errorInfo[1] . ', ';
+ $msg .= 'Driver Message = ' . $errorInfo[2];
}
return $msg;
}
@@ -492,13 +698,26 @@ class Connection extends \Doctrine\DBAL\Connection {
*/
public function dropTable($table) {
$table = $this->tablePrefix . trim($table);
- $schema = $this->getSchemaManager();
+ $schema = $this->createSchemaManager();
if ($schema->tablesExist([$table])) {
$schema->dropTable($table);
}
}
/**
+ * Truncate a table data if it exists
+ *
+ * @param string $table table name without the prefix
+ * @param bool $cascade whether to truncate cascading
+ *
+ * @throws Exception
+ */
+ public function truncateTable(string $table, bool $cascade) {
+ $this->executeStatement($this->getDatabasePlatform()
+ ->getTruncateTableSQL($this->tablePrefix . trim($table), $cascade));
+ }
+
+ /**
* Check if a table exists
*
* @param string $table table name without the prefix
@@ -508,10 +727,20 @@ class Connection extends \Doctrine\DBAL\Connection {
*/
public function tableExists($table) {
$table = $this->tablePrefix . trim($table);
- $schema = $this->getSchemaManager();
+ $schema = $this->createSchemaManager();
return $schema->tablesExist([$table]);
}
+ protected function finishQuery(string $statement): string {
+ $statement = $this->replaceTablePrefix($statement);
+ $statement = $this->adapter->fixupStatement($statement);
+ if ($this->logRequestId) {
+ return $statement . ' /* reqid: ' . $this->requestId . ' */';
+ } else {
+ return $statement;
+ }
+ }
+
// internal use
/**
* @param string $statement
@@ -583,25 +812,159 @@ class Connection extends \Doctrine\DBAL\Connection {
return $migrator->generateChangeScript($toSchema);
} else {
$migrator->migrate($toSchema);
+ foreach ($this->getShardConnections() as $shardConnection) {
+ $shardConnection->migrateToSchema($toSchema);
+ }
}
}
private function getMigrator() {
// TODO properly inject those dependencies
- $random = \OC::$server->getSecureRandom();
+ $random = \OC::$server->get(ISecureRandom::class);
$platform = $this->getDatabasePlatform();
$config = \OC::$server->getConfig();
- $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class);
+ $dispatcher = Server::get(\OCP\EventDispatcher\IEventDispatcher::class);
if ($platform instanceof SqlitePlatform) {
return new SQLiteMigrator($this, $config, $dispatcher);
} elseif ($platform instanceof OraclePlatform) {
return new OracleMigrator($this, $config, $dispatcher);
- } elseif ($platform instanceof MySQLPlatform) {
- return new MySQLMigrator($this, $config, $dispatcher);
- } elseif ($platform instanceof PostgreSQL94Platform) {
- return new PostgreSqlMigrator($this, $config, $dispatcher);
} else {
return new Migrator($this, $config, $dispatcher);
}
}
+
+ public function beginTransaction() {
+ if (!$this->inTransaction()) {
+ $this->transactionBacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+ $this->transactionActiveSince = microtime(true);
+ }
+ return parent::beginTransaction();
+ }
+
+ public function commit() {
+ $result = parent::commit();
+ if ($this->getTransactionNestingLevel() === 0) {
+ $timeTook = microtime(true) - $this->transactionActiveSince;
+ $this->transactionBacktrace = null;
+ $this->transactionActiveSince = null;
+ if ($timeTook > 1) {
+ $logLevel = match (true) {
+ $timeTook > 20 * 60 => ILogger::ERROR,
+ $timeTook > 5 * 60 => ILogger::WARN,
+ $timeTook > 10 => ILogger::INFO,
+ default => ILogger::DEBUG,
+ };
+ $this->logger->log(
+ $logLevel,
+ 'Transaction took ' . $timeTook . 's',
+ [
+ 'exception' => new \Exception('Transaction took ' . $timeTook . 's'),
+ 'timeSpent' => $timeTook,
+ ]
+ );
+ }
+ }
+ return $result;
+ }
+
+ public function rollBack() {
+ $result = parent::rollBack();
+ if ($this->getTransactionNestingLevel() === 0) {
+ $timeTook = microtime(true) - $this->transactionActiveSince;
+ $this->transactionBacktrace = null;
+ $this->transactionActiveSince = null;
+ if ($timeTook > 1) {
+ $logLevel = match (true) {
+ $timeTook > 20 * 60 => ILogger::ERROR,
+ $timeTook > 5 * 60 => ILogger::WARN,
+ $timeTook > 10 => ILogger::INFO,
+ default => ILogger::DEBUG,
+ };
+ $this->logger->log(
+ $logLevel,
+ 'Transaction rollback took longer than 1s: ' . $timeTook,
+ [
+ 'exception' => new \Exception('Long running transaction rollback'),
+ 'timeSpent' => $timeTook,
+ ]
+ );
+ }
+ }
+ return $result;
+ }
+
+ private function reconnectIfNeeded(): void {
+ if (
+ !isset($this->lastConnectionCheck[$this->getConnectionName()])
+ || time() <= $this->lastConnectionCheck[$this->getConnectionName()] + 30
+ || $this->isTransactionActive()
+ ) {
+ return;
+ }
+
+ try {
+ $this->_conn->query($this->getDriver()->getDatabasePlatform()->getDummySelectSQL());
+ $this->lastConnectionCheck[$this->getConnectionName()] = time();
+ } catch (ConnectionLost|\Exception $e) {
+ $this->logger->warning('Exception during connectivity check, closing and reconnecting', ['exception' => $e]);
+ $this->close();
+ }
+ }
+
+ private function getConnectionName(): string {
+ return $this->isConnectedToPrimary() ? 'primary' : 'replica';
+ }
+
+ /**
+ * @return IDBConnection::PLATFORM_MYSQL|IDBConnection::PLATFORM_ORACLE|IDBConnection::PLATFORM_POSTGRES|IDBConnection::PLATFORM_SQLITE|IDBConnection::PLATFORM_MARIADB
+ */
+ public function getDatabaseProvider(bool $strict = false): string {
+ $platform = $this->getDatabasePlatform();
+ if ($strict && $platform instanceof MariaDBPlatform) {
+ return IDBConnection::PLATFORM_MARIADB;
+ } elseif ($platform instanceof MySQLPlatform) {
+ return IDBConnection::PLATFORM_MYSQL;
+ } elseif ($platform instanceof OraclePlatform) {
+ return IDBConnection::PLATFORM_ORACLE;
+ } elseif ($platform instanceof PostgreSQLPlatform) {
+ return IDBConnection::PLATFORM_POSTGRES;
+ } elseif ($platform instanceof SqlitePlatform) {
+ return IDBConnection::PLATFORM_SQLITE;
+ } else {
+ throw new \Exception('Database ' . $platform::class . ' not supported');
+ }
+ }
+
+ /**
+ * @internal Should only be used inside the QueryBuilder, ExpressionBuilder and FunctionBuilder
+ * All apps and API code should not need this and instead use provided functionality from the above.
+ */
+ public function getServerVersion(): string {
+ /** @var ServerInfoAwareConnection $this->_conn */
+ return $this->_conn->getServerVersion();
+ }
+
+ /**
+ * Log a database exception if enabled
+ *
+ * @param \Exception $exception
+ * @return void
+ */
+ public function logDatabaseException(\Exception $exception): void {
+ if ($this->logDbException) {
+ if ($exception instanceof Exception\UniqueConstraintViolationException) {
+ $this->logger->info($exception->getMessage(), ['exception' => $exception, 'transaction' => $this->transactionBacktrace]);
+ } else {
+ $this->logger->error($exception->getMessage(), ['exception' => $exception, 'transaction' => $this->transactionBacktrace]);
+ }
+ }
+ }
+
+ public function getShardDefinition(string $name): ?ShardDefinition {
+ return $this->shards[$name] ?? null;
+ }
+
+ public function getCrossShardMoveHelper(): CrossShardMoveHelper {
+ return new CrossShardMoveHelper($this->shardConnectionManager);
+ }
}