fix(completion): respect identifier quoting for upper-folding dialects

Closes #9775
This commit is contained in:
skyler authored and t8y2 committed 2026-09-21 01:44:23 +08:00
1 parent d66242508e
commit 7e5bcade78
2 files changed
+50 -2

No files matched your search

@@ -0,0 +1,47 @@
import { describe, expect, it } from "vitest";
import { buildSqlCompletionItems } from "@/lib/sql/sqlCompletion";
describe("upper-folding completion with identifier quoting disabled (#9775)", () => {
const sql = "SELECT t.c FROM abc t";
const cursor = "SELECT t.c".length;
const columnApplyOf = (databaseType: "dameng" | "db2", extra: { quoteIdentifiers?: boolean }) =>
buildSqlCompletionItems(sql, cursor, {
databaseType,
tables: [{ name: "abc", type: "table" as const }],
columnsByTable: new Map([["abc", [{ name: "col1", table: "abc" }]]]),
...extra,
}).find((item) => item.label === "col1")?.apply;
it.each(["dameng", "db2"] as const)("keeps lowercase columns bare on %s when quoting is disabled", (databaseType) => {
expect(columnApplyOf(databaseType, { quoteIdentifiers: false })).toBe("col1");
});
it.each(["dameng", "db2"] as const)("still quotes lowercase columns on %s by default and when quoting is enabled", (databaseType) => {
expect(columnApplyOf(databaseType, {})).toBe('"col1"');
expect(columnApplyOf(databaseType, { quoteIdentifiers: true })).toBe('"col1"');
});
it.each(["dameng", "db2"] as const)("keeps mixed-case table names bare on %s when quoting is disabled", (databaseType) => {
const tableSql = "SELECT * FROM Ab";
const items = buildSqlCompletionItems(tableSql, tableSql.length, {
databaseType,
tables: [{ name: "Abc", type: "table" as const }],
columnsByTable: new Map(),
quoteIdentifiers: false,
});
expect(items.find((item) => item.label === "Abc")?.apply).toBe("Abc");
});
it.each(["dameng", "db2"] as const)("keeps all-uppercase columns bare on %s regardless of the setting", (databaseType) => {
const upperSql = "SELECT t.c FROM abc t";
const applyOf = (extra: { quoteIdentifiers?: boolean }) =>
buildSqlCompletionItems(upperSql, "SELECT t.c".length, {
databaseType,
tables: [{ name: "abc", type: "table" as const }],
columnsByTable: new Map([["abc", [{ name: "COL1", table: "abc" }]]]),
...extra,
}).find((item) => item.label === "COL1")?.apply;
expect(applyOf({ quoteIdentifiers: false })).toBe("COL1");
expect(applyOf({ quoteIdentifiers: true })).toBe("COL1");
});
});
+3 -2
View File
@@ -1659,8 +1659,9 @@ class SqlCompletionProvider {
) {
this.t = input.translations;
const dialect = sqlCompletionApplyDialect(input.databaseType, input.dialect);
// "Quote identifiers in generated SQL" off: Oracle completion inserts bare names instead of quoting lowercase ones.
this.dialect = dialect === "oracle" && input.quoteIdentifiers === false ? undefined : dialect;
// "Quote identifiers in generated SQL" off: Oracle-like and upper-folding (Dameng, DB2)
// completions insert bare names instead of quoting mixed-case ones.
this.dialect = input.quoteIdentifiers === false && (dialect === "oracle" || dialect === "upper") ? undefined : dialect;
this.databaseType = input.databaseType;
}