From 465ed195cfb0a306ef8cba5c8fa3c3c77d3bcc30 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Mon, 8 Jun 2026 17:45:09 +0800 Subject: [PATCH] fix(sql): decode opened SQL files with charset fallback --- apps/desktop/src/App.vue | 3 +-- src-tauri/src/commands/external_sql.rs | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/apps/desktop/src/App.vue b/apps/desktop/src/App.vue index 68e96fa127..3b73224860 100644 --- a/apps/desktop/src/App.vue +++ b/apps/desktop/src/App.vue @@ -454,10 +454,9 @@ async function openSqlFile() { try { if (isTauriRuntime()) { const { open } = await import("@tauri-apps/plugin-dialog"); - const { readTextFile } = await import("@tauri-apps/plugin-fs"); const path = await open({ filters: [{ name: "SQL", extensions: ["sql"] }], multiple: false }); if (path) { - const content = await readTextFile(path as string); + const content = await api.readExternalSqlFile(path as string); queryStore.updateSql(tab.id, content); } } else { diff --git a/src-tauri/src/commands/external_sql.rs b/src-tauri/src/commands/external_sql.rs index e33d9d4454..a162d8f187 100644 --- a/src-tauri/src/commands/external_sql.rs +++ b/src-tauri/src/commands/external_sql.rs @@ -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 { 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 { 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) -> Vec { @@ -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()));