feat(editor): include database name in generated SQL

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-22 13:40:42 +08:00
1 parent fcb6ea92db
commit a6f4cebd18
34 files changed
+379 -23

No files matched your search

+2 -1
View File
@@ -39,7 +39,8 @@ pub use identifiers::{
};
pub(crate) use identifiers::{parse_sqlserver_linked_schema_ref, qualified_transfer_table, quote_transfer_identifier};
pub use table_select::{
build_count_table_sql, build_table_data_select_sql, build_table_select_sql, DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX,
build_count_table_sql, build_table_data_select_sql, build_table_data_select_sql_with_database,
build_table_select_sql, DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX,
};
pub(crate) use table_select::{
quote_table_data_identifier, table_data_qualified_table_name, table_data_schema, uses_connection_identifier_quote,
@@ -180,7 +180,16 @@ pub(crate) fn table_data_schema<'a>(
}
}
/// Builds the SQL used by the data-table grid. Database qualification is opt-in
/// so existing callers retain their current SQL shape.
pub fn build_table_data_select_sql(options: TableDataSelectSqlOptions) -> String {
build_table_data_select_sql_with_database(options, false)
}
pub fn build_table_data_select_sql_with_database(
options: TableDataSelectSqlOptions,
include_database_name: bool,
) -> String {
let database_type = options.database_type;
let schema = table_data_schema(database_type, options.driver_profile.as_deref(), options.schema.as_deref());
let limit = options.limit.unwrap_or(100);
@@ -202,6 +211,22 @@ pub fn build_table_data_select_sql(options: TableDataSelectSqlOptions) -> String
// Doris / StarRocks multi-catalog: prefix the catalog for external-catalog tables.
} else if uses_connection_identifier_quote(database_type, options.identifier_quote.as_deref()) {
table_data_qualified_table_name(database_type, schema, &options.table_name, options.identifier_quote.as_deref())
} else if include_database_name {
database_qualified_table_name(
database_type,
options.catalog.as_deref(),
options.database.as_deref(),
&options.table_name,
)
.unwrap_or_else(|| {
qualified_table_name_with_catalog(
database_type,
options.catalog.as_deref(),
schema,
options.database.as_deref(),
&options.table_name,
)
})
} else {
qualified_table_name_with_catalog(
database_type,
@@ -343,6 +368,29 @@ pub fn build_table_data_select_sql(options: TableDataSelectSqlOptions) -> String
}
}
/// Returns a `database.table` reference for engines whose active database is
/// normally omitted from table-data SQL. Doris and StarRocks retain an external
/// catalog prefix when one is selected.
fn database_qualified_table_name(
database_type: Option<DatabaseType>,
catalog: Option<&str>,
database: Option<&str>,
table_name: &str,
) -> Option<String> {
let database = database.map(str::trim).filter(|database| !database.is_empty())?;
match database_type {
Some(DatabaseType::ClickHouse) => Some(format!(
"{}.{}",
quote_table_identifier(database_type, database),
quote_table_identifier(database_type, table_name)
)),
Some(DatabaseType::Mysql | DatabaseType::Goldendb | DatabaseType::Doris | DatabaseType::StarRocks) => {
Some(qualified_table_name_with_catalog(database_type, catalog, Some(database), Some(database), table_name))
}
_ => None,
}
}
pub(crate) fn table_data_qualified_table_name(
database_type: Option<DatabaseType>,
schema: Option<&str>,
@@ -747,6 +795,13 @@ mod tests {
assert!(sql.contains("FROM `hive_catalog`.`sales`.`orders`"), "sql was: {sql}");
}
#[test]
fn table_data_select_optionally_qualifies_database() {
let options = opts(DatabaseType::Mysql, None, Some("aaa"), "apis");
assert_eq!(build_table_data_select_sql(options.clone()), "SELECT * FROM `apis` LIMIT 10;");
assert_eq!(build_table_data_select_sql_with_database(options, true), "SELECT * FROM `aaa`.`apis` LIMIT 10;");
}
#[test]
fn doris_external_catalog_without_database_degrades_to_two_part() {
// When neither schema nor database is provided the name degrades to the
+3 -1
View File
@@ -126,6 +126,8 @@ pub struct BuildDroppedFilePreviewSqlRequest {
#[serde(rename_all = "camelCase")]
pub struct BuildTableSelectSqlRequest {
pub options: dbx_core::sql_dialect::TableDataSelectSqlOptions,
#[serde(default)]
pub include_database_name: bool,
}
#[derive(Deserialize)]
@@ -782,7 +784,7 @@ pub async fn build_dropped_file_preview_sql(
}
pub async fn build_table_select_sql(Json(req): Json<BuildTableSelectSqlRequest>) -> Json<String> {
Json(dbx_core::sql_dialect::build_table_data_select_sql(req.options))
Json(dbx_core::sql_dialect::build_table_data_select_sql_with_database(req.options, req.include_database_name))
}
pub async fn build_database_search_sql(