Skip to content

Commit ce4b23b

Browse files
committed
consider dialect for format query
1 parent 601166e commit ce4b23b

File tree

2 files changed

+34
-8
lines changed
  • apps/web/src/components/v2Editor/customBlocks/sql
  • packages/editor/src/blocks

2 files changed

+34
-8
lines changed

apps/web/src/components/v2Editor/customBlocks/sql/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,16 @@ function SQLBlock(props: Props) {
180180
(props.isEditable
181181
? isCodeHiddenProp
182182
: localCodeHidden === null
183-
? isCodeHiddenProp
184-
: localCodeHidden)
183+
? isCodeHiddenProp
184+
: localCodeHidden)
185185

186186
const isResultHidden =
187187
(!props.dashboardMode || !dashboardModeHasControls(props.dashboardMode)) &&
188188
(props.isEditable
189189
? isResultHiddenProp
190190
: localResultHidden === null
191-
? isResultHiddenProp
192-
: localResultHidden)
191+
? isResultHiddenProp
192+
: localResultHidden)
193193

194194
const { startedAt: environmentStartedAt } = useEnvironmentStatus(
195195
props.document.workspaceId
@@ -443,7 +443,10 @@ function SQLBlock(props: Props) {
443443
}, [router, props.document.workspaceId])
444444

445445
const onToggleFormatSQLCode = useCallback(() => {
446-
const sqlCodeFormatted = getSQLCodeFormatted(source)
446+
const sqlCodeFormatted = getSQLCodeFormatted(
447+
source,
448+
dataSource?.config.type ?? null
449+
)
447450

448451
if (!sqlCodeFormatted) {
449452
return

packages/editor/src/blocks/sql.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type DataSourceType } from '@briefer/database'
12
import * as Y from 'yjs'
23
import {
34
RunQueryResult,
@@ -15,7 +16,7 @@ import {
1516
} from './index.js'
1617
import { ResultStatus, updateYText } from '../index.js'
1718
import { clone } from 'ramda'
18-
import { format as formatSQL } from 'sql-formatter'
19+
import { format as formatSQL, SqlLanguage } from 'sql-formatter'
1920

2021
export type DataframeName = {
2122
value: string
@@ -354,13 +355,35 @@ export function getSQLBlockErrorMessage(
354355
}
355356
}
356357

357-
export function getSQLCodeFormatted(source: Y.Text) {
358+
export function getSQLCodeFormatted(
359+
source: Y.Text,
360+
dialect: DataSourceType | null
361+
) {
358362
if (!source.length) {
359363
return null
360364
}
361365

366+
const language: SqlLanguage = (() => {
367+
switch (dialect) {
368+
case 'psql':
369+
return 'postgresql'
370+
case 'bigquery':
371+
case 'mysql':
372+
case 'snowflake':
373+
case 'trino':
374+
case 'redshift':
375+
return dialect
376+
case 'athena':
377+
case 'oracle':
378+
case 'sqlserver':
379+
case 'databrickssql':
380+
case null:
381+
return 'sql'
382+
}
383+
})()
384+
362385
const formatted = formatSQL(source.toString(), {
363-
language: 'sql',
386+
language,
364387
tabWidth: 4,
365388
})
366389

0 commit comments

Comments
 (0)