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(mysql): preserve binary literals during transfer

Guoyu Suc87d42fb5e38
diff --git a/crates/dbx-core/src/transfer.rs b/crates/dbx-core/src/transfer.rs
index 46baecf678..afed6e52d6 100644
--- a/crates/dbx-core/src/transfer.rs
+++ b/crates/dbx-core/src/transfer.rs
@@ -1011,6 +1011,9 @@ pub fn escape_value_typed(val: &serde_json::Value, db_type: &DatabaseType, colum
if let Some(binary_literal) = format_postgres_binary_sql_literal(s, db_type, column_type) {
return binary_literal;
}
+ if let Some(binary_literal) = format_mysql_binary_sql_literal(s, db_type, column_type) {
+ return binary_literal;
+ }
if let Some(numeric_literal) = format_mysql_numeric_string_literal(s, db_type, column_type) {
return numeric_literal;
}
@@ -1105,6 +1108,20 @@ fn format_postgres_binary_sql_literal(
Some(format!("decode('{hex}', 'hex')"))
}
+fn format_mysql_binary_sql_literal(value: &str, db_type: &DatabaseType, column_type: Option<&str>) -> Option<String> {
+ if !matches!(db_type, DatabaseType::Mysql) || !column_type.is_some_and(is_binary_transfer_column_type) {
+ return None;
+ }
+
+ let trimmed = value.trim();
+ let hex = trimmed.strip_prefix("0x").or_else(|| trimmed.strip_prefix("0X"))?;
+ if hex.as_bytes().iter().all(|byte| byte.is_ascii_hexdigit()) {
+ Some(if hex.is_empty() { "X''".to_string() } else { format!("0x{hex}") })
+ } else {
+ None
+ }
+}
+
fn format_oracle_date_sql_literal(value: &str, db_type: &DatabaseType, column_type: Option<&str>) -> Option<String> {
if !matches!(db_type, DatabaseType::Oracle | DatabaseType::OceanbaseOracle) {
return None;
@@ -5927,6 +5944,47 @@ mod tests {
);
}
+ #[test]
+ fn mysql_insert_formats_blob_prefixed_hex_as_binary_literal() {
+ let sql = generate_insert_typed(
+ &[String::from("id"), String::from("payload"), String::from("empty_blob"), String::from("note")],
+ &[
+ Some(String::from("int")),
+ Some(String::from("MEDIUMBLOB")),
+ Some(String::from("blob")),
+ Some(String::from("varchar(64)")),
+ ],
+ &[vec![json!(1), json!("0x0001ABff"), json!("0X"), json!("0x0001ABff")]],
+ "files",
+ "",
+ &DatabaseType::Mysql,
+ );
+
+ assert_eq!(
+ sql,
+ r#"INSERT INTO `files` (`id`, `payload`, `empty_blob`, `note`) VALUES
+(1, 0x0001ABff, X'', '0x0001ABff')"#
+ );
+ }
+
+ #[test]
+ fn mysql_insert_keeps_invalid_blob_hex_as_string_literal() {
+ let sql = generate_insert_typed(
+ &[String::from("id"), String::from("payload")],
+ &[Some(String::from("int")), Some(String::from("mediumblob"))],
+ &[vec![json!(1), json!("0xnothex")]],
+ "files",
+ "",
+ &DatabaseType::Mysql,
+ );
+
+ assert_eq!(
+ sql,
+ r#"INSERT INTO `files` (`id`, `payload`) VALUES
+(1, '0xnothex')"#
+ );
+ }
+
#[test]
fn mysql_insert_keeps_backslash_escape_style() {
let sql = generate_insert_typed(