Skip to content

Commit

Permalink
better sql dialect detection
Browse files Browse the repository at this point in the history
  • Loading branch information
julienbourdeau committed May 4, 2024
1 parent ebf8edc commit c40e240
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client/src/components/queries/QueryItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function copyToClipboard(text: string) {
}
function sqlFormat(query: string) {
return format(query, { language: configStore.config.activeRecord.adapter })
return format(query, { language: configStore.config.sqlDialect })
}
</script>

Expand Down
17 changes: 17 additions & 0 deletions client/src/models/Config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import defaultsDeep from "lodash/defaultsDeep"
import { SqlLanguage } from "sql-formatter"

export type DebugbarConfigOptions = {
mode: "ws" | "poll" | "off"
Expand All @@ -17,13 +18,27 @@ export type DebugbarConfigOptions = {
activeRecord: {
adapter: "mysql" | "postgresql" | "sql" | "sqlite"
}
sqlDialect: SqlLanguage
}

export type DebugbarConfig = DebugbarConfigOptions & {
actionCableUrl: string
pollUrl: string
}

function sqlDialect(arAdapter): SqlLanguage {
// We use regex to handle adapter names like "mysql2", "jdbcpostgresql", etc.
if (/mysql|triology/.test(arAdapter)) {
return "mysql"
} else if (/pg|postgres/.test(arAdapter)) {
return "postgresql"
} else if (/sqlite/.test(arAdapter)) {
return "sqlite"
} else {
return "sql"
}
}

export function newDebugbarConfig(options: DebugbarConfigOptions) {
const obj: DebugbarConfig = defaultsDeep(options, {
mode: "ws",
Expand All @@ -47,5 +62,7 @@ export function newDebugbarConfig(options: DebugbarConfigOptions) {
obj.actionCableUrl = `${obj.cable.url}${obj.prefix}/cable`
obj.pollUrl = `${obj.poll.url}${obj.prefix}/poll`

obj.sqlDialect = sqlDialect(obj.activeRecord.adapter)

return obj
}

0 comments on commit c40e240

Please sign in to comment.