From ad6577dc3a99dbd2d3170f6eaf862069434822c1 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Mon, 27 Nov 2023 16:58:57 +0700 Subject: [PATCH] Do not call `CommandInterface::getRawSql()` if no `logger` or `profiler` (#781) Co-authored-by: Sergei Predvoditelev --- CHANGELOG.md | 1 + src/Driver/Pdo/AbstractPdoCommand.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91c1c94bd..47d810a74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ `DbArrayHelper::populate()` methods to `array[]` (@vjik) - Enh #779: Specify populate closure type in `BatchQueryResultInterface` (@vjik) - Enh #778: Deprecate unnecessary argument `$rawSql` of `AbstractCommand::internalExecute()` (@Tigrov) +- Enh #781: Skip calling `CommandInterface::getRawSql()` if no `logger` or `profiler` is set (@Tigrov) - Enh #785: Refactor `AbstractCommand::getRawSql()` (@Tigrov) - Bug #785: Fix bug of `AbstractCommand::getRawSql()` when a param value is `Stringable` object (@Tigrov) - Enh #786: Refactor `AbstractSchema::getDataType()` (@Tigrov) diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index bf3ae8252..4285e2148 100644 --- a/src/Driver/Pdo/AbstractPdoCommand.php +++ b/src/Driver/Pdo/AbstractPdoCommand.php @@ -265,14 +265,21 @@ protected function logQuery(string $rawSql, string $category): void protected function queryInternal(int $queryMode): mixed { - $rawSql = $this->getRawSql(); $logCategory = self::class . '::' . $this->getQueryMode($queryMode); - $this->logQuery($rawSql, $logCategory); + if ($this->logger !== null) { + $rawSql = $this->getRawSql(); + $this->logQuery($rawSql, $logCategory); + } $queryContext = new CommandContext(__METHOD__, $logCategory, $this->getSql(), $this->getParams()); - $this->profiler?->begin($rawSql, $queryContext); + /** + * @psalm-var string $rawSql + * @psalm-suppress RedundantConditionGivenDocblockType + * @psalm-suppress DocblockTypeContradiction + */ + $this->profiler?->begin($rawSql ??= $this->getRawSql(), $queryContext); try { /** @psalm-var mixed $result */ $result = parent::queryInternal($queryMode);