Skip to content

Commit

Permalink
feat: change dialect based on the driver
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Sep 10, 2024
1 parent ca0bdb3 commit cc14c93
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
55 changes: 38 additions & 17 deletions src/components/gui/sql-editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import CodeMirror, {
EditorView,
Extension,
ReactCodeMirrorRef,
} from "@uiw/react-codemirror";
import { LanguageSupport } from "@codemirror/language";
import {
acceptCompletion,
completionStatus,
startCompletion,
} from "@codemirror/autocomplete";
import { sql, SQLNamespace } from "@codemirror/lang-sql";
import { sql, SQLNamespace, MySQL as MySQLDialect } from "@codemirror/lang-sql";
import { forwardRef, KeyboardEventHandler, useMemo } from "react";

import { defaultKeymap, insertTab } from "@codemirror/commands";
Expand All @@ -19,9 +21,11 @@ import { sqliteDialect } from "@/drivers/sqlite/sqlite-dialect";
import { functionTooltip } from "./function-tooltips";
import sqliteFunctionList from "@/drivers/sqlite/function-tooltip.json";
import { toast } from "sonner";
import { SupportedDialect } from "@/drivers/base-driver";

interface SqlEditorProps {
value: string;
dialect: SupportedDialect;
readOnly?: boolean;
onChange?: (value: string) => void;
schema?: SQLNamespace;
Expand All @@ -38,6 +42,7 @@ interface SqlEditorProps {
const SqlEditor = forwardRef<ReactCodeMirrorRef, SqlEditorProps>(
function SqlEditor(
{
dialect,
value,
onChange,
schema,
Expand Down Expand Up @@ -119,6 +124,37 @@ const SqlEditor = forwardRef<ReactCodeMirrorRef, SqlEditorProps>(
]);
}, [fontSize, onFontSizeChanged]);

const extensions = useMemo(() => {
let sqlDialect: LanguageSupport | undefined = undefined;
let tooltipExtension: Extension | undefined = undefined;

if (dialect === "sqlite") {
sqlDialect = sql({
dialect: sqliteDialect,
schema,
});
tooltipExtension = functionTooltip(sqliteFunctionList);
} else {
sqlDialect = sql({
dialect: MySQLDialect,
});
}

return [
keyExtensions,
sqlDialect,
tooltipExtension,
tableNameHighlightPlugin,
EditorView.updateListener.of((state) => {
const pos = state.state.selection.main.head;
const line = state.state.doc.lineAt(pos);
const lineNumber = line.number;
const columnNumber = pos - line.from;
if (onCursorChange) onCursorChange(pos, lineNumber, columnNumber);
}),
].filter(Boolean) as Extension[];
}, [dialect, onCursorChange]);

return (
<CodeMirror
ref={ref}
Expand All @@ -137,22 +173,7 @@ const SqlEditor = forwardRef<ReactCodeMirrorRef, SqlEditorProps>(
fontSize: 20,
height: "100%",
}}
extensions={[
keyExtensions,
sql({
dialect: sqliteDialect,
schema,
}),
functionTooltip(sqliteFunctionList),
tableNameHighlightPlugin,
EditorView.updateListener.of((state) => {
const pos = state.state.selection.main.head;
const line = state.state.doc.lineAt(pos);
const lineNumber = line.number;
const columnNumber = pos - line.from;
if (onCursorChange) onCursorChange(pos, lineNumber, columnNumber);
}),
]}
extensions={extensions}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/gui/tabs/query-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export default function QueryWindow({
<div className="grow overflow-hidden">
<SqlEditor
ref={editorRef}
dialect={databaseDriver.getFlags().dialect}
value={code}
onChange={setCode}
schema={autoCompleteSchema}
Expand Down
5 changes: 4 additions & 1 deletion src/components/gui/tabs/trigger-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ export default function TriggerTab({
</div>
<div className="grow overflow-hidden">
<div className="h-full">
<SqlEditor value={trigger?.statement ?? ""} />
<SqlEditor
value={trigger?.statement ?? ""}
dialect={databaseDriver.getFlags().dialect}
/>
</div>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function describeTableColumnType(type: TableColumnDataType) {
}
}

export type SupportedDialect = "sqlite" | "mysql";
export type SqlOrder = "ASC" | "DESC";
export type DatabaseRow = Record<string, unknown>;

Expand Down Expand Up @@ -202,6 +203,7 @@ export interface DriverFlags {
supportBigInt: boolean;
supportCreateUpdateTable: boolean;
mismatchDetection: boolean;
dialect: SupportedDialect;
}

export interface DatabaseTableColumnChange {
Expand Down
1 change: 1 addition & 0 deletions src/drivers/mysql/mysql-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default abstract class MySQLLikeDriver extends CommonSQLImplement {
supportBigInt: false,
mismatchDetection: false,
supportCreateUpdateTable: false,
dialect: "mysql",
};
}

Expand Down
1 change: 1 addition & 0 deletions src/drivers/sqlite-base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement {
optionalSchema: true,
mismatchDetection: false,
supportCreateUpdateTable: true,
dialect: "sqlite",
};
}

Expand Down

0 comments on commit cc14c93

Please sign in to comment.