From 1afa11d665db0d120475d2bd8db046b15da35337 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Mon, 18 May 2026 11:29:33 +0800 Subject: [PATCH] fix(grid): qualify jdbc table sort columns --- apps/desktop/src/lib/tableSelectSql.ts | 15 +++++++++++---- packages/app-tests/tableSelectSql.test.ts | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/apps/desktop/src/lib/tableSelectSql.ts b/apps/desktop/src/lib/tableSelectSql.ts index 96046b4031..baf2d992ec 100644 --- a/apps/desktop/src/lib/tableSelectSql.ts +++ b/apps/desktop/src/lib/tableSelectSql.ts @@ -36,7 +36,8 @@ function isOracleRowId(databaseType: DatabaseType | undefined, name: string): bo function quoteOrderIdentifier(databaseType: DatabaseType | undefined, name: string, tableAlias?: string): string { if (isOracleRowId(databaseType, name)) return tableAlias ? `${tableAlias}.ROWID` : "ROWID"; if (isTdengineTbname(databaseType, name)) return DBX_TDENGINE_TBNAME_COLUMN; - return quoteTableIdentifier(databaseType, name); + const quoted = quoteTableIdentifier(databaseType, name); + return tableAlias ? `${tableAlias}.${quoted}` : quoted; } export function qualifiedTableName( @@ -63,8 +64,9 @@ export function buildTableSelectSql(options: BuildTableSelectSqlOptions): string const predicate = normalizeWhereInput(options.whereInput); const where = predicate ? ` WHERE (${predicate})` : ""; const rowIdAlias = options.includeRowId && databaseType === "oracle" ? "t" : undefined; + const defaultOrderAlias = databaseType === "jdbc" ? "dbx_t" : rowIdAlias; const defaultOrderBy = options.primaryKeys?.length - ? options.primaryKeys.map((pk) => `${quoteOrderIdentifier(databaseType, pk, rowIdAlias)} ASC`).join(", ") + ? options.primaryKeys.map((pk) => `${quoteOrderIdentifier(databaseType, pk, defaultOrderAlias)} ASC`).join(", ") : options.fallbackOrderColumns?.length ? options.fallbackOrderColumns.map((column) => `${quoteTableIdentifier(databaseType, column)} ASC`).join(", ") : undefined; @@ -75,7 +77,12 @@ export function buildTableSelectSql(options: BuildTableSelectSqlOptions): string options.includeRowId && databaseType === "oracle" ? `ROWIDTOCHAR(t.ROWID) AS "${DBX_ROWID_COLUMN}", t.*` : buildSelectColumns(databaseType, options.columns); - const tableAlias = options.includeRowId && usesFetchFirst(databaseType) ? `${table} t` : table; + const tableAlias = + options.includeRowId && usesFetchFirst(databaseType) + ? `${table} t` + : databaseType === "jdbc" && defaultOrderBy + ? `${table} dbx_t` + : table; if (usesFetchFirst(databaseType)) { const offset = options.offset ? ` OFFSET ${options.offset} ROWS` : ""; @@ -88,7 +95,7 @@ export function buildTableSelectSql(options: BuildTableSelectSqlOptions): string } const offset = options.offset ? ` OFFSET ${options.offset}` : ""; - return `SELECT ${selectColumns} FROM ${table}${where}${order} LIMIT ${limit}${offset};`; + return `SELECT ${selectColumns} FROM ${tableAlias}${where}${order} LIMIT ${limit}${offset};`; } function buildSelectColumns(databaseType: DatabaseType | undefined, columns?: string[]): string { diff --git a/packages/app-tests/tableSelectSql.test.ts b/packages/app-tests/tableSelectSql.test.ts index 076026456b..f94c32759f 100644 --- a/packages/app-tests/tableSelectSql.test.ts +++ b/packages/app-tests/tableSelectSql.test.ts @@ -90,6 +90,21 @@ test("builds Access table data queries with backtick identifiers", () => { assert.equal(sql, "SELECT * FROM `Order Details` ORDER BY `Order ID` ASC LIMIT 100 OFFSET 200;"); }); +test("qualifies JDBC table data default order columns with a table alias", () => { + const sql = buildTableSelectSql({ + databaseType: "jdbc", + schema: "SJT_THEME", + tableName: "BATCH_QUERY", + primaryKeys: ["TABLE_NAME"], + limit: 100, + }); + + assert.equal( + sql, + 'SELECT * FROM "SJT_THEME"."BATCH_QUERY" dbx_t ORDER BY dbx_t."TABLE_NAME" ASC LIMIT 100;', + ); +}); + test("expands Hive table data queries into aliased table columns", () => { const sql = buildTableSelectSql({ databaseType: "hive",