fix(sqlserver): display binary cells as hex

This commit is contained in:
t8y2 2026-06-26 11:44:09 +08:00
parent f17f91c3be
commit 2b6a8919d2
2 changed files with 9 additions and 12 deletions

View File

@ -639,18 +639,6 @@ fn sqlserver_cell_to_json(cell: &ColumnData<'static>) -> serde_json::Value {
return serde_json::Value::String(v.to_string());
}
if let Ok(Some(v)) = <Vec<u8> as tiberius::FromSqlOwned>::from_sql_owned(cell.clone()) {
// Try to decode as UTF-16 LE (SQL Server NVARCHAR encoding) with lossy conversion
// This handles cases where sys.sql_modules.definition contains invalid UTF-8 sequences
if v.len() >= 2 && v.len() % 2 == 0 {
let utf16_units: Vec<u16> =
v.chunks_exact(2).map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]])).collect();
if let Ok(decoded) = String::from_utf16(&utf16_units) {
return serde_json::Value::String(decoded);
}
// Use lossy conversion as fallback for invalid UTF-16 sequences
let lossy_decoded = String::from_utf16_lossy(&utf16_units);
return serde_json::Value::String(lossy_decoded);
}
return super::binary_value_to_json(&v);
}
serde_json::Value::Null
@ -2066,6 +2054,14 @@ mod tests {
assert_eq!(sqlserver_cell_to_json(&cell), serde_json::json!("2026-05-13 09:08:07.123"));
}
#[test]
fn sqlserver_binary_cells_are_json_hex_strings() {
let cell =
ColumnData::Binary(Some(std::borrow::Cow::Owned(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xCF, 0x53])));
assert_eq!(sqlserver_cell_to_json(&cell), serde_json::json!("0x000000000001cf53"));
}
#[test]
fn sqlserver_detects_geometry_result_columns() {
assert!(is_sqlserver_spatial_column(&SqlServerDescribedColumn {

View File

@ -45,6 +45,7 @@ fn postgres_test_config(id: &str, database: &str) -> ConnectionConfig {
redis_sentinel_tls: false,
redis_cluster_nodes: String::new(),
redis_key_separator: dbx_core::models::connection::default_redis_key_separator(),
redis_scan_page_size: None,
etcd_endpoints: String::new(),
gbase_server: String::new(),
informix_server: String::new(),