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(query): reuse the Gaussdb quote helper for derived count aliases

Andrew79acc066e131
diff --git a/crates/dbx-sql-data/src/query_result_sql.rs b/crates/dbx-sql-data/src/query_result_sql.rs
index 9c198f8b89..1b0130597f 100644
--- a/crates/dbx-sql-data/src/query_result_sql.rs
+++ b/crates/dbx-sql-data/src/query_result_sql.rs
@@ -6,8 +6,8 @@ use serde::{Deserialize, Serialize};
use crate::models::connection::DatabaseType;
use crate::sql::{find_statement_at_cursor, find_statement_at_cursor_for_database};
use crate::sql_dialect::{
- firebird_rows_clause, pagination_strategy, quote_iris_identifier, quote_table_identifier, PaginationContext,
- TablePaginationStrategy,
+ firebird_rows_clause, pagination_strategy, quote_gaussdb_jdbc_identifier, quote_iris_identifier,
+ quote_table_identifier, PaginationContext, TablePaginationStrategy,
};
use sqlparser::ast::{
visit_expressions, Expr, ForClause, GroupByExpr, LimitClause, ObjectNamePart, OrderByKind, Select, SelectItem,
@@ -405,7 +405,7 @@ pub fn build_count_query_sql(options: CountQuerySqlOptions) -> QuerySqlBuildResu
// double-quoted alias as a string literal, leaving a trailing :%qpar.
"dbx_count".to_string()
} else {
- quote_table_identifier(options.database_type, "dbx_count")
+ derived_table_alias(options.database_type, "dbx_count")
};
if options.database_type == Some(DatabaseType::Hive) && starts_with_cte(&statement) {
if let Some(main_query_start) = top_level_sql_tokens(&statement)
@@ -2168,7 +2168,7 @@ fn add_outer_standard_limit(
offset: usize,
order_sql: &str,
) -> String {
- let alias = quote_table_identifier(database_type, "dbx_page");
+ let alias = derived_table_alias(database_type, "dbx_page");
derived_table_sql("SELECT * FROM", statement, &format!("{alias}{order_sql} LIMIT {limit} OFFSET {offset};"))
}
@@ -2248,6 +2248,33 @@ fn is_qualified_identifier_part(sql: &str, token: &SqlToken) -> bool {
|| sql[token_end..].chars().find(|ch| !ch.is_whitespace()) == Some('.')
}
+/// Quote an alias DBX generates for its own derived-table wrappers.
+///
+/// The static default from `quote_table_identifier` is a double-quoted
+/// identifier, which only parses where `"..."` delimits identifiers. The
+/// Gaussdb database type covers native GaussDB, the openGauss driver profile,
+/// and GaussDB M mode (`driver_profile: gaussdb-m`); the M profile speaks
+/// MySQL syntax, where a double-quoted name is a string literal, so the
+/// generated count query is rejected at the alias with
+/// `syntax error at or near ""dbx_count""` instead of counting rows.
+///
+/// The quote decision is left to `quote_gaussdb_jdbc_identifier`, the same
+/// helper `quote_table_data_identifier` uses for this family: it emits an
+/// ordinary lower-snake identifier bare and only delimits names whose spelling
+/// requires it. Wrapper aliases are DBX-owned, never referenced by the outer
+/// query, and always lower snake case, so they come out bare — valid in every
+/// GaussDB mode — while every other dialect keeps its spelling unchanged.
+fn derived_table_alias(database_type: Option<DatabaseType>, name: &str) -> String {
+ if matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss)) {
+ // The connection's reported quote is not plumbed into this builder, so
+ // pass the static double-quote default. Either quote the agent may
+ // report (`"` natively, backtick in M mode) leaves a simple-lower
+ // alias bare, so the choice does not change the emitted SQL here.
+ return quote_gaussdb_jdbc_identifier(name, "\"");
+ }
+ quote_table_identifier(database_type, name)
+}
+
fn derived_table_sql(prefix: &str, statement: &str, suffix: &str) -> String {
format!("{prefix} ({}) {suffix}", statement_for_sql_suffix(statement))
}
@@ -4960,6 +4987,26 @@ WHERE u.id = picked.id;
);
}
+ #[test]
+ fn gaussdb_pagination_wraps_existing_limit_with_bare_page_alias() {
+ // Same MySQL-mode string-literal trap as the count wrapper: the page
+ // alias must reach the server unquoted on GaussDB M mode.
+ for database_type in [DatabaseType::Gaussdb, DatabaseType::OpenGauss] {
+ let result = build_paginated_query_sql(PaginatedQuerySqlOptions {
+ original_sql: "SELECT id FROM users LIMIT 20;".to_string(),
+ database_type: Some(database_type),
+ limit: 5,
+ offset: 10,
+ });
+
+ assert_eq!(
+ result.sql,
+ Some("SELECT * FROM (SELECT id FROM users LIMIT 20) dbx_page LIMIT 5 OFFSET 10;".to_string()),
+ "{database_type:?}"
+ );
+ }
+ }
+
#[test]
fn rejects_multiple_statements_for_pagination() {
let result = build_paginated_query_sql(PaginatedQuerySqlOptions {
@@ -5160,6 +5207,26 @@ WHERE u.id = picked.id;
}
}
+ #[test]
+ fn gaussdb_count_uses_bare_derived_table_alias() {
+ // GaussDB M mode parses MySQL syntax, where a double-quoted alias is a
+ // string literal: `... FROM (SELECT ...) "dbx_count"` fails with
+ // `syntax error at or near ""dbx_count""` instead of counting rows.
+ let sql = "select * from sec_acct.other_sec_acct_info osai";
+ for database_type in [DatabaseType::Gaussdb, DatabaseType::OpenGauss] {
+ let result = build_count_query_sql(CountQuerySqlOptions {
+ original_sql: sql.to_string(),
+ database_type: Some(database_type),
+ });
+
+ assert_eq!(
+ result.sql,
+ Some(format!("SELECT COUNT(*) AS dbx_total_rows FROM ({sql}) dbx_count;")),
+ "{database_type:?}"
+ );
+ }
+ }
+
#[test]
fn count_query_keeps_wrapper_outside_trailing_line_comment() {
let result = build_count_query_sql(CountQuerySqlOptions {
diff --git a/crates/dbx-sql-dialect/src/sql_dialect.rs b/crates/dbx-sql-dialect/src/sql_dialect.rs
index 595fc3a19b..622fd73264 100644
--- a/crates/dbx-sql-dialect/src/sql_dialect.rs
+++ b/crates/dbx-sql-dialect/src/sql_dialect.rs
@@ -39,8 +39,8 @@ pub use identifiers::{
normalize_where_input, qualified_table_name, qualified_table_name_with_catalog, quote_table_identifier,
};
pub use identifiers::{
- parse_sqlserver_linked_schema_ref, qualified_transfer_table, quote_iris_identifier, quote_transfer_identifier,
- transfer_column_identifier,
+ parse_sqlserver_linked_schema_ref, qualified_transfer_table, quote_gaussdb_jdbc_identifier, quote_iris_identifier,
+ quote_transfer_identifier, transfer_column_identifier,
};
pub use table_select::{
build_count_table_sql, build_iris_table_select_sql, build_table_data_select_sql,
diff --git a/crates/dbx-sql-dialect/src/sql_dialect/tests.rs b/crates/dbx-sql-dialect/src/sql_dialect/tests.rs
index 26e7a7be81..1e9699caba 100644
--- a/crates/dbx-sql-dialect/src/sql_dialect/tests.rs
+++ b/crates/dbx-sql-dialect/src/sql_dialect/tests.rs
@@ -1775,6 +1775,12 @@ fn postgres_family_table_data_quoting_resolves_folded_identifiers() {
assert_eq!(quote_gaussdb_jdbc_identifier("mss_check_sales_item", "\""), "mss_check_sales_item");
assert_eq!(quote_gaussdb_jdbc_identifier("MSS_CHECK_SALES_ITEM", "\""), "\"MSS_CHECK_SALES_ITEM\"");
assert_eq!(quote_gaussdb_jdbc_identifier("term", "\""), "term");
+ // DBX-owned derived-table aliases travel through this same helper when the
+ // query builders count or paginate: simple-lower, never referenced by the
+ // outer query, so they stay bare instead of becoming `"dbx_count"`, which
+ // GaussDB M mode reads as a string literal (DBX-LEGACY-0001).
+ assert_eq!(quote_gaussdb_jdbc_identifier("dbx_count", "\""), "dbx_count");
+ assert_eq!(quote_gaussdb_jdbc_identifier("dbx_page", "\""), "dbx_page");
assert_eq!(
table_data_qualified_table_name(