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(editor): resolve quoted identifiers in table previews

Melody Ruch0b5c0bdcc000
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue
index 0a202e2dff..03bf01c212 100644
--- a/apps/desktop/src/components/editor/QueryEditor.vue
+++ b/apps/desktop/src/components/editor/QueryEditor.vue
@@ -66,6 +66,7 @@ import {
import { usesOracleSessionCompletionColumns as shouldUseOracleSessionCompletionColumns } from "@/lib/sql/oracleCompletionSession";
import {
extractIdentifierDetailsAt,
+ extractQualifiedIdentifierAt,
isSqlKeyword,
matchSqlObject,
matchTable,
@@ -79,7 +80,7 @@ import {
sqlObjectNavigationTypeFromCompletionObjectType,
type SqlObjectNavigationTarget,
} from "@/lib/sql/sqlNavigation";
-import { buildHoverTableSql, ddlForHoverPreview, hoverTableMatchesScope, quoteQualifiedName, reformatHoverDdl, scopeHoverTables, type HoverTableScope } from "@/lib/editor/hoverTableSql";
+import { buildHoverTableSql, ddlForHoverPreview, hoverTableMatchesScope, quoteIdentifier, quoteQualifiedName, reformatHoverDdl, scopeHoverTables, type HoverTableScope } from "@/lib/editor/hoverTableSql";
import { constrainSqlHoverLayout } from "@/lib/editor/sqlHoverLayout";
import { lineColumnToOffset, sqlErrorDecorationRange as resolveSqlErrorDecorationRange } from "@/lib/sql/sqlDiagnostics";
import {
@@ -1986,18 +1987,23 @@ function sqlExecutionSnapshotForRange(currentView: EditorViewType, range: Pick<S
};
}
+/**
+ * Locate the qualified identifier at `pos`, delegating to the same quote-aware
+ * parser used by Ctrl+click navigation. A plain word-character scan (the
+ * previous approach here) breaks on quoted identifiers containing characters
+ * outside `[\w$]` (hyphens, spaces, ...), e.g. `schema."my-table"`.
+ *
+ * Every part is re-quoted in the returned text (regardless of whether it was
+ * originally quoted) so downstream re-parsing via `splitQualifiedIdentifier`
+ * round-trips correctly even when a part's raw value isn't a bare word.
+ */
function identifierRangeAt(sql: string, pos: number): { from: number; to: number; text: string } | null {
- const isIdentifierChar = (ch: string | undefined) => !!ch && /[\w$.]/.test(ch);
- if (!isIdentifierChar(sql[pos]) && !isIdentifierChar(sql[pos - 1])) return null;
-
- let from = pos;
- while (from > 0 && isIdentifierChar(sql[from - 1])) from--;
- let to = pos;
- while (to < sql.length && isIdentifierChar(sql[to])) to++;
-
- const text = sql.slice(from, to).replace(/^\.+|\.+$/g, "");
- if (!text || isSqlKeyword(text)) return null;
- return { from, to, text };
+ const located = extractQualifiedIdentifierAt(sql, pos);
+ if (!located) return null;
+ if (located.parts.length === 1 && !located.parts[0].quoted && isSqlKeyword(located.parts[0].value)) return null;
+ const text = located.parts.map((part) => quoteIdentifier(part.value)).join(".");
+ if (!text) return null;
+ return { from: located.start, to: located.end, text };
}
type CompletionMetadataScope = Pick<SqlCompletionScope, "database" | "schema">;
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlNavigation.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlNavigation.spec.ts
index 97c72a61ab..5ba7290f21 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlNavigation.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlNavigation.spec.ts
@@ -1,7 +1,9 @@
import { describe, expect, it } from "vitest";
+import { quoteIdentifier } from "@/lib/editor/hoverTableSql";
import {
extractIdentifierAt,
extractIdentifierDetailsAt,
+ extractQualifiedIdentifierAt,
isSqlCallSiteIdentifierAt,
isSqlKeyword,
isSqlObjectNavigationRoutineType,
@@ -75,6 +77,25 @@ describe("splitQualifiedIdentifier", () => {
});
});
+describe("extractQualifiedIdentifierAt", () => {
+ it("keeps the full quoted segment when it contains a hyphen", () => {
+ const sql = 'select * from log."public-rate_kingdee"';
+
+ const located = extractQualifiedIdentifierAt(sql, sql.indexOf("rate_kingdee"));
+
+ expect(located?.parts.map((part) => part.value)).toEqual(["log", "public-rate_kingdee"]);
+ });
+
+ it("re-quoting each part before rejoining survives a splitQualifiedIdentifier round trip", () => {
+ const sql = 'select * from log."public-rate_kingdee"';
+ const located = extractQualifiedIdentifierAt(sql, sql.indexOf("rate_kingdee"));
+
+ const requoted = located!.parts.map((part) => quoteIdentifier(part.value)).join(".");
+
+ expect(splitQualifiedIdentifier(requoted)).toEqual(["log", "public-rate_kingdee"]);
+ });
+});
+
describe("matchTable", () => {
it("matches schema-qualified table identifiers", () => {
const table = { schema: "MAAC00", name: "Accounts", type: "view" as const };