fix(query): single-column results editable on case-preserving engines

This commit is contained in:
zipg authored and GitHub committed 2026-09-25 09:37:11 +08:00
1 parent a77a92a80c
commit ba422f96f8
5 files changed
+113 -7

No files matched your search

@@ -29,6 +29,25 @@ describe("editable query hidden primary keys", () => {
expect(result?.projections.map((projection) => projection.alias)).toEqual(["__DBX_PK_1", "__DBX_PK_2"]);
});
it("quotes the appended key with each reported engine's own dialect", () => {
// #10233 reports OceanBase (Oracle mode), Dameng and HighGo. All three draw their
// quoting from the dialect adapter rather than a hard-coded quote character:
// Dameng/OceanBase Oracle mode ride the Oracle adapter, HighGo the PostgreSQL one.
for (const databaseType of ["dameng", "oceanbase-oracle", "highgo"] as const) {
expect(
buildQueryWithHiddenPrimaryKeys({
sql: "SELECT name FROM users",
databaseType,
primaryKeys: ["id"],
existingResultNames: ["name"],
}),
).toEqual({
sql: 'SELECT name, "id" AS "__DBX_PK_0" FROM users',
projections: [{ sourceName: "id", alias: "__DBX_PK_0" }],
});
}
});
it("preserves SQL Server TOP and Oracle optimizer hints", () => {
expect(
buildQueryWithHiddenPrimaryKeys({
@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import { resolveMetadataColumnName } from "@/lib/sql/sqlAnalysis";
/**
* Dialect folding (Oracle-compatible -> upper case, PostgreSQL-compatible ->
* lower case) is only an assumption about how the server stores an unquoted
* identifier. Dameng keeps the case the identifier was created with, so a table
* created as `(id int primary key, name varchar(50))` reports lowercase columns
* while the analyzer folds the query's `name` to `NAME` (issue #10233). The
* folded spelling must stay the primary answer, and a case-only match is only a
* rescue when it identifies exactly one column.
*/
describe("metadata column name resolution tolerates server-preserved casing", () => {
it("rescues a lowercase dameng column when the folded spelling does not exist", () => {
expect(resolveMetadataColumnName("dameng", "name", false, ["id", "name", "amount"])).toBe("name");
});
it("still prefers the folded spelling when the server stored upper case", () => {
expect(resolveMetadataColumnName("dameng", "name", false, ["ID", "NAME"])).toBe("NAME");
});
it("rescues a PostgreSQL-compatible engine that reports an upper-case column", () => {
expect(resolveMetadataColumnName("highgo", "label", false, ["ID", "LABEL"])).toBe("LABEL");
});
it("keeps PostgreSQL lower-case folding unchanged", () => {
expect(resolveMetadataColumnName("highgo", "Label", false, ["id", "label"])).toBe("label");
});
it("refuses a case-only match that is ambiguous", () => {
expect(resolveMetadataColumnName("dameng", "value", false, ["value", "Value"])).toBeUndefined();
});
it("keeps quoted identifiers exact", () => {
expect(resolveMetadataColumnName("dameng", "name", true, ["NAME"])).toBeUndefined();
expect(resolveMetadataColumnName("dameng", "NAME", true, ["NAME"])).toBe("NAME");
});
});
+14 -6
View File
@@ -75,23 +75,31 @@ export function resolveMetadataColumnName(databaseType: string, sourceName: stri
if (sourceNameQuoted === undefined) {
const exact = metadataColumns.find((column) => column === sourceName);
if (exact || POSTGRES_FOLDED_IDENTIFIER_TYPES.has(databaseType) || ORACLE_FOLDED_IDENTIFIER_TYPES.has(databaseType)) return exact;
const caseOnlyMatches = metadataColumns.filter((column) => column.toLowerCase() === sourceName.toLowerCase());
return caseOnlyMatches.length === 1 ? caseOnlyMatches[0] : undefined;
return uniqueCaseInsensitiveColumn(metadataColumns, sourceName);
}
// Dialect folding is only an assumption about what the server stored: Dameng
// keeps the case an unquoted identifier was created with (issue #10233), so
// the folded spelling can miss even though the column exists. Fall back to a
// case-only match when it is unambiguous -- never when the folded name itself
// resolves, so quoted or equally-spelled columns keep their exact identity.
if (POSTGRES_FOLDED_IDENTIFIER_TYPES.has(databaseType)) {
const folded = sourceName.toLowerCase();
return metadataColumns.find((column) => column === folded);
return metadataColumns.find((column) => column === folded) ?? uniqueCaseInsensitiveColumn(metadataColumns, folded);
}
if (ORACLE_FOLDED_IDENTIFIER_TYPES.has(databaseType)) {
const folded = sourceName.toUpperCase();
return metadataColumns.find((column) => column === folded);
return metadataColumns.find((column) => column === folded) ?? uniqueCaseInsensitiveColumn(metadataColumns, folded);
}
const exact = metadataColumns.find((column) => column === sourceName);
if (exact) return exact;
const caseOnlyMatches = metadataColumns.filter((column) => column.toLowerCase() === sourceName.toLowerCase());
return caseOnlyMatches.length === 1 ? caseOnlyMatches[0] : undefined;
return uniqueCaseInsensitiveColumn(metadataColumns, sourceName);
}
function uniqueCaseInsensitiveColumn(metadataColumns: readonly string[], name: string): string | undefined {
const matches = metadataColumns.filter((column) => column.toLowerCase() === name.toLowerCase());
return matches.length === 1 ? matches[0] : undefined;
}
export interface ResolvedSourceColumnRef {