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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(sql): honor configured keyword case in new query templates (#12949)
Loc Nguyenc95d2f009c50
diff --git a/apps/desktop/src/App.vue b/apps/desktop/src/App.vueindex 2c158f8ec2..27a8bc8e0b 100644--- a/apps/desktop/src/App.vue+++ b/apps/desktop/src/App.vue@@ -3331,6 +3331,7 @@ async function newQuery(options: NewQueryOptions = {}) {identifierQuote: connectionStore.connectionIdentifierQuote?.(target.connectionId),includeDatabaseName: settingsStore.editorSettings.generateSqlIncludeDatabaseName,quoteIdentifiers: settingsStore.editorSettings.generateSqlQuoteIdentifiers,+ keywordCase: settingsStore.editorSettings.sqlFormatter.keywordCase,});const tabId = queryStore.createTab(conn.id, target.database, undefined, "query", target.schema, initialSql, target.catalog, { schemaQualifiesSql: target.schemaQualifiesSql === true });if (initialSql) {diff --git a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vueindex cbd25d0ee2..d7788b0873 100644--- a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue+++ b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue@@ -2187,6 +2187,7 @@ async function newQuery(options: { reuseQueryTabByScope?: boolean } = {}) {schema: node.schema,includeDatabaseName: settingsStore.editorSettings.generateSqlIncludeDatabaseName,quoteIdentifiers: settingsStore.editorSettings.generateSqlQuoteIdentifiers,+ keywordCase: settingsStore.editorSettings.sqlFormatter.keywordCase,tableName: node.label,columns: [],});@@ -2288,6 +2289,7 @@ async function newSelectTemplate() {schema: context.tableSchema,includeDatabaseName: settingsStore.editorSettings.generateSqlIncludeDatabaseName,quoteIdentifiers: settingsStore.editorSettings.generateSqlQuoteIdentifiers,+ keywordCase: settingsStore.editorSettings.sqlFormatter.keywordCase,tableName: context.node.label,columns: context.columns,}),@@ -2307,6 +2309,7 @@ async function newSelectTemplate() {schema: context.tableSchema,includeDatabaseName: settingsStore.editorSettings.generateSqlIncludeDatabaseName,quoteIdentifiers: settingsStore.editorSettings.generateSqlQuoteIdentifiers,+ keywordCase: settingsStore.editorSettings.sqlFormatter.keywordCase,tableName: context.node.label,columns: context.columns,});diff --git a/apps/desktop/src/lib/__tests__/sql/newQueryContext.spec.ts b/apps/desktop/src/lib/__tests__/sql/newQueryContext.spec.tsindex b9d879a05a..f9abe0fc01 100644--- a/apps/desktop/src/lib/__tests__/sql/newQueryContext.spec.ts+++ b/apps/desktop/src/lib/__tests__/sql/newQueryContext.spec.ts@@ -312,6 +312,14 @@ describe("buildSelectAllSql", () => {expect(buildSelectAllSql("influxdb3", { tableName: "cpu" })).toBe(`SELECT * FROM "cpu" WHERE time > now() - INTERVAL '5 minutes' ORDER BY time DESC LIMIT 100`);});+ it("honors lower keywordCase for new-query SQL prefill (#12933)", () => {+ expect(buildSelectAllSql("postgres", { schema: "public", tableName: "users" }, undefined, undefined, false, true, "lower")).toBe('select * from "users"');+ expect(buildSelectAllSql("postgres", { schema: "public", tableName: "users" }, undefined, undefined, false, true, "upper")).toBe('SELECT * FROM "users"');+ expect(buildSelectAllSql("postgres", { schema: "public", tableName: "users" }, undefined, undefined, false, true, "preserve")).toBe('SELECT * FROM "users"');+ expect(buildSelectAllSql("influxdb", { tableName: "cpu" }, undefined, undefined, false, true, "lower")).toBe('select * from "cpu" where time > now() - 5m order by time desc limit 100');+ expect(buildSelectAllSql("influxdb3", { tableName: "cpu" }, undefined, undefined, false, true, "lower")).toBe(`select * from "cpu" where time > now() - interval '5 minutes' order by time desc limit 100`);+ });+it("bracket-quotes a SQL Server table", () => {expect(buildSelectAllSql("sqlserver", { schema: "dbo", tableName: "users" })).toBe("SELECT * FROM [dbo].[users]");expect(buildSelectAllSql("sqlserver", { tableName: "users" })).toBe("SELECT * FROM [users]");@@ -392,6 +400,19 @@ describe("resolveNewQueryInitialSql", () => {).toBe("SELECT * FROM public.users");});+ it("honors lower keywordCase when resolving initial new-query SQL (#12933)", () => {+ expect(+ resolveNewQueryInitialSql({+ activeTab: dataTab(),+ prefillEnabled: true,+ targetConnectionId: "conn-1",+ targetDatabase: "app_db",+ databaseType: "postgres",+ keywordCase: "lower",+ }),+ ).toBe('select * from "users"');+ });+it("passes the Phoenix driver profile into the initial SQL builder", () => {expect(resolveNewQueryInitialSql({diff --git a/apps/desktop/src/lib/__tests__/table/tableSqlTemplates.spec.ts b/apps/desktop/src/lib/__tests__/table/tableSqlTemplates.spec.tsindex 47b532be1d..809d47d106 100644--- a/apps/desktop/src/lib/__tests__/table/tableSqlTemplates.spec.ts+++ b/apps/desktop/src/lib/__tests__/table/tableSqlTemplates.spec.ts@@ -15,6 +15,27 @@ describe("buildTableSelectTemplate", () => {).toBe("SELECT *\nFROM public.dbx_smoke;");});+ it("honors lower keywordCase for a new-query table reference (#12933)", () => {+ expect(+ buildTableSelectTemplate({+ databaseType: "postgres",+ schema: "public",+ tableName: "dbx_smoke",+ quoteIdentifiers: false,+ keywordCase: "lower",+ }),+ ).toBe("select *\nfrom public.dbx_smoke;");+ expect(+ buildTableSelectTemplate({+ databaseType: "postgres",+ schema: "public",+ tableName: "dbx_smoke",+ quoteIdentifiers: false,+ keywordCase: "upper",+ }),+ ).toBe("SELECT *\nFROM public.dbx_smoke;");+ });+it("keeps required quotes for columns when identifier quoting is disabled", () => {expect(buildTableSelectTemplate({diff --git a/apps/desktop/src/lib/sql/newQueryContext.ts b/apps/desktop/src/lib/sql/newQueryContext.tsindex 3f906fb2b0..abc2408c03 100644--- a/apps/desktop/src/lib/sql/newQueryContext.ts+++ b/apps/desktop/src/lib/sql/newQueryContext.ts@@ -1,5 +1,6 @@import { resolveDefaultDatabase } from "@/lib/database/defaultDatabase";import { normalizeSqliteNamespace } from "@/lib/database/sqliteNamespace";+import type { SqlFormatterCase } from "@/lib/sql/sqlFormatterConfig";import { metricRangeQuery, qualifiedTableName } from "@/lib/table/tableSelectSql";import type { ConnectionConfig, DatabaseType, QueryTab, TreeNode } from "@/types/database";@@ -122,6 +123,7 @@ export interface ResolveNewQueryInitialSqlInput extends ResolveNewQueryTableInpuidentifierQuote?: string;includeDatabaseName?: boolean;quoteIdentifiers?: boolean;+ keywordCase?: SqlFormatterCase;}// Database types whose "table" view does not use standard SQL `SELECT * FROM <table>`@@ -185,16 +187,25 @@ 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, quoteIdentifiers = true): 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,+ keywordCase?: SqlFormatterCase,+): 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, quoteIdentifiers });+ const keywords = (sql: string) => (keywordCase === "lower" ? sql.toLowerCase() : sql);if (databaseType === "influxdb") {- return `SELECT * FROM ${ref} WHERE time > now() - 5m ORDER BY time DESC LIMIT 100`;+ return `${keywords("SELECT * FROM")} ${ref} ${keywords("WHERE time > now() - 5m ORDER BY time DESC LIMIT 100")}`;}if (databaseType === "influxdb3") {- return `SELECT * FROM ${ref} WHERE time > now() - INTERVAL '5 minutes' ORDER BY time DESC LIMIT 100`;+ return `${keywords("SELECT * FROM")} ${ref} ${keywords("WHERE time > now() - INTERVAL '5 minutes' ORDER BY time DESC LIMIT 100")}`;}- return `SELECT * FROM ${ref}`;+ return `${keywords("SELECT * FROM")} ${ref}`;}/**@@ -208,5 +219,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, input.quoteIdentifiers);+ return buildSelectAllSql(input.databaseType, table, input.identifierQuote, input.driverProfile, input.includeDatabaseName, input.quoteIdentifiers, input.keywordCase);}diff --git a/apps/desktop/src/lib/table/tableSqlTemplates.ts b/apps/desktop/src/lib/table/tableSqlTemplates.tsindex d8daa18eea..5deb6f5c79 100644--- a/apps/desktop/src/lib/table/tableSqlTemplates.ts+++ b/apps/desktop/src/lib/table/tableSqlTemplates.ts@@ -1,4 +1,5 @@import type { ColumnInfo, DatabaseType } from "@/types/database";+import type { SqlFormatterCase } from "@/lib/sql/sqlFormatterConfig";import { metricRangeQuery, qualifiedTableName, quoteTableDataIdentifier, quoteTableIdentifierIfNeeded } from "@/lib/table/tableSelectSql";export interface TableSqlTemplateOptions {@@ -11,6 +12,7 @@ export interface TableSqlTemplateOptions {includeDatabaseName?: boolean;/** Omit optional identifier quotes while retaining quotes required by the dialect. */quoteIdentifiers?: boolean;+ keywordCase?: SqlFormatterCase;tableName: string;columns?: ColumnInfo[];tableType?: string;@@ -37,11 +39,14 @@ export function buildTableSelectTemplate(options: TableSqlTemplateOptions): striif (options.databaseType === "victoriametrics") return metricRangeQuery(options.tableName);const tableName = templateTableName(options);const columns = options.columns ?? [];+ const lower = options.keywordCase === "lower";+ const selectKw = lower ? "select" : "SELECT";+ const fromKw = lower ? "from" : "FROM";if (!columns.length) {- return `SELECT *\nFROM ${tableName};`;+ return `${selectKw} *\n${fromKw} ${tableName};`;}const selectColumns = columns.map((column) => templateIdentifier(options, column.name));- return `SELECT ${selectColumns.join(", ")}\nFROM ${tableName};`;+ return `${selectKw} ${selectColumns.join(", ")}\n${fromKw} ${tableName};`;}export function buildTableInsertTemplate(options: TableSqlTemplateOptions): string {diff --git a/packages/app-tests/tableSqlTemplates.test.ts b/packages/app-tests/tableSqlTemplates.test.tsindex f57d186e3d..f3d10776a0 100644--- a/packages/app-tests/tableSqlTemplates.test.ts+++ b/packages/app-tests/tableSqlTemplates.test.ts@@ -27,6 +27,46 @@ test("builds SELECT template with explicit table columns", () => {);});+test("builds SELECT template honoring lower keywordCase (#12933)", () => {+ assert.equal(+ buildTableSelectTemplate({+ databaseType: "postgres",+ schema: "public",+ tableName: "users",+ columns,+ keywordCase: "lower",+ }),+ 'select "id", "name", "created_at"\nfrom "public"."users";',+ );+ assert.equal(+ buildTableSelectTemplate({+ databaseType: "postgres",+ schema: "public",+ tableName: "users",+ keywordCase: "lower",+ }),+ 'select *\nfrom "public"."users";',+ );+ assert.equal(+ buildTableSelectTemplate({+ databaseType: "postgres",+ schema: "public",+ tableName: "users",+ keywordCase: "upper",+ }),+ 'SELECT *\nFROM "public"."users";',+ );+ assert.equal(+ buildTableSelectTemplate({+ databaseType: "postgres",+ schema: "public",+ tableName: "users",+ keywordCase: "preserve",+ }),+ 'SELECT *\nFROM "public"."users";',+ );+});+test("builds a MetricsQL range query for VictoriaMetrics metrics", () => {assert.equal(buildTableSelectTemplate({ databaseType: "victoriametrics", tableName: "flag" }), '{__name__="flag"}[1h]');});