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(dameng): preserve NUL characters in string exports

zipg61f442f9b9c8
diff --git a/crates/dbx-core/src/database_export.rs b/crates/dbx-core/src/database_export.rs
index 88ca3b85c8..f26893bcab 100644
--- a/crates/dbx-core/src/database_export.rs
+++ b/crates/dbx-core/src/database_export.rs
@@ -293,13 +293,32 @@ fn quote_export_sql_string(text: &str) -> String {
}
fn quote_export_sql_string_for_database(text: &str, database_type: Option<DatabaseType>) -> String {
- if is_mysql_compatible_export_literal_target(database_type) {
- quote_mysql_compatible_export_sql_string(text)
- } else {
- quote_export_sql_string(text)
+ match database_type {
+ Some(DatabaseType::Dameng) => quote_dameng_export_sql_string(text),
+ database_type if is_mysql_compatible_export_literal_target(database_type) => {
+ quote_mysql_compatible_export_sql_string(text)
+ }
+ _ => quote_export_sql_string(text),
}
}
+fn quote_dameng_export_sql_string(text: &str) -> String {
+ if !text.contains('\0') {
+ return quote_export_sql_string(text);
+ }
+
+ let mut parts = Vec::new();
+ for (index, segment) in text.split('\0').enumerate() {
+ if index > 0 {
+ parts.push("CHR(0)".to_string());
+ }
+ if !segment.is_empty() {
+ parts.push(quote_export_sql_string(segment));
+ }
+ }
+ parts.join(" || ")
+}
+
fn quote_mysql_compatible_export_sql_string(text: &str) -> String {
format!("'{}'", escape_mysql_compatible_export_sql_string(text))
}
@@ -2439,6 +2458,47 @@ mod tests {
);
}
+ #[test]
+ fn dameng_strings_export_nul_as_chr_expression() {
+ let statements = build_export_insert_statements(BuildExportInsertStatementsOptions {
+ database_type: Some(DatabaseType::Dameng),
+ schema: Some("DBX_TEST".to_string()),
+ table_name: Some("NUL_VALUES".to_string()),
+ qualified_table_name: None,
+ columns: vec![
+ "PLAIN".to_string(),
+ "TRAILING".to_string(),
+ "LEADING".to_string(),
+ "MIDDLE".to_string(),
+ "CONSECUTIVE".to_string(),
+ "ONLY_NUL".to_string(),
+ ],
+ column_types: vec![Some("VARCHAR".to_string()); 6],
+ column_extras: Vec::new(),
+ rows: vec![vec![
+ json!("plain"),
+ json!("eHall\0"),
+ json!("\0leading"),
+ json!("left\0right"),
+ json!("left\0\0right"),
+ json!("\0"),
+ ]],
+ batch_size: Some(10),
+ })
+ .unwrap();
+
+ assert_eq!(
+ statements,
+ vec![concat!(
+ "INSERT INTO \"DBX_TEST\".\"NUL_VALUES\" ",
+ "(\"PLAIN\", \"TRAILING\", \"LEADING\", \"MIDDLE\", \"CONSECUTIVE\", \"ONLY_NUL\") ",
+ "VALUES ('plain', 'eHall' || CHR(0), CHR(0) || 'leading', 'left' || CHR(0) || 'right', ",
+ "'left' || CHR(0) || CHR(0) || 'right', CHR(0));"
+ )]
+ );
+ assert!(!statements[0].contains('\0'));
+ }
+
#[test]
fn mysql_export_uses_typed_literals_for_numeric_and_blob_columns() {
let statements = build_export_insert_statements(BuildExportInsertStatementsOptions {