Skip to content

Commit a13b726

Browse files
Add method QueryExecuted::toRawSql() (#52192)
* Add method `QueryExecuted::toRawSql()` This makes it easier to debug the executed queries when using `DB::listen()`. Before, we did something like this and got a result that was hardly readable: ```php DB::listen(function (QueryExecuted $query): void { $sql = str_replace("\n", ' ', $query->sql); $bindings = json_encode($query->bindings); file_put_contents( filename: storage_path('logs/query.log'), data: "SQL: {$sql} ||| Bindings: {$bindings} ||| Time: {$query->time}ms\n", flags: FILE_APPEND, ); }); // SQL: insert into `competence_detail_criteria` (`competence_criteria_id`, `competence_detail_id`, `valid_from`, `valid_to`, `userid`, `first_id`) values (?, ?, ?, ?, ?, ?) ||| Bindings: [3,1,"2024-07-19 10:59:02","9999-12-31 23:55:55",1,0] ||| Time: 0.84ms ``` With this added method, achieving a readable result becomes much simpler: ```php DB::listen(function (QueryExecuted $query): void { file_put_contents( filename: storage_path('logs/query.log'), data: "SQL: {$query->toRawSql()} ||| Time: {$query->time}ms\n", flags: FILE_APPEND, ); }); // SQL: insert into `competence_detail_criteria` (`competence_criteria_id`, `competence_detail_id`, `valid_from`, `valid_to`, `userid`, `first_id`) values (4, 1, '2024-07-19 11:10:29', '9999-12-31 23:55:55', 1, 0) ||| Time: 0.2ms ``` * Update QueryExecuted.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7363052 commit a13b726

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Illuminate/Database/Events/QueryExecuted.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,17 @@ public function __construct($sql, $bindings, $time, $connection)
5656
$this->connection = $connection;
5757
$this->connectionName = $connection->getName();
5858
}
59+
60+
/**
61+
* Get the raw SQL representation of the query with embedded bindings.
62+
*
63+
* @return string
64+
*/
65+
public function toRawSql()
66+
{
67+
return $this->connection
68+
->query()
69+
->getGrammar()
70+
->substituteBindingsIntoRawSql($this->sql, $this->connection->prepareBindings($this->bindings));
71+
}
5972
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database;
4+
5+
use Illuminate\Database\Capsule\Manager as DB;
6+
use Illuminate\Database\Events\QueryExecuted;
7+
use Illuminate\Events\Dispatcher;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class DatabaseIntegrationTest extends TestCase
11+
{
12+
protected function setUp(): void
13+
{
14+
$db = new DB;
15+
$db->addConnection([
16+
'driver' => 'sqlite',
17+
'database' => ':memory:',
18+
]);
19+
$db->setAsGlobal();
20+
$db->setEventDispatcher(new Dispatcher);
21+
}
22+
23+
public function testQueryExecutedToRawSql(): void
24+
{
25+
$connection = DB::connection();
26+
27+
$connection->listen(function (QueryExecuted $query) use (&$queryExecuted): void {
28+
$queryExecuted = $query;
29+
});
30+
31+
$connection->select('select ?', [true]);
32+
33+
$this->assertInstanceOf(QueryExecuted::class, $queryExecuted);
34+
$this->assertSame('select ?', $queryExecuted->sql);
35+
$this->assertSame([true], $queryExecuted->bindings);
36+
$this->assertSame('select 1', $queryExecuted->toRawSql());
37+
}
38+
}

0 commit comments

Comments
 (0)