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。

15.8 KB
Raw Blame
use std::sync::{
    atomic::{AtomicU64, Ordering},
    Arc, Mutex,
};

use tauri::{AppHandle, Emitter, State};

use crate::commands::connection::AppState;

use dbx_core::query_cancel::RunningTaskMetadata;
pub use dbx_core::query_result_export::QueryResultExportRequest;
use dbx_core::query_result_export::StagedExportTarget;
use dbx_core::table_export::ExportStatus;
pub use dbx_core::table_export::TableExportProgress;

/// Allocate a temporary XLSX destination and remove files older than one week.
#[tauri::command]
pub async fn create_query_result_temp_file(extension: String) -> Result<String, String> {
    let extension = extension.trim().trim_start_matches('.');
    if !matches!(extension, "xlsx" | "html") {
        return Err("unsupported temporary export extension".to_string());
    }
    let dir = std::env::temp_dir().join("dbx-query-results");
    std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
    let now = std::time::SystemTime::now();
    if let Ok(entries) = std::fs::read_dir(&dir) {
        for entry in entries.flatten() {
            let stale = entry
                .metadata()
                .ok()
                .and_then(|m| m.modified().ok())
                .and_then(|t| now.duration_since(t).ok())
                .is_some_and(|age| age.as_secs() > 7 * 24 * 60 * 60);
            if stale {
                let _ = std::fs::remove_file(entry.path());
            }
        }
    }
    Ok(dir.join(format!("dbx-query-result-{}.{}", uuid::Uuid::new_v4(), extension)).to_string_lossy().to_string())
}

fn emit_progress(app: &AppHandle, progress: TableExportProgress) {
    let _ = app.emit("query-result-export-progress", progress);
}

#[derive(Default)]
struct RoutedExportProgress {
    terminal: Mutex<Option<TableExportProgress>>,
    rows_exported: AtomicU64,
}

impl RoutedExportProgress {
    fn take_terminal(&self) -> Option<TableExportProgress> {
        self.terminal.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).take()
    }

    fn rows_exported(&self) -> u64 {
        self.rows_exported.load(Ordering::SeqCst)
    }
}

fn route_core_progress(
    progress: TableExportProgress,
    routed: &RoutedExportProgress,
    emit: impl FnOnce(TableExportProgress),
) {
    routed.rows_exported.store(progress.rows_exported, Ordering::SeqCst);
    match progress.status {
        ExportStatus::Done => {
            let mut terminal = routed.terminal.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            if !terminal.as_ref().is_some_and(|progress| matches!(progress.status, ExportStatus::Cancelled)) {
                *terminal = Some(progress);
            }
        }
        ExportStatus::Cancelled => {
            *routed.terminal.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(progress);
        }
        _ => emit(progress),
    }
}

fn export_error_progress(export_id: &str, rows_exported: u64, error: String) -> TableExportProgress {
    TableExportProgress {
        export_id: export_id.to_string(),
        table_name: String::new(),
        rows_exported,
        total_rows: None,
        status: ExportStatus::Error,
        error_message: Some(error),
    }
}

fn finalize_staged_export(
    target: StagedExportTarget,
    export_id: &str,
    result: Result<(), String>,
    terminal: Option<TableExportProgress>,
    cancellation_requested: bool,
    rows_exported: u64,
    emit: impl Fn(TableExportProgress),
) -> Result<(), String> {
    if cancellation_requested
        || terminal.as_ref().is_some_and(|progress| matches!(progress.status, ExportStatus::Cancelled))
    {
        target.discard()?;
        let cancelled =
            terminal.filter(|progress| matches!(progress.status, ExportStatus::Cancelled)).unwrap_or_else(|| {
                TableExportProgress {
                    export_id: export_id.to_string(),
                    table_name: String::new(),
                    rows_exported,
                    total_rows: None,
                    status: ExportStatus::Cancelled,
                    error_message: Some("Export cancelled".to_string()),
                }
            });
        emit(cancelled);
        return Ok(());
    }

    if let Err(error) = result {
        target.discard()?;
        emit(export_error_progress(export_id, rows_exported, error));
        return Ok(());
    }

    let Some(done) = terminal.filter(|progress| matches!(progress.status, ExportStatus::Done)) else {
        target.discard()?;
        emit(export_error_progress(
            export_id,
            rows_exported,
            "Export finished without a completion status".to_string(),
        ));
        return Ok(());
    };

    let completion = target.commit_with_completion();
    completion.cleanup?;
    match completion.result {
        Ok(()) => emit(done),
        Err(error) => emit(export_error_progress(export_id, rows_exported, error)),
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::{Path, PathBuf};

    static NEXT_TEST_DIR: AtomicU64 = AtomicU64::new(0);

    struct TestDir(PathBuf);

    impl TestDir {
        fn new() -> Self {
            let path = std::env::temp_dir().join(format!(
                "dbx-query-result-export-{}-{}",
                std::process::id(),
                NEXT_TEST_DIR.fetch_add(1, Ordering::SeqCst)
            ));
            std::fs::create_dir_all(&path).expect("create test dir");
            Self(path)
        }

        fn path(&self) -> &Path {
            &self.0
        }
    }

    impl Drop for TestDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    fn terminal_progress(status: ExportStatus, rows_exported: u64) -> TableExportProgress {
        TableExportProgress {
            export_id: "export-1".to_string(),
            table_name: String::new(),
            rows_exported,
            total_rows: Some(rows_exported),
            status,
            error_message: None,
        }
    }

    #[test]
    fn route_core_progress_buffers_terminal_status_until_finalization() {
        let routed = RoutedExportProgress::default();
        let emitted = Mutex::new(Vec::new());
        route_core_progress(terminal_progress(ExportStatus::Done, 42), &routed, |progress| {
            emitted.lock().expect("emitted lock").push(progress);
        });

        assert!(emitted.lock().expect("emitted lock").is_empty());
        assert_eq!(routed.rows_exported(), 42);
        assert!(matches!(routed.take_terminal(), Some(TableExportProgress { status: ExportStatus::Done, .. })));
    }

    fn assert_native_stream_cancellation_emits_one_cancelled_terminal() {
        let dir = TestDir::new();
        let destination = dir.path().join("result.csv");
        std::fs::write(&destination, "original").expect("write destination");
        let target = StagedExportTarget::new(destination.to_str().expect("destination path")).expect("target");
        std::fs::write(target.path(), "partial").expect("write partial export");
        let routed = RoutedExportProgress::default();
        let emitted = Mutex::new(Vec::new());
        route_core_progress(terminal_progress(ExportStatus::Cancelled, 7), &routed, |progress| {
            emitted.lock().expect("emitted lock").push(progress);
        });
        route_core_progress(terminal_progress(ExportStatus::Done, 7), &routed, |progress| {
            emitted.lock().expect("emitted lock").push(progress);
        });
        finalize_staged_export(
            target,
            "export-1",
            Err("native stream cancelled".to_string()),
            routed.take_terminal(),
            true,
            routed.rows_exported(),
            |progress| emitted.lock().expect("emitted lock").push(progress),
        )
        .expect("finalize cancellation");

        assert!(matches!(
            emitted.lock().expect("emitted lock").as_slice(),
            [TableExportProgress { status: ExportStatus::Cancelled, .. }]
        ));
        assert_eq!(std::fs::read_to_string(destination).expect("read destination"), "original");
    }

    #[test]
    fn postgres_native_stream_cancellation_emits_one_cancelled_terminal() {
        assert_native_stream_cancellation_emits_one_cancelled_terminal();
    }

    #[test]
    fn sqlserver_native_stream_cancellation_emits_one_cancelled_terminal() {
        assert_native_stream_cancellation_emits_one_cancelled_terminal();
    }

    #[test]
    fn export_failure_preserves_existing_destination_and_emits_one_error() {
        let dir = TestDir::new();
        let destination = dir.path().join("result.csv");
        std::fs::write(&destination, "original").expect("write destination");
        let target = StagedExportTarget::new(destination.to_str().expect("destination path")).expect("target");
        std::fs::write(target.path(), "partial").expect("write partial export");
        let emitted = Mutex::new(Vec::new());

        finalize_staged_export(target, "export-1", Err("write failed".to_string()), None, false, 3, |progress| {
            emitted.lock().expect("emitted lock").push(progress);
        })
        .expect("finalize failure");

        assert!(matches!(
            emitted.lock().expect("emitted lock").as_slice(),
            [TableExportProgress { status: ExportStatus::Error, .. }]
        ));
        assert_eq!(std::fs::read_to_string(destination).expect("read destination"), "original");
    }

    #[test]
    fn staged_cleanup_failure_does_not_emit_a_releasable_terminal() {
        for commit in [false, true] {
            let dir = TestDir::new();
            let destination = dir.path().join("result.csv");
            let target = StagedExportTarget::new(destination.to_str().expect("destination path")).expect("target");
            let staged = target.path().to_path_buf();
            std::fs::remove_file(&staged).expect("remove staged file");
            std::fs::create_dir(&staged).expect("block file cleanup");
            let emitted = Mutex::new(Vec::new());
            let result = finalize_staged_export(
                target,
                "export-1",
                if commit { Ok(()) } else { Err("read failed".to_string()) },
                commit.then(|| terminal_progress(ExportStatus::Done, 3)),
                false,
                3,
                |progress| {
                    emitted.lock().expect("emitted lock").push(progress);
                },
            );
            assert!(result.is_err());
            assert!(emitted.lock().expect("emitted lock").is_empty());
            std::fs::remove_dir(staged).expect("remove cleanup blocker");
        }
    }

    #[test]
    fn replace_failure_preserves_existing_destination_and_suppresses_done() {
        let dir = TestDir::new();
        let destination = dir.path().join("result.csv");
        std::fs::write(&destination, "original").expect("write destination");
        let target = StagedExportTarget::new(destination.to_str().expect("destination path")).expect("target");
        std::fs::write(target.path(), "replacement").expect("write replacement");
        std::fs::remove_file(target.path()).expect("remove staged path");
        let emitted = Mutex::new(Vec::new());

        finalize_staged_export(
            target,
            "export-1",
            Ok(()),
            Some(terminal_progress(ExportStatus::Done, 4)),
            false,
            4,
            |progress| emitted.lock().expect("emitted lock").push(progress),
        )
        .expect("finalize replacement failure");

        assert!(matches!(
            emitted.lock().expect("emitted lock").as_slice(),
            [TableExportProgress { status: ExportStatus::Error, .. }]
        ));
        assert_eq!(std::fs::read_to_string(destination).expect("read destination"), "original");
    }
}

#[tauri::command]
pub async fn start_query_result_export(
    app: AppHandle,
    state: State<'_, Arc<AppState>>,
    mut request: QueryResultExportRequest,
) -> Result<(), String> {
    dbx_core::query::mysql_data_charset_policy::check_export(&state, &request.connection_id, &request.format).await?;
    let state = state.inner().clone();
    let export_id = request.export_id.clone();
    let target = StagedExportTarget::new(&request.file_path)?;
    request.file_path = target.path_string()?;

    // Exports interleave async fetches with synchronous row formatting and
    // buffered disk writes; run them off the async workers (see spawn_export_task).
    dbx_core::export_runtime::spawn_export_task(async move {
        let execution_id = request.execution_id.clone().filter(|id| !id.trim().is_empty());
        let registered_query = execution_id.as_ref().map(|id| {
            state.running_queries.register_task(
                id.clone(),
                RunningTaskMetadata::query(
                    request.connection_id.clone(),
                    request.database.clone(),
                    request.client_session_id.clone(),
                ),
            )
        });
        let cancel_token = registered_query.as_ref().map(|query| query.token());
        let routed_progress = Arc::new(RoutedExportProgress::default());
        let routed_progress_handler = routed_progress.clone();
        let completion = dbx_core::query_result_export::export_query_result_core_with_completion(
            &state,
            &request,
            cancel_token.clone(),
            |progress| {
                route_core_progress(progress, &routed_progress_handler, |progress| {
                    emit_progress(&app, progress);
                });
            },
        )
        .await;
        drop(registered_query);
        let terminal = routed_progress.take_terminal();
        let cancellation_requested =
            terminal.as_ref().is_some_and(|progress| matches!(progress.status, ExportStatus::Cancelled))
                || cancel_token.as_ref().is_some_and(|token| token.is_cancelled())
                || dbx_core::database_export::is_export_cancelled(&export_id).await;
        let result = if let Err(error) = completion.cleanup {
            let _ = target.discard();
            Err(error)
        } else {
            finalize_staged_export(
                target,
                &export_id,
                completion.result,
                terminal,
                cancellation_requested,
                routed_progress.rows_exported(),
                |progress| emit_progress(&app, progress),
            )
        };
        if let Err(error) = result {
            let _ = app.emit(
                "query-result-export-progress",
                serde_json::json!({
                    "exportId": export_id, "tableName": "Query Result",
                    "rowsExported": routed_progress.rows_exported(), "totalRows": null,
                    "status": "Unknown", "errorMessage": error,
                }),
            );
        }

        dbx_core::database_export::clear_export_cancelled(&export_id).await;
    });

    Ok(())
}

#[tauri::command]
pub async fn cancel_query_result_export(
    state: State<'_, Arc<AppState>>,
    export_id: String,
    execution_id: Option<String>,
) -> Result<(), String> {
    dbx_core::database_export::set_export_cancelled(&export_id).await;
    if let Some(execution_id) = execution_id.filter(|id| !id.trim().is_empty()) {
        state.running_queries.cancel(&execution_id);
    }
    Ok(())
}

#[tauri::command]
pub async fn open_query_result_temp_file(app: AppHandle, path: String) -> Result<(), String> {
    use tauri_plugin_opener::OpenerExt;
    let root = std::fs::canonicalize(std::env::temp_dir().join("dbx-query-results")).map_err(|e| e.to_string())?;
    let resolved = std::fs::canonicalize(&path).map_err(|e| e.to_string())?;
    if resolved.parent() != Some(root.as_path())
        || !matches!(resolved.extension().and_then(|e| e.to_str()), Some("xlsx") | Some("html"))
        || !resolved.is_file()
    {
        return Err("invalid temporary query result path".to_string());
    }
    app.opener().open_path(resolved.to_string_lossy().to_string(), None::<&str>).map_err(|e| e.to_string())
}