Skip to content

Commit 80319cf

Browse files
fuwasegumpywExeQuetaylorotwell
authored
[10.x] Add getRawQueryLog() method (#47623)
* feature: add getRawQueryLog method * feature: add @metohd annotation * feature: add test * fix typo * feat use prepareBindings * Update src/Illuminate/Support/Facades/DB.php Co-authored-by: mpyw <[email protected]> * Update src/Illuminate/Support/Facades/DB.php Co-authored-by: Morten Harders <[email protected]> * formatting --------- Co-authored-by: mpyw <[email protected]> Co-authored-by: Morten Harders <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 71c5e0b commit 80319cf

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,22 @@ public function getQueryLog()
15481548
return $this->queryLog;
15491549
}
15501550

1551+
/**
1552+
* Get the connection query log with embedded bindings.
1553+
*
1554+
* @return array
1555+
*/
1556+
public function getRawQueryLog()
1557+
{
1558+
return array_map(fn (array $log) => [
1559+
'raw_query' => $this->queryGrammar->substituteBindingsIntoRawSql(
1560+
$log['query'],
1561+
$this->prepareBindings($log['bindings'])
1562+
),
1563+
'time' => $log['time'],
1564+
], $this->getQueryLog());
1565+
}
1566+
15511567
/**
15521568
* Clear the query log.
15531569
*

src/Illuminate/Support/Facades/DB.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
* @method static void unsetTransactionManager()
8888
* @method static bool pretending()
8989
* @method static array getQueryLog()
90+
* @method static array getRawQueryLog()
9091
* @method static void flushQueryLog()
9192
* @method static void enableQueryLog()
9293
* @method static void disableQueryLog()

tests/Database/DatabaseConnectionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,32 @@ public function testSchemaBuilderCanBeCreated()
502502
$this->assertSame($connection, $schema->getConnection());
503503
}
504504

505+
public function testGetRawQueryLog()
506+
{
507+
$mock = $this->getMockConnection(['getQueryLog']);
508+
$mock->expects($this->once())->method('getQueryLog')->willReturn([
509+
[
510+
'query' => 'select * from tbl where col = ?',
511+
'bindings' => [
512+
0 => 'foo',
513+
],
514+
'time' => 1.23,
515+
]
516+
]);
517+
518+
$queryGrammar = $this->createMock(Grammar::class);
519+
$queryGrammar->expects($this->once())
520+
->method('substituteBindingsIntoRawSql')
521+
->with('select * from tbl where col = ?', ['foo'])
522+
->willReturn("select * from tbl where col = 'foo'");
523+
$mock->setQueryGrammar($queryGrammar);
524+
525+
$log = $mock->getRawQueryLog();
526+
527+
$this->assertEquals("select * from tbl where col = 'foo'", $log[0]['raw_query']);
528+
$this->assertEquals(1.23, $log[0]['time']);
529+
}
530+
505531
protected function getMockConnection($methods = [], $pdo = null)
506532
{
507533
$pdo = $pdo ?: new DatabaseConnectionTestMockPDO;

0 commit comments

Comments
 (0)