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(sql): keep query after inline explain separate (#12811) (#12824)
lxk955a6275be47fb8
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.tsindex bf9303ad02..c8a2c0dc37 100644--- a/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts+++ b/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts@@ -1592,6 +1592,44 @@ AND ts = toDateTime('2026-07-15 12:00:00');`;expect(rangeSqlTexts(executableStatementRanges(sql, "postgres"))).toEqual([expected, "SELECT 2"]);});+ it("does not merge a query after an inline EXPLAIN statement without semicolons (#12811)", () => {+ const sql = "SELECT 1\nEXPLAIN SELECT 2\nSELECT 3";+ expect(statementRangeAtCursor(sql, indexOf(sql, "EXPLAIN"), "postgres")?.sql.trim()).toBe("EXPLAIN SELECT 2");+ expect(rangeSqlTexts(executableStatementRanges(sql, "postgres"))).toEqual(["SELECT 1", "EXPLAIN SELECT 2", "SELECT 3"]);+ });++ it("does not merge a query after an inline EXPLAIN ANALYZE statement (#12811)", () => {+ const sql = "SELECT 1\nEXPLAIN ANALYZE SELECT 2\nSELECT 3";+ expect(statementRangeAtCursor(sql, indexOf(sql, "EXPLAIN"), "postgres")?.sql.trim()).toBe("EXPLAIN ANALYZE SELECT 2");+ expect(rangeSqlTexts(executableStatementRanges(sql, "postgres"))).toEqual(["SELECT 1", "EXPLAIN ANALYZE SELECT 2", "SELECT 3"]);+ });++ it("does not merge a query after an inline EXPLAIN with parenthesized options (#12811)", () => {+ const sql = "EXPLAIN (ANALYZE, BUFFERS) SELECT 1\nSELECT 2";+ expect(statementRangeAtCursor(sql, indexOf(sql, "EXPLAIN"), "postgres")?.sql.trim()).toBe("EXPLAIN (ANALYZE, BUFFERS) SELECT 1");+ expect(rangeSqlTexts(executableStatementRanges(sql, "postgres"))).toEqual(["EXPLAIN (ANALYZE, BUFFERS) SELECT 1", "SELECT 2"]);+ });++ it("does not merge a query after MySQL EXPLAIN with options", () => {+ const sql = "EXPLAIN FORMAT = JSON SELECT 1\nSELECT 2";+ expect(statementRangeAtCursor(sql, indexOf(sql, "EXPLAIN"), "mysql")?.sql.trim()).toBe("EXPLAIN FORMAT = JSON SELECT 1");+ expect(rangeSqlTexts(executableStatementRanges(sql, "mysql"))).toEqual(["EXPLAIN FORMAT = JSON SELECT 1", "SELECT 2"]);+ });++ it("does not merge a query after a plain MySQL EXPLAIN table statement", () => {+ const sql = "EXPLAIN users\nSELECT * FROM users;";+ expect(statementRangeAtCursor(sql, indexOf(sql, "EXPLAIN"), "mysql")?.sql.trim()).toBe("EXPLAIN users");+ expect(statementRangeAtCursor(sql, indexOf(sql, "SELECT"), "mysql")?.sql.trim()).toBe("SELECT * FROM users");+ expect(rangeSqlTexts(executableStatementRanges(sql, "mysql"))).toEqual(["EXPLAIN users", "SELECT * FROM users"]);+ });++ it("does not merge a query after an EXPLAIN UPDATE statement", () => {+ const sql = "EXPLAIN UPDATE test_orders a\nJOIN test_users b ON a.id=b.id\nSET a.name = '张三'\nWHERE b.id > 10\nSELECT 1";+ const expectedExplain = "EXPLAIN UPDATE test_orders a\nJOIN test_users b ON a.id=b.id\nSET a.name = '张三'\nWHERE b.id > 10";+ expect(statementRangeAtCursor(sql, indexOf(sql, "EXPLAIN"), "mysql")?.sql.trim()).toBe(expectedExplain);+ expect(rangeSqlTexts(executableStatementRanges(sql, "mysql"))).toEqual([expectedExplain, "SELECT 1"]);+ });+it("keeps EXPLAIN options CTE UPDATE assignments as one statement", () => {const sql = "EXPLAIN (ANALYZE)\nWITH tmp AS (SELECT 1)\nUPDATE t\nSET x = 1;";diff --git a/apps/desktop/src/lib/sql/sqlStatementRanges.ts b/apps/desktop/src/lib/sql/sqlStatementRanges.tsindex 8f7f696aae..3484eae4a3 100644--- a/apps/desktop/src/lib/sql/sqlStatementRanges.ts+++ b/apps/desktop/src/lib/sql/sqlStatementRanges.ts@@ -284,7 +284,46 @@ const DATABASE_SOFT_STATEMENT_KEYWORDS: Partial<Record<DatabaseType, readonly st};const WITH_MAIN_STATEMENT_KEYWORDS = new Set(["SELECT", "INSERT", "UPDATE", "DELETE", "MERGE"]);-const EXPLAIN_STATEMENT_KEYWORDS = new Set(["SELECT", "WITH", "INSERT", "UPDATE", "DELETE", "MERGE", "CREATE", "ALTER", "DROP"]);+const EXPLAIN_STATEMENT_KEYWORDS = new Set(["SELECT", "WITH", "INSERT", "UPDATE", "DELETE", "MERGE", "CREATE", "ALTER", "DROP", "VALUES", "EXECUTE"]);+const EXPLAIN_OPTION_KEYWORDS = new Set([+ "ANALYZE",+ "ANALYSE",+ "VERBOSE",+ "COSTS",+ "BUFFERS",+ "TIMING",+ "SUMMARY",+ "FORMAT",+ "SETTINGS",+ "GENERIC_PLAN",+ "WAL",+ "MEMORY",+ "SERIALIZE",+ "TEXT",+ "XML",+ "JSON",+ "YAML",+ "TREE",+ "TRADITIONAL",+ "EXTENDED",+ "PARTITIONS",+ "QUERY",+ "PLAN",+ "FOR",+ "SET",+ "STATEMENT_ID",+ "INTO",+ "AST",+ "SYNTAX",+ "PIPELINE",+ "ESTIMATE",+ "TABLE",+ "OVERRIDE",+ "TRUE",+ "FALSE",+ "ON",+ "OFF",+]);const CREATE_BODY_KEYWORDS = new Set(["SELECT", "WITH", "BEGIN", "DECLARE"]);const STARROCKS_CREATE_MATERIALIZED_VIEW_REFRESH_MODIFIERS = new Set(["ASYNC", "MANUAL", "SCHEDULE", "DEFERRED", "IMMEDIATE"]);const INSERT_BODY_KEYWORDS = new Set(["SELECT", "WITH"]);@@ -824,10 +863,11 @@ function splitStatementRangeAtSoftStarts(sql: string, statement: RawStatement, dconst boundaries: Array<{ hitFrom: number; from: number; keyword: string }> = [];let currentKeyword = softStatementKeywordAt(sql, statement.from, databaseType, parameterOptions);- let currentExplainTargetKeyword = explainLikeTargetKeywordAt(sql, statement.from, databaseType, parameterOptions);+ let currentExplain = parseExplainLikePrefixAt(sql, statement.from, databaseType, parameterOptions);+ let currentExplainTargetKeyword = currentExplain.targetKeyword;let currentBodyKeyword = currentExplainTargetKeyword ?? currentKeyword;let consumedWithMainStatement = false;- let consumedExplainStatement = false;+ let consumedExplainStatement = currentExplainTargetKeyword !== null || (currentKeyword === "EXPLAIN" && !currentExplain.isPendingPrefix);boundaries.push({ hitFrom: statement.hitFrom, from: statement.from, keyword: currentKeyword ?? "" });@@ -897,10 +937,11 @@ function splitStatementRangeAtSoftStarts(sql: string, statement: RawStatement, dboundaries.push(lineStart);currentKeyword = lineStart.keyword;- currentExplainTargetKeyword = explainLikeTargetKeywordAt(sql, lineStart.from, databaseType, parameterOptions);+ currentExplain = parseExplainLikePrefixAt(sql, lineStart.from, databaseType, parameterOptions);+ currentExplainTargetKeyword = currentExplain.targetKeyword;currentBodyKeyword = currentExplainTargetKeyword ?? currentKeyword;consumedWithMainStatement = false;- consumedExplainStatement = false;+ consumedExplainStatement = currentExplainTargetKeyword !== null || (currentKeyword === "EXPLAIN" && !currentExplain.isPendingPrefix);}if (boundaries.length <= 1) return [statement];@@ -1588,25 +1629,103 @@ function explainOptionsParenAt(sql: string, pos: number): number | null {return sql[i] === "(" ? i : null;}-function explainLikeTargetKeywordAt(sql: string, pos: number, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): string | null {+function parseExplainLikePrefixAt(sql: string, pos: number, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): { targetKeyword: string | null; isPendingPrefix: boolean } {const prefixMatch = /^[A-Za-z_][\w$]*/.exec(sql.slice(pos));const prefix = prefixMatch?.[0]?.toUpperCase();- if (!isExplainLikeKeyword(prefix ?? null)) return null;+ if (!isExplainLikeKeyword(prefix ?? null)) {+ return { targetKeyword: null, isPendingPrefix: false };+ }let i = pos + (prefixMatch?.[0].length ?? 0);- while (i < sql.length && isSqlWhitespace(sql[i])) i += 1;- // Parenthesized EXPLAIN options (e.g. Postgres `EXPLAIN (ANALYZE, BUFFERS) ...`)- // sit between the keyword and its target statement. DESC/DESCRIBE take no- // options — a paren there is a subquery (ClickHouse `DESCRIBE (SELECT ...)`).- if (prefix === "EXPLAIN" && sql[i] === "(") {- const optionsEnd = skipBalancedParens(sql, i, databaseType, parameterOptions);- if (optionsEnd === null) return null;- i = optionsEnd;- while (i < sql.length && isSqlWhitespace(sql[i])) i += 1;++ const skipInlineWhitespaceAndComments = (): boolean => {+ while (i < sql.length) {+ if (sql[i] === " " || sql[i] === "\t") {+ i += 1;+ continue;+ }+ if (startsBlockComment(sql, i)) {+ const commentEnd = sql.indexOf("*/", i + 2);+ if (commentEnd === -1) return false;+ i = commentEnd + 2;+ continue;+ }+ break;+ }+ return true;+ };++ if (!skipInlineWhitespaceAndComments()) {+ return { targetKeyword: null, isPendingPrefix: false };+ }++ // DESC and DESCRIBE take no options; the very next token is either the target+ // statement (e.g. `DESC UPDATE ...`) or a table/subquery (e.g. `DESC users`).+ if (prefix !== "EXPLAIN") {+ const targetMatch = /^[A-Za-z_][\w$]*/.exec(sql.slice(i));+ const targetKeyword = targetMatch?.[0]?.toUpperCase();+ return {+ targetKeyword: targetKeyword && EXPLAIN_STATEMENT_KEYWORDS.has(targetKeyword) ? targetKeyword : null,+ isPendingPrefix: false,+ };}- const targetMatch = /^[A-Za-z_][\w$]*/.exec(sql.slice(i));- const targetKeyword = targetMatch?.[0]?.toUpperCase();- return targetKeyword && EXPLAIN_STATEMENT_KEYWORDS.has(targetKeyword) ? targetKeyword : null;++ // Scan EXPLAIN options and possible inline target statement keyword on the current line+ while (i < sql.length) {+ if (sql[i] === "\n" || sql[i] === "\r" || sql[i] === ";") break;+ if (startsLineComment(sql, i, databaseType, parameterOptions)) break;++ if (sql[i] === "(") {+ const optionsEnd = skipBalancedParens(sql, i, databaseType, parameterOptions);+ if (optionsEnd === null) return { targetKeyword: null, isPendingPrefix: true };+ i = optionsEnd;+ if (!skipInlineWhitespaceAndComments()) return { targetKeyword: null, isPendingPrefix: false };+ continue;+ }++ if (sql[i] === "'" || sql[i] === '"') {+ const quote = sql[i];+ i += 1;+ while (i < sql.length && sql[i] !== quote && sql[i] !== "\n") i += 1;+ if (i < sql.length && sql[i] === quote) i += 1;+ if (!skipInlineWhitespaceAndComments()) return { targetKeyword: null, isPendingPrefix: false };+ continue;+ }++ if (sql[i] === "=" || sql[i] === ",") {+ i += 1;+ if (!skipInlineWhitespaceAndComments()) return { targetKeyword: null, isPendingPrefix: false };+ continue;+ }++ const digitMatch = /^\d+/.exec(sql.slice(i));+ if (digitMatch) {+ i += digitMatch[0].length;+ if (!skipInlineWhitespaceAndComments()) return { targetKeyword: null, isPendingPrefix: false };+ continue;+ }++ const tokenMatch = /^[A-Za-z_][\w$]*/.exec(sql.slice(i));+ if (!tokenMatch) {+ i += 1;+ continue;+ }++ const token = tokenMatch[0].toUpperCase();+ if (EXPLAIN_STATEMENT_KEYWORDS.has(token)) {+ return { targetKeyword: token, isPendingPrefix: false };+ }++ if (EXPLAIN_OPTION_KEYWORDS.has(token)) {+ i += tokenMatch[0].length;+ if (!skipInlineWhitespaceAndComments()) return { targetKeyword: null, isPendingPrefix: false };+ continue;+ }++ return { targetKeyword: null, isPendingPrefix: false };+ }++ return { targetKeyword: null, isPendingPrefix: true };}function skipBalancedParens(sql: string, pos: number, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): number | null {