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(sqlserver): export binary columns as 0x hex literals

zipgfd6b7bd502ca
diff --git a/crates/dbx-core/src/data/database_export.rs b/crates/dbx-core/src/data/database_export.rs
index fc8bfd01f7..0facacdb0b 100644
--- a/crates/dbx-core/src/data/database_export.rs
+++ b/crates/dbx-core/src/data/database_export.rs
@@ -628,6 +628,11 @@ fn format_export_sql_literal_typed(
}
}
}
+ if is_sqlserver_binary_export_column(database_type, column_type) {
+ if let Some(literal) = format_sqlserver_binary_export_literal(value) {
+ return literal;
+ }
+ }
if let Some(arr) = value.as_array() {
if matches!(database_type, Some(DatabaseType::ClickHouse) | Some(DatabaseType::Databend)) {
return format_ch_array_sql_literal(arr);
@@ -680,6 +685,34 @@ fn quote_standard_export_sql_string(text: &str) -> String {
format!("'{}'", text.replace('\'', "''"))
}
+/// SQL Server binary column types. The driver exposes their values as
+/// `0x`-prefixed hex text, and T-SQL only accepts that text as an unquoted
+/// binary literal: exporting it as a quoted string makes the import fail with
+/// SQL Server error 257 ("Implicit conversion from data type varchar to
+/// varbinary(max) is not allowed. Use the CONVERT function to run this query.").
+fn is_sqlserver_binary_export_column(database_type: Option<DatabaseType>, column_type: Option<&str>) -> bool {
+ if database_type != Some(DatabaseType::SqlServer) {
+ return false;
+ }
+ column_type.is_some_and(|column_type| {
+ let normalized = column_type.trim().to_ascii_lowercase();
+ let base = normalized.split(['(', ' ', '\t', '\n']).next().unwrap_or("").trim();
+ matches!(base, "binary" | "varbinary" | "image" | "timestamp" | "rowversion")
+ })
+}
+
+/// Renders a SQL Server binary value as a T-SQL binary literal (`0x..`). Values
+/// that are not `0x`-prefixed hex text keep the previous string quoting, so an
+/// unexpected driver encoding is not silently reinterpreted.
+fn format_sqlserver_binary_export_literal(value: &Value) -> Option<String> {
+ let text = value.as_str()?.trim();
+ let hex = text.strip_prefix("0x").or_else(|| text.strip_prefix("0X"))?;
+ if !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) {
+ return None;
+ }
+ Some(format!("0x{hex}"))
+}
+
fn is_sqlserver_unicode_export_type(column_type: &str) -> bool {
let base = column_type.trim().split(|ch: char| ch == '(' || ch.is_whitespace()).next().unwrap_or("");
["nchar", "nvarchar", "ntext", "sysname"].iter().any(|candidate| base.eq_ignore_ascii_case(candidate))
@@ -5397,6 +5430,76 @@ mod tests {
);
}
+ #[test]
+ fn sqlserver_export_keeps_binary_values_as_hex_literals() {
+ let statements = build_export_insert_statements(BuildExportInsertStatementsOptions {
+ database_type: Some(DatabaseType::SqlServer),
+ identifier_quote: None,
+ schema: Some("dbo".to_string()),
+ table_name: Some("bin_probe".to_string()),
+ qualified_table_name: None,
+ columns: vec!["id".to_string(), "payload".to_string(), "label".to_string(), "empty_payload".to_string()],
+ column_types: vec![
+ Some("int".to_string()),
+ Some("varbinary(max)".to_string()),
+ Some("nvarchar(50)".to_string()),
+ Some("binary(8)".to_string()),
+ ],
+ column_extras: Vec::new(),
+ spatial_columns: Vec::new(),
+ spatial_values: Vec::new(),
+ rows: vec![
+ vec![json!(1), json!("0x0011FFEE"), json!("中文"), json!("0x")],
+ vec![json!(2), Value::Null, json!("plain"), json!("0XABcd")],
+ vec![json!(3), json!("0xzz"), json!("text"), json!("not-hex")],
+ ],
+ batch_size: Some(10),
+ })
+ .unwrap();
+
+ assert_eq!(
+ statements,
+ vec![
+ "INSERT INTO [dbo].[bin_probe] ([id], [payload], [label], [empty_payload]) VALUES\n(1, 0x0011FFEE, N'中文', 0x),\n(2, NULL, N'plain', 0xABcd),\n(3, '0xzz', N'text', 'not-hex');"
+ ]
+ );
+ }
+
+ #[test]
+ fn sqlserver_database_export_keeps_binary_values_as_hex_literals() {
+ let sql = build_database_sql_export(BuildDatabaseSqlExportOptions {
+ database_name: "dbx10411".to_string(),
+ exported_at: Some("2026-09-28T00:00:00.000Z".to_string()),
+ tables: vec![ExportedTableSql {
+ display_name: "test.dbo.bin_probe".to_string(),
+ database_type: Some(DatabaseType::SqlServer),
+ identifier_quote: None,
+ schema: Some("dbo".to_string()),
+ table_name: Some("bin_probe".to_string()),
+ qualified_table_name: None,
+ ddl: None,
+ columns: vec!["id".to_string(), "payload".to_string()],
+ column_types: vec![Some("int".to_string()), Some("varbinary(max)".to_string())],
+ column_extras: Vec::new(),
+ spatial_columns: Vec::new(),
+ spatial_values: Vec::new(),
+ rows: vec![vec![json!(1), json!("0x0011ffee")], vec![json!(2), Value::Null]],
+ truncated: false,
+ }],
+ row_limit_per_table: None,
+ insert_batch_size: None,
+ insert_dialect: SqlInsertDialect::Source,
+ connection_id: None,
+ database: None,
+ schema: None,
+ omit_auto_increment: false,
+ })
+ .expect("build SQL Server database export");
+
+ assert!(sql.contains("(1, 0x0011ffee)"), "binary literal missing from export: {sql}");
+ assert!(!sql.contains("'0x0011ffee'"), "binary values must not be exported as strings: {sql}");
+ }
+
#[test]
fn mysql_export_inserts_escape_control_characters() {
let statements = build_export_insert_statements(BuildExportInsertStatementsOptions {