Skip to content

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

feat(sql-editor): split history items and query items (#4544)

Liu Jif6ee4273a273
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 29199fa278..798e393abe 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1466,7 +1466,7 @@
},
"read-only-mode-not-allowed": "Instance {instance} is not accessible in read-only mode.",
"run-selected": "Run selected",
- "clear-history": "Clear history"
+ "clear-screen": "Clear screen"
},
"schema-editor": {
"self": "Schema Editor",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index cddd5cd04a..ec34131edf 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1466,7 +1466,7 @@
},
"read-only-mode-not-allowed": "实例{instance}无法通过只读模式访问。",
"run-selected": "运行选中的语句",
- "clear-history": "清除历史"
+ "clear-screen": "清屏"
},
"schema-editor": {
"self": "Schema 编辑器",
diff --git a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
index 7e351d6dcc..4c635e04a3 100644
--- a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
@@ -23,11 +23,11 @@
{{ $t("sql-editor.format") }} (⇧+⌥+F)
</NButton>
<NButton
- v-if="showClearHistory"
+ v-if="showClearScreen"
:disabled="queryList.length <= 1 || isExecutingSQL"
- @click="handleClearHistory"
+ @click="handleClearScreen"
>
- {{ $t("sql-editor.clear-history") }} (⇧+⌥+C)
+ {{ $t("sql-editor.clear-screen") }} (⇧+⌥+C)
</NButton>
</div>
<div class="action-right space-x-2 flex justify-end items-center">
@@ -86,7 +86,7 @@ const emit = defineEmits<{
config: ExecuteConfig,
option?: ExecuteOption
): void;
- (e: "clear-history"): void;
+ (e: "clear-screen"): void;
}>();
const instanceStore = useInstanceStore();
@@ -137,7 +137,7 @@ const allowSave = computed(() => {
return true;
});
-const showClearHistory = computed(() => {
+const showClearScreen = computed(() => {
return tabStore.currentTab.mode === TabMode.Admin;
});
@@ -170,7 +170,7 @@ const handleFormatSQL = () => {
sqlEditorStore.setShouldFormatContent(true);
};
-const handleClearHistory = () => {
- emit("clear-history");
+const handleClearScreen = () => {
+ emit("clear-screen");
};
</script>
diff --git a/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue b/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
index ea169f34e0..c81dd622a5 100644
--- a/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
+++ b/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
@@ -54,7 +54,7 @@ const emit = defineEmits<{
option?: ExecuteOption
): void;
(e: "history", direction: "up" | "down"): void;
- (e: "clear-history"): void;
+ (e: "clear-screen"): void;
}>();
const MIN_EDITOR_HEIGHT = 40; // ~= 1 line
@@ -172,34 +172,36 @@ const handleEditorReady = async () => {
editor?.addAction({
id: "ExplainQuery",
label: "Explain Query",
- keybindings: [
- monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyC,
- ],
- contextMenuGroupId: "operation",
- contextMenuOrder: 2,
- precondition: "!readonly",
- run: () => {
- emit("clear-history");
- },
- });
-
- editor?.addAction({
- id: "ClearHistory",
- label: "Clear History",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyE],
contextMenuGroupId: "operation",
- contextMenuOrder: 3,
+ contextMenuOrder: 1,
precondition: "!readonly",
run: async () => {
emit(
"execute",
props.sql,
- { databaseType: selectedInstanceEngine.value },
+ {
+ databaseType: selectedInstanceEngine.value,
+ },
{ explain: true }
);
},
});
+ editor?.addAction({
+ id: "ClearScreen",
+ label: "Clear Screen",
+ keybindings: [
+ monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyC,
+ ],
+ contextMenuGroupId: "operation",
+ contextMenuOrder: 3,
+ precondition: "!readonly",
+ run: () => {
+ emit("clear-screen");
+ },
+ });
+
// Create an editor context value to check if the SQL ends with semicolon ";"
const endsWithSemicolon = editor?.createContextKey<boolean>(
"endsWithSemicolon",
diff --git a/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue b/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue
index 8e73d69fbc..d4f9f9af21 100644
--- a/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue
+++ b/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue
@@ -2,10 +2,7 @@
<div
class="flex h-full w-full flex-col justify-start items-start overflow-hidden"
>
- <EditorAction
- @execute="handleExecute"
- @clear-history="handleClearHistory"
- />
+ <EditorAction @execute="handleExecute" @clear-screen="handleClearScreen" />
<ConnectionPathBar />
@@ -26,7 +23,7 @@
:readonly="!isEditableQueryItem(query)"
@execute="handleExecute"
@history="handleHistory"
- @clear-history="handleClearHistory"
+ @clear-screen="handleClearScreen"
/>
<div v-if="query.queryResult" class="max-h-[20rem] overflow-y-auto">
<TableView
@@ -63,10 +60,14 @@
</template>
<script lang="ts" setup>
-import { computed, reactive, ref, watch } from "vue";
+import { computed, ref, watch } from "vue";
import { useElementSize } from "@vueuse/core";
-import { ExecuteConfig, ExecuteOption, WebTerminalQueryItem } from "@/types";
+import type {
+ ExecuteConfig,
+ ExecuteOption,
+ WebTerminalQueryItem,
+} from "@/types";
import { createQueryItem, useTabStore, useWebTerminalStore } from "@/store";
import CompactSQLEditor from "./CompactSQLEditor.vue";
import {
@@ -77,11 +78,7 @@ import {
} from "../EditorCommon";
import { useExecuteSQL } from "@/composables/useExecuteSQL";
import { useCancelableTimeout } from "@/composables/useCancelableTimeout";
-import { minmax } from "@/utils";
-
-type LocalState = {
- historyIndex: number;
-};
+import { useHistory } from "./useHistory";
const QUERY_TIMEOUT_MS = 5000;
const MAX_QUERY_ITEM_COUNT = 20;
@@ -89,10 +86,6 @@ const MAX_QUERY_ITEM_COUNT = 20;
const tabStore = useTabStore();
const webTerminalStore = useWebTerminalStore();
-const state = reactive<LocalState>({
- historyIndex: -1,
-});
-
const queryList = computed(() => {
return webTerminalStore.getQueryListByTab(tabStore.currentTab);
});
@@ -106,6 +99,8 @@ const currentQuery = computed(
const { execute } = useExecuteSQL();
+const { move: moveHistory } = useHistory();
+
const queryTimer = useCancelableTimeout(QUERY_TIMEOUT_MS);
const { expired } = queryTimer;
@@ -162,39 +157,18 @@ const handleExecute = async (
}
};
-const handleClearHistory = () => {
+const handleClearScreen = () => {
const list = queryList.value;
while (list.length > 1) {
list.shift();
}
};
-watch(
- () => queryList.value.length,
- (len) => {
- state.historyIndex = len - 1;
- },
- { immediate: true }
-);
const handleHistory = (direction: "up" | "down") => {
if (currentQuery.value.status !== "IDLE") {
return;
}
- const list = queryList.value;
- const delta = direction === "up" ? -1 : 1;
- const nextIndex = minmax(state.historyIndex + delta, 0, list.length - 1);
- if (nextIndex === state.historyIndex) {
- return;
- }
- if (nextIndex === list.length - 1) {
- currentQuery.value.sql = "";
- } else {
- const historyQuery = list[nextIndex];
- if (historyQuery) {
- currentQuery.value.sql = historyQuery.sql;
- }
- }
- state.historyIndex = nextIndex;
+ moveHistory(direction);
};
const handleCancelQuery = async () => {
diff --git a/frontend/src/views/sql-editor/TerminalPanel/useHistory.ts b/frontend/src/views/sql-editor/TerminalPanel/useHistory.ts
new file mode 100644
index 0000000000..002e46e118
--- /dev/null
+++ b/frontend/src/views/sql-editor/TerminalPanel/useHistory.ts
@@ -0,0 +1,83 @@
+import { computed, watch } from "vue";
+
+import { TabMode, WebTerminalQueryItem } from "@/types";
+import { useTabStore, useWebTerminalStore } from "@/store";
+import { minmax } from "@/utils";
+
+const MAX_HISTORY_ITEM_COUNT = 1000;
+
+type HistoryState = {
+ index: number;
+ list: WebTerminalQueryItem[];
+};
+
+export const useHistory = () => {
+ const tabStore = useTabStore();
+ const webTerminalStore = useWebTerminalStore();
+ const historyByTabId = new Map<string, HistoryState>();
+
+ const currentQuery = computed(() => {
+ const queryList = webTerminalStore.getQueryListByTab(tabStore.currentTab);
+ return queryList[queryList.length - 1];
+ });
+
+ const currentStack = () => {
+ const tab = tabStore.currentTab;
+ if (tab.mode === TabMode.ReadOnly) return undefined;
+
+ const existed = historyByTabId.get(tab.id);
+ if (existed) {
+ return existed;
+ }
+ const initial: HistoryState = {
+ index: -1,
+ list: [],
+ };
+ historyByTabId.set(tab.id, initial);
+ return initial;
+ };
+
+ const push = (query: WebTerminalQueryItem) => {
+ const stack = currentStack();
+ if (!stack) return;
+ const { list } = stack;
+ list.push(query);
+ if (list.length > MAX_HISTORY_ITEM_COUNT) {
+ list.shift();
+ }
+ stack.index = list.length - 1;
+ };
+
+ const move = (direction: "up" | "down") => {
+ const stack = currentStack();
+ if (!stack) return;
+ const { index, list } = stack;
+ const delta = direction === "up" ? -1 : 1;
+ const nextIndex = minmax(index + delta, 0, list.length - 1);
+ if (nextIndex === index) {
+ return;
+ }
+ if (nextIndex === list.length - 1) {
+ currentQuery.value.sql = "";
+ } else {
+ const historyQuery = list[nextIndex];
+ if (historyQuery) {
+ currentQuery.value.sql = historyQuery.sql;
+ }
+ }
+
+ stack.index = nextIndex;
+ };
+
+ watch(
+ () => currentQuery.value,
+ (query) => {
+ push(query);
+ },
+ {
+ immediate: true,
+ }
+ );
+
+ return { push, move };
+};