fix: escape backslashes in SQL export and transfer literal formatting

JSON fields containing backslashes (regex patterns, escaped quotes, etc.)
were not properly escaped during database export and data transfer. The
backslash was written as-is in INSERT statements, causing data corruption
when re-importing. This aligns with the existing fix in data_grid_sql.rs.
This commit is contained in:
t8y2 2026-05-25 10:44:55 +08:00
parent 9b41bb8d81
commit 98b34a4db2
2 changed files with 3 additions and 3 deletions

View File

@ -126,7 +126,7 @@ pub fn format_export_sql_literal(value: &Value) -> String {
return if value { "TRUE" } else { "FALSE" }.to_string();
}
let text = value.as_str().map_or_else(|| value.to_string(), ToString::to_string);
format!("'{}'", text.replace('\'', "''"))
format!("'{}'", text.replace('\\', "\\\\").replace('\'', "''"))
}
pub fn build_export_insert_statements(options: BuildExportInsertStatementsOptions) -> Result<Vec<String>, String> {

View File

@ -106,11 +106,11 @@ pub fn escape_value_typed(val: &serde_json::Value, db_type: &DatabaseType, colum
},
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::String(s) => {
format!("'{}'", format_literal_string(s, db_type, column_type).replace('\'', "''"))
format!("'{}'", format_literal_string(s, db_type, column_type).replace('\\', "\\\\").replace('\'', "''"))
}
_ => {
let s = val.to_string();
format!("'{}'", s.replace('\'', "''"))
format!("'{}'", s.replace('\\', "\\\\").replace('\'', "''"))
}
}
}