Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(schema): normalize object names in browsers

t8y25cd9c9b7222a
diff --git a/apps/desktop/src/components/objects/ObjectBrowser.vue b/apps/desktop/src/components/objects/ObjectBrowser.vue
index 6a8f818c11..14f5b8aa3e 100644
--- a/apps/desktop/src/components/objects/ObjectBrowser.vue
+++ b/apps/desktop/src/components/objects/ObjectBrowser.vue
@@ -33,6 +33,7 @@ import * as api from "@/lib/api";
import type { ConnectionConfig, ObjectInfo, ObjectSourceKind } from "@/types/database";
import { isSchemaAware } from "@/lib/databaseCapabilities";
import { buildTableSelectSql, qualifiedTableName } from "@/lib/tableSelectSql";
+import { normalizeDatabaseObjectName } from "@/lib/tableTree";
import { useToast } from "@/composables/useToast";
import { buildExecutableObjectSourceSql, objectSourceSaveExecutionMode } from "@/lib/objectSourceEditor";
import { buildRenameObjectSql, supportsObjectRename } from "@/lib/objectRenameSql";
@@ -444,13 +445,20 @@ async function loadObjects() {
const schema = needsSchema.value ? selectedSchema.value || "" : props.database;
const objects: ObjectInfo[] = await api.listObjects(props.connection.id, props.database, schema);
if (id !== loadId) return;
- rows.value = objects.map((object) => ({
- id: `${object.schema || schema || props.database}:${object.name}:${object.object_type}`,
- name: object.name,
- schema: object.schema || (needsSchema.value ? schema : undefined),
- type: normalizeType(object.object_type),
- comment: object.comment,
- }));
+ rows.value = objects.flatMap((object) => {
+ const name = normalizeDatabaseObjectName(object.name);
+ if (!name) return [];
+ const objectSchema = object.schema ? normalizeDatabaseObjectName(object.schema) : undefined;
+ return [
+ {
+ id: `${objectSchema || schema || props.database}:${name}:${object.object_type}`,
+ name,
+ schema: objectSchema || (needsSchema.value ? schema : undefined),
+ type: normalizeType(object.object_type),
+ comment: object.comment,
+ },
+ ];
+ });
} catch (e: any) {
if (id !== loadId) return;
error.value = e?.message || String(e);
diff --git a/apps/desktop/src/lib/tableTree.ts b/apps/desktop/src/lib/tableTree.ts
index 1ec12253d2..5c437ccac5 100644
--- a/apps/desktop/src/lib/tableTree.ts
+++ b/apps/desktop/src/lib/tableTree.ts
@@ -1,5 +1,9 @@
import type { ObjectInfo, TableInfo, TreeNode, TreeNodeType } from "@/types/database";
+export function normalizeDatabaseObjectName(name: string): string {
+ return name.trim();
+}
+
export function buildTableTreeNodes({
nodeId,
connectionId,
@@ -13,16 +17,22 @@ export function buildTableTreeNodes({
schema?: string;
tables: TableInfo[];
}): TreeNode[] {
- return tables.map((table) => ({
- id: `${nodeId}:${table.name}`,
- label: table.name,
- type: table.table_type === "VIEW" ? "view" : "table",
- connectionId,
- database,
- schema,
- isExpanded: false,
- children: [],
- }));
+ return tables.flatMap((table) => {
+ const name = normalizeDatabaseObjectName(table.name);
+ if (!name) return [];
+ return [
+ {
+ id: `${nodeId}:${name}`,
+ label: name,
+ type: table.table_type === "VIEW" ? ("view" as const) : ("table" as const),
+ connectionId,
+ database,
+ schema,
+ isExpanded: false,
+ children: [],
+ },
+ ];
+ });
}
function normalizeObjectType(type: string): "TABLE" | "VIEW" | "PROCEDURE" | "FUNCTION" {
@@ -87,9 +97,11 @@ export function buildGroupedObjectTreeNodes({
}): TreeNode[] {
const buckets = new Map<string, ObjectInfo[]>();
for (const obj of objects) {
+ const name = normalizeDatabaseObjectName(obj.name);
+ if (!name) continue;
const t = normalizeObjectType(obj.object_type);
const arr = buckets.get(t) ?? [];
- arr.push(obj);
+ arr.push({ ...obj, name, schema: obj.schema ? normalizeDatabaseObjectName(obj.schema) : obj.schema });
buckets.set(t, arr);
}
diff --git a/packages/app-tests/tableTree.test.ts b/packages/app-tests/tableTree.test.ts
index d792b6afec..81d475183f 100644
--- a/packages/app-tests/tableTree.test.ts
+++ b/packages/app-tests/tableTree.test.ts
@@ -43,6 +43,24 @@ test("preserves table and view node types", () => {
);
});
+test("normalizes padded table names from database drivers", () => {
+ const nodes = buildTableTreeNodes({
+ nodeId: "conn:db:public",
+ connectionId: "conn",
+ database: "db",
+ schema: "public",
+ tables: [table(" users "), table("\norders\t"), table(" ")],
+ });
+
+ assert.deepEqual(
+ nodes.map((node) => [node.id, node.label]),
+ [
+ ["conn:db:public:users", "users"],
+ ["conn:db:public:orders", "orders"],
+ ],
+ );
+});
+
test("expands cached object-browser nodes back into regular table nodes", () => {
const nodes = expandCachedObjectBrowserNodes([
{