Skip to content

Commit d1b79d7

Browse files
[12.x] Adds a new method "getRawSql" (with embedded bindings) to the QueryException class (#54849)
* [12.x] Get Raw SQL (with embedded bindings) from QueryException (new method) * Reordering the imports * Reverting string concatenation format on untouched method * Reverting string concatenation format on untouched method * Resolving StyleCI complaint about extra line between namespace declaration and imports * Update QueryException.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 908e33c commit d1b79d7

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/Illuminate/Database/QueryException.php

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

33
namespace Illuminate\Database;
44

5+
use Illuminate\Support\Facades\DB;
56
use Illuminate\Support\Str;
67
use PDOException;
78
use Throwable;
@@ -87,6 +88,16 @@ public function getSql()
8788
return $this->sql;
8889
}
8990

91+
/**
92+
* Get the raw SQL representation of the query with embedded bindings.
93+
*/
94+
public function getRawSql(): string
95+
{
96+
return DB::connection($this->getConnectionName())
97+
->getQueryGrammar()
98+
->substituteBindingsIntoRawSql($this->getSql(), $this->getBindings());
99+
}
100+
90101
/**
91102
* Get the bindings for the query.
92103
*
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database;
4+
5+
use Illuminate\Database\Connection;
6+
use Illuminate\Database\Query\Grammars\Grammar;
7+
use Illuminate\Database\QueryException;
8+
use Illuminate\Support\Facades\DB;
9+
use Mockery as m;
10+
use PDOException;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class DatabaseQueryExceptionTest extends TestCase
14+
{
15+
public function testIfItEmbedsBindingsIntoSql()
16+
{
17+
$connection = $this->getConnection();
18+
19+
$sql = 'SELECT * FROM huehue WHERE a = ? and hue = ?';
20+
$bindings = [1, 'br'];
21+
22+
$expectedSql = "SELECT * FROM huehue WHERE a = 1 and hue = 'br'";
23+
24+
$pdoException = new PDOException('Mock SQL error');
25+
$exception = new QueryException($connection->getName(), $sql, $bindings, $pdoException);
26+
27+
DB::shouldReceive('connection')->andReturn($connection);
28+
$result = $exception->getRawSql();
29+
30+
$this->assertSame($expectedSql, $result);
31+
}
32+
33+
public function testIfItReturnsSameSqlWhenThereAreNoBindings()
34+
{
35+
$connection = $this->getConnection();
36+
37+
$sql = "SELECT * FROM huehue WHERE a = 1 and hue = 'br'";
38+
$bindings = [];
39+
40+
$expectedSql = $sql;
41+
42+
$pdoException = new PDOException('Mock SQL error');
43+
$exception = new QueryException($connection->getName(), $sql, $bindings, $pdoException);
44+
45+
DB::shouldReceive('connection')->andReturn($connection);
46+
$result = $exception->getRawSql();
47+
48+
$this->assertSame($expectedSql, $result);
49+
}
50+
51+
protected function getConnection()
52+
{
53+
$connection = m::mock(Connection::class);
54+
55+
$grammar = new Grammar($connection);
56+
57+
$connection->shouldReceive('getName')->andReturn('default');
58+
$connection->shouldReceive('getQueryGrammar')->andReturn($grammar);
59+
$connection->shouldReceive('escape')->with(1, false)->andReturn(1);
60+
$connection->shouldReceive('escape')->with('br', false)->andReturn("'br'");
61+
62+
return $connection;
63+
}
64+
}

0 commit comments

Comments
 (0)