From b4f09e71f85e2c1de4666c188d2084a54443b086 Mon Sep 17 00:00:00 2001 From: Piotr Olaszewski Date: Thu, 10 Oct 2024 15:16:14 +0200 Subject: [PATCH] Add an option to truncate bound values (#323) --- src/Ouzo/Core/Db/StatementExecutor.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Ouzo/Core/Db/StatementExecutor.php b/src/Ouzo/Core/Db/StatementExecutor.php index 15d9d8ab..e8af016e 100644 --- a/src/Ouzo/Core/Db/StatementExecutor.php +++ b/src/Ouzo/Core/Db/StatementExecutor.php @@ -7,6 +7,7 @@ namespace Ouzo\Db; use Closure; +use Ouzo\Config; use Ouzo\Logger\Backtrace; use Ouzo\Logger\Logger; use Ouzo\Utilities\Objects; @@ -76,10 +77,24 @@ public function fetchIterator(array $options = []): StatementIterator public function createPdoStatement(array $options = []): PDOStatement { - $sqlString = sprintf("%s with params: %s", $this->humanizedSql, Objects::toString($this->boundValues)); + $sqlString = $this->prepareSqlString(); + $callingClass = Backtrace::getCallingClass(); Logger::getLogger(__CLASS__)->info("From: %s Query: %s", [$callingClass, $sqlString]); return $this->pdoExecutor->createPDOStatement($this->dbHandle, $this->sql, $this->boundValues, $sqlString, $options); } + + private function prepareSqlString(): string + { + $truncateLimit = Config::getValue('db', 'truncate_bound_values_string_limit'); + + $boundValuesAsString = Objects::toString($this->boundValues); + $boundValuesAsStringLength = mb_strlen($boundValuesAsString); + if (!is_null($truncateLimit) && $boundValuesAsStringLength > $truncateLimit) { + $boundValuesAsString = trim(mb_substr($boundValuesAsString, 0, $truncateLimit)) . "...\"] (truncated from {$boundValuesAsStringLength})"; + } + + return sprintf("%s with params: %s", $this->humanizedSql, $boundValuesAsString); + } }