Skip to content

Commit

Permalink
Merge pull request #91 from nextras/logger
Browse files Browse the repository at this point in the history
Logger
  • Loading branch information
hrach committed Apr 19, 2020
2 parents 7545696 + ee118ea commit f2091dd
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 169 deletions.
3 changes: 2 additions & 1 deletion src/Bridges/NetteTracy/BluescreenQueryPanel.php
Expand Up @@ -9,6 +9,7 @@
namespace Nextras\Dbal\Bridges\NetteTracy;

use Nextras\Dbal\QueryException;
use Nextras\Dbal\Utils\SqlHighlighter;


class BluescreenQueryPanel
Expand All @@ -24,7 +25,7 @@ public static function renderBluescreenPanel(?\Throwable $exception): ?array

return [
'tab' => 'SQL',
'panel' => '<pre class="sql">' . ConnectionPanel::highlight($query) . "</pre>",
'panel' => '<pre class="sql">' . SqlHighlighter::highlight($query) . "</pre>",
];
}
}
61 changes: 26 additions & 35 deletions src/Bridges/NetteTracy/ConnectionPanel.php
Expand Up @@ -8,15 +8,17 @@

namespace Nextras\Dbal\Bridges\NetteTracy;

use Nextras\Dbal\Connection;
use Nextras\Dbal\DriverException;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Result\Result;
use Nextras\Dbal\Utils\SqlHighlighter;
use Tracy\Debugger;
use Tracy\IBarPanel;


class ConnectionPanel implements IBarPanel
class ConnectionPanel implements IBarPanel, ILogger
{
/** @var int */
private $maxQueries = 100;
Expand All @@ -29,44 +31,54 @@ class ConnectionPanel implements IBarPanel

/**
* @var array
* @phpstan-var array<array{Connection, string, float, ?int}>
* @phpstan-var array<array{IConnection, string, float, ?int}>
*/
private $queries = [];

/** @var Connection */
/** @var IConnection */
private $connection;

/** @var bool */
private $doExplain;


public static function install(Connection $connection, bool $doExplain = true): void
public static function install(IConnection $connection, bool $doExplain = true): void
{
$doExplain = $doExplain && $connection->getPlatform()->isSupported(IPlatform::SUPPORT_QUERY_EXPLAIN);
Debugger::getBar()->addPanel(new ConnectionPanel($connection, $doExplain));
}


public function __construct(Connection $connection, bool $doExplain)
public function __construct(IConnection $connection, bool $doExplain)
{
$connection->onQuery[] = [$this, 'logQuery'];
$connection->addLogger($this);
$this->connection = $connection;
$this->doExplain = $doExplain;
}


public function logQuery(Connection $connection, string $sql, float $elapsedTime, Result $result = null, DriverException $exception = null): void
public function onConnect(): void
{
}


public function onDisconnect(): void
{
}


public function onQuery(string $sqlQuery, float $timeTaken, ?Result $result, ?DriverException $exception): void
{
$this->count++;
if ($this->count > $this->maxQueries) {
return;
}

$this->totalTime += $elapsedTime;
$this->totalTime += $timeTaken;
$this->queries[] = [
$connection,
$sql,
$elapsedTime,
$this->connection,
$sqlQuery,
$timeTaken,
$result ? $result->count() : null,
];
}
Expand Down Expand Up @@ -98,34 +110,13 @@ public function getPanel(): ?string
$row[3] = null; // rows count is also irrelevant
}

$row[1] = self::highlight($row[1]);
$row[1] = SqlHighlighter::highlight($row[1]);
return $row;
}, $queries);
$whitespaceExplain = $this->connection->getPlatform()->getName() === 'pgsql';
$whitespaceExplain = $this->connection->getPlatform()->isSupported(IPlatform::SUPPORT_WHITESPACE_EXPLAIN);

ob_start();
require __DIR__ . '/ConnectionPanel.panel.phtml';
return (string) ob_get_clean();
}


public static function highlight(string $sql): string
{
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|SHOW|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|COMMIT|ROLLBACK|(?:RELEASE\s+|ROLLBACK\s+TO\s+)?SAVEPOINT';
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|[RI]?LIKE|REGEXP|TRUE|FALSE';

$sql = " $sql ";
$sql = htmlspecialchars($sql, ENT_IGNORE, 'UTF-8');
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", function ($matches) {
if (!empty($matches[1])) { // comment
return '<em style="color:gray">' . $matches[1] . '</em>';
} elseif (!empty($matches[2])) { // most important keywords
return '<strong style="color:#2D44AD">' . $matches[2] . '</strong>';
} elseif (!empty($matches[3])) { // other keywords
return '<strong>' . $matches[3] . '</strong>';
}
}, $sql);
assert($sql !== null);
return trim($sql);
}
}

0 comments on commit f2091dd

Please sign in to comment.