From 5f870be0d942fe09e1584bde3259c51124872841 Mon Sep 17 00:00:00 2001
From: t8y2 <1156263951@qq.com>
Date: Tue, 16 Jun 2026 00:51:34 +0800
Subject: [PATCH] feat(editor): open table ddl panel from SQL
---
apps/desktop/src/App.vue | 29 +++++++----
.../src/components/editor/QueryEditor.vue | 51 +++++++++++++++++++
apps/desktop/src/components/grid/DataGrid.vue | 12 ++++-
.../src/components/layout/ContentArea.vue | 6 ++-
.../src/composables/useNavigationTargets.ts | 6 ++-
apps/desktop/src/types/database.ts | 3 ++
6 files changed, 92 insertions(+), 15 deletions(-)
diff --git a/apps/desktop/src/App.vue b/apps/desktop/src/App.vue
index 9f07c81a82..93b8de78dd 100644
--- a/apps/desktop/src/App.vue
+++ b/apps/desktop/src/App.vue
@@ -64,6 +64,7 @@ import { buildHistoryAiAnalysisPrompt } from "@/lib/historyAiAnalysis";
import { countAvailableAgentDriverUpdates, type AgentDriverUpdateBadgeState } from "@/lib/agentDriverUpdateBadge";
import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/safeStorage";
import { rankSavedSqlHistory } from "@/lib/savedSqlHistory";
+import { isSchemaAware, isSingleDatabase } from "@/lib/databaseFeatureSupport";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
@@ -757,20 +758,28 @@ async function onClickTable(tableName: string) {
const tab = activeTab.value;
if (!tab) return;
const connectionId = tab.connectionId;
- const database = tab.database;
+ let database = tab.database;
+ let schema = tab.schema;
- // Parse schema.table if needed
- const [schema, rawTableName] = tableName.includes(".") ? tableName.split(".") : [database, tableName];
+ const parts = tableName.split(".").filter(Boolean);
+ const rawTableName = parts[parts.length - 1] || tableName;
+ if (parts.length >= 3) {
+ database = parts[parts.length - 3] || database;
+ schema = parts[parts.length - 2];
+ } else if (parts.length === 2) {
+ const dbType = connectionStore.getConfig(connectionId)?.db_type;
+ if (dbType && !isSchemaAware(dbType) && !isSingleDatabase(dbType)) {
+ database = parts[0] || database;
+ schema = undefined;
+ } else {
+ schema = parts[0];
+ }
+ }
try {
- await connectionStore.ensureConnected(connectionId);
- const ddl = await api.getTableDdl(connectionId, database, schema || database, rawTableName);
-
- // Create a new tab with the DDL
- const tabId = queryStore.createTab(connectionId, database, `DDL - ${rawTableName}`);
- queryStore.updateSql(tabId, ddl);
+ await openTableTarget({ connectionId, database, schema, tableName: rawTableName }, { tableInfoTab: "ddl" });
} catch (e: any) {
- toast(`Failed to get table DDL: ${e?.message || String(e)}`, 5000);
+ toast(t("connection.connectFailed", { message: translateBackendError(t, e?.message || String(e)) }), 5000);
}
}
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue
index c20415776d..e57f9ebe5b 100644
--- a/apps/desktop/src/components/editor/QueryEditor.vue
+++ b/apps/desktop/src/components/editor/QueryEditor.vue
@@ -184,6 +184,7 @@ let editorIsActive = true;
let tableReferenceDropListenerRegistered = false;
let imeCompositionActive = false;
let pendingImeModelEmit = false;
+const tableNavigationHoverClass = "query-editor--table-navigation-hover";
function editorThemeAppearance() {
return isDark.value ? "dark" : "light";
@@ -295,6 +296,33 @@ function focusEditor() {
view.value?.focus();
}
+function clearTableNavigationHover() {
+ editorRef.value?.classList.remove(tableNavigationHoverClass);
+}
+
+function tableNavigationIdentifierAt(currentView: EditorViewType, event: MouseEvent): string | null {
+ if (!props.connectionId || props.database == null) return null;
+ const pos = currentView.posAtCoords({ x: event.clientX, y: event.clientY });
+ if (pos == null) return null;
+ const identifier = extractIdentifierAt(currentView.state.doc.toString(), pos);
+ if (!identifier || isSqlKeyword(identifier)) return null;
+ return identifier;
+}
+
+function updateTableNavigationHover(currentView: EditorViewType, event: MouseEvent) {
+ if (!event.metaKey && !event.ctrlKey) {
+ clearTableNavigationHover();
+ return false;
+ }
+ const identifier = tableNavigationIdentifierAt(currentView, event);
+ editorRef.value?.classList.toggle(tableNavigationHoverClass, !!identifier);
+ return !!identifier;
+}
+
+function clearTableNavigationHoverOnModifierRelease(event: KeyboardEvent) {
+ if (!event.metaKey && !event.ctrlKey) clearTableNavigationHover();
+}
+
function executeFromContextMenu() {
if (!canExecuteContextSql.value) return;
executeCurrentSql();
@@ -1473,6 +1501,8 @@ onMounted(async () => {
codeMirrorRedo = redo;
codeMirrorSelectAll = selectAll;
codeMirrorInsertNewlineKeepIndent = insertNewlineKeepIndent;
+ window.addEventListener("keyup", clearTableNavigationHoverOnModifierRelease);
+ window.addEventListener("blur", clearTableNavigationHover);
const diagnosticTheme = EditorView.baseTheme({
".cm-sql-error": {
@@ -1701,7 +1731,18 @@ onMounted(async () => {
scheduleFontSizeCommit(next);
return true;
},
+ mousemove: (event: MouseEvent) => {
+ const currentView = view.value;
+ if (!currentView) return false;
+ updateTableNavigationHover(currentView, event);
+ return false;
+ },
+ mouseleave: () => {
+ clearTableNavigationHover();
+ return false;
+ },
mousedown: (event: MouseEvent) => {
+ clearTableNavigationHover();
// Click without modifier -> close column panel
if (!event.metaKey && !event.ctrlKey) {
if (event.button === 0) {
@@ -1948,6 +1989,7 @@ watch(
function pauseQueryEditorBackgroundWork() {
flushEditorViewport();
flushEditorSelection();
+ clearTableNavigationHover();
editorIsActive = false;
semanticDiagnosticRunId++;
if (semanticDiagnosticTimer) clearTimeout(semanticDiagnosticTimer);
@@ -1980,6 +2022,8 @@ onBeforeUnmount(() => {
viewportRestoreFrame = null;
}
view.value?.scrollDOM.removeEventListener("scroll", scheduleEditorViewportEmit);
+ window.removeEventListener("keyup", clearTableNavigationHoverOnModifierRelease);
+ window.removeEventListener("blur", clearTableNavigationHover);
zoomCommitScheduler.dispose();
view.value?.destroy();
});
@@ -2124,3 +2168,10 @@ defineExpose({ openSearch, openReplace, scrollCursorIntoView });
+
+
diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue
index 93bfb0f938..2f4cdfc8b7 100644
--- a/apps/desktop/src/components/grid/DataGrid.vue
+++ b/apps/desktop/src/components/grid/DataGrid.vue
@@ -78,7 +78,7 @@ import DangerConfirmDialog from "@/components/editor/DangerConfirmDialog.vue";
import ImagePreviewDialog from "@/components/grid/ImagePreviewDialog.vue";
import TemporalCellEditor from "@/components/grid/TemporalCellEditor.vue";
import EnumCellEditor from "@/components/grid/EnumCellEditor.vue";
-import type { QueryResult, ColumnInfo, DatabaseType, ForeignKeyInfo, IndexInfo, TriggerInfo } from "@/types/database";
+import type { QueryResult, ColumnInfo, DatabaseType, ForeignKeyInfo, IndexInfo, TriggerInfo, TableInfoTab } from "@/types/database";
import * as api from "@/lib/api";
import { coerceDataGridCellValue, dataGridCellDisplayText, dataGridCellEditorText } from "@/lib/dataGridCellCoercion";
import { createColumnDrafts } from "@/lib/tableStructureEditorState";
@@ -174,6 +174,7 @@ const props = defineProps<{
columns: ColumnInfo[];
primaryKeys: string[];
};
+ tableInfoTab?: TableInfoTab;
pageOffset?: number;
pageLimit?: number;
countSql?: string;
@@ -5395,7 +5396,6 @@ async function prefetchCopyStatements() {
const sqlOneLiner = computed(() => props.sql?.replace(/\s+/g, " ").trim() || "");
-type TableInfoTab = "columns" | "indexes" | "foreignKeys" | "triggers" | "ddl";
type TableInfoTabItem = { id: TableInfoTab; label: string; icon: Component; count?: number };
const TABLE_INFO_DRAWER_MIN_WIDTH = 240;
@@ -5543,6 +5543,14 @@ async function selectTableInfoTab(tab: TableInfoTab) {
else if (nextTab === "triggers") await fetchTriggers();
}
+watch(
+ () => [props.tableInfoTab, props.connectionId, props.database, props.tableMeta?.schema, props.tableMeta?.tableName] as const,
+ ([tab]) => {
+ if (tab) void selectTableInfoTab(tab);
+ },
+ { immediate: true },
+);
+
async function fetchDdl() {
if (!props.connectionId || !props.tableMeta) return;
showTableInfo.value = true;
diff --git a/apps/desktop/src/components/layout/ContentArea.vue b/apps/desktop/src/components/layout/ContentArea.vue
index 656c79fd9d..63244ecf56 100644
--- a/apps/desktop/src/components/layout/ContentArea.vue
+++ b/apps/desktop/src/components/layout/ContentArea.vue
@@ -54,7 +54,7 @@ import { formatShortcut } from "@/lib/shortcutRegistry";
import { effectiveDatabaseTypeForConnection } from "@/lib/jdbcDialect";
import { chartableColumnIndexes } from "@/lib/chartData";
import { useTabScroll } from "@/composables/useTabScroll";
-import type { QueryTab, ConnectionConfig } from "@/types/database";
+import type { QueryTab, ConnectionConfig, TableInfoTab } from "@/types/database";
import type { SqlFormatDialect } from "@/lib/sqlFormatter";
type DataGridHandle = {
@@ -74,7 +74,7 @@ type DataGridHandle = {
canToggleAllNullColumns: boolean;
toggleAllNullColumns: () => void;
showDdl: boolean;
- toggleDdl: () => void;
+ toggleDdl: (tab?: TableInfoTab) => void;
multiRowTranspose: boolean;
setMultiRowTranspose: (value: boolean) => void;
exportCsv: () => Promise;
@@ -712,6 +712,7 @@ defineExpose({ focusSearch, refreshData, handleModRTarget });
:database="activeTab.database"
:schema="activeTab.schema"
:table-meta="activeTab.tableMeta"
+ :table-info-tab="activeTab.tableInfoTab"
:page-offset="activeTab.resultPageOffset"
:page-limit="activeTab.resultPageLimit"
:count-sql="activeTab.resultCountSql"
@@ -900,6 +901,7 @@ defineExpose({ focusSearch, refreshData, handleModRTarget });
:connection-id="activeTab.connectionId"
:database="activeTab.database"
:table-meta="activeDataTabTableMeta"
+ :table-info-tab="activeTab.tableInfoTab"
:page-offset="activeTab.resultPageOffset"
:page-limit="activeTab.resultPageLimit"
:on-execute-sql="async (sql: string) => emit('executeSql', sql)"
diff --git a/apps/desktop/src/composables/useNavigationTargets.ts b/apps/desktop/src/composables/useNavigationTargets.ts
index 9e28bd4987..0d49e7e0a5 100644
--- a/apps/desktop/src/composables/useNavigationTargets.ts
+++ b/apps/desktop/src/composables/useNavigationTargets.ts
@@ -5,6 +5,7 @@ import { editablePrimaryKeys, usesSyntheticRowIdKey } from "@/lib/tableEditing";
import { useConnectionStore } from "@/stores/connectionStore";
import { useQueryStore } from "@/stores/queryStore";
import { useSettingsStore } from "@/stores/settingsStore";
+import type { TableInfoTab } from "@/types/database";
export type NavigationTarget = {
connectionId: string;
@@ -15,7 +16,7 @@ export type NavigationTarget = {
whereInput?: string;
};
-async function openTableTarget(target: NavigationTarget) {
+async function openTableTarget(target: NavigationTarget, options: { tableInfoTab?: TableInfoTab } = {}) {
const connectionStore = useConnectionStore();
const queryStore = useQueryStore();
const settingsStore = useSettingsStore();
@@ -30,12 +31,15 @@ async function openTableTarget(target: NavigationTarget) {
if (existing) {
existing.title = tabTitle;
existing.schema = target.schema;
+ existing.tableInfoTab = options.tableInfoTab;
queryStore.activeTabId = existing.id;
return existing.id;
}
}
return queryStore.createTab(target.connectionId, target.database, tabTitle, "data", target.schema);
})();
+ const targetTab = queryStore.tabs.find((tab) => tab.id === tabId);
+ if (targetTab) targetTab.tableInfoTab = options.tableInfoTab;
queryStore.setExecuting(tabId, true);
try {
diff --git a/apps/desktop/src/types/database.ts b/apps/desktop/src/types/database.ts
index 6c5bf1178b..30fe36d719 100644
--- a/apps/desktop/src/types/database.ts
+++ b/apps/desktop/src/types/database.ts
@@ -461,6 +461,8 @@ export interface TreeNode {
};
}
+export type TableInfoTab = "columns" | "indexes" | "foreignKeys" | "triggers" | "ddl";
+
export interface QueryTab {
id: string;
title: string;
@@ -530,6 +532,7 @@ export interface QueryTab {
columns: ColumnInfo[];
primaryKeys: string[];
};
+ tableInfoTab?: TableInfoTab;
queryAnalysis?: {
schema?: string;
schemaQuoted?: boolean;