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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(import): quote MySQL identifiers on generic JDBC table imports
Jeffa623c956c433
diff --git a/crates/dbx-core/src/data/table_import.rs b/crates/dbx-core/src/data/table_import.rsindex e128085f51..334d2b1ce2 100644--- a/crates/dbx-core/src/data/table_import.rs+++ b/crates/dbx-core/src/data/table_import.rs@@ -26,7 +26,7 @@ use sqlparser::parser::Parser;use crate::connection::{task_client_session_id, AppState, PoolKind};use crate::db;-use crate::models::connection::DatabaseType;+use crate::models::connection::{ConnectionConfig, DatabaseType};use crate::sql::SqlParsingOptions;use crate::sql_file_import::{SqlFileStreamDecoder, StreamingSqlFileSplitter};use crate::transfer::{@@ -4977,6 +4977,37 @@ fn build_import_insert_batches_with_plan(date_time_format: Option<&str>,hard_sql_bytes: Option<usize>,start_row_offset: usize,+) -> Result<Vec<ImportSqlBatch>, String> {+ build_import_insert_batches_with_plan_and_identifier_quote(+ rows,+ plan,+ table,+ schema,+ db_type,+ kingbase_oracle_mode,+ None,+ conflict_policy,+ primary_key_columns,+ date_time_format,+ hard_sql_bytes,+ start_row_offset,+ )+}++#[allow(clippy::too_many_arguments)]+fn build_import_insert_batches_with_plan_and_identifier_quote(+ rows: &[Vec<serde_json::Value>],+ plan: &CompiledImportPlan,+ table: &str,+ schema: &str,+ db_type: &DatabaseType,+ kingbase_oracle_mode: bool,+ identifier_quote: Option<&str>,+ conflict_policy: TableImportConflictPolicy,+ primary_key_columns: &[String],+ date_time_format: Option<&str>,+ hard_sql_bytes: Option<usize>,+ start_row_offset: usize,) -> Result<Vec<ImportSqlBatch>, String> {if rows.is_empty() {return Ok(Vec::new());@@ -4995,6 +5026,7 @@ fn build_import_insert_batches_with_plan(None,limits,conflict_policy == TableImportConflictPolicy::Skip,+ identifier_quote,)?}TableImportConflictPolicy::UpdateExisting => {@@ -5113,6 +5145,7 @@ fn build_import_execution_batches(schema: &str,db_type: &DatabaseType,kingbase_oracle_mode: bool,+ identifier_quote: Option<&str>,conflict_policy: TableImportConflictPolicy,primary_key_columns: &[String],date_time_format: Option<&str>,@@ -5120,13 +5153,14 @@ fn build_import_execution_batches(start_row_offset: usize,) -> Result<Vec<ImportSqlBatch>, String> {if let Some(plan) = plan {- return build_import_insert_batches_with_plan(+ return build_import_insert_batches_with_plan_and_identifier_quote(rows,plan,table,schema,db_type,kingbase_oracle_mode,+ identifier_quote,conflict_policy,primary_key_columns,date_time_format,@@ -5147,13 +5181,14 @@ fn build_import_execution_batches();}let plan = compile_import_plan(columns, mappings, target_column_types)?;- build_import_insert_batches_with_plan(+ build_import_insert_batches_with_plan_and_identifier_quote(rows,&plan,table,schema,db_type,kingbase_oracle_mode,+ identifier_quote,conflict_policy,primary_key_columns,date_time_format,@@ -5670,6 +5705,17 @@ pub fn build_import_create_table_plan(table: &str,schema: &str,db_type: &DatabaseType,+) -> Result<ImportCreateTablePlan, String> {+ build_import_create_table_plan_with_identifier_quote(data, mappings, table, schema, db_type, None)+}++fn build_import_create_table_plan_with_identifier_quote(+ data: &ParsedImportFile,+ mappings: &[TableImportColumnMapping],+ table: &str,+ schema: &str,+ db_type: &DatabaseType,+ identifier_quote: Option<&str>,) -> Result<ImportCreateTablePlan, String> {if table.trim().is_empty() {return Err("Target table name is required".to_string());@@ -5699,10 +5745,22 @@ pub fn build_import_create_table_plan(return Err("No columns mapped for import".to_string());}- let full_table = qualified_table(table.trim(), schema, db_type, None);+ let full_table = crate::data::transfer::qualified_table_with_explicit_quote(+ table.trim(),+ schema,+ db_type,+ None,+ identifier_quote,+ );let column_sql = columns.iter()- .map(|column| format!("{} {}", quote_identifier(&column.name, db_type), column.data_type))+ .map(|column| {+ format!(+ "{} {}",+ crate::data::transfer::quote_identifier_with_explicit_quote(&column.name, db_type, identifier_quote),+ column.data_type+ )+ }).collect::<Vec<_>>().join(",\n ");let engine_clause =@@ -5710,6 +5768,46 @@ pub fn build_import_create_table_plan(Ok(ImportCreateTablePlan { sql: format!("CREATE TABLE {full_table} (\n {column_sql}\n){engine_clause}"), columns })}+/// A generic JDBC connection can reach any engine, so the static `Jdbc` mapping (ANSI double+/// quotes) breaks write SQL on MySQL-family servers, where a double quote starts a string literal.+/// The JDBC plugin does not report an identifier quote, so this recognises the MySQL family from+/// the same connection fields the frontend's `inferJdbcDialect` reads. That inference is also what+/// offers table import for these connections in the first place.+///+/// NOTE: `TRUNCATE` deliberately keeps the static quoting. Generic JDBC truncates in a separate+/// statement and then writes the batches in a transaction the JDBC plugin cannot open, so a+/// MySQL-valid `TRUNCATE` would empty the table before the import fails.+fn jdbc_import_identifier_quote(config: &ConnectionConfig) -> Option<&'static str> {+ if config.db_type != DatabaseType::Jdbc {+ return None;+ }+ let info = config.database_info.as_ref();+ let haystack = [+ config.driver_profile.as_deref(),+ config.driver_label.as_deref(),+ config.connection_string.as_deref(),+ config.jdbc_driver_class.as_deref(),+ info.and_then(|info| info.product_name.as_deref()),+ info.and_then(|info| info.server_comment.as_deref()),+ info.and_then(|info| info.driver_name.as_deref()),+ ]+ .into_iter()+ .flatten()+ .chain(config.jdbc_driver_paths.iter().map(String::as_str))+ .collect::<Vec<_>>()+ .join("\n")+ .to_ascii_lowercase();+ // `inferJdbcDialect` checks Phoenix before MySQL and keeps it on generic JDBC SQL.+ if haystack.contains("phoenix") {+ return None;+ }+ (haystack.contains("mysql") || haystack.contains("mariadb")).then_some("`")+}++async fn import_target_identifier_quote(state: &AppState, connection_id: &str) -> Option<&'static str> {+ state.configs.read().await.get(connection_id).and_then(jdbc_import_identifier_quote)+}+fn import_error_message(request: &TableImportRequest, rows_imported: usize, error: impl AsRef<str>) -> String {format!("Import into table '{}' failed after {} imported rows: {}", request.table, rows_imported, error.as_ref())}@@ -6366,6 +6464,7 @@ async fn execute_import_rows_batch(postgres_copy_accumulator: &mut Option<PostgresCopyAccumulator>,sqlite_append_transaction: &mut Option<SqliteAppendTransaction>,kingbase_oracle_mode: bool,+ identifier_quote: Option<&str>,conflict_policy: TableImportConflictPolicy,primary_key_columns: &[String],date_time_format: Option<&str>,@@ -6442,6 +6541,7 @@ async fn execute_import_rows_batch(schema,db_type,kingbase_oracle_mode,+ identifier_quote,conflict_policy,primary_key_columns,date_time_format,@@ -7398,6 +7498,7 @@ wherestate.touch_pool_activity(pool_key).await;let _activity_touch = state.pool_activity_touch(pool_key);let kingbase_oracle_mode = kingbase_oracle_compatibility_mode(state, pool_key, db_type).await;+ let identifier_quote = import_target_identifier_quote(state, &request.connection_id).await;let conflict_policy = request.effective_conflict_policy();if let Err(error) = validate_update_existing_target(conflict_policy, db_type, request.create_table) {return Err(emit_import_error(&mut progress_callback, request, 0, 0, started_at, error));@@ -7540,12 +7641,13 @@ where}};let total_rows = parsed.total_rows;- let plan = match build_import_create_table_plan(+ let plan = match build_import_create_table_plan_with_identifier_quote(&parsed,&request.mappings,&request.table,&request.schema,db_type,+ identifier_quote,) {Ok(plan) => plan,Err(error) => {@@ -7784,6 +7886,7 @@ where&mut postgres_copy_accumulator,&mut sqlite_append_transaction,kingbase_oracle_mode,+ identifier_quote,conflict_policy,&primary_key_columns,request.date_time_format.as_deref(),@@ -8272,6 +8375,7 @@ where&mut postgres_copy_accumulator,&mut sqlite_append_transaction,kingbase_oracle_mode,+ identifier_quote,conflict_policy,&primary_key_columns,request.date_time_format.as_deref(),@@ -8611,6 +8715,7 @@ where&mut postgres_copy_accumulator,&mut sqlite_append_transaction,kingbase_oracle_mode,+ identifier_quote,conflict_policy,&primary_key_columns,request.date_time_format.as_deref(),@@ -12970,6 +13075,115 @@ mod tests {assert_eq!(plan.columns[0].data_type, "VARCHAR");}+ #[test]+ fn jdbc_import_identifier_quote_recognises_mysql_family_connections() {+ let jdbc = |fields: serde_json::Value| -> ConnectionConfig {+ let mut config = serde_json::json!({+ "id": "jdbc-import",+ "name": "jdbc import",+ "db_type": "jdbc",+ "host": "",+ "port": 0,+ "username": "",+ "password": ""+ });+ config.as_object_mut().unwrap().extend(fields.as_object().unwrap().clone());+ serde_json::from_value(config).unwrap()+ };++ // #13575: a gateway wrapper driver whose URL does not name MySQL, identified only by the+ // product the driver reported on connect.+ let wrapped = jdbc(serde_json::json!({+ "connection_string": "jdbc:gateway://10.0.0.1:3306/app",+ "database_info": { "productName": "MySQL", "driverName": "MySQL Connector/J" }+ }));+ assert_eq!(jdbc_import_identifier_quote(&wrapped), Some("`"));+ assert_eq!(+ jdbc_import_identifier_quote(&jdbc(serde_json::json!({ "connection_string": "jdbc:mariadb://db/app" }))),+ Some("`")+ );+ assert_eq!(+ jdbc_import_identifier_quote(&jdbc(serde_json::json!({ "connection_string": "jdbc:postgresql://db/app" }))),+ None+ );+ assert_eq!(+ jdbc_import_identifier_quote(&jdbc(serde_json::json!({+ "connection_string": "jdbc:phoenix:zk1",+ "jdbc_driver_paths": ["/drivers/phoenix-mysql-shim.jar"]+ }))),+ None+ );+ let mut native_mysql = wrapped.clone();+ native_mysql.db_type = DatabaseType::Mysql;+ assert_eq!(jdbc_import_identifier_quote(&native_mysql), None);+ }++ #[test]+ fn jdbc_import_write_sql_uses_the_mysql_identifier_quote() {+ // #13575: a generic JDBC connection to MySQL wrote ANSI double-quoted identifiers,+ // which MySQL rejects without `ANSI_QUOTES`.+ let columns = vec!["id".to_string(), "col_name".to_string()];+ let mappings = columns+ .iter()+ .map(|column| TableImportColumnMapping {+ source_column: column.clone(),+ target_column: column.clone(),+ target_data_type: None,+ generator: None,+ })+ .collect::<Vec<_>>();+ let target_column_types =+ vec![("id".to_string(), "INT".to_string()), ("col_name".to_string(), "VARCHAR(100)".to_string())];+ let rows = vec![vec![serde_json::json!("1"), serde_json::json!("Ada")]];+ let insert_sql = |identifier_quote: Option<&str>| {+ build_import_execution_batches(+ &rows,+ None,+ &columns,+ &mappings,+ &target_column_types,+ "table_b",+ "",+ &DatabaseType::Jdbc,+ false,+ identifier_quote,+ TableImportConflictPolicy::Error,+ &[],+ None,+ None,+ 0,+ )+ .unwrap()+ .into_iter()+ .map(|batch| batch.sql)+ .collect::<Vec<_>>()+ };++ assert_eq!(insert_sql(Some("`")), vec!["INSERT INTO `table_b` (`id`, `col_name`) VALUES\n('1', 'Ada')"]);+ let data =+ ParsedImportFile { columns: columns.clone(), rows: rows.clone(), total_rows: 1, effective_encoding: None };+ let typed_mappings = mappings+ .iter()+ .map(|mapping| TableImportColumnMapping {+ target_data_type: Some("VARCHAR(100)".to_string()),+ ..mapping.clone()+ })+ .collect::<Vec<_>>();+ let plan = build_import_create_table_plan_with_identifier_quote(+ &data,+ &typed_mappings,+ "table_b",+ "",+ &DatabaseType::Jdbc,+ Some("`"),+ )+ .unwrap();+ assert_eq!(plan.sql, "CREATE TABLE `table_b` (\n `id` VARCHAR(100),\n `col_name` VARCHAR(100)\n)");++ // Other generic JDBC targets keep the previous SQL.+ assert_eq!(insert_sql(None), vec!["INSERT INTO \"table_b\" (\"id\", \"col_name\") VALUES\n('1', 'Ada')"]);+ }+#[test]fn create_table_plan_rejects_unsafe_user_defined_column_type() {let data = ParsedImportFile {@@ -13921,6 +14135,7 @@ mod tests {&mut sqlite_append_transaction,&mut postgres_copy_accumulator,false,+ None,TableImportConflictPolicy::Error,&[],None,@@ -14137,6 +14352,7 @@ mod tests {&mut self.postgres_copy_accumulator,&mut self.transaction,false,+ None,TableImportConflictPolicy::Error,&[],None,diff --git a/crates/dbx-core/src/data/transfer.rs b/crates/dbx-core/src/data/transfer.rsindex 741f435c36..9fc2035f65 100644--- a/crates/dbx-core/src/data/transfer.rs+++ b/crates/dbx-core/src/data/transfer.rs@@ -730,6 +730,43 @@ fn qualified_table_with_identifier_quote(}}+/// Whether write SQL should use an identifier quote the caller resolved for the connection+/// instead of the static transfer mapping. Callers that pass `None` keep the transfer mapping, so+/// their generated SQL is unchanged.+fn uses_explicit_identifier_quote(db_type: &DatabaseType, identifier_quote: Option<&str>) -> bool {+ identifier_quote.is_some() && crate::sql_dialect::uses_connection_identifier_quote(Some(*db_type), identifier_quote)+}++/// Quotes a write-target identifier with the connection's identifier quote, falling back to+/// [`quote_identifier`] when there is none.+pub(crate) fn quote_identifier_with_explicit_quote(+ name: &str,+ db_type: &DatabaseType,+ identifier_quote: Option<&str>,+) -> String {+ if uses_explicit_identifier_quote(db_type, identifier_quote) {+ quote_identifier_with_identifier_quote(name, db_type, identifier_quote)+ } else {+ quote_identifier(name, db_type)+ }+}++/// Qualifies a write-target table with the connection's identifier quote, falling back to+/// [`qualified_table`] when there is none.+pub(crate) fn qualified_table_with_explicit_quote(+ table: &str,+ schema: &str,+ db_type: &DatabaseType,+ catalog: Option<&str>,+ identifier_quote: Option<&str>,+) -> String {+ if uses_explicit_identifier_quote(db_type, identifier_quote) {+ qualified_table_with_identifier_quote(table, schema, db_type, catalog, identifier_quote)+ } else {+ qualified_table(table, schema, db_type, catalog)+ }+}+pub fn validate_transfer_target_table_names(request: &TransferRequest) -> Result<(), String> {let mut targets: HashMap<String, String> = HashMap::new();for source_table in &request.tables {@@ -5296,7 +5333,16 @@ impl InsertSqlTemplate {catalog: Option<&str>,overrides_postgres_system_values: bool,) -> Self {- Self::new_with_column_quoting(columns, table, schema, db_type, catalog, overrides_postgres_system_values, true)+ Self::new_with_column_quoting(+ columns,+ table,+ schema,+ db_type,+ catalog,+ overrides_postgres_system_values,+ true,+ None,+ )}#[allow(clippy::too_many_arguments)]@@ -5308,11 +5354,18 @@ impl InsertSqlTemplate {catalog: Option<&str>,overrides_postgres_system_values: bool,quote_target_column_names: bool,+ identifier_quote: Option<&str>,) -> Self {- let full_table = qualified_table(table, schema, db_type, catalog);+ let full_table = qualified_table_with_explicit_quote(table, schema, db_type, catalog, identifier_quote);let col_list = columns.iter()- .map(|column| transfer_column_identifier(column, db_type, quote_target_column_names))+ .map(|column| {+ if uses_explicit_identifier_quote(db_type, identifier_quote) {+ quote_identifier_with_identifier_quote(column, db_type, identifier_quote)+ } else {+ transfer_column_identifier(column, db_type, quote_target_column_names)+ }+ }).collect::<Vec<_>>().join(", ");let overriding =@@ -5817,6 +5870,7 @@ fn generate_upsert_typed_for_transfer(catalog,false,quote_target_column_names,+ None,);template.build(&value_rows_sql(rows, column_types, db_type, mysql_spatial_markers))}@@ -6700,6 +6754,7 @@ fn generate_transfer_write_sql(catalog,overrides_postgres_system_values,quote_target_column_names,+ None,);template.build(&value_rows_sql(rows, column_types, db_type, mysql_spatial_markers))}@@ -6733,7 +6788,7 @@ pub(crate) fn generate_insert_typed_sql_batches_from_value_rows(limits: SqlBatchLimits,) -> Result<Vec<(String, usize)>, String> {generate_insert_typed_sql_batches_from_value_rows_with_options(- columns, value_rows, table, schema, db_type, catalog, limits, false,+ columns, value_rows, table, schema, db_type, catalog, limits, false, None,)}@@ -6747,10 +6802,20 @@ pub(crate) fn generate_insert_typed_sql_batches_from_value_rows_with_options(catalog: Option<&str>,limits: SqlBatchLimits,skip_duplicate_rows: bool,+ identifier_quote: Option<&str>,) -> Result<Vec<(String, usize)>, String> {if !skip_duplicate_rows {return generate_insert_sql_batches_from_value_rows(- columns, value_rows, table, schema, db_type, catalog, limits, false, true,+ columns,+ value_rows,+ table,+ schema,+ db_type,+ catalog,+ limits,+ false,+ true,+ identifier_quote,);}@@ -6767,7 +6832,16 @@ pub(crate) fn generate_insert_typed_sql_batches_from_value_rows_with_options(let target_sql_bytes = limits.target_sql_bytes.max(1);let batch_sql_bytes = limits.hard_sql_bytes.map_or(target_sql_bytes, |hard| target_sql_bytes.min(hard));- let template = InsertSqlTemplate::new_with_column_quoting(columns, table, schema, db_type, catalog, false, true);+ let template = InsertSqlTemplate::new_with_column_quoting(+ columns,+ table,+ schema,+ db_type,+ catalog,+ false,+ true,+ identifier_quote,+ );let value_row_bytes = value_rows.iter().map(|row| sql_text_bytes(row, db_type)).collect::<Vec<_>>();@@ -6875,6 +6949,7 @@ pub(crate) fn generate_primary_key_upsert_sql_batches_from_value_rows(adjusted_limits,false,true,+ None,)?;batches@@ -6930,6 +7005,7 @@ fn generate_insert_typed_sql_batches_for_transfer(limits,overrides_postgres_system_values,quote_target_column_names,+ None,)}@@ -6944,6 +7020,7 @@ fn generate_insert_sql_batches_from_value_rows(limits: SqlBatchLimits,overrides_postgres_system_values: bool,quote_target_column_names: bool,+ identifier_quote: Option<&str>,) -> Result<Vec<(String, usize)>, String> {if value_rows.is_empty() {return Ok(Vec::new());@@ -6965,6 +7042,7 @@ fn generate_insert_sql_batches_from_value_rows(catalog,overrides_postgres_system_values,quote_target_column_names,+ identifier_quote,);let value_row_bytes = value_rows.iter().map(|row| sql_text_bytes(row, db_type)).collect::<Vec<_>>();let mut statements = Vec::new();@@ -23461,6 +23539,7 @@ SELECT 1 FROM dual"#None,SqlBatchLimits { max_rows: 100, target_sql_bytes: 1024, hard_sql_bytes: None },true,+ None,).unwrap();@@ -23482,6 +23561,7 @@ SELECT 1 FROM dual"#None,SqlBatchLimits { max_rows: 100, target_sql_bytes: 1024, hard_sql_bytes: None },true,+ None,).unwrap();@@ -23504,6 +23584,7 @@ SELECT 1 FROM dual"#None,SqlBatchLimits { max_rows: 100, target_sql_bytes: 1024, hard_sql_bytes: None },true,+ None,).unwrap();diff --git a/crates/dbx-core/src/data/transfer/iris_tests.rs b/crates/dbx-core/src/data/transfer/iris_tests.rsindex 8f08318643..fb6ce65057 100644--- a/crates/dbx-core/src/data/transfer/iris_tests.rs+++ b/crates/dbx-core/src/data/transfer/iris_tests.rs@@ -116,6 +116,7 @@ fn iris_transfer_limits_and_unsupported_upsert_keep_existing_policies() {limits,false,true,+ None,).unwrap();assert_eq!(batches.len(), expected_batches);@@ -130,6 +131,7 @@ fn iris_transfer_limits_and_unsupported_upsert_keep_existing_policies() {limits.with_hard_sql_bytes(Some(1)),false,true,+ None,).unwrap_err().contains("exceeds the 1 byte hard limit"));diff --git a/crates/dbx-core/src/data/transfer/mysql_xugu.rs b/crates/dbx-core/src/data/transfer/mysql_xugu.rsindex 14505807fb..39d886a5bf 100644--- a/crates/dbx-core/src/data/transfer/mysql_xugu.rs+++ b/crates/dbx-core/src/data/transfer/mysql_xugu.rs@@ -564,6 +564,7 @@ pub(super) fn write_batches(limits,false,quote_column_names,+ None,).map(|batches| batches.into_iter().map(|(sql, _)| sql).collect())}