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

[10.x] Add getRawQueryLog() method #47623

Merged
merged 8 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,22 @@ public function getQueryLog()
return $this->queryLog;
}

/**
* Get the connection query log with embedded bindings.
*
* @return array
*/
public function getRawQueryLog()
{
return array_map(fn (array $log) => [
'raw_query' => $this->queryGrammar->substituteBindingsIntoRawSql(
$log['query'],
$this->prepareBindings($log['bindings'])
),
'time' => $log['time'],
], $this->getQueryLog());
}

/**
* Clear the query log.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
* @method static void unsetTransactionManager()
* @method static bool pretending()
* @method static array getQueryLog()
* @method static array getRawQueryLog()
* @method static void flushQueryLog()
* @method static void enableQueryLog()
* @method static void disableQueryLog()
Expand Down
26 changes: 26 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,32 @@ public function testSchemaBuilderCanBeCreated()
$this->assertSame($connection, $schema->getConnection());
}

public function testGetRawQueryLog()
{
$mock = $this->getMockConnection(['getQueryLog']);
$mock->expects($this->once())->method('getQueryLog')->willReturn([
[
'query' => 'select * from tbl where col = ?',
'bindings' => [
0 => 'foo',
],
'time' => 1.23,
]
]);

$queryGrammar = $this->createMock(Grammar::class);
$queryGrammar->expects($this->once())
->method('substituteBindingsIntoRawSql')
->with('select * from tbl where col = ?', ['foo'])
->willReturn("select * from tbl where col = 'foo'");
$mock->setQueryGrammar($queryGrammar);

$log = $mock->getRawQueryLog();

$this->assertEquals("select * from tbl where col = 'foo'", $log[0]['raw_query']);
$this->assertEquals(1.23, $log[0]['time']);
}

protected function getMockConnection($methods = [], $pdo = null)
{
$pdo = $pdo ?: new DatabaseConnectionTestMockPDO;
Expand Down