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): prevent double-click selecting entire multi-line procedure body (#12966) (#12993)

lxk9556bfd08f0589f
diff --git a/apps/desktop/src/lib/__tests__/editor/queryEditorStringMouseSelection.spec.ts b/apps/desktop/src/lib/__tests__/editor/queryEditorStringMouseSelection.spec.ts
index 5eda2c5bff..1cf9650f0f 100644
--- a/apps/desktop/src/lib/__tests__/editor/queryEditorStringMouseSelection.spec.ts
+++ b/apps/desktop/src/lib/__tests__/editor/queryEditorStringMouseSelection.spec.ts
@@ -1,5 +1,5 @@
import { EditorSelection, EditorState } from "@codemirror/state";
-import type { EditorView, ViewUpdate } from "@codemirror/view";
+import type { EditorView } from "@codemirror/view";
import { describe, expect, it } from "vitest";
import { createQueryEditorStringMouseSelection } from "@/lib/editor/queryEditorStringMouseSelection";
import { sqlStringContentRangeAt, trimSqlStringWildcardBoundaries, type SqlSemanticSelectionOptions } from "@/lib/editor/sqlSemanticSelectionRanges";
@@ -184,4 +184,51 @@ describe("query editor SQL string mouse selection", () => {
// A range with no boundary wildcards is returned unchanged.
expect(trimSqlStringWildcardBoundaries("abc", { from: 0, to: 3 })).toEqual({ from: 0, to: 3 });
});
+
+ it("does not treat PostgreSQL stored procedure or function bodies as string literal selections (#12966)", () => {
+ const procedure = ["CREATE OR REPLACE PROCEDURE public.process_orders(p_status text)", " LANGUAGE plpgsql", "AS $procedure$", "BEGIN", " UPDATE orders SET status = p_status WHERE active = true;", " SELECT * FROM my_table;", "END;", "$procedure$;"].join("\n");
+
+ const state = EditorState.create({ doc: procedure });
+ const view = fakeView(state).view;
+
+ // Double clicking on table name, variable, or statement inside the procedure body should not select the entire body.
+ for (const needle of ["my_table", "orders", "p_status", "UPDATE", "SELECT"]) {
+ const pos = procedure.indexOf(needle);
+ expect(contentAt(procedure, pos, 1, { dialect: "postgres" })).toBeNull();
+ expect(createQueryEditorStringMouseSelection(view, eventAt(pos), { dialect: "postgres" })).toBeNull();
+ }
+ });
+
+ it("does not select procedural bodies in single-line procedures, functions, DO blocks, or multi-line strings (#12966)", () => {
+ const cases: Array<{ doc: string; needle: string; options?: SqlSemanticSelectionOptions }> = [
+ {
+ doc: "CREATE PROCEDURE clear_logs() LANGUAGE sql AS $$ DELETE FROM app_logs; $$;",
+ needle: "app_logs",
+ options: { dialect: "postgres" },
+ },
+ {
+ doc: "CREATE OR REPLACE FUNCTION get_user_count() RETURNS int LANGUAGE sql AS $$ SELECT count(*) FROM users; $$;",
+ needle: "users",
+ options: { dialect: "postgres" },
+ },
+ {
+ doc: "DO $$\nBEGIN\n PERFORM cleanup_temp_tables();\nEND;\n$$;",
+ needle: "cleanup_temp_tables",
+ options: { dialect: "postgres" },
+ },
+ {
+ doc: "SELECT 'first line\nsecond line with my_table\nthird line';",
+ needle: "my_table",
+ options: { dialect: "postgres" },
+ },
+ ];
+
+ for (const { doc, needle, options } of cases) {
+ const pos = doc.indexOf(needle);
+ const state = EditorState.create({ doc });
+ const view = fakeView(state).view;
+ expect(contentAt(doc, pos, 1, options)).toBeNull();
+ expect(createQueryEditorStringMouseSelection(view, eventAt(pos), options)).toBeNull();
+ }
+ });
});
diff --git a/apps/desktop/src/lib/editor/sqlSemanticSelectionRanges.ts b/apps/desktop/src/lib/editor/sqlSemanticSelectionRanges.ts
index abc6a857eb..4c317a3906 100644
--- a/apps/desktop/src/lib/editor/sqlSemanticSelectionRanges.ts
+++ b/apps/desktop/src/lib/editor/sqlSemanticSelectionRanges.ts
@@ -384,8 +384,42 @@ function collectSqlRanges(input: string, statement: StatementWindow, maps: { ope
return ranges;
}
-function stringContentRange(token: SqlSelectionToken): SemanticSelectionRange | null {
+function isRoutineStatement(statement: StatementWindow): boolean {
+ const words = statement.tokens.filter((t) => t.kind === "word").map((t) => t.normalized);
+ if (words.length === 0) return false;
+ if (words[0] === "do") return true;
+ if (words[0] === "create" || words[0] === "alter") {
+ const prefix = words.slice(1, 6);
+ return prefix.some((word) => word === "procedure" || word === "proc" || word === "function" || word === "trigger" || word === "package");
+ }
+ return false;
+}
+
+function isRoutineBodyToken(token: SqlSelectionToken, statement: StatementWindow): boolean {
+ const index = statement.tokens.indexOf(token);
+ if (index > 0 && statement.tokens[index - 1]?.normalized === "as") {
+ return true;
+ }
+ return false;
+}
+
+function stringContentRange(token: SqlSelectionToken, statement?: PreparedStatementWindow): SemanticSelectionRange | null {
if (token.kind !== "string") return null;
+
+ // Multi-line strings (such as procedure/function bodies, long text blocks, or template strings)
+ // should never select the entire multi-line block on double-click; fall back to word selection (#12966).
+ if (token.text.includes("\n") || token.text.includes("\r")) return null;
+
+ // Dollar-quoted strings containing statement terminators (;) are procedural/SQL bodies rather than
+ // scalar string literals; allow normal word selection inside them (#12966).
+ if (token.quote?.startsWith("$") && token.text.includes(";")) return null;
+
+ // Inside routine definitions (CREATE/ALTER PROCEDURE, FUNCTION, TRIGGER, PACKAGE, or DO blocks),
+ // dollar-quoted or body string tokens represent code rather than scalar string values (#12966).
+ if (statement && isRoutineStatement(statement) && (token.quote?.startsWith("$") || isRoutineBodyToken(token, statement))) {
+ return null;
+ }
+
const quote = token.quote ?? token.text[0] ?? "";
const quoteLength = quote.startsWith("$") ? quote.length : 1;
const from = token.from + quoteLength;
@@ -402,7 +436,7 @@ export function analyzeSqlSemanticSelectionRanges(doc: string, options: SqlSeman
const statementValidationTokens = validationTokens.filter((token) => token.from >= statement.start && token.to <= statement.end);
return validateTokens(doc.slice(statement.start, statement.end), statementValidationTokens);
});
- const stringContents = statements.flatMap((statement) => statement.tokens.map(stringContentRange).filter((range): range is SemanticSelectionRange => range != null));
+ const stringContents = statements.flatMap((statement) => statement.tokens.map((token) => stringContentRange(token, statement)).filter((range): range is SemanticSelectionRange => range != null));
return { doc, statements, stringContents };
}