Skip to content

armgit / dbx

Public Imported from GitHub
0

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。

fix(grid): qualify jdbc table sort columns

t8y21afa11d665db
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",