fix(sql): decode opened SQL files with charset fallback

This commit is contained in:
t8y2 committed 2026-06-08 17:45:09 +08:00
1 parent 3fc6c0eaba
commit 465ed195cf
2 files changed
+16 -3

No files matched your search

+15 -1
View File
@@ -1,6 +1,8 @@
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use dbx_core::sql::decode_sql_file_bytes;
#[tauri::command]
pub fn pending_open_sql_files(state: tauri::State<'_, ExternalSqlOpenState>) -> Vec<String> {
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
@@ -64,7 +66,8 @@ pub fn read_external_sql_file_content(path: &Path) -> Result<String, String> {
if !is_sql_file_path(path) {
return Err("Only .sql files can be opened this way".to_string());
}
std::fs::read_to_string(path).map_err(|e| format!("Failed to read SQL file: {e}"))
let bytes = std::fs::read(path).map_err(|e| format!("Failed to read SQL file: {e}"))?;
decode_sql_file_bytes(&bytes)
}
fn dedupe_paths(paths: Vec<String>) -> Vec<String> {
@@ -115,6 +118,17 @@ mod tests {
assert_eq!(result.unwrap(), "select 1;");
}
#[test]
fn reads_gbk_external_sql_file_content() {
let path = std::env::temp_dir().join(format!("dbx-test-{}.sql", uuid::Uuid::new_v4()));
std::fs::write(&path, b"select '\xD6\xD0\xCE\xC4';").unwrap();
let result = read_external_sql_file_content(&path);
let _ = std::fs::remove_file(&path);
assert_eq!(result.unwrap(), "select '中文';");
}
#[test]
fn rejects_external_non_sql_file_content() {
let path = std::env::temp_dir().join(format!("dbx-test-{}.txt", uuid::Uuid::new_v4()));