feat(editor): add toggle for SQL identifier quoting

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-07 01:11:42 +08:00
1 parent 14e1d4f25b
commit 2eaf7f9914
23 files changed
+125 -23

No files matched your search

@@ -208,6 +208,10 @@ describe("buildSelectAllSql", () => {
expect(buildSelectAllSql("postgres", { schema: "public", tableName: "users" })).toBe('SELECT * FROM "public"."users"');
});
it("can omit identifier quotes while preserving schema qualification", () => {
expect(buildSelectAllSql("postgres", { schema: "public", tableName: "users" }, undefined, undefined, false, false)).toBe("SELECT * FROM public.users");
});
it("preserves the Phoenix schema for new-query prefill", () => {
expect(buildSelectAllSql("jdbc", { schema: "APP", tableName: "USERS" }, '"', "phoenix")).toBe('SELECT * FROM "APP"."USERS"');
});
@@ -280,6 +284,19 @@ describe("resolveNewQueryInitialSql", () => {
).toBe('SELECT * FROM "public"."users"');
});
it("omits identifier quotes when configured for new-query prefill", () => {
expect(
resolveNewQueryInitialSql({
activeTab: dataTab(),
prefillEnabled: true,
targetConnectionId: "conn-1",
targetDatabase: "app_db",
databaseType: "postgres",
quoteIdentifiers: false,
}),
).toBe("SELECT * FROM public.users");
});
it("passes the Phoenix driver profile into the initial SQL builder", () => {
expect(
resolveNewQueryInitialSql({
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { buildTableSelectTemplate } from "@/lib/table/tableSqlTemplates";
describe("buildTableSelectTemplate", () => {
it("can omit identifier quotes for a new-query table reference", () => {
expect(
buildTableSelectTemplate({
databaseType: "postgres",
schema: "public",
tableName: "dbx_smoke",
quoteIdentifiers: false,
}),
).toBe("SELECT *\nFROM public.dbx_smoke;");
});
});
@@ -68,6 +68,10 @@ describe("EDITOR_SETTINGS_DRAFT_KEYS", () => {
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataTabReuseMode");
});
it("includes generated SQL identifier quote preference", () => {
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("generateSqlQuoteIdentifiers");
});
it("includes adjacent data-tab opening", () => {
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("openDataTabsNextToActive");
});
@@ -240,6 +240,7 @@ describe("settings search", () => {
{ titleKey: "settings.clickTableNavigationTarget", category: "navigation", targetId: "navigation" },
{ titleKey: "settings.prefillNewQueryWithSelect", category: "navigation", targetId: "navigation" },
{ titleKey: "settings.generateSqlIncludeDatabaseName", category: "editor", targetId: "editor" },
{ titleKey: "settings.generateSqlQuoteIdentifiers", category: "editor", targetId: "editor" },
{ titleKey: "settings.formatSqlOnSqlFileSave", category: "editor", targetId: "editor" },
{ titleKey: "settings.showTableDdlHoverPreview", category: "editor", targetId: "editor" },
{ titleKey: "settings.sqlFormatterKeywordCase", category: "formatter", targetId: "formatter" },
@@ -75,6 +75,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"openDataTabsNextToActive",
"prefillNewQueryWithSelect",
"generateSqlIncludeDatabaseName",
"generateSqlQuoteIdentifiers",
"formatSqlOnSqlFileSave",
"showTableDdlHoverPreview",
"updateNotificationsEnabled",
@@ -123,6 +123,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
{ id: "editor-app-close-unsaved-tabs", category: "editor", titleKey: "settings.appCloseUnsavedTabsMode", descriptionKey: "settings.appCloseUnsavedTabsModeDescription", targetId: "editor" },
{ id: "editor-prefill-query", category: "navigation", titleKey: "settings.prefillNewQueryWithSelect", descriptionKey: "settings.prefillNewQueryWithSelectDescription", targetId: "navigation" },
{ id: "editor-generate-sql-include-database", category: "editor", titleKey: "settings.generateSqlIncludeDatabaseName", descriptionKey: "settings.generateSqlIncludeDatabaseNameDescription", targetId: "editor" },
{ id: "editor-generate-sql-quote-identifiers", category: "editor", titleKey: "settings.generateSqlQuoteIdentifiers", descriptionKey: "settings.generateSqlQuoteIdentifiersDescription", targetId: "editor" },
{ id: "editor-format-sql-on-sql-file-save", category: "editor", titleKey: "settings.formatSqlOnSqlFileSave", descriptionKey: "settings.formatSqlOnSqlFileSaveDescription", targetId: "editor" },
{ id: "editor-table-ddl-hover-preview", category: "editor", titleKey: "settings.showTableDdlHoverPreview", descriptionKey: "settings.showTableDdlHoverPreviewDescription", targetId: "editor" },
{ id: "editor-diagnostics", category: "editor", titleKey: "settings.sqlSemanticDiagnosticsEnabled", descriptionKey: "settings.sqlSemanticDiagnosticsEnabledDescription", targetId: "editor" },
+4 -3
View File
@@ -93,6 +93,7 @@ export interface ResolveNewQueryInitialSqlInput extends ResolveNewQueryTableInpu
driverProfile?: string;
identifierQuote?: string;
includeDatabaseName?: boolean;
quoteIdentifiers?: boolean;
}
// Database types whose "table" view does not use standard SQL `SELECT * FROM <table>`
@@ -156,9 +157,9 @@ export function resolveNewQueryTable(input: ResolveNewQueryTableInput): NewQuery
* full-history scan the moment the user hits Run. VictoriaMetrics
* already has its own metric range template above.
*/
export function buildSelectAllSql(databaseType: DatabaseType | undefined, table: Pick<NewQueryTable, "schema" | "catalog" | "tableName"> & Partial<Pick<NewQueryTable, "database">>, identifierQuote?: string, driverProfile?: string, includeDatabaseName = false): string {
export function buildSelectAllSql(databaseType: DatabaseType | undefined, table: Pick<NewQueryTable, "schema" | "catalog" | "tableName"> & Partial<Pick<NewQueryTable, "database">>, identifierQuote?: string, driverProfile?: string, includeDatabaseName = false, quoteIdentifiers = true): string {
if (databaseType === "victoriametrics") return metricRangeQuery(table.tableName);
const ref = qualifiedTableName({ databaseType, driverProfile, identifierQuote, database: table.database, schema: table.schema, catalog: table.catalog, tableName: table.tableName, includeDatabaseName });
const ref = qualifiedTableName({ databaseType, driverProfile, identifierQuote, database: table.database, schema: table.schema, catalog: table.catalog, tableName: table.tableName, includeDatabaseName, quoteIdentifiers });
if (databaseType === "influxdb") {
return `SELECT * FROM ${ref} WHERE time > now() - 5m ORDER BY time DESC LIMIT 100`;
}
@@ -179,5 +180,5 @@ export function resolveNewQueryInitialSql(input: ResolveNewQueryInitialSqlInput)
const table = resolveNewQueryTable(input);
if (!table || table.connectionId !== input.targetConnectionId || table.database !== input.targetDatabase) return undefined;
return buildSelectAllSql(input.databaseType, table, input.identifierQuote, input.driverProfile, input.includeDatabaseName);
return buildSelectAllSql(input.databaseType, table, input.identifierQuote, input.driverProfile, input.includeDatabaseName, input.quoteIdentifiers);
}
+24 -20
View File
@@ -33,6 +33,8 @@ export interface BuildTableSelectSqlOptions {
database?: string;
/** Include the active database when this dialect supports `database.table` references. */
includeDatabaseName?: boolean;
/** Emit bare identifiers instead of dialect-specific identifier quotes. */
quoteIdentifiers?: boolean;
}
const DATABASE_QUALIFIED_TABLE_TYPES = new Set<DatabaseType>(["mysql", "clickhouse", "doris", "starrocks", "goldendb"]);
@@ -107,44 +109,46 @@ function quoteCypherIdentifier(name: string): string {
return `\`${name.replace(/`/g, "``")}\``;
}
export function qualifiedTableName(options: Pick<BuildTableSelectSqlOptions, "databaseType" | "driverProfile" | "identifierQuote" | "schema" | "tableName" | "catalog" | "database" | "includeDatabaseName">): string {
const { databaseType, driverProfile, identifierQuote, schema, tableName, catalog, database, includeDatabaseName } = options;
export function qualifiedTableName(options: Pick<BuildTableSelectSqlOptions, "databaseType" | "driverProfile" | "identifierQuote" | "schema" | "tableName" | "catalog" | "database" | "includeDatabaseName" | "quoteIdentifiers">): string {
const { databaseType, driverProfile, identifierQuote, schema, tableName, catalog, database, includeDatabaseName, quoteIdentifiers } = options;
const quoteTable = (name: string) => (quoteIdentifiers === false ? name : quoteTableIdentifier(databaseType, name));
const quoteTableData = (name: string) => (quoteIdentifiers === false ? name : quoteTableDataIdentifier(databaseType, name, identifierQuote));
if (databaseType === "informix" && driverProfile?.trim().toLowerCase() === "gbase8s") {
return quoteTableDataIdentifier(databaseType, tableName, identifierQuote);
return quoteTableData(tableName);
}
// Doris / StarRocks multi-catalog: address external-catalog tables with the
// 3-part `catalog.database.table` form, which the engines accept directly.
if (catalog && catalog !== "internal" && (databaseType === "doris" || databaseType === "starrocks")) {
const quotedCatalog = quoteTableIdentifier(databaseType, catalog);
const quotedTable = quoteTableIdentifier(databaseType, tableName);
const quotedCatalog = quoteTable(catalog);
const quotedTable = quoteTable(tableName);
// Doris/StarRocks have no separate schema concept; the database under the
// external catalog is the middle segment. Prefer schema when a caller
// passes it that way, otherwise fall back to database.
const middle = schema?.trim() || database?.trim();
if (middle) {
return `${quotedCatalog}.${quoteTableIdentifier(databaseType, middle)}.${quotedTable}`;
return `${quotedCatalog}.${quoteTable(middle)}.${quotedTable}`;
}
return `${quotedCatalog}.${quotedTable}`;
}
if (databaseType === "iotdb") {
const trimmedSchema = schema?.trim();
if (trimmedSchema && tableName !== trimmedSchema && !tableName.startsWith(`${trimmedSchema}.`)) {
return `${quoteTableIdentifier(databaseType, trimmedSchema)}.${quoteTableIdentifier(databaseType, tableName)}`;
return `${quoteTable(trimmedSchema)}.${quoteTable(tableName)}`;
}
return quoteTableIdentifier(databaseType, tableName);
return quoteTable(tableName);
}
if ((databaseType === "gaussdb" || databaseType === "opengauss" || databaseType === "postgres" || databaseType === "kingbase") && identifierQuote != null) {
const quotedTable = quoteTableDataIdentifier(databaseType, tableName, identifierQuote);
const quotedTable = quoteTableData(tableName);
const trimmedSchema = schema?.trim();
if (trimmedSchema) {
return `${quoteTableDataIdentifier(databaseType, trimmedSchema, identifierQuote)}.${quotedTable}`;
return `${quoteTableData(trimmedSchema)}.${quotedTable}`;
}
return quotedTable;
}
if (databaseType === "jdbc" && jdbcDriverProfileUsesSchemaQualification(driverProfile)) {
const quotedTable = quoteTableDataIdentifier(databaseType, tableName, identifierQuote);
const quotedTable = quoteTableData(tableName);
const trimmedSchema = schema?.trim();
return trimmedSchema ? `${quoteTableDataIdentifier(databaseType, trimmedSchema, identifierQuote)}.${quotedTable}` : quotedTable;
return trimmedSchema ? `${quoteTableData(trimmedSchema)}.${quotedTable}` : quotedTable;
}
// Cloud Spanner mirrors `uses_connection_identifier_quote` on the backend, which
// counts Spanner unconditionally: the branch must not be gated on a reported
@@ -154,34 +158,34 @@ export function qualifiedTableName(options: Pick<BuildTableSelectSqlOptions, "da
// `` `s`.`t` `` with an empty `s` is a Spanner syntax error; a missing quote falls
// back to the GoogleSQL backtick default, exactly like `identifiers.rs`.
if (databaseType === "spanner") {
const quotedTable = quoteTableDataIdentifier(databaseType, tableName, identifierQuote);
const quotedTable = quoteTableData(tableName);
// `database` holds the resource path `projects/{p}/instances/{i}/databases/{d}`, and callers
// that treat the database as the schema (the sidebar SQL templates collapse
// `node.schema || node.database`) would otherwise emit `` `projects/…`.`singers` ``. A Spanner
// schema name is letters, digits and underscores, so the path separator identifies it.
const trimmedSchema = schema?.trim();
const schemaQualifier = trimmedSchema && !trimmedSchema.includes("/") ? trimmedSchema : undefined;
return schemaQualifier ? `${quoteTableDataIdentifier(databaseType, schemaQualifier, identifierQuote)}.${quotedTable}` : quotedTable;
return schemaQualifier ? `${quoteTableData(schemaQualifier)}.${quotedTable}` : quotedTable;
}
if (databaseType === "informix" && identifierQuote != null) {
const quotedTable = quoteTableDataIdentifier(databaseType, tableName, identifierQuote);
const quotedTable = quoteTableData(tableName);
const trimmedSchema = schema?.trim();
return trimmedSchema ? `${quoteTableDataIdentifier(databaseType, trimmedSchema, identifierQuote)}.${quotedTable}` : quotedTable;
return trimmedSchema ? `${quoteTableData(trimmedSchema)}.${quotedTable}` : quotedTable;
}
if ((isSchemaAware(databaseType) || databaseType === "sqlite") && !usesDatabaseObjectTreeMode(databaseType) && schema) {
if (databaseType === "sqlserver") {
const linked = parseSqlServerLinkedSchema(schema);
if (linked) return sqlServerLinkedTableName(linked, tableName);
if (linked) return quoteIdentifiers === false ? [linked.server, linked.catalog, linked.schema, tableName].join(".") : sqlServerLinkedTableName(linked, tableName);
}
return `${quoteTableIdentifier(databaseType, schema)}.${quoteTableIdentifier(databaseType, tableName)}`;
return `${quoteTable(schema)}.${quoteTable(tableName)}`;
}
// MySQL-style engines use the selected database as their table namespace.
// Keep this opt-in so existing generated SQL remains unchanged by default.
const trimmedDatabase = database?.trim();
if (includeDatabaseName && trimmedDatabase && databaseType && DATABASE_QUALIFIED_TABLE_TYPES.has(databaseType)) {
return `${quoteTableIdentifier(databaseType, trimmedDatabase)}.${quoteTableIdentifier(databaseType, tableName)}`;
return `${quoteTable(trimmedDatabase)}.${quoteTable(tableName)}`;
}
return quoteTableIdentifier(databaseType, tableName);
return quoteTable(tableName);
}
interface SqlCteVisibility {
@@ -9,6 +9,8 @@ export interface TableSqlTemplateOptions {
catalog?: string;
database?: string;
includeDatabaseName?: boolean;
/** Emit bare table identifiers instead of dialect-specific quotes. */
quoteIdentifiers?: boolean;
tableName: string;
columns?: ColumnInfo[];
tableType?: string;
@@ -107,6 +109,7 @@ function templateTableName(options: TableSqlTemplateOptions): string {
catalog: options.catalog,
database: options.database,
includeDatabaseName: options.includeDatabaseName,
quoteIdentifiers: options.quoteIdentifiers,
schema: options.schema,
tableName: options.tableName,
});