fix(sqlserver): preserve database-qualified editing targets

This commit is contained in:
guoyongchang authored and GitHub committed 2026-07-30 20:55:14 +08:00
1 parent 9d7f87feb8
commit b89e0079a5
4 files changed
+132 -15

No files matched your search

+29
View File
@@ -4183,6 +4183,35 @@ mod tests {
assert_eq!(result.statements, vec!["UPDATE [dbo].[users] SET [UserId] = 144847503924137986 WHERE [Id] = 1;"]);
}
#[test]
fn prepares_sqlserver_cross_database_update() {
let result = prepare_data_grid_save(DataGridSaveStatementOptions {
database_type: Some(DatabaseType::SqlServer),
identifier_quote: None,
table_meta: DataGridTableMeta {
catalog: Some("BarDB".to_string()),
database: Some("BarDB".to_string()),
schema: Some("dbo".to_string()),
table_name: "TUser".to_string(),
primary_keys: vec!["ID".to_string()],
columns: Some(vec![column("ID", "int", false, None), column("UserId", "bigint", false, None)]),
},
columns: vec!["ID".to_string(), "UserId".to_string()],
source_columns: None,
rows: vec![vec![json!(1), json!(10279)]],
dirty_rows: vec![(0, vec![(1, json!(10280))])],
deleted_rows: vec![],
new_rows: vec![],
});
assert_eq!(result.validation_error, None);
assert_eq!(result.statements, vec!["UPDATE [BarDB].[dbo].[TUser] SET [UserId] = 10280 WHERE [ID] = 1;"]);
assert_eq!(
result.rollback_statements,
vec!["UPDATE [BarDB].[dbo].[TUser] SET [UserId] = 10279 WHERE [ID] = 1 AND [UserId] = 10280;"]
);
}
#[test]
fn prepares_kingbase_update_when_source_primary_key_case_differs() {
let result = prepare_data_grid_save(DataGridSaveStatementOptions {
+12 -13
View File
@@ -71,17 +71,12 @@ pub fn qualified_table_name(database_type: Option<DatabaseType>, schema: Option<
quote_table_identifier(database_type, table_name)
}
/// Like `qualified_table_name`, but prefixes a Doris/StarRocks external
/// catalog (`<catalog>.<database>.<table>`) when `catalog` is present,
/// non-empty, and not the engine's `internal` catalog. The middle segment is
/// the database — Doris/StarRocks have no separate schema concept, so
/// `schema` is only used when a caller passes it that way; otherwise `database`
/// fills the middle slot. When neither is set the name degrades to the 2-part
/// `<catalog>.<table>` form. The `internal` guard is defensive — built-in
/// catalog tables never carry a catalog in the first place (the sidebar routes
/// them through the standard path), so this only ever prefixes genuine
/// external catalogs. Other engines ignore `catalog` (they have no 3-part
/// catalog naming).
/// Like `qualified_table_name`, but also supports 3-part names for SQL Server
/// (`<database>.<schema>.<table>`) and Doris/StarRocks external catalogs
/// (`<catalog>.<database>.<table>`). SQL parsing stores the first segment of a
/// 3-part source in `catalog`; for SQL Server that segment is its database.
/// Doris/StarRocks use `schema` as the middle segment when present, otherwise
/// `database`, and ignore their built-in `internal` catalog.
pub fn qualified_table_name_with_catalog(
database_type: Option<DatabaseType>,
catalog: Option<&str>,
@@ -89,9 +84,13 @@ pub fn qualified_table_name_with_catalog(
database: Option<&str>,
table_name: &str,
) -> String {
let catalog = catalog.map(str::trim).filter(|catalog| !catalog.is_empty() && *catalog != "internal");
let catalog = catalog.map(str::trim).filter(|catalog| !catalog.is_empty());
match (catalog, database_type) {
(Some(catalog), Some(DatabaseType::Doris | DatabaseType::StarRocks)) => {
(Some(database), Some(DatabaseType::SqlServer)) => {
let table = qualified_table_name(database_type, schema, table_name);
format!("{}.{}", quote_table_identifier(database_type, database), table)
}
(Some(catalog), Some(DatabaseType::Doris | DatabaseType::StarRocks)) if catalog != "internal" => {
let middle = schema
.map(str::trim)
.filter(|schema| !schema.is_empty())