summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2024-04-17 14:26:43 +0200
committerRobin Appelman <robin@icewind.nl>2024-04-22 13:11:50 +0200
commit2530337d36ad9c9ffe35bd0878dd19a01095b723 (patch)
tree4ac87da767cf34c67e47c32762bc5d45ce4df4d2 /lib/private
parent277052a605295f3925e9d504301bd7d23fb0c2a5 (diff)
downloadnextcloud-server-2530337d36ad9c9ffe35bd0878dd19a01095b723.tar.gz
nextcloud-server-2530337d36ad9c9ffe35bd0878dd19a01095b723.zip
feat: add request id as comment to all queries
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/DB/Connection.php29
1 files changed, 21 insertions, 8 deletions
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php
index 85c6a72dfdb..6b19863b56a 100644
--- a/lib/private/DB/Connection.php
+++ b/lib/private/DB/Connection.php
@@ -54,6 +54,7 @@ use OCP\PreConditionNotMetException;
use OCP\Profiler\IProfiler;
use OC\DB\QueryBuilder\QueryBuilder;
use OC\SystemConfig;
+use OCP\Server;
use Psr\Log\LoggerInterface;
class Connection extends \Doctrine\DBAL\Connection {
@@ -79,6 +80,9 @@ class Connection extends \Doctrine\DBAL\Connection {
/** @var DbDataCollector|null */
protected $dbDataCollector = null;
+ protected bool $logRequestId;
+ protected string $requestId;
+
/**
* Initializes a new instance of the Connection class.
*
@@ -106,6 +110,9 @@ class Connection extends \Doctrine\DBAL\Connection {
$this->systemConfig = \OC::$server->getSystemConfig();
$this->logger = \OC::$server->get(LoggerInterface::class);
+ $this->logRequestId = $this->systemConfig->getValue('db.log_request_id', false);
+ $this->requestId = Server::get(IRequestId::class)->getId();
+
/** @var \OCP\Profiler\IProfiler */
$profiler = \OC::$server->get(IProfiler::class);
if ($profiler->isEnabled()) {
@@ -233,8 +240,7 @@ class Connection extends \Doctrine\DBAL\Connection {
$platform = $this->getDatabasePlatform();
$statement = $platform->modifyLimitQuery($statement, $limit, $offset);
}
- $statement = $this->replaceTablePrefix($statement);
- $statement = $this->adapter->fixupStatement($statement);
+ $statement = $this->finishQuery($statement);
return parent::prepare($statement);
}
@@ -255,8 +261,7 @@ class Connection extends \Doctrine\DBAL\Connection {
* @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);
+ $sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return parent::executeQuery($sql, $params, $types, $qcp);
@@ -266,8 +271,7 @@ class Connection extends \Doctrine\DBAL\Connection {
* @throws Exception
*/
public function executeUpdate(string $sql, array $params = [], array $types = []): int {
- $sql = $this->replaceTablePrefix($sql);
- $sql = $this->adapter->fixupStatement($sql);
+ $sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return parent::executeUpdate($sql, $params, $types);
@@ -288,8 +292,7 @@ class Connection extends \Doctrine\DBAL\Connection {
* @throws \Doctrine\DBAL\Exception
*/
public function executeStatement($sql, array $params = [], array $types = []): int {
- $sql = $this->replaceTablePrefix($sql);
- $sql = $this->adapter->fixupStatement($sql);
+ $sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return (int)parent::executeStatement($sql, $params, $types);
@@ -516,6 +519,16 @@ class Connection extends \Doctrine\DBAL\Connection {
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