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。

56.2 KB
Raw Blame
use std::sync::Arc;
use std::time::{Duration, Instant};
use tauri::{AppHandle, Emitter, State};

use crate::commands::connection::AppState;
use dbx_core::backend_error::BackendError;
use dbx_core::db;
use dbx_core::models::connection::DatabaseType;
use dbx_core::query_cancel::RunningTaskMetadata;
use dbx_core::sql::split_sql_statements;

fn query_messages_callback(
    app: &AppHandle,
    execution_id: Option<&str>,
) -> Option<dbx_core::query::query_messages::QueryMessagesCallback> {
    let execution_id = execution_id?.to_string();
    let app = app.clone();
    Some(Arc::new(move |messages| {
        let _ = app.emit("query-messages", serde_json::json!({ "executionId": execution_id, "messages": messages }));
    }))
}

#[derive(Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ExecuteMultiProgress {
    execution_id: String,
    statement_index: usize,
    completed: usize,
    total: usize,
    success: bool,
    execution_time_ms: u128,
    affected_rows: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<BackendError>,
}

#[derive(Debug, serde::Serialize)]
#[serde(untagged)]
pub enum ManualTransactionCommandError {
    Structured(Box<BackendError>),
    Legacy(String),
}

fn manual_transaction_command_error(error: String) -> ManualTransactionCommandError {
    if let Some(error) = dbx_core::query::sqlserver_manual_transaction::backend_error(&error) {
        return ManualTransactionCommandError::Structured(Box::new(error));
    }
    if dbx_core::query::is_manual_transaction_session_expired_error(&error) {
        ManualTransactionCommandError::Structured(Box::new(BackendError::from_manual_transaction_session_expired()))
    } else {
        ManualTransactionCommandError::Legacy(error)
    }
}

#[tauri::command]
#[allow(clippy::too_many_arguments)]
pub async fn execute_query(
    app: AppHandle,
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    sql: String,
    schema: Option<String>,
    catalog: Option<String>,
    execution_id: Option<String>,
    max_rows: Option<usize>,
    fetch_size: Option<usize>,
    page_size: Option<usize>,
    row_offset: Option<usize>,
    result_session_id: Option<String>,
    client_session_id: Option<String>,
    timeout_secs: Option<u64>,
    execution_mode: Option<dbx_core::query::QueryExecutionMode>,
) -> Result<db::QueryResult, BackendError> {
    let execution_id = execution_id.filter(|id| !id.trim().is_empty());
    let registered_query = execution_id.as_ref().map(|id| {
        state.running_queries.register_task(
            id.clone(),
            RunningTaskMetadata::query(connection_id.clone(), database.clone(), client_session_id.clone()),
        )
    });
    let cancel_token = registered_query.as_ref().map(|query| query.token());

    let messages = query_messages_callback(&app, execution_id.as_deref());
    let result = dbx_core::query::query_messages::with_query_messages(
        messages,
        dbx_core::query::execute_sql_statement_with_options_typed(
            &state,
            &connection_id,
            &database,
            &sql,
            schema.as_deref(),
            cancel_token,
            dbx_core::query::QueryExecutionOptions {
                max_rows,
                fetch_size,
                page_size,
                row_offset,
                catalog,
                result_session_id,
                client_session_id,
                timeout_secs,
                execution_id,
                execution_mode: execution_mode.unwrap_or_default(),
                ..Default::default()
            },
        ),
    )
    .await;

    if let Some(registered_query) = registered_query {
        registered_query.finish(&result);
    }

    result.map_err(dbx_core::query::QueryExecutionError::into_backend_error)
}

#[tauri::command]
#[allow(clippy::too_many_arguments)]
pub async fn execute_conditional_update(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    sql: String,
    schema: Option<String>,
    catalog: Option<String>,
    execution_id: Option<String>,
    max_rows: Option<usize>,
    fetch_size: Option<usize>,
    page_size: Option<usize>,
    row_offset: Option<usize>,
    result_session_id: Option<String>,
    client_session_id: Option<String>,
    timeout_secs: Option<u64>,
    execution_mode: Option<dbx_core::query::QueryExecutionMode>,
    type_recovery: Option<dbx_core::storage::TypeRecoveryContext>,
) -> Result<db::QueryResult, BackendError> {
    let execution_id =
        execution_id.filter(|id| !id.trim().is_empty()).unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
    let recovery = type_recovery.map(|context| dbx_core::storage::TypeExecutionBinding {
        execution_id: execution_id.clone(),
        context,
        connection_id: connection_id.clone(),
        database: database.clone(),
        schema: schema.clone(),
        sql: sql.clone(),
    });
    if let Some(binding) = &recovery {
        state.storage.claim_type_execution(binding).await.map_err(|error| BackendError::from_legacy_backend(&error))?;
    }
    let durable_recovery = recovery.is_some();
    let registered = state.running_queries.register_task_for_terminal_confirmation(
        execution_id.clone(),
        RunningTaskMetadata::query(connection_id.clone(), database.clone(), client_session_id.clone()),
    );
    let cancel_token = registered.token();
    let response_timeout = dbx_core::query::query_timeout_duration(timeout_secs);
    let app_state = state.inner().clone();
    let (result_tx, result_rx) = tokio::sync::oneshot::channel();

    tokio::spawn(async move {
        let result = dbx_core::query::execute_sql_statement_with_options_typed(
            &app_state,
            &connection_id,
            &database,
            &sql,
            schema.as_deref(),
            Some(cancel_token),
            dbx_core::query::QueryExecutionOptions {
                max_rows,
                fetch_size,
                page_size,
                row_offset,
                catalog,
                result_session_id,
                client_session_id,
                timeout_secs: Some(0),
                await_cancel_completion: true,
                execution_id: Some(execution_id),
                execution_mode: execution_mode.unwrap_or_default(),
                ..Default::default()
            },
        )
        .await;
        let result = dbx_core::query_cancel::finish_conditional_update(
            registered,
            &app_state.storage,
            recovery.as_ref(),
            result,
        )
        .await;
        let _ = result_tx.send(result);
    });

    match response_timeout {
        Some(timeout) => match tokio::time::timeout(timeout, result_rx).await {
            Ok(Ok(result)) => result,
            Ok(Err(_)) => {
                let message = "Conditional update execution task stopped unexpectedly";
                Err(if durable_recovery {
                    BackendError::from_legacy_backend(message)
                } else {
                    BackendError::from_sql_detail(message)
                })
            }
            Err(_) => Err(BackendError::from_timeout_detail(&format!(
                "Query timed out after {} seconds",
                timeout.as_secs().max(1)
            ))),
        },
        None => match result_rx.await {
            Ok(result) => result,
            Err(_) => {
                let message = "Conditional update execution task stopped unexpectedly";
                Err(if durable_recovery {
                    BackendError::from_legacy_backend(message)
                } else {
                    BackendError::from_sql_detail(message)
                })
            }
        },
    }
}

#[tauri::command]
#[allow(clippy::too_many_arguments)]
pub async fn execute_multi(
    app: AppHandle,
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    sql: String,
    schema: Option<String>,
    catalog: Option<String>,
    execution_id: Option<String>,
    max_rows: Option<usize>,
    fetch_size: Option<usize>,
    page_size: Option<usize>,
    row_offset: Option<usize>,
    max_result_bytes: Option<usize>,
    result_key_columns: Option<Vec<String>>,
    table_data_preview: Option<bool>,
    result_session_id: Option<String>,
    client_session_id: Option<String>,
    timeout_secs: Option<u64>,
    use_transaction: Option<bool>,
    continue_on_error: Option<bool>,
    execution_mode: Option<dbx_core::query::QueryExecutionMode>,
    preserve_explicit_transaction: Option<bool>,
    defer_lobs: Option<bool>,
) -> Result<Vec<dbx_core::query::ExecuteMultiResult>, BackendError> {
    let execution_id = execution_id.filter(|id| !id.trim().is_empty());
    let registered_query = execution_id.as_ref().map(|id| {
        state.running_queries.register_task(
            id.clone(),
            RunningTaskMetadata::query(connection_id.clone(), database.clone(), client_session_id.clone()),
        )
    });
    let cancel_token = registered_query.as_ref().map(|query| query.token());
    let progress = execution_id.as_ref().map(|execution_id| {
        let app = app.clone();
        let execution_id = execution_id.clone();
        Arc::new(move |progress: dbx_core::query::ExecuteMultiProgress| {
            let _ = app.emit(
                "query-batch-progress",
                ExecuteMultiProgress {
                    execution_id: execution_id.clone(),
                    statement_index: progress.statement_index,
                    completed: progress.completed,
                    total: progress.total,
                    success: progress.success,
                    execution_time_ms: progress.execution_time_ms,
                    affected_rows: progress.affected_rows,
                    error: progress.error,
                },
            );
        }) as dbx_core::query::ExecuteMultiProgressCallback
    });
    let trace_id = execution_id.as_deref().unwrap_or("no-execution-id").to_string();
    let started_at = Instant::now();
    dbx_core::sql_diagnostics::debug_sql("query:execute_multi:start", &sql);
    log::info!(
        "[query][execute_multi:start] trace_id={} connection_id={} database={} schema={:?}",
        trace_id,
        connection_id,
        database,
        schema
    );

    let messages = query_messages_callback(&app, execution_id.as_deref());
    let result = dbx_core::query::query_messages::with_query_messages(
        messages,
        dbx_core::query::batch_progress::with_coalesced_execute_multi_progress(progress, |progress| {
            dbx_core::query::execute_multi_core_with_options_for_client_and_progress_typed(
                &state,
                &connection_id,
                &database,
                &sql,
                schema.as_deref(),
                cancel_token,
                dbx_core::query::QueryExecutionOptions {
                    max_rows,
                    fetch_size,
                    page_size,
                    row_offset,
                    max_result_bytes,
                    result_key_columns: result_key_columns.unwrap_or_default(),
                    table_data_preview: table_data_preview.unwrap_or(false),
                    catalog,
                    result_session_id,
                    client_session_id,
                    timeout_secs,
                    await_cancel_completion: false,
                    execution_id,
                    use_transaction,
                    continue_on_error: continue_on_error.unwrap_or(false),
                    execution_mode: execution_mode.unwrap_or_default(),
                    preserve_explicit_transaction: preserve_explicit_transaction.unwrap_or(false),
                    defer_lobs: defer_lobs.unwrap_or(false),
                },
                progress,
            )
        }),
    )
    .await;
    match &result {
        Ok(results) => log::info!(
            "[query][execute_multi:done] trace_id={} elapsed_ms={} result_count={} row_counts={:?} backend_execution_times_ms={:?}",
            trace_id,
            started_at.elapsed().as_millis(),
            results.len(),
            results.iter().map(|result| result.result.rows.len()).collect::<Vec<_>>(),
            results.iter().map(|result| result.result.execution_time_ms).collect::<Vec<_>>()
        ),
        Err(error) => log::error!(
            "[query][execute_multi:error] trace_id={} elapsed_ms={} error={}",
            trace_id,
            started_at.elapsed().as_millis(),
            error
        ),
    }

    if let Some(registered_query) = registered_query {
        registered_query.finish(&result);
    }

    result.map_err(dbx_core::query::QueryExecutionError::into_backend_error)
}

#[tauri::command]
pub async fn cancel_query(state: State<'_, Arc<AppState>>, execution_id: String) -> Result<bool, String> {
    Ok(state.running_queries.cancel(&execution_id))
}

#[tauri::command]
pub async fn cancel_conditional_update(
    state: State<'_, Arc<AppState>>,
    execution_id: String,
    cancel: Option<bool>,
    type_recovery: Option<dbx_core::storage::TypeExecutionBinding>,
) -> Result<dbx_core::query_cancel::CancellationWaitResult, String> {
    if let Some(binding) = type_recovery {
        if cancel != Some(false) || binding.execution_id != execution_id {
            return Err("TYPE recovery observation requires the original execution ID and cancel=false".into());
        }
        return dbx_core::query_cancel::observe_type_execution(
            &state.storage,
            &state.running_queries,
            &binding,
            Duration::from_secs(10),
        )
        .await;
    }
    Ok(if cancel == Some(false) {
        state.running_queries.wait_for_completion(&execution_id, Duration::from_secs(10)).await
    } else {
        state.running_queries.cancel_and_wait(&execution_id, Duration::from_secs(10)).await
    })
}

#[tauri::command]
pub async fn close_query_session(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    session_id: String,
    client_session_id: Option<String>,
    catalog: Option<String>,
    txn_session_id: Option<String>,
) -> Result<bool, String> {
    if let Some(txn_session_id) = txn_session_id {
        return dbx_core::query::close_manual_transaction_query_session(
            &state,
            &connection_id,
            &txn_session_id,
            &session_id,
        )
        .await;
    }
    dbx_core::query::close_query_session(
        &state,
        &connection_id,
        &database,
        &session_id,
        client_session_id.as_deref(),
        catalog.as_deref(),
    )
    .await
}

#[tauri::command]
pub async fn read_large_value_chunk(
    state: State<'_, Arc<AppState>>,
    request: dbx_core::query::LargeValueRequest,
    report_outcome: Option<bool>,
) -> Result<serde_json::Value, String> {
    if report_outcome == Some(true) {
        Ok(snapshot_outcome(dbx_core::query::request_large_value_reported(&state, request).await))
    } else {
        dbx_core::query::request_large_value(&state, request, false).await
    }
}

fn snapshot_outcome<T: serde::Serialize>(
    result: Result<T, dbx_core::query::LargeValueOperationError>,
) -> serde_json::Value {
    match result {
        Ok(value) => serde_json::json!({"status": "succeeded", "value": value}),
        Err(error) => serde_json::json!({"status": error.status, "message": error.message}),
    }
}

#[tauri::command]
pub async fn release_large_value(
    state: State<'_, Arc<AppState>>,
    request: dbx_core::query::LargeValueRequest,
) -> Result<serde_json::Value, String> {
    dbx_core::query::request_large_value(&state, request, true).await
}

#[tauri::command]
pub async fn download_large_value(
    state: State<'_, Arc<AppState>>,
    request: dbx_core::query::LargeValueRequest,
    file_path: String,
) -> Result<u64, String> {
    let path = std::path::Path::new(&file_path);
    let parent = path.parent().filter(|parent| !parent.as_os_str().is_empty()).unwrap_or(std::path::Path::new("."));
    let mut temporary = tempfile::NamedTempFile::new_in(parent).map_err(|error| error.to_string())?;
    let written = dbx_core::query::write_large_value_snapshot(&state, request, temporary.as_file_mut()).await?;
    temporary.persist(path).map_err(|error| error.to_string())?;
    Ok(written)
}

#[tauri::command]
pub async fn close_client_connection_session(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    client_session_id: String,
    catalog: Option<String>,
    report_completion: Option<bool>,
) -> Result<bool, String> {
    let database = query_session_database(&database, catalog.as_deref());
    if report_completion == Some(true) {
        return state.close_client_session_pool_with_completion(&connection_id, database, &client_session_id).await;
    }
    state.close_client_session_pool(&connection_id, database, &client_session_id).await
}

#[tauri::command]
pub async fn export_snapshot_result(
    state: State<'_, Arc<AppState>>,
    request: dbx_core::query::snapshot_export::SnapshotExportRequest,
    file_path: String,
    report_outcome: Option<bool>,
) -> Result<serde_json::Value, String> {
    if report_outcome == Some(true) {
        return Ok(snapshot_outcome(write_reported_snapshot(&state, request, &file_path).await));
    }
    let path = std::path::Path::new(&file_path);
    let parent = path.parent().filter(|parent| !parent.as_os_str().is_empty()).unwrap_or(std::path::Path::new("."));
    let mut temporary = tempfile::NamedTempFile::new_in(parent).map_err(|error| error.to_string())?;
    dbx_core::query::snapshot_export::write_snapshot_export(&state, request, temporary.as_file_mut()).await?;
    temporary.persist(path).map_err(|error| error.to_string())?;
    Ok(serde_json::Value::Null)
}

async fn write_reported_snapshot(
    state: &AppState,
    request: dbx_core::query::snapshot_export::SnapshotExportRequest,
    file_path: &str,
) -> Result<(), dbx_core::query::LargeValueOperationError> {
    use dbx_core::query::{LargeValueCompletion, LargeValueOperationError};
    let path = std::path::Path::new(file_path);
    let parent = path.parent().filter(|parent| !parent.as_os_str().is_empty()).unwrap_or(std::path::Path::new("."));
    let mut temporary = tempfile::NamedTempFile::new_in(parent).map_err(|error| error.to_string())?;
    if let Err(error) =
        dbx_core::query::snapshot_export::write_snapshot_export_reported(state, request, temporary.as_file_mut()).await
    {
        return match temporary.close() {
            Ok(()) => Err(error),
            Err(cleanup) => Err(LargeValueOperationError {
                status: LargeValueCompletion::Unknown,
                message: format!("{}; snapshot cleanup failed: {cleanup}", error.message),
            }),
        };
    }
    match temporary.persist(path) {
        Ok(_) => Ok(()),
        Err(error) => {
            let message = error.error.to_string();
            match error.file.close() {
                Ok(()) => Err(message.into()),
                Err(cleanup) => Err(LargeValueOperationError {
                    status: LargeValueCompletion::Unknown,
                    message: format!("{message}; snapshot cleanup failed: {cleanup}"),
                }),
            }
        }
    }
}

fn query_session_database<'a>(database: &'a str, catalog: Option<&str>) -> Option<&'a str> {
    if database.trim().is_empty() || catalog.is_some() {
        None
    } else {
        Some(database)
    }
}

#[tauri::command]
pub async fn execute_batch(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    statements: Vec<String>,
    schema: Option<String>,
    timeout_secs: Option<u64>,
    use_transaction: Option<bool>,
    bound_statements: Option<Vec<db::BlobBoundStatement>>,
) -> Result<db::QueryResult, String> {
    if let Some(bound) = bound_statements.as_ref() {
        return dbx_core::query::execute_blob_bound_statements(
            &state,
            &connection_id,
            &database,
            &statements,
            bound,
            schema.as_deref(),
            use_transaction == Some(true),
            timeout_secs,
        )
        .await;
    }
    dbx_core::query::execute_statements_with_transaction_option(
        &state,
        &connection_id,
        &database,
        &statements,
        schema.as_deref(),
        use_transaction == Some(true),
        timeout_secs,
    )
    .await
}

#[tauri::command]
pub async fn execute_script(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    sql: String,
    schema: Option<String>,
) -> Result<db::QueryResult, String> {
    let db_type = {
        let configs = state.configs.read().await;
        configs.get(&connection_id).map(|config| config.db_type)
    };

    dbx_core::query::execute_statements(
        &state,
        &connection_id,
        &database,
        &db_type.map_or_else(
            || split_sql_statements(&sql),
            |db_type| dbx_core::sql::split_sql_statements_for_database(&sql, db_type),
        ),
        schema.as_deref(),
        None,
    )
    .await
}

#[tauri::command]
pub async fn execute_in_transaction(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    statements: Vec<String>,
    schema: Option<String>,
    catalog: Option<String>,
    bound_statements: Option<Vec<db::BlobBoundStatement>>,
) -> Result<db::QueryResult, String> {
    if let Some(bound) = bound_statements.as_ref() {
        return dbx_core::query::execute_blob_bound_statements(
            &state,
            &connection_id,
            &database,
            &statements,
            bound,
            schema.as_deref(),
            true,
            None,
        )
        .await;
    }
    dbx_core::query::execute_statements_in_transaction(
        &state,
        &connection_id,
        &database,
        &statements,
        schema.as_deref(),
        catalog.as_deref(),
    )
    .await
}

/// Schema Diff deploy entrypoint (legacy command name kept for API compatibility).
///
/// Executes as one single-connection transaction via [`execute_schema_diff_deploy`].
/// On failure, status is `rolled_back` when DDL/DML atomicity is guaranteed for the
/// target, otherwise `mixed` with a best-effort `executed_count` (e.g. MySQL/Oracle DDL).
pub async fn execute_script_with_2pc_core(
    app: Arc<AppState>,
    connection_id: String,
    database: String,
    statements: Vec<String>,
    schema: Option<String>,
    destructive_confirmed: bool,
) -> dbx_core::query::SchemaDiffDeployResult {
    dbx_core::query::execute_schema_diff_deploy(
        &app,
        &connection_id,
        &database,
        &statements,
        schema.as_deref(),
        destructive_confirmed,
    )
    .await
}

#[tauri::command]
pub async fn execute_script_with_2pc(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    statements: Vec<String>,
    schema: Option<String>,
    destructive_confirmed: Option<bool>,
) -> Result<dbx_core::query::SchemaDiffDeployResult, String> {
    let app: Arc<AppState> = (*state).clone();
    Ok(execute_script_with_2pc_core(
        app,
        connection_id,
        database,
        statements,
        schema,
        destructive_confirmed.unwrap_or(false),
    )
    .await)
}

#[tauri::command]
pub async fn begin_manual_transaction(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    schema: Option<String>,
    catalog: Option<String>,
) -> Result<String, ManualTransactionCommandError> {
    dbx_core::query::begin_manual_transaction(&state, &connection_id, &database, schema.as_deref(), catalog.as_deref())
        .await
        .map_err(manual_transaction_command_error)
}

#[tauri::command]
#[allow(clippy::too_many_arguments)]
pub async fn execute_in_manual_transaction(
    app: AppHandle,
    state: State<'_, Arc<AppState>>,
    txn_session_id: String,
    sql: String,
    database: String,
    schema: Option<String>,
    max_rows: Option<usize>,
    table_data_preview: Option<bool>,
    page_size: Option<usize>,
    result_session_id: Option<String>,
    classification_sql: Option<String>,
    execution_id: Option<String>,
    timeout_secs: Option<u64>,
    bound_statements: Option<Vec<db::BlobBoundStatement>>,
    defer_lobs: Option<bool>,
) -> Result<Vec<dbx_core::query::ExecuteMultiResult>, ManualTransactionCommandError> {
    let messages = query_messages_callback(&app, execution_id.as_deref());
    dbx_core::query::query_messages::with_query_messages(
        messages,
        dbx_core::query::execute_in_manual_transaction_with_options(
            &state,
            &txn_session_id,
            &sql,
            &database,
            schema.as_deref(),
            dbx_core::query::ManualTransactionExecutionOptions {
                defer_lobs: defer_lobs.unwrap_or(false),
                bound_statements,
                max_rows,
                table_data_preview: table_data_preview.unwrap_or(false),
                page_size,
                result_session_id,
                classification_sql,
                execution_id,
                timeout_secs,
            },
        ),
    )
    .await
    .map_err(manual_transaction_command_error)
}

#[tauri::command]
pub async fn commit_manual_transaction(
    state: State<'_, Arc<AppState>>,
    txn_session_id: String,
) -> Result<db::QueryResult, ManualTransactionCommandError> {
    dbx_core::query::commit_manual_transaction(&state, &txn_session_id).await.map_err(manual_transaction_command_error)
}

#[tauri::command]
pub async fn rollback_manual_transaction(
    state: State<'_, Arc<AppState>>,
    txn_session_id: String,
) -> Result<db::QueryResult, ManualTransactionCommandError> {
    dbx_core::query::rollback_manual_transaction(&state, &txn_session_id)
        .await
        .map_err(manual_transaction_command_error)
}

#[tauri::command]
pub async fn analyze_sql_references(
    sql: String,
    dialect: Option<String>,
) -> Result<dbx_core::sql_analysis::SqlReferenceAnalysis, String> {
    dbx_core::sql_analysis::analyze_sql_references(&sql, dialect.as_deref())
}

#[tauri::command]
pub fn find_statement_at_cursor(
    sql: String,
    cursor_pos: usize,
    database_type: Option<DatabaseType>,
) -> Result<String, String> {
    Ok(database_type
        .map(|db_type| dbx_core::sql::find_statement_at_cursor_for_database(&sql, cursor_pos, db_type))
        .unwrap_or_else(|| dbx_core::sql::find_statement_at_cursor(&sql, cursor_pos)))
}

#[tauri::command]
pub fn prepare_query_pagination_execution_plan(
    options: dbx_core::query_result_sql::QueryPaginationExecutionPlanOptions,
) -> Result<dbx_core::query_result_sql::QueryPaginationExecutionPlan, String> {
    Ok(dbx_core::query_result_sql::build_query_pagination_execution_plan(options))
}

#[tauri::command]
pub fn build_sorted_query_sql(
    options: dbx_core::query_result_sql::SortedQuerySqlOptions,
) -> Result<dbx_core::query_result_sql::QuerySqlBuildResult, String> {
    Ok(dbx_core::query_result_sql::build_sorted_query_sql(options))
}

#[tauri::command]
pub fn build_explain_sql(
    options: dbx_core::query_execution_sql::ExplainSqlOptions,
) -> Result<dbx_core::query_execution_sql::ExplainSqlBuildResult, String> {
    Ok(dbx_core::query_execution_sql::build_explain_sql(options))
}

#[tauri::command]
pub fn build_dropped_file_preview_sql(
    options: dbx_core::query_execution_sql::DroppedFilePreviewSqlOptions,
) -> Result<Option<String>, String> {
    Ok(dbx_core::query_execution_sql::build_dropped_file_preview_sql(options))
}

#[tauri::command]
pub fn build_table_select_sql(
    options: dbx_core::sql_dialect::TableDataSelectSqlOptions,
    include_database_name: Option<bool>,
) -> Result<String, String> {
    Ok(dbx_core::sql_dialect::build_table_data_select_sql_with_database(
        options,
        include_database_name.unwrap_or(false),
    ))
}

#[tauri::command]
pub fn build_database_search_sql(
    options: dbx_core::database_search_sql::DatabaseSearchSqlOptions,
) -> Result<Option<dbx_core::database_search_sql::DatabaseSearchSql>, String> {
    Ok(dbx_core::database_search_sql::build_database_search_sql(options))
}

#[tauri::command]
pub fn build_database_structure_search_sql(
    options: dbx_core::database_structure_search_sql::DatabaseStructureSearchSqlOptions,
) -> Result<Option<dbx_core::database_structure_search_sql::DatabaseStructureSearchSql>, String> {
    Ok(dbx_core::database_structure_search_sql::build_database_structure_search_sql(options))
}

#[tauri::command]
pub fn build_database_search_columns_sql(
    options: dbx_core::database_search_columns_sql::DatabaseSearchColumnsSqlOptions,
) -> Result<Option<dbx_core::database_search_columns_sql::DatabaseSearchColumnsSql>, String> {
    Ok(dbx_core::database_search_columns_sql::build_database_search_columns_sql(options))
}

#[tauri::command]
pub fn build_database_search_probe_sql(
    options: dbx_core::database_search_probe_sql::DatabaseSearchProbeSqlOptions,
) -> Result<Option<dbx_core::database_search_probe_sql::DatabaseSearchProbeSql>, String> {
    Ok(dbx_core::database_search_probe_sql::build_database_search_probe_sql(options))
}

#[tauri::command]
pub fn supports_database_structure_search(
    database_type: Option<dbx_core::models::connection::DatabaseType>,
    driver_profile: Option<String>,
) -> Result<bool, String> {
    Ok(dbx_core::database_structure_search_sql::supports_database_structure_search(
        database_type,
        driver_profile.as_deref(),
    ))
}

#[tauri::command]
pub fn build_search_result_where(
    options: dbx_core::database_search_sql::SearchResultWhereOptions,
) -> Result<String, String> {
    Ok(dbx_core::database_search_sql::build_search_result_where(options))
}

#[tauri::command]
pub fn build_rename_object_sql(options: dbx_core::db_admin_sql::RenameObjectSqlOptions) -> Result<String, String> {
    dbx_core::db_admin_sql::build_rename_object_sql(options)
}

#[tauri::command]
pub fn build_rename_database_sql(
    database_type: Option<dbx_core::models::connection::DatabaseType>,
    old_name: String,
    new_name: String,
    terminate_connections: bool,
) -> Result<String, String> {
    dbx_core::db_admin_sql::build_rename_database_sql(database_type, &old_name, &new_name, terminate_connections)
}

#[tauri::command]
pub fn build_rename_database_preflight_sql(
    database_type: Option<dbx_core::models::connection::DatabaseType>,
    database_name: String,
) -> Result<String, String> {
    dbx_core::db_admin_sql::build_rename_database_preflight_sql(database_type, &database_name)
}

#[tauri::command]
pub fn build_create_database_sql(options: dbx_core::db_admin_sql::CreateDatabaseSqlOptions) -> Result<String, String> {
    dbx_core::db_admin_sql::build_create_database_sql(options)
}

#[cfg(feature = "duckdb-sidecar")]
#[tauri::command]
pub fn build_duckdb_attach_database_sql(
    options: dbx_core::db_admin_sql::DuckDbAttachDatabaseSqlOptions,
) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_duckdb_attach_database_sql(options))
}

#[tauri::command]
pub fn build_sqlite_attach_database_sql(
    options: dbx_core::db_admin_sql::SqliteAttachDatabaseSqlOptions,
) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_sqlite_attach_database_sql(options))
}

#[tauri::command]
pub fn build_drop_object_sql(options: dbx_core::db_admin_sql::DropObjectSqlOptions) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_drop_object_sql(options))
}

#[tauri::command]
pub fn build_drop_table_sql(options: dbx_core::db_admin_sql::TableAdminSqlOptions) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_drop_table_sql(options))
}

#[tauri::command]
pub fn build_drop_table_child_object_sql(
    options: dbx_core::db_admin_sql::DropTableChildObjectSqlOptions,
) -> Result<String, String> {
    dbx_core::db_admin_sql::build_drop_table_child_object_sql(options)
}

#[tauri::command]
pub fn build_empty_table_sql(options: dbx_core::db_admin_sql::TableAdminSqlOptions) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_empty_table_sql(options))
}

#[tauri::command]
pub fn build_truncate_table_sql(options: dbx_core::db_admin_sql::TableAdminSqlOptions) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_truncate_table_sql(options))
}

#[tauri::command]
pub fn build_vacuum_table_sql(options: dbx_core::db_admin_sql::VacuumTableSqlOptions) -> Result<String, String> {
    dbx_core::db_admin_sql::build_vacuum_table_sql(options)
}

#[tauri::command]
pub fn build_mysql_auto_increment_sql(
    options: dbx_core::db_admin_sql::MysqlAutoIncrementSqlOptions,
) -> Result<String, String> {
    dbx_core::db_admin_sql::build_mysql_auto_increment_sql(options)
}

#[tauri::command]
pub fn build_drop_database_sql(options: dbx_core::db_admin_sql::DatabaseNameSqlOptions) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_drop_database_sql(options))
}

#[tauri::command]
pub fn build_create_schema_sql(options: dbx_core::db_admin_sql::SchemaNameSqlOptions) -> Result<String, String> {
    dbx_core::db_admin_sql::build_create_schema_sql(options)
}

#[tauri::command]
pub fn build_update_database_properties_sql(
    options: dbx_core::db_admin_sql::DatabasePropertyEditSqlOptions,
) -> Result<String, String> {
    dbx_core::db_admin_sql::build_update_database_properties_sql(options)
}

#[tauri::command]
pub fn build_drop_schema_sql(options: dbx_core::db_admin_sql::SchemaNameSqlOptions) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_drop_schema_sql(options))
}

#[tauri::command]
pub fn build_duplicate_table_structure_sql(
    options: dbx_core::db_admin_sql::DuplicateTableStructureSqlOptions,
) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_duplicate_table_structure_sql(options))
}

#[tauri::command]
pub fn build_copy_table_data_sql(options: dbx_core::db_admin_sql::CopyTableDataSqlOptions) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_copy_table_data_sql(options))
}

#[tauri::command]
pub fn build_executable_object_source_statements(
    input: dbx_core::object_source_sql::EditableObjectSourceSqlInput,
) -> Result<Vec<String>, String> {
    dbx_core::object_source_sql::build_executable_object_source_statements(input)
}

#[tauri::command]
pub fn validate_oracle_executable_object_source(
    input: dbx_core::object_source_sql::OracleObjectSourceValidationInput,
) -> Result<(), String> {
    dbx_core::object_source_sql::validate_oracle_executable_object_source(input)
}

#[tauri::command]
pub fn prepare_oracle_trigger_sql(
    input: dbx_core::object_source_sql::OracleTriggerSqlInput,
) -> Result<dbx_core::object_source_sql::PreparedOracleTriggerSql, String> {
    dbx_core::object_source_sql::prepare_oracle_trigger_sql(input)
}

#[tauri::command]
pub fn fingerprint_oracle_routine_object_source(
    input: dbx_core::object_source_sql::EditableObjectSourceSqlInput,
) -> Result<String, String> {
    dbx_core::object_source_sql::fingerprint_oracle_routine_object_source(input)
}

#[tauri::command]
pub fn build_executable_object_source_sql(
    input: dbx_core::object_source_sql::EditableObjectSourceSqlInput,
) -> Result<String, String> {
    dbx_core::object_source_sql::build_executable_object_source_sql(input)
}

#[tauri::command]
pub fn build_editable_object_source(
    input: dbx_core::object_source_sql::EditableObjectSourceSqlInput,
) -> Result<String, String> {
    Ok(dbx_core::object_source_sql::build_editable_object_source(input))
}

#[tauri::command]
pub fn build_routine_rename_object_source_statements(
    input: dbx_core::object_source_sql::RoutineRenameObjectSourceInput,
) -> Result<Vec<String>, String> {
    dbx_core::object_source_sql::build_routine_rename_object_source_statements(input)
}

#[tauri::command]
pub fn rewrite_oracle_routine_references(
    input: dbx_core::object_source_sql::OracleRoutineReferenceRewriteInput,
) -> Result<String, String> {
    dbx_core::object_source_sql::rewrite_oracle_routine_references(input)
}

#[tauri::command]
pub fn rewrite_oracle_table_references(
    input: dbx_core::object_source_sql::OracleTableReferenceRewriteInput,
) -> Result<String, String> {
    dbx_core::object_source_sql::rewrite_oracle_table_references(input)
}

#[tauri::command]
pub fn build_view_ddl_sql(input: dbx_core::object_source_sql::BuildViewDdlInput) -> Result<String, String> {
    Ok(dbx_core::object_source_sql::build_view_ddl_sql(input))
}

#[tauri::command]
pub fn parse_oracle_object_name(input: String) -> Result<String, String> {
    dbx_core::oracle_view_rename::parse_oracle_object_name(&input)
}

#[tauri::command]
pub fn build_oracle_view_rename_source(
    input: dbx_core::oracle_view_rename::OracleViewRenameSourceInput,
) -> Result<dbx_core::oracle_view_rename::OracleViewRenameSource, String> {
    dbx_core::oracle_view_rename::build_oracle_view_rename_source(input)
}

#[tauri::command]
pub fn review_oracle_view_rename_source(
    input: dbx_core::oracle_view_rename::OracleViewRenameSourceInput,
) -> dbx_core::oracle_view_rename::OracleViewRenameSourceReview {
    dbx_core::oracle_view_rename::review_oracle_view_rename_source(input)
}

#[tauri::command]
pub fn build_table_structure_change_sql(
    options: dbx_core::table_structure_sql::TableStructureSqlOptions,
    starrocks: Option<dbx_core::table_structure_sql::StarRocksAlterOptions>,
    mysql_table_charset: Option<dbx_core::table_structure_sql::MysqlTableCharsetChange>,
) -> Result<dbx_core::table_structure_sql::TableStructureSqlPlan, String> {
    let table_charset = mysql_table_charset
        .map(|change| dbx_core::table_structure_sql::build_mysql_table_charset_change_sql(&options, change));
    let mut result = dbx_core::table_structure_sql::build_table_structure_change_sql_plan(options, starrocks);
    if let Some(extra) = table_charset {
        result.sql.statements.extend(extra.statements);
        result.sql.warnings.extend(extra.warnings);
    }
    Ok(result)
}

#[tauri::command]
pub fn build_table_owner_change_sql(
    options: dbx_core::table_structure_sql::TableOwnerChangeSqlOptions,
) -> Result<dbx_core::table_structure_sql::TableStructureSqlResult, String> {
    Ok(dbx_core::table_structure_sql::build_table_owner_change_sql(options))
}

#[tauri::command]
pub async fn preview_sqlite_table_structure_change(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    options: dbx_core::table_structure_sql::TableStructureSqlOptions,
) -> Result<dbx_core::table_structure_sql::SqliteTableStructurePreview, String> {
    dbx_core::table_structure_sql::preview_sqlite_table_structure_change(&state, &connection_id, &database, options)
        .await
}

#[tauri::command]
pub async fn apply_sqlite_table_structure_change(
    state: State<'_, Arc<AppState>>,
    connection_id: String,
    database: String,
    options: dbx_core::table_structure_sql::TableStructureSqlOptions,
    schema_revision: String,
) -> Result<db::QueryResult, String> {
    dbx_core::table_structure_sql::apply_sqlite_table_structure_change(
        &state,
        &connection_id,
        &database,
        options,
        &schema_revision,
    )
    .await
}

#[tauri::command]
pub fn build_create_table_sql(
    options: dbx_core::table_structure_sql::TableStructureSqlOptions,
    server_version: Option<String>,
    dialect_options: Option<dbx_core::table_structure_sql::CreateTableDialectOptions>,
) -> Result<dbx_core::table_structure_sql::TableStructureSqlPlan, String> {
    Ok(dbx_core::table_structure_sql::build_create_table_sql_plan_with_dialect_options(
        options,
        server_version.as_deref(),
        dialect_options.unwrap_or_default(),
    ))
}

#[tauri::command]
pub fn build_create_partitioned_table_sql(
    options: dbx_core::table_structure_sql::TableStructureSqlOptions,
    partitioning: dbx_core::table_structure_sql::TablePartitionDefinition,
) -> Result<dbx_core::table_structure_sql::TableStructureSqlResult, String> {
    Ok(dbx_core::table_structure_sql::build_create_partitioned_table_sql(options, partitioning))
}

#[tauri::command]
pub fn build_table_partition_operation_sql(
    options: dbx_core::table_structure_sql::TablePartitionSqlOptions,
) -> Result<dbx_core::table_structure_sql::TableStructureSqlResult, String> {
    Ok(dbx_core::table_structure_sql::build_table_partition_operation_sql(options))
}

#[tauri::command]
pub fn build_single_column_alter_sql(
    options: dbx_core::table_structure_sql::SingleColumnAlterSqlOptions,
) -> Result<dbx_core::table_structure_sql::TableStructureSqlResult, String> {
    Ok(dbx_core::table_structure_sql::build_single_column_alter_sql(options))
}

#[tauri::command]
pub fn analyze_editable_query_editability(sql: String) -> Result<dbx_core::sql_editability::QueryEditability, String> {
    Ok(dbx_core::sql_editability::analyze_editable_query_editability(&sql))
}

#[tauri::command]
pub fn prepare_data_grid_save(
    options: dbx_core::data_grid_sql::DataGridSaveStatementOptions,
    driver_profile: Option<String>,
) -> Result<dbx_core::data_grid_sql::DataGridSavePreparation, String> {
    Ok(dbx_core::data_grid_sql::prepare_data_grid_save_for_driver_profile(options, driver_profile.as_deref()))
}

#[tauri::command]
pub async fn extract_data_grid_selection(
    request: dbx_core::data_grid_extractors::DataGridExtractRequest,
) -> Result<dbx_core::data_grid_extractors::DataGridExtractResult, dbx_core::data_grid_extractors::DataGridExtractError>
{
    tauri::async_runtime::spawn_blocking(move || {
        // Cells pasted from the grid land in a spreadsheet, so formula-triggering text
        // is neutralized before the extractor renders it (see dbx_core::data::grid_clipboard_guard).
        let request = dbx_core::data::grid_clipboard_guard::neutralize_spreadsheet_formulas(request);
        dbx_core::data_grid_extractors::extract_data_grid_selection(request)
    })
    .await
    .map_err(|error| {
        dbx_core::data_grid_extractors::DataGridExtractError::new(
            dbx_core::data_grid_extractors::DataGridExtractErrorCode::ExecutionFailed,
            format!("Data grid extractor worker failed: {error}"),
        )
    })?
}

#[tauri::command]
pub fn build_data_grid_copy_update_statements(
    options: dbx_core::data_grid_sql::DataGridCopyUpdateStatementOptions,
) -> Result<Vec<String>, String> {
    Ok(dbx_core::data_grid_sql::build_data_grid_copy_update_statements(options))
}

#[tauri::command]
pub fn build_data_grid_copy_insert_statement(
    options: dbx_core::data_grid_sql::DataGridCopyInsertStatementOptions,
) -> Result<Option<String>, String> {
    Ok(dbx_core::data_grid_sql::build_data_grid_copy_insert_statement(options))
}

#[tauri::command]
pub fn build_dml_change_preview_sql(
    options: dbx_core::dml_preview_sql::DmlChangePreviewSqlOptions,
) -> Result<dbx_core::dml_preview_sql::DmlChangePreviewSqlResult, String> {
    dbx_core::dml_preview_sql::build_dml_change_preview_sql(options)
}

#[tauri::command]
pub fn build_data_grid_context_filter_condition(
    options: dbx_core::data_grid_sql::DataGridContextFilterConditionOptions,
) -> Result<Option<String>, String> {
    Ok(dbx_core::data_grid_sql::build_data_grid_context_filter_condition(options))
}

#[tauri::command]
pub fn build_data_grid_column_value_filter_condition(
    options: dbx_core::data_grid_sql::DataGridColumnValueFilterConditionOptions,
) -> Result<Option<String>, String> {
    Ok(dbx_core::data_grid_sql::build_data_grid_column_value_filter_condition(options))
}

#[tauri::command]
pub fn build_data_grid_column_values_filter_condition(
    options: dbx_core::data_grid_sql::DataGridColumnValuesFilterConditionOptions,
) -> Result<Option<String>, String> {
    Ok(dbx_core::data_grid_sql::build_data_grid_column_values_filter_condition(options))
}

#[tauri::command]
pub fn build_data_grid_column_distinct_values_sql(
    options: dbx_core::data_grid_sql::DataGridColumnDistinctValuesSqlOptions,
) -> Result<String, String> {
    Ok(dbx_core::data_grid_sql::build_data_grid_column_distinct_values_sql(options))
}

#[tauri::command]
pub fn build_data_grid_count_sql(options: dbx_core::data_grid_sql::DataGridCountSqlOptions) -> Result<String, String> {
    Ok(dbx_core::data_grid_sql::build_data_grid_count_sql(options))
}

#[tauri::command]
pub fn build_data_grid_conditional_update_sql(
    options: dbx_core::data_grid_sql::DataGridConditionalUpdateSqlOptions,
) -> Result<Option<String>, String> {
    Ok(dbx_core::data_grid_sql::build_data_grid_conditional_update_sql(options))
}

#[tauri::command]
pub fn build_hive_table_properties_sql(
    options: dbx_core::data_grid_sql::HiveTablePropertiesSqlOptions,
) -> Result<String, String> {
    Ok(dbx_core::data_grid_sql::build_hive_table_properties_sql(options))
}

#[tauri::command]
pub fn build_export_insert_statements(
    options: dbx_core::database_export::BuildExportInsertStatementsOptions,
) -> Result<Vec<String>, String> {
    dbx_core::database_export::build_export_insert_statements(options)
}

#[tauri::command]
pub fn build_export_sql_insert(
    options: dbx_core::database_export::BuildExportSqlInsertOptions,
) -> Result<String, String> {
    dbx_core::database_export::build_export_sql_insert(options)
}

#[tauri::command]
pub async fn build_database_sql_export(
    state: tauri::State<'_, std::sync::Arc<dbx_core::connection::AppState>>,
    mut options: dbx_core::database_export::BuildDatabaseSqlExportOptions,
) -> Result<String, String> {
    // Sort tables by FK dependency when connection info is available.
    if let (Some(ref conn_id), Some(ref database), Some(ref schema)) =
        (&options.connection_id, &options.database, &options.schema)
    {
        if options.tables.len() > 1 {
            let table_names: Vec<String> = options.tables.iter().filter_map(|t| t.table_name.clone()).collect();
            if table_names.len() > 1 {
                if let Ok(sorted_names) = dbx_core::transfer::sort_tables_by_fk_dependency(
                    &state,
                    conn_id,
                    database,
                    schema,
                    &table_names,
                    true,
                )
                .await
                {
                    options.tables.sort_by_key(|t| {
                        sorted_names
                            .iter()
                            .position(|n| Some(n.as_str()) == t.table_name.as_deref())
                            .unwrap_or(usize::MAX)
                    });
                }
            }
        }
    }
    dbx_core::database_export::build_database_sql_export(options)
}

#[tauri::command]
pub async fn get_explain_info(
    state: tauri::State<'_, std::sync::Arc<dbx_core::connection::AppState>>,
    connection_id: String,
    database: Option<String>,
    schema: Option<String>,
    sql: String,
    mode: Option<String>,
    execution_id: Option<String>,
    timeout_secs: Option<u64>,
) -> Result<String, String> {
    dbx_core::agent_explain::get_agent_explain_info_core_with_execution_id(
        &state,
        &connection_id,
        database.as_deref(),
        schema.as_deref(),
        &sql,
        mode.as_deref(),
        timeout_secs,
        execution_id.as_deref(),
    )
    .await
}

#[tauri::command]
pub async fn get_plugin_plan_capabilities(
    state: tauri::State<'_, std::sync::Arc<dbx_core::connection::AppState>>,
    connection_id: String,
) -> Result<dbx_core::query::plugin_plan::PluginPlanCapabilities, String> {
    dbx_core::query::plugin_plan::plugin_plan_capabilities(&state, &connection_id).await
}

/// Read-only estimated plan acquisition for the plugin Host API. The request
/// carries the original SQL only; the host generates and owns the EXPLAIN.
#[tauri::command]
pub async fn get_plugin_estimated_plan(
    state: tauri::State<'_, std::sync::Arc<dbx_core::connection::AppState>>,
    request: dbx_core::query::plugin_plan::PluginPlanRequest,
) -> Result<dbx_core::query::plugin_plan::PluginPlanResult, String> {
    dbx_core::query::plugin_plan::explain_estimated_plan(&state, request).await
}

/// Read-only data query for the plugin Host API (`host.data:read`). The
/// plugin id is bound by the host bridge; the core enforces the manifest
/// permission, the user's grant, and the read-only statement gate.
#[tauri::command]
pub async fn query_plugin_data(
    state: tauri::State<'_, std::sync::Arc<dbx_core::connection::AppState>>,
    plugin_id: String,
    request: dbx_core::query::plugin_data::PluginDataQueryRequest,
) -> Result<dbx_core::query::plugin_data::PluginDataQueryResult, String> {
    dbx_core::query::plugin_data::query_plugin_data(&state, &plugin_id, request).await
}

#[tauri::command]
pub async fn get_plugin_data_grants(
    state: tauri::State<'_, std::sync::Arc<dbx_core::connection::AppState>>,
    plugin_id: String,
) -> Result<Vec<dbx_core::query::plugin_data::PluginDataGrant>, String> {
    dbx_core::query::plugin_data::list_plugin_data_grants(&state, &plugin_id).await
}

#[tauri::command]
pub async fn set_plugin_data_grant(
    state: tauri::State<'_, std::sync::Arc<dbx_core::connection::AppState>>,
    plugin_id: String,
    connection_id: String,
    granted: bool,
) -> Result<Vec<dbx_core::query::plugin_data::PluginDataGrant>, String> {
    dbx_core::query::plugin_data::set_plugin_data_grant(&state, &plugin_id, &connection_id, granted).await
}

#[tauri::command]
pub fn build_create_user_sql(username: String, password: String, tablespace: String) -> Result<String, String> {
    Ok(dbx_core::db_admin_sql::build_create_user_sql(&username, &password, &tablespace))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    async fn test_app_state() -> Arc<AppState> {
        let dir = std::env::temp_dir().join(format!("dbx-query-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = dbx_core::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        Arc::new(AppState::new_with_plugin_dir(storage, dir.join("plugins")))
    }

    fn snapshot_request(format: &str) -> dbx_core::query::snapshot_export::SnapshotExportRequest {
        serde_json::from_value(serde_json::json!({
            "context": {"connectionId":"original", "database":"APP", "valueRef":""},
            "format": format, "columns":["C"], "rows":[["complete"]], "cells":[]
        }))
        .unwrap()
    }

    #[tokio::test]
    async fn reported_snapshot_publishes_success_and_cleans_failed_staging_before_reply() {
        let state = test_app_state().await;
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("result.json");
        let result = write_reported_snapshot(&state, snapshot_request("json"), path.to_str().unwrap()).await;
        assert_eq!(snapshot_outcome(result), serde_json::json!({"status":"succeeded","value":null}));
        assert_eq!(
            serde_json::from_slice::<serde_json::Value>(&std::fs::read(&path).unwrap()).unwrap(),
            serde_json::json!([{"C":"complete"}])
        );
        let result = write_reported_snapshot(&state, snapshot_request("unsupported"), path.to_str().unwrap()).await;
        assert_eq!(snapshot_outcome(result)["status"], "failed");
        assert_eq!(std::fs::read_dir(directory.path()).unwrap().count(), 1);
        assert_eq!(
            serde_json::from_slice::<serde_json::Value>(&std::fs::read(&path).unwrap()).unwrap(),
            serde_json::json!([{"C":"complete"}])
        );
        // A following operation owns this path only after the failed staged file is gone.
        write_reported_snapshot(&state, snapshot_request("csv"), path.to_str().unwrap()).await.unwrap();
        assert!(std::fs::read_to_string(path).unwrap().contains("complete"));
        assert_eq!(std::fs::read_dir(directory.path()).unwrap().count(), 1);
    }

    #[tokio::test]
    async fn reported_snapshot_preserves_existing_target_on_persist_failure() {
        let state = test_app_state().await;
        let directory = tempfile::tempdir().unwrap();
        let target = directory.path().join("existing-directory");
        std::fs::create_dir(&target).unwrap();
        let result = write_reported_snapshot(&state, snapshot_request("json"), target.to_str().unwrap()).await;
        assert_eq!(snapshot_outcome(result)["status"], "failed");
        assert!(target.is_dir());
        assert_eq!(std::fs::read_dir(directory.path()).unwrap().count(), 1);
        let result = write_reported_snapshot(
            &state,
            snapshot_request("json"),
            target.join("missing/result.json").to_str().unwrap(),
        )
        .await;
        assert_eq!(snapshot_outcome(result)["status"], "failed");
    }

    #[tokio::test]
    async fn execute_script_with_2pc_returns_structured_result() {
        let state = test_app_state().await;
        let result = execute_script_with_2pc_core(
            state,
            "conn-1".to_string(),
            "testdb".to_string(),
            vec!["SELECT 1".to_string()],
            None,
            false,
        )
        .await;

        assert!(!result.transaction_id.is_empty());
        assert!(!result.participants.is_empty());
        // No real connection: rolled_back with error, not silent auto-commit success.
        assert_eq!(result.status, "rolled_back");
        assert!(result.error.as_ref().is_some_and(|e| !e.is_empty()));
        assert_eq!(result.statement_count, 1);
        assert_eq!(result.executed_count, 0);
    }

    #[tokio::test]
    async fn execute_script_with_2pc_empty_statements_succeeds() {
        let state = test_app_state().await;
        let result =
            execute_script_with_2pc_core(state, "conn-empty".to_string(), "testdb".to_string(), vec![], None, false)
                .await;

        assert_eq!(result.status, "committed");
        assert_eq!(result.statement_count, 0);
        assert_eq!(result.executed_count, 0);
        assert!(result.error.is_none());
        assert_eq!(result.participants.len(), 1);
    }

    #[tokio::test]
    async fn execute_script_with_2pc_comment_only_is_empty_success() {
        let state = test_app_state().await;
        let result = execute_script_with_2pc_core(
            state,
            "conn-comment".to_string(),
            "testdb".to_string(),
            vec!["-- WARNING: incomplete\n-- manual only".to_string()],
            None,
            false,
        )
        .await;

        assert_eq!(result.status, "committed");
        assert_eq!(result.statement_count, 0);
        assert!(result.error.is_none());
    }

    #[tokio::test]
    async fn execute_script_with_2pc_propagates_structured_failure_fields() {
        let state = test_app_state().await;
        let result = execute_script_with_2pc_core(
            state,
            "missing-conn".to_string(),
            "testdb".to_string(),
            vec!["CREATE TABLE t1 (id INT)".to_string(), "CREATE TABLE t2 (id INT)".to_string()],
            None,
            false,
        )
        .await;

        assert!(result.status == "rolled_back" || result.status == "mixed", "status={}", result.status);
        assert_eq!(result.statement_count, 2);
        assert!(result.error.as_ref().is_some_and(|e| !e.is_empty()));
        assert_eq!(result.executed_count, 0);
    }

    #[tokio::test]
    async fn execute_script_with_2pc_blocks_unconfirmed_destructive_sql() {
        let state = test_app_state().await;
        let result = execute_script_with_2pc_core(
            state,
            "missing-conn".to_string(),
            "testdb".to_string(),
            vec!["DROP INDEX idx_old ON users".to_string()],
            None,
            false,
        )
        .await;

        assert_eq!(result.status, "rolled_back");
        assert_eq!(result.executed_count, 0);
        assert_eq!(result.metadata["blocked"], "destructive_confirmation_required");
        assert_eq!(result.metadata["destructive_statement_count"], 1);
    }

    #[tokio::test]
    async fn execute_script_with_2pc_allows_confirmed_destructive_sql_to_reach_execution() {
        let state = test_app_state().await;
        let result = execute_script_with_2pc_core(
            state,
            "missing-conn".to_string(),
            "testdb".to_string(),
            vec!["DROP INDEX idx_old ON users".to_string()],
            None,
            true,
        )
        .await;

        assert_ne!(
            result.metadata.get("blocked").and_then(|value| value.as_str()),
            Some("destructive_confirmation_required")
        );
        assert!(result.error.as_ref().is_some_and(|error| !error.is_empty()));
    }

    #[tokio::test]
    async fn execute_script_with_2pc_does_not_block_drop_text_in_comments() {
        let state = test_app_state().await;
        let result = execute_script_with_2pc_core(
            state,
            "missing-conn".to_string(),
            "testdb".to_string(),
            vec!["-- DROP INDEX idx_fake\nSELECT 1".to_string()],
            None,
            false,
        )
        .await;

        assert_ne!(
            result.metadata.get("blocked").and_then(|value| value.as_str()),
            Some("destructive_confirmation_required")
        );
        assert!(result.error.as_ref().is_some_and(|error| !error.is_empty()));
    }
}