Skip to content

Commit

Permalink
Add method QueryExecuted::toRawSql() (#52192)
Browse files Browse the repository at this point in the history
* 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 <taylor@laravel.com>
  • Loading branch information
spawnia and taylorotwell authored Jul 22, 2024
1 parent 7363052 commit a13b726
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Events/QueryExecuted.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@ public function __construct($sql, $bindings, $time, $connection)
$this->connection = $connection;
$this->connectionName = $connection->getName();
}

/**
* Get the raw SQL representation of the query with embedded bindings.
*
* @return string
*/
public function toRawSql()
{
return $this->connection
->query()
->getGrammar()
->substituteBindingsIntoRawSql($this->sql, $this->connection->prepareBindings($this->bindings));
}
}
38 changes: 38 additions & 0 deletions tests/Database/DatabaseIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Events\Dispatcher;
use PHPUnit\Framework\TestCase;

class DatabaseIntegrationTest extends TestCase
{
protected function setUp(): void
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->setAsGlobal();
$db->setEventDispatcher(new Dispatcher);
}

public function testQueryExecutedToRawSql(): void
{
$connection = DB::connection();

$connection->listen(function (QueryExecuted $query) use (&$queryExecuted): void {
$queryExecuted = $query;
});

$connection->select('select ?', [true]);

$this->assertInstanceOf(QueryExecuted::class, $queryExecuted);
$this->assertSame('select ?', $queryExecuted->sql);
$this->assertSame([true], $queryExecuted->bindings);
$this->assertSame('select 1', $queryExecuted->toRawSql());
}
}

0 comments on commit a13b726

Please sign in to comment.