Skip to content

Commit d375f78

Browse files
[5.x] Include driver in Query content to enable proper formatting (#1620)
* return driver * handle postgres * Update preview.vue --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0b9e168 commit d375f78

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

resources/js/screens/queries/preview.vue

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script type="text/ecmascript-6">
22
import hljs from 'highlight.js/lib/core';
33
import sql from 'highlight.js/lib/languages/sql';
4-
import { format } from 'sql-formatter';
4+
import { format, supportedDialects } from 'sql-formatter';
55
66
hljs.registerLanguage('sql', sql);
77
@@ -12,8 +12,20 @@ export default {
1212
hljs.highlightElement(this.$refs.sqlcode);
1313
});
1414
},
15-
formatSql(sql) {
16-
return format(sql);
15+
formatSql(sql, driver) {
16+
let formatterConfig = {};
17+
18+
if (driver) {
19+
if (driver === 'pgsql') {
20+
driver = 'postgresql';
21+
}
22+
23+
if (supportedDialects.includes(driver)) {
24+
formatterConfig = {language: driver};
25+
}
26+
}
27+
28+
return format(sql, formatterConfig);
1729
}
1830
}
1931
}
@@ -54,8 +66,8 @@ export default {
5466
</li>
5567
</ul>
5668
<div class="code-bg p-4 mb-0 text-white">
57-
<copy-clipboard :data="formatSql(slotProps.entry.content.sql)">
58-
<pre class="code-bg text-white" ref="sqlcode">{{ formatSql(slotProps.entry.content.sql) }}</pre>
69+
<copy-clipboard :data="formatSql(slotProps.entry.content.sql, slotProps.entry.content.driver)">
70+
<pre class="code-bg text-white" ref="sqlcode">{{ formatSql(slotProps.entry.content.sql, slotProps.entry.content.driver) }}</pre>
5971
</copy-clipboard>
6072
</div>
6173
</div>

src/Watchers/QueryWatcher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function recordQuery(QueryExecuted $event)
3838
if ($caller = $this->getCallerFromStackTrace()) {
3939
Telescope::recordQuery(IncomingEntry::make([
4040
'connection' => $event->connectionName,
41+
'driver' => $event->connection->getDriverName(),
4142
'bindings' => [],
4243
'sql' => $this->replaceBindings($event),
4344
'time' => number_format($time, 2, '.', ''),

tests/Watchers/QueryWatcherTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function test_query_watcher_registers_database_queries()
3434
$this->assertSame(EntryType::QUERY, $entry->type);
3535
$this->assertSame('select count(*) as aggregate from "telescope_entries"', $entry->content['sql']);
3636
$this->assertSame('testbench', $entry->content['connection']);
37+
$this->assertSame('sqlite', $entry->content['driver']);
3738
$this->assertFalse($entry->content['slow']);
3839
}
3940

0 commit comments

Comments
 (0)