fix(connection): hide system database objects

This commit is contained in:
t8y2
2026-05-09 19:50:28 +08:00
parent 9c71442f43
commit afb177db85
9 changed files with 195 additions and 29 deletions
+29 -13
View File
@@ -220,7 +220,9 @@ async function toggle() {
queryStore.updateSql(tab, node.label);
} else if (node.type === "database" && node.connectionId && node.database) {
const config = connectionStore.getConfig(node.connectionId);
if (config?.db_type && TREE_SCHEMA_TYPES.has(config.db_type)) {
if (config?.db_type === "sqlserver") {
await connectionStore.loadSqlServerDatabaseObjects(node.connectionId, node.database);
} else if (config?.db_type && TREE_SCHEMA_TYPES.has(config.db_type)) {
await connectionStore.loadSchemas(node.connectionId, node.database);
} else {
await connectionStore.loadTables(node.connectionId, node.database);
@@ -443,6 +445,18 @@ function dropTable() {
showDropTableConfirm.value = true;
}
async function refreshTableList(node: TreeNode) {
if (!node.connectionId || !node.database) return;
const config = connectionStore.getConfig(node.connectionId);
if (config?.db_type === "sqlserver" && node.schema?.toLowerCase() === "dbo") {
await connectionStore.loadSqlServerDatabaseObjects(node.connectionId, node.database, { force: true });
} else if (node.schema) {
await connectionStore.loadTables(node.connectionId, node.database, node.schema, { force: true });
} else {
await connectionStore.loadTables(node.connectionId, node.database, undefined, { force: true });
}
}
async function confirmDropTable() {
const node = props.node;
if (!node.connectionId || !node.database) return;
@@ -450,11 +464,7 @@ async function confirmDropTable() {
await connectionStore.ensureConnected(node.connectionId);
await api.executeQuery(node.connectionId, node.database, buildDropTableSql(), node.schema);
toast(t("contextMenu.dropTableSuccess", { name: node.label }), 3000);
if (node.schema) {
await connectionStore.loadTables(node.connectionId, node.database, node.schema, { force: true });
} else {
await connectionStore.loadTables(node.connectionId, node.database, undefined, { force: true });
}
await refreshTableList(node);
} catch (e: any) {
toast(t("contextMenu.tableOperationFailed", { message: e?.message || String(e) }), 5000);
}
@@ -556,7 +566,12 @@ async function confirmCreateSchema() {
const sql = `CREATE SCHEMA ${quoteIdent(name)};`;
await api.executeQuery(node.connectionId, node.database, sql);
toast(t("contextMenu.createSchemaSuccess", { name }), 3000);
await connectionStore.loadSchemas(node.connectionId, node.database, { force: true });
const config = connectionStore.getConfig(node.connectionId);
if (config?.db_type === "sqlserver") {
await connectionStore.loadSqlServerDatabaseObjects(node.connectionId, node.database, { force: true });
} else {
await connectionStore.loadSchemas(node.connectionId, node.database, { force: true });
}
} catch (e: any) {
toast(t("contextMenu.tableOperationFailed", { message: e?.message || String(e) }), 5000);
}
@@ -573,7 +588,12 @@ async function confirmDropSchema() {
await connectionStore.ensureConnected(node.connectionId);
await api.executeQuery(node.connectionId, node.database, buildDropSchemaSql());
toast(t("contextMenu.dropSchemaSuccess", { name: node.label }), 3000);
await connectionStore.loadSchemas(node.connectionId, node.database, { force: true });
const config = connectionStore.getConfig(node.connectionId);
if (config?.db_type === "sqlserver") {
await connectionStore.loadSqlServerDatabaseObjects(node.connectionId, node.database, { force: true });
} else {
await connectionStore.loadSchemas(node.connectionId, node.database, { force: true });
}
} catch (e: any) {
toast(t("contextMenu.tableOperationFailed", { message: e?.message || String(e) }), 5000);
}
@@ -608,11 +628,7 @@ async function confirmDuplicateStructure() {
}
await api.executeQuery(node.connectionId, node.database, sql, node.schema);
toast(t("contextMenu.duplicateStructureSuccess", { name: newName }), 3000);
if (node.schema) {
await connectionStore.loadTables(node.connectionId, node.database, node.schema, { force: true });
} else {
await connectionStore.loadTables(node.connectionId, node.database, undefined, { force: true });
}
await refreshTableList(node);
} catch (e: any) {
toast(t("contextMenu.tableOperationFailed", { message: e?.message || String(e) }), 5000);
}
+42
View File
@@ -0,0 +1,42 @@
import type { TableInfo, TreeNode } from "@/types/database";
export const SQLSERVER_DEFAULT_SCHEMA = "dbo";
function isDefaultSchema(schema: string): boolean {
return schema.toLowerCase() === SQLSERVER_DEFAULT_SCHEMA;
}
export function buildSqlServerDatabaseTreeNodes(
connectionId: string,
database: string,
schemas: string[],
defaultSchemaTables: TableInfo[],
): TreeNode[] {
const databaseNodeId = `${connectionId}:${database}`;
const defaultSchema = schemas.find(isDefaultSchema) || SQLSERVER_DEFAULT_SCHEMA;
const defaultTableNodes = defaultSchemaTables.map((table) => ({
id: `${databaseNodeId}:${defaultSchema}:${table.name}`,
label: table.name,
type: (table.table_type === "VIEW" ? "view" : "table") as "view" | "table",
connectionId,
database,
schema: defaultSchema,
isExpanded: false,
children: [],
}));
const schemaNodes = schemas
.filter((schema) => !isDefaultSchema(schema))
.map((schema) => ({
id: `${databaseNodeId}:${schema}`,
label: schema,
type: "schema" as const,
connectionId,
database,
schema,
isExpanded: false,
children: [],
}));
return [...defaultTableNodes, ...schemaNodes];
}
+29 -1
View File
@@ -22,6 +22,7 @@ import * as api from "@/lib/api";
import { isTauriRuntime } from "@/lib/tauriRuntime";
import { isSchemaAware, TREE_SCHEMA_TYPES } from "@/lib/databaseCapabilities";
import { buildDatabaseTreeNodes } from "@/lib/databaseTree";
import { buildSqlServerDatabaseTreeNodes, SQLSERVER_DEFAULT_SCHEMA } from "@/lib/sqlServerTree";
const PINNED_TREE_NODES_STORAGE_KEY = "dbx-pinned-tree-nodes";
@@ -536,6 +537,30 @@ export const useConnectionStore = defineStore("connection", () => {
}
}
async function loadSqlServerDatabaseObjects(connectionId: string, database: string, options?: LoadTreeOptions) {
const nodeId = `${connectionId}:${database}`;
const node = findNode(treeNodes.value, nodeId);
if (!node) return;
if (useCachedChildren(node, options)) return;
const cacheKey = schemaCacheKey(connectionId, database, "sqlserver-objects");
if (!options?.force && (await loadPersistedTreeChildren(node, cacheKey))) return;
node.isLoading = true;
try {
await ensureConnected(connectionId);
const [schemas, defaultSchemaTables] = await Promise.all([
api.listSchemas(connectionId, database),
api.listTables(connectionId, database, SQLSERVER_DEFAULT_SCHEMA),
]);
const children = buildSqlServerDatabaseTreeNodes(connectionId, database, schemas, defaultSchemaTables);
setChildren(node, children);
await savePersistedTreeChildren(cacheKey, children);
node.isExpanded = true;
} finally {
node.isLoading = false;
}
}
async function loadTables(connectionId: string, database: string, schema?: string, options?: LoadTreeOptions) {
const nodeId = schema ? `${connectionId}:${database}:${schema}` : `${connectionId}:${database}`;
const node = findNode(treeNodes.value, nodeId);
@@ -762,7 +787,9 @@ export const useConnectionStore = defineStore("connection", () => {
await loadMongoCollections(node.connectionId, node.database);
} else if (node.type === "database" && node.connectionId && node.database) {
const config = getConfig(node.connectionId);
if (config?.db_type && TREE_SCHEMA_TYPES.has(config.db_type)) {
if (config?.db_type === "sqlserver") {
await loadSqlServerDatabaseObjects(node.connectionId, node.database, options);
} else if (config?.db_type && TREE_SCHEMA_TYPES.has(config.db_type)) {
await loadSchemas(node.connectionId, node.database, options);
} else {
await loadTables(node.connectionId, node.database, undefined, options);
@@ -1112,6 +1139,7 @@ export const useConnectionStore = defineStore("connection", () => {
loadMongoDatabases,
loadMongoCollections,
loadSchemas,
loadSqlServerDatabaseObjects,
loadTables,
loadTableGroups,
loadColumns,