Skip to content

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

feat(sql-editor): introduce connection tree node context menu feature (#1001)

Yunwei Xiao41e6d8b7f62a
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index 01d03693a1..28ac78ee76 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -27,9 +27,11 @@ export const getDefaultConnectionContext = () => ({
instanceName: "",
databaseId: UNKNOWN_ID,
databaseName: "",
+ databaseType: "",
tableId: UNKNOWN_ID,
tableName: "",
isLoadingTree: false,
+ option: {},
});
const state: () => SqlEditorState = () => ({
@@ -118,11 +120,20 @@ const getters = {
const connectionContext = state.connectionContext;
return `${connectionContext.instanceId}/${connectionContext.databaseId}/${connectionContext.tableId}`;
},
- // in case of the connection is not set, miss the instance id, we can not create the sheet
+ /**
+ * check the connection whether disconnected
+ * 1、If the context is not set the instanceId, return true
+ * 2、If the context is set the instanceId, but not set the databaseId and databaseType is not MYSQL or TIDB, return true
+ * @param state
+ * @returns boolean
+ */
isDisconnected(state: SqlEditorState) {
+ const ctx = state.connectionContext;
return (
- state.connectionContext.instanceId === UNKNOWN_ID ||
- state.connectionContext.databaseId === UNKNOWN_ID
+ ctx.instanceId === UNKNOWN_ID ||
+ (ctx.databaseId === UNKNOWN_ID &&
+ ctx.databaseType !== "MYSQL" &&
+ ctx.databaseType !== "TIDB")
);
},
};
@@ -226,6 +237,7 @@ const actions = {
instanceName: instance.name,
databaseId,
databaseName: database.name,
+ databaseType: instance.engine,
});
},
async fetchQueryHistoryList({ commit, dispatch }: any) {
diff --git a/frontend/src/store/pinia-modules/tab.ts b/frontend/src/store/pinia-modules/tab.ts
index 166bd55b01..3274358f50 100644
--- a/frontend/src/store/pinia-modules/tab.ts
+++ b/frontend/src/store/pinia-modules/tab.ts
@@ -30,7 +30,7 @@ export const useTabStore = defineStore("tab", {
setTabState(payload: Partial<TabState>) {
Object.assign(this, payload);
},
- addTab(payload: AnyTabInfo) {
+ addTab(payload?: AnyTabInfo) {
const defaultTab = getDefaultTab();
const newTab = {
diff --git a/frontend/src/types/sqlEditor.ts b/frontend/src/types/sqlEditor.ts
index 6ff6064170..6a5c70a507 100644
--- a/frontend/src/types/sqlEditor.ts
+++ b/frontend/src/types/sqlEditor.ts
@@ -1,4 +1,5 @@
import * as monaco from "monaco-editor";
+import { DropdownOption } from "naive-ui";
import {
ProjectId,
@@ -39,11 +40,13 @@ export type ConnectionContext = {
projectName: string;
instanceId: InstanceId;
instanceName: string;
- databaseId?: DatabaseId;
- databaseName?: string;
+ databaseId: DatabaseId;
+ databaseName: string;
+ databaseType: string;
tableId?: TableId;
tableName?: string;
isLoadingTree: boolean;
+ option: DropdownOption;
};
export interface QueryHistory {
diff --git a/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue b/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue
index 7aff02355e..05360022c2 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue
@@ -48,7 +48,7 @@ const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
const databasePaneSzie = ref(FULL_HEIGHT);
const handleResized = (data: any) => {
- const [top, bottom] = data;
+ const [top] = data;
databasePaneSzie.value = top.size;
};
@@ -57,9 +57,11 @@ const handleCloseTableSchemaPane = () => {
};
watch(
- () => connectionContext.value.tableName,
- () => {
- databasePaneSzie.value = DATABASE_PANE_SIZE;
+ () => connectionContext.value.option,
+ (option) => {
+ if (option && option.type === "table") {
+ databasePaneSzie.value = DATABASE_PANE_SIZE;
+ }
}
);
</script>
diff --git a/frontend/src/views/SqlEditor/AsidePanel/DatabaseTree.vue b/frontend/src/views/SqlEditor/AsidePanel/DatabaseTree.vue
index 4c7a1d41dd..3dfbdf4648 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/DatabaseTree.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/DatabaseTree.vue
@@ -20,8 +20,20 @@
:pattern="searchPattern"
:default-expanded-keys="defaultExpanedKeys"
:default-selected-keys="defaultSelectedKeys"
- :on-update:selected-keys="handleSelectedKeysChange"
+ :selected-keys="selectedKeys"
:render-label="renderLabel"
+ :render-suffix="renderSuffix"
+ :node-props="nodeProps"
+ />
+ <n-dropdown
+ trigger="manual"
+ placement="bottom-start"
+ :show="showDropdown"
+ :options="(dropdownOptions as any)"
+ :x="x"
+ :y="y"
+ @select="handleSelect"
+ @clickoutside="handleClickoutside"
/>
</div>
</div>
@@ -34,29 +46,40 @@
import { ref, computed, h } from "vue";
import {
useNamespacedState,
+ useNamespacedGetters,
useNamespacedActions,
} from "vuex-composition-helpers";
import { cloneDeep, omit, escape } from "lodash-es";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
-import { TreeOption } from "naive-ui";
+import { TreeOption, DropdownOption } from "naive-ui";
import {
- SqlEditorState,
ConnectionAtom,
+ SqlEditorState,
+ SqlEditorGetters,
SqlEditorActions,
ConnectionContext,
+ Database,
UNKNOWN_ID,
+ Instance,
} from "@/types";
import { connectionSlug, getHighlightHTMLByKeyWords } from "@/utils";
import InstanceEngineIconVue from "@/components/InstanceEngineIcon.vue";
import HeroiconsOutlineDatabase from "~icons/heroicons-outline/database.vue";
import HeroiconsOutlineTable from "~icons/heroicons-outline/table.vue";
+import HeroiconsSolidDotsHorizontal from "~icons/heroicons-solid/dots-horizontal.vue";
+import { useTabStore } from "@/store";
const store = useStore();
const router = useRouter();
+const tabStore = useTabStore();
-const searchPattern = ref();
+const { findProjectIdByDatabaseId, connectionInfo } =
+ useNamespacedGetters<SqlEditorGetters>("sqlEditor", [
+ "findProjectIdByDatabaseId",
+ "connectionInfo",
+ ]);
const { connectionTree, connectionContext } =
useNamespacedState<SqlEditorState>("sqlEditor", [
"connectionTree",
@@ -67,6 +90,45 @@ const { setConnectionContext } = useNamespacedActions<SqlEditorActions>(
["setConnectionContext"]
);
+const searchPattern = ref();
+const showDropdown = ref(false);
+const x = ref(0);
+const y = ref(0);
+const selectedKeys = ref<string[] | number[]>([]);
+const sheetContext = ref<DropdownOption>();
+const dropdownOptions = computed(() => {
+ if (!sheetContext.value) {
+ return [];
+ }
+ if (sheetContext.value.type === "table") {
+ return [
+ {
+ label: "Alter table",
+ key: "editor.sheet.alter-table",
+ item: sheetContext.value,
+ },
+ // TODO Just a thought
+ // {
+ // label: "Copy name",
+ // key: "editor.sheet.copy-name",
+ // item: sheetContext.value,
+ // },
+ ];
+ }
+ return [
+ {
+ label: "Open in new tab",
+ key: "editor.sheet.new",
+ item: sheetContext.value,
+ },
+ {
+ label: "Set as context",
+ key: "editor.sheet.set-context",
+ item: sheetContext.value,
+ },
+ ];
+});
+
const treeData = computed(() => {
const tree = cloneDeep(connectionTree.value);
@@ -147,13 +209,8 @@ const getFlattenConnectionTree = () => {
};
};
-const handleSelectedKeysChange = (
- keys: number[],
- options: Array<ConnectionAtom>
-) => {
- const [selectedItem] = options;
-
- if (selectedItem) {
+const setSheetContext = (option: any) => {
+ if (option) {
let ctx: ConnectionContext = cloneDeep(connectionContext.value);
const { instanceList, databaseList } = getFlattenConnectionTree();
@@ -161,31 +218,39 @@ const handleSelectedKeysChange = (
const instance = instanceList?.find((item) => item.id === id);
return instance ? instance.label : "";
};
+ const getInstanceEngineByInstanceId = (id: number) => {
+ const selectedInstance = store.getters["instance/instanceById"](
+ id
+ ) as Instance;
+ return selectedInstance ? selectedInstance.engine : "MYSQL";
+ };
// If selected item is instance node
- if (selectedItem.type === "instance") {
- ctx.instanceId = selectedItem.id;
- ctx.instanceName = selectedItem.label;
+ if (option.type === "instance") {
+ ctx.instanceId = option.id;
+ ctx.instanceName = option.label;
ctx.databaseId = UNKNOWN_ID;
ctx.databaseName = "";
+ ctx.databaseType = getInstanceEngineByInstanceId(option.id);
ctx.tableId = UNKNOWN_ID;
ctx.tableName = "";
}
// If selected item is database node
- if (selectedItem.type === "database") {
- const instanceId = selectedItem.parentId;
+ if (option.type === "database") {
+ const instanceId = option.parentId;
ctx.instanceId = instanceId;
ctx.instanceName = getInstanceNameByInstanceId(instanceId);
- ctx.databaseId = selectedItem.id;
- ctx.databaseName = selectedItem.label;
+ ctx.databaseId = option.id;
+ ctx.databaseName = option.label;
+ ctx.databaseType = getInstanceEngineByInstanceId(instanceId);
ctx.tableId = UNKNOWN_ID;
ctx.tableName = "";
}
// If selected item is table node
- if (selectedItem.type === "table") {
- const databaseId = selectedItem.parentId;
+ if (option.type === "table") {
+ const databaseId = option.parentId;
const databaseInfo = databaseList?.find((item) => item.id === databaseId);
const databaseName = databaseInfo?.label || "";
const instanceId = databaseInfo?.parentId || UNKNOWN_ID;
@@ -193,8 +258,9 @@ const handleSelectedKeysChange = (
ctx.instanceName = getInstanceNameByInstanceId(instanceId);
ctx.databaseId = databaseId;
ctx.databaseName = databaseName;
- ctx.tableId = selectedItem.id;
- ctx.tableName = selectedItem.label;
+ ctx.databaseType = getInstanceEngineByInstanceId(instanceId);
+ ctx.tableId = option.id;
+ ctx.tableName = option.label;
}
ctx.hasSlug = true;
@@ -215,6 +281,7 @@ const handleSelectedKeysChange = (
}
};
+// dynamic render the highlight keywords
const renderLabel = ({ option }: { option: TreeOption }) => {
const renderLabelHTML = searchPattern.value
? h("span", {
@@ -222,11 +289,101 @@ const renderLabel = ({ option }: { option: TreeOption }) => {
escape(option.label),
escape(searchPattern.value)
),
+ class: "truncate",
})
: escape(option.label);
return renderLabelHTML;
};
+
+// render the suffix icon
+const renderSuffix = ({ option }: { option: TreeOption }) => {
+ const renderSuffixHTML = h(HeroiconsSolidDotsHorizontal, {
+ id: "tree-node-suffix",
+ class:
+ "n-tree-node-content__suffix-icon h-4 w-4 absolute right-0 top-0 hidden",
+ onClick: (e: MouseEvent) => {
+ sheetContext.value = option;
+ showDropdown.value = true;
+ x.value = e.clientX;
+ y.value = e.clientY;
+ e.preventDefault();
+ },
+ });
+
+ return renderSuffixHTML;
+};
+
+const gotoAlterSchema = (option: any) => {
+ console.log(option);
+ const databaseId = option.parentId;
+ const projectId = findProjectIdByDatabaseId.value(databaseId);
+ const databaseList =
+ connectionInfo.value.databaseListByProjectId.get(projectId);
+ const databaseName = databaseList.find(
+ (database: Database) => database.id === databaseId
+ ).name;
+
+ router.push({
+ name: "workspace.issue.detail",
+ params: {
+ issueSlug: "new",
+ },
+ query: {
+ template: "bb.issue.database.schema.update",
+ name: `[${databaseName}] Alter schema`,
+ project: projectId,
+ databaseList: databaseId,
+ sql: `ALTER TABLE ${option.label}`,
+ },
+ });
+};
+
+const handleSelect = (key: string) => {
+ const option = dropdownOptions.value.find(
+ (item) => item.key === key
+ ) as DropdownOption;
+ if (key === "editor.sheet.alter-table") {
+ gotoAlterSchema(option.item);
+ }
+ if (key === "editor.sheet.set-context") {
+ setSheetContext(option.item);
+ }
+ if (key === "editor.sheet.new") {
+ // set the sheet context first
+ setSheetContext(option.item);
+ // and then create a new tab
+ tabStore.addTab();
+ }
+ showDropdown.value = false;
+};
+
+const handleClickoutside = () => {
+ showDropdown.value = false;
+};
+
+const nodeProps = ({ option }: { option: TreeOption }) => {
+ return {
+ onClick: (e: any) => {
+ if (e?.target?.id === "tree-node-suffix") return;
+ if (option) {
+ let ctx: ConnectionContext = cloneDeep(connectionContext.value);
+ ctx.option = option;
+ setConnectionContext(ctx);
+ }
+ },
+ onContextmenu(e: MouseEvent) {
+ sheetContext.value = option;
+ showDropdown.value = true;
+ x.value = e.clientX;
+ y.value = e.clientY;
+ if (option && option.key) {
+ selectedKeys.value = [option.key as string];
+ }
+ e.preventDefault();
+ },
+ };
+};
</script>
<style>
@@ -237,6 +394,21 @@ const renderLabel = ({ option }: { option: TreeOption }) => {
border-bottom: none;
border-bottom-color: transparent;
}
+
+.n-tree .n-tree-node-content .n-tree-node-content__text {
+ @apply truncate;
+}
+
+.n-tree .n-tree-node .n-tree-node-content__suffix {
+ @apply w-4 h-4 flex items-center relative ml-1;
+}
+
+.n-tree
+ .n-tree-node:hover
+ .n-tree-node-content__suffix
+ .n-tree-node-content__suffix-icon {
+ @apply block;
+}
</style>
<style scoped>
diff --git a/frontend/src/views/SqlEditor/AsidePanel/TableSchema.vue b/frontend/src/views/SqlEditor/AsidePanel/TableSchema.vue
index ebcfdbab75..68fed6159b 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/TableSchema.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/TableSchema.vue
@@ -71,7 +71,6 @@ import {
useNamespacedState,
useNamespacedActions,
} from "vuex-composition-helpers";
-import { isEmpty } from "lodash-es";
import type {
SqlEditorState,
@@ -128,19 +127,20 @@ const handleClosePane = () => {
};
watch(
- () => connectionContext.value.tableName,
- async (newVal, oldVal) => {
- if (!isEmpty(newVal)) {
+ () => connectionContext.value.option,
+ async (option) => {
+ if (option && option.type === "table") {
const res = await fetchTableByDatabaseIdAndTableName({
- databaseId: ctx.databaseId,
- tableName: ctx.tableName,
+ databaseId: option.parentId,
+ tableName: option.label,
});
tableInfo.value = res;
} else {
- tableInfo.value = "";
+ tableInfo.value = null;
}
- }
+ },
+ { deep: true }
);
</script>
diff --git a/frontend/src/views/SqlEditor/TabListContainer.vue b/frontend/src/views/SqlEditor/TabListContainer.vue
index 5f12dc0c3c..a1f7720b06 100644
--- a/frontend/src/views/SqlEditor/TabListContainer.vue
+++ b/frontend/src/views/SqlEditor/TabListContainer.vue
@@ -128,7 +128,6 @@ import { useDialog } from "naive-ui";
import { useTabStore } from "@/store";
import { TabInfo, SheetGetters, SheetActions } from "@/types";
-import { getDefaultTab } from "@/utils/tab";
import { useSQLEditorConnection } from "@/composables/useSQLEditorConnection";
const tabStore = useTabStore();
@@ -237,8 +236,7 @@ const handleSelectTab = async (tab: TabInfo) => {
setConnectionContextFromCurrentTab();
};
const handleAddTab = () => {
- tabStore.addTab(getDefaultTab());
-
+ tabStore.addTab();
nextTick(recalculateScrollWidth);
};
@@ -287,8 +285,7 @@ const handleScollTabList = debounce((e: WheelEvent) => {
onMounted(async () => {
if (!tabStore.hasTabs) {
- tabStore.addTab(getDefaultTab());
- recalculateScrollWidth();
+ handleAddTab();
}
});