fix(postgres): quote data grid condition completions
This commit is contained in:
1 parent
ff4eac3782
commit
4e926cf22a
6 files changed
+77
-4
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { dataGridConditionColumnOptions } from "@/lib/dataGrid/dataGridConditionCompletion";
|
||||
|
||||
describe("dataGridConditionColumnOptions", () => {
|
||||
it("reuses PostgreSQL completion quoting while preserving display metadata", () => {
|
||||
expect(dataGridConditionColumnOptions([{ name: "OrderId", comment: "Mixed case" }, { name: "order", comment: null }, { name: "article", comment: "Safe identifier" }, { name: 'has"quote' }], "postgres")).toEqual([
|
||||
{ name: "OrderId", comment: "Mixed case", insertText: '"OrderId"' },
|
||||
{ name: "order", comment: null, insertText: '"order"' },
|
||||
{ name: "article", comment: "Safe identifier", insertText: "article" },
|
||||
{ name: 'has"quote', insertText: '"has""quote"' },
|
||||
]);
|
||||
});
|
||||
|
||||
it.each(["mysql", "sqlserver", "oracle"] as const)("keeps existing %s condition insertions unchanged", (databaseType) => {
|
||||
expect(dataGridConditionColumnOptions(["OrderId", "order"], databaseType)).toEqual([
|
||||
{ name: "OrderId", insertText: "OrderId" },
|
||||
{ name: "order", insertText: "order" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { DataGridConditionColumnOption } from "@/composables/useDataGridConditionEditor";
|
||||
import { codeMirrorSqlDialect } from "@/lib/database/jdbcDialect";
|
||||
import { quoteSqlIdentifier } from "@/lib/sql/sqlCompletion";
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
|
||||
export function dataGridConditionColumnOptions(columns: readonly DataGridConditionColumnOption[], databaseType?: DatabaseType): DataGridConditionColumnOption[] {
|
||||
const dialect = codeMirrorSqlDialect(databaseType);
|
||||
return columns.map((column) => {
|
||||
const name = typeof column === "string" ? column : column.name;
|
||||
const insertText = quoteSqlIdentifier(name, dialect);
|
||||
const comment = typeof column === "string" ? undefined : column.comment;
|
||||
return { name, insertText, ...(comment !== undefined ? { comment } : {}) };
|
||||
});
|
||||
}
|
||||
@@ -2777,7 +2777,7 @@ function unquoteIdentifier(value: string): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
function quoteSqlIdentifier(identifier: string, dialect?: "mysql" | "postgres" | "sqlserver"): string {
|
||||
export function quoteSqlIdentifier(identifier: string, dialect?: "mysql" | "postgres" | "sqlserver"): string {
|
||||
if (dialect !== "postgres" || !requiresPostgresIdentifierQuote(identifier)) return identifier;
|
||||
return `"${identifier.replaceAll('"', '""')}"`;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user