Skip to content

Commit 3beb650

Browse files
committed
Refactored from DB::getQueryLog() to DB::getRawQueryLog()
1 parent a164bda commit 3beb650

12 files changed

+61
-399
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# CHANGELOG
22

3-
## [1.0.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.0.1...main)
3+
## [1.1.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.1.0...main)
44

55
- ...
66

7+
## [1.1.0 (2023-07-14)](https://github.com/onlime/laravel-sql-reporter/compare/v1.0.1...v1.1.0)
8+
9+
- Drop Laravel 9 support, require Laravel v10.15 or higher for the new `DB::getRawQueryLog()` support.
10+
- PHP code style fixes by `laravel/pint` v1.10, now using more strict style rules (`laravel` preset).
11+
- Refactored whole codebase from `DB::getQueryLog()` to use the new `DB::getRawQueryLog()` method, so `ReplacesBindings` is no longer needed.
12+
713
## [1.0.1 (2023-02-26)](https://github.com/onlime/laravel-sql-reporter/compare/v1.0.0...v1.0.1)
814

915
- Allow bindings to be null.

src/Concerns/ReplacesBindings.php

Lines changed: 0 additions & 125 deletions
This file was deleted.

src/Config.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
class Config
88
{
9-
/**
10-
* Config constructor.
11-
*/
129
public function __construct(
1310
protected ConfigRepository $repository
1411
) {

src/FileName.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
class FileName
99
{
10-
/**
11-
* FileName constructor.
12-
*/
1310
public function __construct(
1411
private Container $app,
1512
private Config $config

src/Formatter.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@
99
use Illuminate\Support\Facades\DB;
1010
use Illuminate\Support\Facades\Request;
1111
use Illuminate\Support\Str;
12-
use Onlime\LaravelSqlReporter\Concerns\ReplacesBindings;
1312

1413
class Formatter
1514
{
16-
use ReplacesBindings;
17-
18-
/**
19-
* Formatter constructor.
20-
*/
2115
public function __construct(
2216
private Container $app,
2317
private Config $config
@@ -50,7 +44,7 @@ public function getHeader(): string
5044
return '';
5145
}
5246

53-
$queryLog = DB::getQueryLog();
47+
$queryLog = DB::getRawQueryLog();
5448
$times = Arr::pluck($queryLog, 'time');
5549
$totalTime = $this->time(array_sum($times));
5650
$ip = Request::ip();
@@ -110,7 +104,7 @@ protected function originLine(): string
110104
*/
111105
protected function getQueryLine(SqlQuery $query): string
112106
{
113-
return $query->get().';';
107+
return $query->rawQuery().';';
114108
}
115109

116110
/**

src/SqlLogger.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ public function __construct(
2222
/**
2323
* Log queries
2424
*/
25-
public function log()
25+
public function log(): void
2626
{
27-
foreach (DB::getQueryLog() as $query) {
28-
$sqlQuery = new SqlQuery(++$this->queryNumber, $query['query'], $query['bindings'], $query['time']);
29-
$this->writer->writeQuery($sqlQuery);
27+
foreach (DB::getRawQueryLog() as $query) {
28+
$this->writer->writeQuery(
29+
new SqlQuery(++$this->queryNumber, $query['raw_query'], $query['time'])
30+
);
3031
}
3132
}
3233
}

src/SqlQuery.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
namespace Onlime\LaravelSqlReporter;
44

5-
use Onlime\LaravelSqlReporter\Concerns\ReplacesBindings;
6-
75
class SqlQuery
86
{
9-
use ReplacesBindings;
10-
117
public function __construct(
128
private int $number,
13-
private string $sql,
14-
private ?array $bindings,
9+
private string $rawQuery,
1510
private float $time
1611
) {
1712
}
@@ -25,19 +20,11 @@ public function number(): int
2520
}
2621

2722
/**
28-
* Get raw SQL (without bindings).
29-
*/
30-
public function raw(): string
31-
{
32-
return $this->sql;
33-
}
34-
35-
/**
36-
* Get bindings.
23+
* Get raw SQL query with embedded bindings.
3724
*/
38-
public function bindings(): array
25+
public function rawQuery(): string
3926
{
40-
return $this->bindings ?? [];
27+
return $this->rawQuery;
4128
}
4229

4330
/**
@@ -47,12 +34,4 @@ public function time(): float
4734
{
4835
return $this->time;
4936
}
50-
51-
/**
52-
* Get full query with values from bindings inserted.
53-
*/
54-
public function get(): string
55-
{
56-
return $this->replaceBindings($this->sql, $this->bindings());
57-
}
5837
}

src/Writer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ protected function shouldLogQuery(SqlQuery $query): bool
7171
{
7272
return $this->config->queriesEnabled() &&
7373
$query->time() >= $this->config->queriesMinExecTime() &&
74-
preg_match($this->config->queriesIncludePattern(), $query->raw()) &&
75-
! preg_match($this->config->queriesExcludePattern(), $query->raw());
74+
preg_match($this->config->queriesIncludePattern(), $query->rawQuery()) &&
75+
! preg_match($this->config->queriesExcludePattern(), $query->rawQuery());
7676
}
7777

7878
/**

tests/Unit/FormatterTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function it_formats_header_in_valid_way_when_running_via_http()
3333
$now = '2015-03-04 08:12:07';
3434
Carbon::setTestNow($now);
3535

36-
DB::shouldReceive('getQueryLog')->once()->withNoArgs()->andReturn([
37-
['query' => 'foo', 'bindings' => [], 'time' => 1.23],
38-
['query' => 'bar', 'bindings' => [], 'time' => 4.56],
36+
DB::shouldReceive('getRawQueryLog')->once()->withNoArgs()->andReturn([
37+
['raw_query' => 'foo', 'time' => 1.23],
38+
['raw_query' => 'bar', 'time' => 4.56],
3939
]);
4040
Auth::shouldReceive('user')->once()->withNoArgs()->andReturn(null);
4141
\Illuminate\Support\Facades\Request::shouldReceive('ip')->once()->withNoArgs()->andReturn('127.0.0.1');
@@ -79,7 +79,7 @@ public function it_formats_header_in_valid_way_when_running_in_console()
7979
$now = '2015-03-04 08:12:07';
8080
Carbon::setTestNow($now);
8181

82-
DB::shouldReceive('getQueryLog')->once()->withNoArgs()->andReturn([]);
82+
DB::shouldReceive('getRawQueryLog')->once()->withNoArgs()->andReturn([]);
8383
Auth::shouldReceive('user')->once()->withNoArgs()->andReturn(null);
8484
\Illuminate\Support\Facades\Request::shouldReceive('ip')->once()->withNoArgs()->andReturn('127.0.0.1');
8585
\Illuminate\Support\Facades\Request::shouldReceive('userAgent')->once()->withNoArgs()->andReturn('Mozilla/5.0');
@@ -122,7 +122,7 @@ public function it_formats_line_in_valid_way_when_milliseconds_are_used()
122122
$time = 617.24;
123123
$sql = 'SELECT * FROM somewhere';
124124
$query->shouldReceive('number')->once()->withNoArgs()->andReturn($number);
125-
$query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql);
125+
$query->shouldReceive('rawQuery')->once()->withNoArgs()->andReturn($sql);
126126
$query->shouldReceive('time')->once()->withNoArgs()->andReturn($time);
127127

128128
$formatter = new Formatter($app, $config);
@@ -155,7 +155,7 @@ public function it_formats_line_in_valid_way_when_custom_entry_format_was_used()
155155
$time = 617.24;
156156
$sql = 'SELECT * FROM somewhere';
157157
$query->shouldReceive('number')->once()->withNoArgs()->andReturn($number);
158-
$query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql);
158+
$query->shouldReceive('rawQuery')->once()->withNoArgs()->andReturn($sql);
159159
$query->shouldReceive('time')->once()->withNoArgs()->andReturn($time);
160160

161161
$formatter = new Formatter($app, $config);
@@ -189,7 +189,7 @@ public function it_formats_line_in_valid_way_when_seconds_are_used()
189189
$time = 617.24;
190190
$sql = 'SELECT * FROM somewhere';
191191
$query->shouldReceive('number')->once()->withNoArgs()->andReturn($number);
192-
$query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql);
192+
$query->shouldReceive('rawQuery')->once()->withNoArgs()->andReturn($sql);
193193
$query->shouldReceive('time')->once()->withNoArgs()->andReturn($time);
194194

195195
$formatter = new Formatter($app, $config);

tests/Unit/SqlLoggerTest.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ protected function setUp(): void
2929
/** @test */
3030
public function it_runs_writer_with_valid_query()
3131
{
32-
DB::shouldReceive('getQueryLog')->once()->withNoArgs()->andReturn([
33-
['query' => 'anything', 'bindings' => [], 'time' => 1.23],
32+
DB::shouldReceive('getRawQueryLog')->once()->withNoArgs()->andReturn([
33+
['raw_query' => 'anything', 'time' => 1.23],
3434
]);
3535

36-
$sqlQuery = new SqlQuery(1, 'anything', [], 1.23);
37-
// $this->writer->shouldReceive('writeQuery')->once()->with($sqlQuery)
38-
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(function ($arg) use ($sqlQuery) {
39-
return $sqlQuery == $arg;
40-
}));
36+
$sqlQuery = new SqlQuery(1, 'anything', 1.23);
37+
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery == $arg));
4138

4239
$this->logger->log();
4340
$this->assertTrue(true);
@@ -46,22 +43,16 @@ public function it_runs_writer_with_valid_query()
4643
/** @test */
4744
public function it_uses_valid_query_number_for_multiple_queries()
4845
{
49-
DB::shouldReceive('getQueryLog')->once()->withNoArgs()->andReturn([
50-
['query' => 'anything', 'bindings' => ['one', 1], 'time' => 1.23],
51-
['query' => 'anything2', 'bindings' => ['two', 2], 'time' => 4.56],
46+
DB::shouldReceive('getRawQueryLog')->once()->withNoArgs()->andReturn([
47+
['raw_query' => 'anything', 'time' => 1.23],
48+
['raw_query' => 'anything2', 'time' => 4.56],
5249
]);
5350

54-
$sqlQuery = new SqlQuery(1, 'anything', ['one', 1], 1.23);
55-
// $this->writer->shouldReceive('writeQuery')->once()->with($sqlQuery);
56-
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(function ($arg) use ($sqlQuery) {
57-
return $sqlQuery == $arg;
58-
}));
51+
$sqlQuery = new SqlQuery(1, 'anything', 1.23);
52+
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery == $arg));
5953

60-
$sqlQuery2 = new SqlQuery(2, 'anything2', ['two', 2], 4.56);
61-
// $this->writer->shouldReceive('writeQuery')->once()->with($sqlQuery2);
62-
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(function ($arg) use ($sqlQuery2) {
63-
return $sqlQuery2 == $arg;
64-
}));
54+
$sqlQuery2 = new SqlQuery(2, 'anything2', 4.56);
55+
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery2 == $arg));
6556

6657
$this->logger->log();
6758
$this->assertTrue(true);

0 commit comments

Comments
 (0)