Skip to content

Commit

Permalink
Add an option to truncate bound values (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrooo authored Oct 10, 2024
1 parent e89b351 commit b4f09e7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Ouzo/Core/Db/StatementExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Ouzo\Db;

use Closure;
use Ouzo\Config;
use Ouzo\Logger\Backtrace;
use Ouzo\Logger\Logger;
use Ouzo\Utilities\Objects;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b4f09e7

Please sign in to comment.