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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(mysql): preserve outer routine labels during parameter parsing
lcq928850b355c7fb9
diff --git a/apps/desktop/src/composables/__tests__/useSqlExecution.spec.ts b/apps/desktop/src/composables/__tests__/useSqlExecution.spec.tsindex 2b8db93d10..0aaab972aa 100644--- a/apps/desktop/src/composables/__tests__/useSqlExecution.spec.ts+++ b/apps/desktop/src/composables/__tests__/useSqlExecution.spec.ts@@ -181,6 +181,22 @@ describe("useSqlExecution", () => {expect(execute).toHaveBeenCalledWith(sql, { tabId: "tab-1" });});+ it("executes a MySQL procedure with a compact outer label without opening the parameter dialog (#13242)", async () => {+ const sql = "DROP PROCEDURE IF EXISTS p;\nCREATE DEFINER=`root`@`127.0.0.1` PROCEDURE p(IN in_username VARCHAR(200))\nrun:BEGIN IF in_username = '' THEN LEAVE run; END IF; SELECT in_username; END;";+ const activeTab = ref<QueryTab | undefined>({ ...queryTab("app"), sql });+ const activeConnection = ref<ConnectionConfig | undefined>(connection("mysql"));+ useSettingsStore().editorSettings.confirmDangerousSqlExecution = false;+ const execute = vi.spyOn(useQueryStore(), "executeCurrentSql").mockResolvedValue(undefined);+ vi.spyOn(useHistoryStore(), "add").mockResolvedValue(undefined);+ const execution = useSqlExecution({ activeTab: computed(() => activeTab.value), activeConnection: computed(() => activeConnection.value), executableSql: computed(() => sql), activeOutputView: ref("result") });++ await execution.tryExecute();++ expect(execution.showSqlParameterDialog.value).toBe(false);+ expect(execution.sqlParameterNames.value).toEqual([]);+ expect(execute).toHaveBeenCalledWith(sql, { tabId: "tab-1" });+ });+it("executes Neo4j graph patterns without a SQL parameter dialog", async () => {const sql = 'MATCH (p:Person)-[:WORK_IN]->(c:Company{name:"星云科技"})\nRETURN p.name, p.job, c.name';const activeTab = ref<QueryTab | undefined>({ ...queryTab("neo4j"), sql });diff --git a/apps/desktop/src/lib/__tests__/sql/sqlParameters.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlParameters.spec.tsindex c5c325b5f3..4bf84c9b6a 100644--- a/apps/desktop/src/lib/__tests__/sql/sqlParameters.spec.ts+++ b/apps/desktop/src/lib/__tests__/sql/sqlParameters.spec.ts@@ -1039,6 +1039,35 @@ describe("extractSqlParameters", () => {expect(extractSqlParameters("read_loop:LOOP")).toEqual(["LOOP"]);});+ it.each([+ "CREATE PROCEDURE p() run:BEGIN SELECT 1; END;",+ "DROP PROCEDURE IF EXISTS p; CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `p`(IN in_username VARCHAR(200))\nrun:BEGIN IF in_username = '' THEN LEAVE run; END IF; END;",+ "CREATE PROCEDURE `app`.`p`(IN amount DECIMAL(10, 2)) SQL SECURITY INVOKER COMMENT 'begin; :hidden' run:BEGIN SELECT amount; END;",+ "CREATE /* procedure header */ PROCEDURE p() /* body label */ `run`:BEGIN SELECT 1; END;",+ "CREATE FUNCTION f() RETURNS INT DETERMINISTIC run:BEGIN RETURN 1; END;",+ "CREATE FUNCTION f() RETURNS SET('a', 'b') DETERMINISTIC run:BEGIN RETURN 'a'; END;",+ "CREATE PROCEDURE p() open:BEGIN SELECT 1; END;",+ "CREATE PROCEDURE p() close:BEGIN SELECT 1; END;",+ "DELIMITER //\nCREATE PROCEDURE p() run:BEGIN SELECT 1; END//\nDELIMITER ;",+ ])("preserves compact outer MySQL routine labels (#13242): %s", (sql) => {+ expect(extractSqlParameters(sql, { databaseType: "mysql" })).toEqual([]);+ expect(substituteSqlParameters(sql, {}, { databaseType: "mysql" })).toBe(sql);+ });++ it.each([+ ["SELECT :BEGIN, :LOOP;", "SELECT 'x', 'y';"],+ ["CREATE PROCEDURE p() run:BEGIN SELECT :BEGIN, :LOOP; END;", "CREATE PROCEDURE p() run:BEGIN SELECT 'x', 'y'; END;"],+ ["CREATE PROCEDURE p() SELECT :BEGIN, :LOOP;", "CREATE PROCEDURE p() SELECT 'x', 'y';"],+ ["CREATE PROCEDURE p() SELECT:BEGIN, :LOOP;", "CREATE PROCEDURE p() SELECT'x', 'y';"],+ ["CREATE FUNCTION f() RETURNS VARCHAR(20) RETURN CONCAT(:BEGIN, :LOOP);", "CREATE FUNCTION f() RETURNS VARCHAR(20) RETURN CONCAT('x', 'y');"],+ ["CREATE PROCEDURE p() BEGIN SELECT 1; END; SELECT :BEGIN, :LOOP;", "CREATE PROCEDURE p() BEGIN SELECT 1; END; SELECT 'x', 'y';"],+ ["SELECT 'CREATE PROCEDURE p()' AS label, :BEGIN, :LOOP;", "SELECT 'CREATE PROCEDURE p()' AS label, 'x', 'y';"],+ ["/* CREATE PROCEDURE p() */ SELECT :BEGIN, :LOOP;", "/* CREATE PROCEDURE p() */ SELECT 'x', 'y';"],+ ])("keeps real MySQL parameters named after block keywords: %s", (sql, expected) => {+ expect(extractSqlParameters(sql, { databaseType: "mysql" })).toEqual(["BEGIN", "LOOP"]);+ expect(substituteSqlParameters(sql, { BEGIN: { kind: "string", value: "x" }, LOOP: { kind: "string", value: "y" } }, { databaseType: "mysql" })).toBe(expected);+ });+it("ignores HANA SQLScript variable references", () => {const sql = "DO BEGIN Dummy1 = SELECT 1 FROM DUMMY; SELECT * FROM :Dummy1; END";expect(extractSqlParameters(sql, { databaseType: "saphana" })).toEqual([]);diff --git a/apps/desktop/src/lib/sql/sqlParameters.ts b/apps/desktop/src/lib/sql/sqlParameters.tsindex 59389c39f1..0e1c12c972 100644--- a/apps/desktop/src/lib/sql/sqlParameters.ts+++ b/apps/desktop/src/lib/sql/sqlParameters.ts@@ -1,6 +1,7 @@import type { DatabaseType } from "@/types/database";import { collectSqlDatabaseLinkStarts } from "@/lib/sql/sqlDatabaseLinks";import { isOracleSubstitutionDatabase } from "@/lib/sql/sqlVariableSyntax";+import { tokenizeSqlSemantic } from "@/lib/sql/semantic/tokens";export type SqlParameterValueKind = "string" | "number" | "boolean" | "null" | "raw";@@ -70,6 +71,7 @@ const SQL_SERVER_TEMP_TABLE_CONTEXT_KEYWORDS = new Set(["table", "from", "join",const MYSQL_ROUTINE_LABEL_STATEMENTS = new Set(["begin", "loop", "while", "repeat"]);const MYSQL_ROUTINE_LABEL_CONTEXTS = new Set(["begin", "then", "else", "do", "loop", "repeat"]);const MYSQL_ROUTINE_LABEL_RE = /(?:`(?:``|[^`])+`|[\p{L}_][\p{L}\p{N}_]*)$/u;+const MYSQL_ROUTINE_BODY_STATEMENTS = new Set(["begin", "return", "select", "set", "insert", "update", "delete", "replace", "call", "if", "case", "loop", "while", "repeat", "declare", "do", "leave", "iterate", "signal", "resignal", "get", "fetch"]);const POSTGRES_QUESTION_PARAMETER_PREFIX_KEYWORDS = new Set(["all","and",@@ -600,7 +602,29 @@ function isMysqlRoutineLabelSeparator(sql: string, colonIndex: number, followingif (!label) return false;const labelStart = colonIndex - label[0].length;const prefix = sql.slice(0, labelStart).trimEnd();- return prefix.length === 0 || prefix.endsWith(";") || MYSQL_ROUTINE_LABEL_CONTEXTS.has(previousKeyword(sql, labelStart));+ if (prefix.length === 0 || prefix.endsWith(";") || MYSQL_ROUTINE_LABEL_CONTEXTS.has(previousKeyword(sql, labelStart))) return true;+ return followingName.toLowerCase() === "begin" && !MYSQL_ROUTINE_BODY_STATEMENTS.has(label[0].toLowerCase()) && isMysqlRoutineHeader(prefix);+}++function isMysqlRoutineHeader(sql: string): boolean {+ const tokens = tokenizeSqlSemantic(sql, "mysql", { mysqlBackslashEscape: true, mysqlDoubleQuoteIsString: true }).filter((token) => token.kind !== "comment");+ let start = tokens.length - 1;+ while (start >= 0 && tokens[start].text !== ";" && !(tokens[start].kind === "word" && tokens[start].normalized === "create")) start -= 1;+ if (start < 0 || tokens[start].normalized !== "create") return false;++ const header = tokens.slice(start + 1);+ const routineIndex = header.findIndex((token) => token.kind === "word" && token.depth === 0 && (token.normalized === "procedure" || token.normalized === "function"));+ if (routineIndex < 0 || (routineIndex !== 0 && header[0]?.normalized !== "definer")) return false;+ const parameterStart = header.findIndex((token, index) => index > routineIndex && token.text === "(");+ if (parameterStart < 0) return false;+ const parameterEnd = header.findIndex((token, index) => index > parameterStart && token.text === ")" && token.depth === 0);+ if (parameterEnd < 0) return false;++ // The outer label follows the signature and optional routine characteristics,+ // before any body statement. Quoted names/comments must not change that context.+ // The word after RETURNS is a data type, which can itself be SET.+ const characteristics = header.slice(parameterEnd + 1);+ return !characteristics.some((token, index) => token.kind === "word" && token.depth === 0 && MYSQL_ROUTINE_BODY_STATEMENTS.has(token.normalized) && characteristics[index - 1]?.normalized !== "returns");}function readMyBatisForeachAt(sql: string, start: number, databaseType?: DatabaseType): { collection: string; render: MyBatisForeach; end: number } | null {