Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(sql-editor): add an explain button on adjacent to the run button (#684)

Yunwei Xiaoa6741e792ea2
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vue
index b3463f164d..2e5e049ea5 100644
--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue
@@ -48,7 +48,13 @@ const emit = defineEmits<{
(e: "update:value", content: string): void;
(e: "change", content: string): void;
(e: "change-selection", content: string): void;
- (e: "run-query", content: string): void;
+ (
+ e: "run-query",
+ content: {
+ explain: boolean;
+ query: string;
+ }
+ ): void;
(e: "save", content: string): void;
}>();
@@ -115,7 +121,27 @@ const init = async () => {
?.getValueInRange(editorInstance.getSelection()) as string;
const query = selectedValue || typedValue;
- emit("run-query", query);
+ emit("run-query", { explain: false, query });
+ },
+ });
+
+ // add the run query action in context menu
+ editorInstance.addAction({
+ id: "ExplainQuery",
+ label: "Explain Query",
+ keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyE],
+ contextMenuGroupId: "operation",
+ contextMenuOrder: 0,
+ run: async () => {
+ const typedValue = editorInstance.getValue();
+ const selectedValue = editorInstance
+ .getModel()
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-expect-error
+ ?.getValueInRange(editorInstance.getSelection()) as string;
+
+ const query = selectedValue || typedValue;
+ emit("run-query", { explain: true, query });
},
});
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index ea567adee4..3f286e02d6 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -12,6 +12,10 @@ import {
isDMLStatement,
} from "../components/MonacoEditor/sqlParser";
+type executeOptions = {
+ explain: boolean;
+};
+
const useExecuteSQL = () => {
const store = useStore();
const { t } = useI18n();
@@ -28,7 +32,7 @@ const useExecuteSQL = () => {
});
};
- const execute = async () => {
+ const execute = async (options?: executeOptions) => {
if (state.isLoadingData) {
notify("INFO", t("common.tips"), t("sql-editor.can-not-execute-query"));
}
@@ -83,12 +87,14 @@ const useExecuteSQL = () => {
}
try {
+ const isExplain = options?.explain || false;
state.isLoadingData = true;
store.dispatch("sqlEditor/setIsExecuting", true);
// remove the comment from the sql statement in front-end
- const statement = data !== null ? transformSQL(data) : sqlStatement;
+ const selectStatement = data !== null ? transformSQL(data) : sqlStatement;
+ const explainStatement = `EXPLAIN ${selectStatement}`;
const res = await store.dispatch("sqlEditor/executeQuery", {
- statement,
+ statement: isExplain ? explainStatement : selectStatement,
});
state.isLoadingData = false;
store.dispatch("sqlEditor/setIsExecuting", false);
diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
index c08fc4cfbd..07c4e7e5b1 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
@@ -1,6 +1,6 @@
<template>
<div class="sqleditor-editor-actions">
- <div class="actions-left w-1/3">
+ <div class="actions-left w-1/3 space-x-2">
<NButton
type="primary"
:disabled="isEmptyStatement || executeState.isLoadingData"
@@ -8,6 +8,12 @@
>
<mdi:play class="h-5 w-5" /> {{ $t("common.run") }} (⌘+⏎)
</NButton>
+ <NButton
+ :disabled="isEmptyStatement || executeState.isLoadingData"
+ @click="handleExplainQuery"
+ >
+ <mdi:play class="h-5 w-5" /> Explain (⌘+E)
+ </NButton>
</div>
<div class="actions-right space-x-2 flex w-2/3 justify-end">
<NPopover trigger="hover" placement="bottom-center" :show-arrow="false">
@@ -134,6 +140,10 @@ const handleRunQuery = () => {
execute();
};
+const handleExplainQuery = () => {
+ execute({ explain: true });
+};
+
const handleUpsertSheet = async () => {
const { name, statement, sheetId } = currentTab.value;
return upsertSheet({
diff --git a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
index 9c4f5557f2..5c563e24ca 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
@@ -59,7 +59,13 @@ const handleSave = async (statement: string) => {
});
};
-const handleRunQuery = () => {
- execute();
+const handleRunQuery = ({
+ explain,
+ query,
+}: {
+ explain: boolean;
+ query: string;
+}) => {
+ execute({ explain });
};
</script>