Skip to content

Commit 251ef7d

Browse files
committed
use QueryExecuted@toRawSql
1 parent cdbfd6f commit 251ef7d

File tree

2 files changed

+7
-90
lines changed

2 files changed

+7
-90
lines changed

src/QueryLogger/QueryLogger.php

Lines changed: 5 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
<?php
22

3-
/*
4-
* The regular expression used in the `prepareQuery()` method and
5-
* the quote emulation used in the `quote()` method where extracted
6-
* from the "barryvdh/laravel-debugbar" package available at:
7-
*
8-
* https://github.com/barryvdh/laravel-debugbar/tree/6420113d90bb746423fa70b9940e9e7c26ebc121
9-
*
10-
* "barryvdh/laravel-debugbar" is licensed under MIT. License is available at:
11-
*
12-
* https://github.com/barryvdh/laravel-debugbar/blob/6420113d90bb746423fa70b9940e9e7c26ebc121/LICENSE
13-
*/
14-
153
namespace RodrigoPedra\QueryLogger;
164

17-
use Illuminate\Contracts\Config\Repository;
5+
use Illuminate\Database\ConnectionResolverInterface;
186
use Illuminate\Database\Events\QueryExecuted;
197
use Illuminate\Support\Arr;
208
use Psr\Log\LoggerInterface;
@@ -23,91 +11,21 @@
2311
{
2412
public function __construct(
2513
private LoggerInterface $logger,
26-
private Repository $config,
14+
private ConnectionResolverInterface $db,
2715
) {}
2816

2917
public function handle(QueryExecuted $event): void
3018
{
31-
$pdo = \method_exists($event->connection, 'getPdo')
32-
? $event->connection->getPdo()
33-
: null;
34-
35-
$dateFormat = $event->connection->getQueryGrammar()->getDateFormat();
36-
37-
$bindings = $event->connection->prepareBindings($event->bindings);
38-
$bindings = \array_map(fn ($value) => $this->prepareValue($pdo, $dateFormat, $value), $bindings);
39-
40-
$query = $this->prepareQuery($event->sql, $bindings);
41-
42-
$this->logger->info($query, [
19+
$this->logger->debug($event->toRawSql(), [
4320
'time' => $event->time,
4421
'connection' => $event->connectionName,
45-
'database' => $this->config->get("database.connections.{$event->connectionName}.database"),
22+
'database' => $this->db->connection($event->connectionName)->getDatabaseName(),
4623
'bindings' => $event->bindings,
4724
'callSpot' => $this->guessCallSpot(),
4825
]);
4926
}
5027

51-
protected function prepareQuery(string $query, array $bindings): string
52-
{
53-
foreach ($bindings as $key => $value) {
54-
$regex = \is_numeric($key)
55-
? "/(?<!\?)\?(?=(?:[^'\\\']*'[^'\\']*')*[^'\\\']*$)(?!\?)/"
56-
: "/:$key(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/";
57-
58-
$query = \preg_replace($regex, $value, $query, 1);
59-
}
60-
61-
return $query;
62-
}
63-
64-
protected function prepareValue(?\PDO $pdo, string $dateFormat, $value): string
65-
{
66-
if (\is_null($value)) {
67-
return 'NULL';
68-
}
69-
70-
if (\is_bool($value)) {
71-
return $value ? '1' : '0';
72-
}
73-
74-
if (\is_int($value) || \is_float($value)) {
75-
return \strval($value);
76-
}
77-
78-
if (\is_string($value) && ! \mb_check_encoding($value, 'UTF-8')) {
79-
return $this->quote($pdo, '[BINARY DATA]');
80-
}
81-
82-
if ($value instanceof \DateTimeInterface) {
83-
$value = $value->format($dateFormat);
84-
}
85-
86-
if ($value instanceof \Stringable) {
87-
$value = \strval($value);
88-
}
89-
90-
if (\is_object($value) && \method_exists($value, 'toString')) {
91-
$value = $value->toString();
92-
}
93-
94-
// objects not implementing __toString() or toString() will fail here
95-
return $this->quote($pdo, \strval($value));
96-
}
97-
98-
protected function quote(?\PDO $pdo, string $value): string
99-
{
100-
if ($pdo) {
101-
return $pdo->quote($value);
102-
}
103-
104-
$search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"];
105-
$replace = ["\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z"];
106-
107-
return "'" . \str_replace($search, $replace, $value) . "'";
108-
}
109-
110-
protected function guessCallSpot(): array
28+
private function guessCallSpot(): array
11129
{
11230
$stack = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
11331
$vendor = \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR;

src/QueryLogger/QueryLoggerServiceProvider.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace RodrigoPedra\QueryLogger;
44

5-
use Illuminate\Contracts\Config\Repository;
65
use Illuminate\Contracts\Events\Dispatcher;
76
use Illuminate\Database\Events\QueryExecuted;
87
use Illuminate\Support\ServiceProvider;
@@ -13,9 +12,9 @@ class QueryLoggerServiceProvider extends ServiceProvider
1312
QueryLogger::class,
1413
];
1514

16-
public function boot(Repository $config, Dispatcher $events): void
15+
public function boot(Dispatcher $events): void
1716
{
18-
if ($config->get('app.debug') === true) {
17+
if ($this->app->hasDebugModeEnabled()) {
1918
$events->listen(QueryExecuted::class, QueryLogger::class);
2019
}
2120
}

0 commit comments

Comments
 (0)