Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Adds a new method "getRawSql" (with embedded bindings) to the QueryException class #54849

Merged
merged 7 commits into from
Feb 28, 2025
11 changes: 11 additions & 0 deletions src/Illuminate/Database/QueryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use PDOException;
use Throwable;
Expand Down Expand Up @@ -87,6 +88,16 @@ public function getSql()
return $this->sql;
}

/**
* Get the raw SQL representation of the query with embedded bindings.
*/
public function getRawSql(): string
{
return DB::connection($this->getConnectionName())
->getQueryGrammar()
->substituteBindingsIntoRawSql($this->getSql(), $this->getBindings());
}

/**
* Get the bindings for the query.
*
Expand Down
64 changes: 64 additions & 0 deletions tests/Database/DatabaseQueryExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\Connection;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Mockery as m;
use PDOException;
use PHPUnit\Framework\TestCase;

class DatabaseQueryExceptionTest extends TestCase
{
public function testIfItEmbedsBindingsIntoSql()
{
$connection = $this->getConnection();

$sql = 'SELECT * FROM huehue WHERE a = ? and hue = ?';
$bindings = [1, 'br'];

$expectedSql = "SELECT * FROM huehue WHERE a = 1 and hue = 'br'";

$pdoException = new PDOException('Mock SQL error');
$exception = new QueryException($connection->getName(), $sql, $bindings, $pdoException);

DB::shouldReceive('connection')->andReturn($connection);
$result = $exception->getRawSql();

$this->assertSame($expectedSql, $result);
}

public function testIfItReturnsSameSqlWhenThereAreNoBindings()
{
$connection = $this->getConnection();

$sql = "SELECT * FROM huehue WHERE a = 1 and hue = 'br'";
$bindings = [];

$expectedSql = $sql;

$pdoException = new PDOException('Mock SQL error');
$exception = new QueryException($connection->getName(), $sql, $bindings, $pdoException);

DB::shouldReceive('connection')->andReturn($connection);
$result = $exception->getRawSql();

$this->assertSame($expectedSql, $result);
}

protected function getConnection()
{
$connection = m::mock(Connection::class);

$grammar = new Grammar($connection);

$connection->shouldReceive('getName')->andReturn('default');
$connection->shouldReceive('getQueryGrammar')->andReturn($grammar);
$connection->shouldReceive('escape')->with(1, false)->andReturn(1);
$connection->shouldReceive('escape')->with('br', false)->andReturn("'br'");

return $connection;
}
}
Loading