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): honor case conversion for literal-only selections

dienaso4819565f2885
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlSelectionCase.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlSelectionCase.spec.ts
index f692bfd2bc..78a7539d82 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlSelectionCase.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlSelectionCase.spec.ts
@@ -57,4 +57,45 @@ describe("convertSqlSelectionCase", () => {
expect(convertSqlSelectionCase(sql, { from: 0, to: sql.length }, "lower", "mysql")).toBe("select 1 /*!40101 SET @Name = 'Mixed Value' */ from dual");
});
+
+ // #10775: a selection made only of string literals joined by punctuation —
+ // like the IN-list `('D','C','B','B','A')` — used to hit the string
+ // protection and come back unchanged, which read as "the command does
+ // nothing". With no identifiers or keywords inside the selection there is no
+ // SQL structure to protect, so the explicit request converts the strings.
+ it("converts an IN-list selection whose only word content is string literals", () => {
+ const sql = "select * from t where code in ('D','C','B','B','A')";
+ const from = sql.indexOf("('D'");
+
+ expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "lower")).toBe("('d','c','b','b','a')");
+ expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "upper")).toBe("('D','C','B','B','A')");
+ });
+
+ it("converts adjacent literals joined by operators or commas", () => {
+ const sql = "select * from t where a = 'AbC' || 'DeF' and b in ('GhI','JkL')";
+
+ expect(convertSqlSelectionCase(sql, { from: sql.indexOf("'AbC'"), to: sql.indexOf("||") - 1 }, "lower")).toBe("'abc'");
+ expect(convertSqlSelectionCase(sql, { from: sql.indexOf("'GhI'"), to: sql.indexOf("'JkL'") + 5 }, "lower")).toBe("'ghi','jkl'");
+ });
+
+ it("still protects literals when the selection reaches identifiers or keywords", () => {
+ const sql = "select * from t where code in ('D','C') and flag = 'E'";
+ const from = sql.indexOf("in (");
+
+ expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "upper")).toBe("IN ('D','C') AND FLAG = 'E'");
+ });
+
+ it("keeps converting a selection fully inside one literal", () => {
+ const sql = "select * from t where code = 'AbC001'";
+ const from = sql.indexOf("bC");
+
+ expect(convertSqlSelectionCase(sql, { from, to: from + 2 }, "upper")).toBe("BC");
+ });
+
+ it("treats number tokens outside literals as structure and keeps the protection", () => {
+ const sql = "select * from t where score > 60 and grade = 'AbC'";
+ const from = sql.indexOf("> 60");
+
+ expect(convertSqlSelectionCase(sql, { from, to: sql.length }, "upper")).toBe("> 60 AND GRADE = 'AbC'");
+ });
});
diff --git a/apps/desktop/src/lib/sql/sqlSelectionCase.ts b/apps/desktop/src/lib/sql/sqlSelectionCase.ts
index 195be1034a..325d98b84d 100644
--- a/apps/desktop/src/lib/sql/sqlSelectionCase.ts
+++ b/apps/desktop/src/lib/sql/sqlSelectionCase.ts
@@ -23,13 +23,29 @@ export function convertSqlSelectionCase(sql: string, range: SqlSelectionRange, m
});
if (protectedTokens.length === 0) return convertCase(sql.slice(from, to), mode);
- // A selection fully contained inside one protected token (e.g. the user selected only
- // text inside a string literal) carries no risk of collateral damage to surrounding SQL,
- // so honor the explicit request instead of silently no-oping it.
- const [only] = protectedTokens;
- if (protectedTokens.length === 1 && only.span.start <= from && to <= only.span.end) {
- return convertCase(sql.slice(from, to), mode);
+ // An explicit selection whose only word-like content sits inside protected
+ // tokens — a bare string literal, or a value list like `('D','C','B')` — has
+ // no SQL structure that case conversion could damage: everything outside the
+ // protected tokens is punctuation and whitespace, which the conversion leaves
+ // unchanged anyway. Honor the request fully instead of silently no-oping it
+ // (issue #10775: selecting `('D','C','B','B','A')` and converting did
+ // nothing). Selections that reach past protected tokens into identifiers or
+ // keywords keep the protection below.
+ const unprotectedWordCharPattern = /[\p{L}\p{N}_]/u;
+ let unprotectedTextHasWord = false;
+ let unprotectedScan = from;
+ for (const item of protectedTokens) {
+ const literalFrom = Math.max(from, item.span.start);
+ if (unprotectedWordCharPattern.test(sql.slice(unprotectedScan, literalFrom))) {
+ unprotectedTextHasWord = true;
+ break;
+ }
+ unprotectedScan = Math.min(to, Math.max(unprotectedScan, item.span.end));
+ }
+ if (!unprotectedTextHasWord && unprotectedWordCharPattern.test(sql.slice(unprotectedScan, to))) {
+ unprotectedTextHasWord = true;
}
+ if (!unprotectedTextHasWord) return convertCase(sql.slice(from, to), mode);
let converted = "";
let cursor = from;