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。

share query execution helper

SuLeed475f1d1bd8
diff --git a/src-tauri/src/commands/query.rs b/src-tauri/src/commands/query.rs
index 596c6bfd86..7ac0f99364 100644
--- a/src-tauri/src/commands/query.rs
+++ b/src-tauri/src/commands/query.rs
@@ -194,42 +194,61 @@ async fn do_execute(
}
}
-#[tauri::command]
-pub async fn execute_query(
- state: State<'_, Arc<AppState>>,
- connection_id: String,
- database: String,
- sql: String,
- execution_id: Option<String>,
+pub(super) async fn execute_sql_statement(
+ state: &AppState,
+ connection_id: &str,
+ database: &str,
+ sql: &str,
+ cancel_token: Option<CancellationToken>,
) -> Result<db::QueryResult, String> {
- let registered_query = execution_id
- .as_ref()
- .filter(|id| !id.trim().is_empty())
- .map(|id| state.running_queries.register(id.clone()));
- let cancel_token = registered_query.as_ref().map(|query| query.token());
-
let pool_key = if database.is_empty() {
- connection_id.clone()
+ connection_id.to_string()
} else {
- state.get_or_create_pool(&connection_id, Some(&database)).await?
+ state.get_or_create_pool(connection_id, Some(database)).await?
};
if is_canceled(&cancel_token) {
return Err(canceled_error());
}
- let result = do_execute(&state, &pool_key, &sql, cancel_token.clone()).await;
+ let result = do_execute(state, &pool_key, sql, cancel_token.clone()).await;
match &result {
Err(e) if is_connection_error(e) && !is_canceled(&cancel_token) => {
- let db_opt = if database.is_empty() { None } else { Some(database.as_str()) };
- let new_key = state.reconnect_pool(&connection_id, db_opt).await?;
- do_execute(&state, &new_key, &sql, cancel_token).await
+ let db_opt = if database.is_empty() { None } else { Some(database) };
+ let new_key = state.reconnect_pool(connection_id, db_opt).await?;
+ do_execute(state, &new_key, sql, cancel_token).await
}
_ => result,
}
}
+#[tauri::command]
+pub async fn execute_query(
+ state: State<'_, Arc<AppState>>,
+ connection_id: String,
+ database: String,
+ sql: String,
+ execution_id: Option<String>,
+) -> Result<db::QueryResult, String> {
+ let registered_query = execution_id
+ .as_ref()
+ .filter(|id| !id.trim().is_empty())
+ .map(|id| state.running_queries.register(id.clone()));
+ let cancel_token = registered_query.as_ref().map(|query| query.token());
+
+ let result = execute_sql_statement(
+ &state,
+ &connection_id,
+ &database,
+ &sql,
+ cancel_token,
+ )
+ .await;
+
+ result
+}
+
#[tauri::command]
pub async fn cancel_query(
state: State<'_, Arc<AppState>>,
@@ -304,4 +323,21 @@ mod tests {
assert_eq!(result.unwrap_err(), QUERY_CANCELED);
}
+
+ #[tokio::test]
+ async fn wait_for_query_without_token_still_times_out() {
+ let result = wait_for_query(None, async {
+ tokio::time::sleep(Duration::from_secs(31)).await;
+ Ok(db::QueryResult {
+ columns: vec![],
+ rows: vec![],
+ affected_rows: 0,
+ execution_time_ms: 0,
+ truncated: false,
+ })
+ })
+ .await;
+
+ assert_eq!(result.unwrap_err(), timeout_error());
+ }
}