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): preserve Kingbase mode boundaries and hash operators (#12828)
Hand Sonicb8de0cab90f6
diff --git a/apps/desktop/src/lib/__tests__/sql/executableStatementRangeCache.spec.ts b/apps/desktop/src/lib/__tests__/sql/executableStatementRangeCache.spec.tsindex 6fe7c4f52e..b969becf0e 100644--- a/apps/desktop/src/lib/__tests__/sql/executableStatementRangeCache.spec.ts+++ b/apps/desktop/src/lib/__tests__/sql/executableStatementRangeCache.spec.ts@@ -115,6 +115,32 @@ describe("executableStatementRangeCacheForDoc", () => {const cache = executableStatementRangeCacheForDoc(null, doc, "sqlserver");expect(executableStatementRangeStartingAt(cache, doc.line(2).from)?.sql).toBe(sql);+ expect(executableStatementRangeAtCursor(cache, doc.line(2).from + 2)?.sql).toBe(sql);+ });++ it("keeps Kingbase SQLServer temporary tables executable at the cursor and gutter", () => {+ const sql = "/*+ hint */\nSELECT\n#temporary_table.id\nFROM #temporary_table";+ const doc = Text.of(sql.split("\n"));+ const cache = executableStatementRangeCacheForDoc(null, doc, "kingbase", { compatibilityMode: "sqlserver" });+ expect(executableStatementRangeStartingAt(cache, doc.line(2).from)?.sql).toBe(sql);+ expect(executableStatementRangeAtCursor(cache, doc.line(3).from + 2)?.sql).toBe(sql);+ });++ it("resolves the SQL gutter after a Kingbase MySQL proxy directive", () => {+ const sql = "/*proxy*/\nSELECT 1;";+ const doc = Text.of(sql.split("\n"));+ const cache = executableStatementRangeCacheForDoc(null, doc, "kingbase", { compatibilityMode: "mysql" });+ expect(executableStatementRangeStartingAt(cache, doc.line(2).from)?.sql).toBe(sql.slice(0, -1));+ });++ it("preserves PG JSON path operators in statements and cursor ranges", () => {+ const statement = `SELECT '{"a":{"b":7}}'::jsonb\n#>> '{a,b}' AS value`;+ const doc = Text.of(`${statement};\nSELECT 2;`.split("\n"));+ for (const type of ["kingbase", "postgres"] as const) {+ const cache = executableStatementRangeCacheForDoc(null, doc, type, { compatibilityMode: "pg" });+ expect(cache.ranges.map((range) => range.sql)).toEqual([statement, "SELECT 2"]);+ expect(executableStatementRangeAtCursor(cache, doc.line(2).from + 2)?.sql).toBe(statement);+ }});it("does not attach ordinary leading block comments to gutter execution", () => {diff --git a/apps/desktop/src/lib/__tests__/sql/kingbaseStatementRanges.spec.ts b/apps/desktop/src/lib/__tests__/sql/kingbaseStatementRanges.spec.tsnew file mode 100644index 0000000000..7b95f30b6f--- /dev/null+++ b/apps/desktop/src/lib/__tests__/sql/kingbaseStatementRanges.spec.ts@@ -0,0 +1,28 @@+import { describe, expect, it } from "vitest";+import { executableStatementRanges, statementRangeAtCursor } from "@/lib/sql/sqlStatementRanges";+import type { DatabaseType } from "@/types/database";++describe("Kingbase compatibility statement boundaries", () => {+ it("uses Oracle alternative quotes without retaining the unresolved-mode hash-comment lexer", () => {+ const sql = "BEGIN\n v := q'[O'Reilly; text]';\n NULL;\nEND;\n/\nSELECT value#suffix FROM t;\nSELECT 2;";+ expect(executableStatementRanges(sql, "kingbase", { compatibilityMode: "oracle" })).toEqual(executableStatementRanges(sql, "gaussdb"));+ });++ it.each([undefined, "future_mode"])("preserves recognizable PL/SQL blocks while the mode is unresolved (%s)", (compatibilityMode) => {+ const sql = "BEGIN\n NULL;\n NULL;\nEND;\n/\nSELECT 1;";+ expect(executableStatementRanges(sql, "kingbase", { compatibilityMode })).toHaveLength(2);+ expect(executableStatementRanges(sql, "postgres", { compatibilityMode: "oracle" })).toEqual(executableStatementRanges(sql, "postgres"));+ });++ it.each([+ ["oracle", "gaussdb", "BEGIN\n NULL;\n NULL;\nEND;\n/\nSELECT 1;"],+ ["mysql", "mysql", "DELIMITER $$\nCREATE PROCEDURE p() BEGIN SELECT 'a;'; SELECT 2; END$$\nDELIMITER ;\nSELECT 3;"],+ ["pg", "postgres", "CREATE FUNCTION f() RETURNS int AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql; SELECT 2;"],+ ["sqlserver", "sqlserver", "CREATE PROCEDURE p AS\nBEGIN\n SELECT 1;\n SELECT 2;\nEND\nGO\nSELECT 3;"],+ ] as const)("matches the %s parsing profile for cursor and execution ranges", (mode, type, sql) => {+ const options = { compatibilityMode: mode };+ expect(executableStatementRanges(sql, "kingbase", options)).toEqual(executableStatementRanges(sql, type as DatabaseType));+ const cursor = sql.indexOf("SELECT");+ expect(statementRangeAtCursor(sql, cursor, "kingbase", options)).toEqual(statementRangeAtCursor(sql, cursor, type as DatabaseType));+ });+});diff --git a/apps/desktop/src/lib/sql/executableStatementRangeCache.ts b/apps/desktop/src/lib/sql/executableStatementRangeCache.tsindex fe08fe7bc4..e3fa75eb5d 100644--- a/apps/desktop/src/lib/sql/executableStatementRangeCache.ts+++ b/apps/desktop/src/lib/sql/executableStatementRangeCache.ts@@ -1,7 +1,7 @@import { RangeSet, RangeValue, type ChangeSet, type Text } from "@codemirror/state";import type { DatabaseType } from "@/types/database";-import { readSqlBracedParameterAt, type SqlParameterOptions } from "@/lib/sql/sqlParameters";-import { executableStatementRanges, type SqlTextRange } from "@/lib/sql/sqlStatementRanges";+import type { SqlParameterOptions } from "@/lib/sql/sqlParameters";+import { executableStatementRanges, startsHashLineComment, statementParsingDatabaseType, type SqlTextRange } from "@/lib/sql/sqlStatementRanges";import { cursorBelongsToTrailingStatementDelimiter } from "@/lib/sql/statementDelimiter";export interface ExecutableStatementRangeCache {@@ -93,6 +93,7 @@ export function executableStatementRangeCacheForDoc(}function executableStartAfterLeadingDirective(sql: string, range: SqlTextRange, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): number | null {+ databaseType = statementParsingDatabaseType(databaseType, parameterOptions);const text = range.sql;const hasExecutableDirective = text.startsWith("/*+") || text.startsWith("/*@") || text.startsWith("/*&") || (databaseType === "mysql" && text.startsWith("/*proxy*/"));if (!hasExecutableDirective) return null;@@ -115,7 +116,7 @@ function executableStartAfterLeadingDirective(sql: string, range: SqlTextRange,continue;}- if (databaseType !== "sqlserver" && text[offset] === "#" && readSqlBracedParameterAt(sql, range.from + offset, parameterOptions)?.syntax !== "mybatis") {+ if (startsHashLineComment(sql, range.from + offset, databaseType, parameterOptions)) {const newline = text.indexOf("\n", offset + 1);if (newline < 0) return null;offset = newline + 1;@@ -137,7 +138,7 @@ export function executableStatementRangeAtCursor(cache: ExecutableStatementRangeconst line = cache.doc.lineAt(pos);const lineText = line.text.trim();const lineContentStart = line.from + line.text.search(/\S|$/);- const startsHashComment = lineText.startsWith("#") && readSqlBracedParameterAt(cache.doc.toString(), lineContentStart, cache.parameterOptions)?.syntax !== "mybatis";+ const startsHashComment = lineText.startsWith("#") && startsHashLineComment(cache.doc.toString(), lineContentStart, statementParsingDatabaseType(cache.databaseType, cache.parameterOptions), cache.parameterOptions);if (!lineText || lineText.startsWith("--") || startsHashComment || isCursorOnLeadingBlockComment(line.text, pos - line.from)) return null;for (let index = 0; index < cache.ranges.length; index += 1) {diff --git a/apps/desktop/src/lib/sql/sqlStatementRanges.ts b/apps/desktop/src/lib/sql/sqlStatementRanges.tsindex 3484eae4a3..fe84155db1 100644--- a/apps/desktop/src/lib/sql/sqlStatementRanges.ts+++ b/apps/desktop/src/lib/sql/sqlStatementRanges.ts@@ -409,6 +409,7 @@ function isAsciiAlphanumeric(ch: string): boolean {* whitespace are excluded so editor highlights stay tight).*/export function splitSqlStatementRanges(sql: string, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): RawStatement[] {+ databaseType = statementParsingDatabaseType(databaseType, parameterOptions);if (isHttpJsonRestDatabaseType(databaseType)) {const requests = splitElasticsearchRestRequestRanges(sql);if (requests) return requests;@@ -758,6 +759,7 @@ export function splitSqlStatementRanges(sql: string, databaseType?: DatabaseType* which lets the editor highlight a tight preview range.*/export function statementRangeAtCursor(sql: string, cursorPos: number, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): SqlTextRange | null {+ databaseType = statementParsingDatabaseType(databaseType, parameterOptions);const pos = clampCursor(sql, cursorPos);if (isCursorOnBlankLine(sql, pos)) return null;@@ -1819,11 +1821,9 @@ function startsLineComment(sql: string, pos: number, databaseType?: DatabaseTypereturn (sql[pos] === "-" && sql[pos + 1] === "-") || startsHashLineComment(sql, pos, databaseType, parameterOptions);}-function startsHashLineComment(sql: string, pos: number, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): boolean {- // `#` is a MySQL-family line-comment marker. Oracle-family engines also allow- // it in unquoted identifiers (for example `V$DATAFILE.FILE#`), so treating it- // as a comment there truncates otherwise valid statements.- if ((databaseType !== undefined && ORACLE_LIKE_PL_SQL_DATABASES.has(databaseType)) || databaseType === "sqlserver" || sql[pos] !== "#") return false;+export function startsHashLineComment(sql: string, pos: number, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): boolean {+ // Oracle/SQLServer allow # in identifiers; PG uses it in operators such as #>>.+ if ((databaseType !== undefined && (ORACLE_LIKE_PL_SQL_DATABASES.has(databaseType) || POSTGRES_FAMILY_DATABASES.has(databaseType))) || databaseType === "sqlserver" || sql[pos] !== "#") return false;return readSqlBracedParameterAt(sql, pos, parameterOptions)?.syntax !== "mybatis";}@@ -2010,7 +2010,26 @@ function isOpenGaussOracleCompatibility(databaseType?: DatabaseType, options?: Sreturn mode === undefined || mode === "A";}+export function statementParsingDatabaseType(databaseType?: DatabaseType, options?: SqlParameterOptions): DatabaseType | undefined {+ if (databaseType !== "kingbase") return databaseType;+ switch (options?.compatibilityMode?.trim().toLowerCase()) {+ case "oracle":+ return "gaussdb";+ case "mysql":+ return "mysql";+ case "sqlserver":+ return "sqlserver";+ case "pg":+ case "postgres":+ case "postgresql":+ return "postgres";+ default:+ return "kingbase";+ }+}+export function sqlStatementParameterOptionsForCompatibility(databaseType?: DatabaseType, compatibilityMode?: string): SqlParameterOptions | undefined {+ if (databaseType === "kingbase") return { compatibilityMode: compatibilityMode?.trim() };if (databaseType !== "opengauss") return undefined;// Keep the browser-side splitter aligned with the backend's conservative// behavior while the compatibility probe is still cold or unavailable.@@ -2018,7 +2037,10 @@ export function sqlStatementParameterOptionsForCompatibility(databaseType?: Data}export function isOracleLikeDatabase(databaseType?: DatabaseType, options?: SqlParameterOptions): boolean {- return !!databaseType && (ORACLE_LIKE_PL_SQL_DATABASES.has(databaseType) || isOpenGaussOracleCompatibility(databaseType, options));+ databaseType = statementParsingDatabaseType(databaseType, options);+ // An unresolved Kingbase mode keeps the legacy lexer, but must not split a+ // recognizable PL/SQL block before mode metadata arrives.+ return !!databaseType && (databaseType === "kingbase" || ORACLE_LIKE_PL_SQL_DATABASES.has(databaseType) || isOpenGaussOracleCompatibility(databaseType, options));}function isPostgresFamilyDatabase(databaseType?: DatabaseType): boolean {@@ -2906,6 +2928,7 @@ function normalizeSql(sql: string): string {* that case only a single candidate is returned to avoid duplicates.*/export function executableStatementRanges(sql: string, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions): SqlTextRange[] {+ databaseType = statementParsingDatabaseType(databaseType, parameterOptions);if (databaseType === "redis") return redisExecutableCommandRanges(sql);if (databaseType === "mongodb") return splitMongoCommandRanges(sql).map(({ from, to, text }) => ({ from, to, sql: text }));return splitSqlStatementRanges(sql, databaseType, parameterOptions).flatMap((statement) => splitStatementRangeAtSoftStarts(sql, statement, databaseType, parameterOptions).map((range) => rangeFor(range, sql)));