feat(grid): add conditional bulk updates for MySQL
This commit is contained in:
29 files changed
+1813
-85
No files matched your search
@@ -457,6 +457,7 @@ async fn main() {
|
||||
.route("/tab-runtime-cache/owner", delete(routes::tab_runtime_cache::delete_tab_runtime_cache_owner))
|
||||
// Query
|
||||
.route("/query/execute", post(routes::query::execute_query))
|
||||
.route("/query/execute-conditional-update", post(routes::query::execute_conditional_update))
|
||||
.route("/query/execute-multi", post(routes::query::execute_multi))
|
||||
.route("/query/execute-batch", post(routes::query::execute_batch))
|
||||
.route("/query/execute-script", post(routes::query::execute_script))
|
||||
@@ -541,6 +542,10 @@ async fn main() {
|
||||
post(routes::query::build_data_grid_column_distinct_values_sql),
|
||||
)
|
||||
.route("/query/build-data-grid-count-sql", post(routes::query::build_data_grid_count_sql))
|
||||
.route(
|
||||
"/query/build-data-grid-conditional-update-sql",
|
||||
post(routes::query::build_data_grid_conditional_update_sql),
|
||||
)
|
||||
.route("/query/build-hive-table-properties-sql", post(routes::query::build_hive_table_properties_sql))
|
||||
.route("/query/build-export-insert-statements", post(routes::query::build_export_insert_statements))
|
||||
.route("/query/build-export-sql-insert", post(routes::query::build_export_sql_insert))
|
||||
@@ -550,6 +555,7 @@ async fn main() {
|
||||
.route("/data-compare/prepare-missing-target", post(routes::data_compare::prepare_data_compare_missing_target))
|
||||
.route("/data-compare/build-sync-plan", post(routes::data_compare::build_data_compare_sync_plan))
|
||||
.route("/query/cancel", post(routes::query::cancel_query))
|
||||
.route("/query/cancel-conditional-update", post(routes::query::cancel_conditional_update))
|
||||
.route("/query/close-session", post(routes::query::close_query_session))
|
||||
.route("/query/close-client-session", post(routes::query::close_client_connection_session))
|
||||
.route("/export/query-result-json", post(routes::text_export::export_query_result_json))
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::extract::State;
|
||||
use axum::http::{header, HeaderMap, HeaderValue};
|
||||
use axum::response::Response;
|
||||
use axum::Json;
|
||||
use serde::Deserialize;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::state::WebState;
|
||||
@@ -42,6 +44,8 @@ pub struct CancelRequest {
|
||||
pub execution_id: String,
|
||||
}
|
||||
|
||||
const CONDITIONAL_UPDATE_CANCEL_WAIT: Duration = Duration::from_secs(10);
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CloseSessionRequest {
|
||||
@@ -324,6 +328,12 @@ pub struct BuildDataGridCountSqlRequest {
|
||||
pub options: dbx_core::data_grid_sql::DataGridCountSqlOptions,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BuildDataGridConditionalUpdateSqlRequest {
|
||||
pub options: dbx_core::data_grid_sql::DataGridConditionalUpdateSqlOptions,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BuildHiveTablePropertiesSqlRequest {
|
||||
@@ -401,6 +411,83 @@ pub async fn execute_query(
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn execute_conditional_update(
|
||||
State(state): State<Arc<WebState>>,
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<ExecuteQueryRequest>,
|
||||
) -> Result<Json<dbx_core::db::QueryResult>, AppError> {
|
||||
let allow_database_switch = req.client_session_id.as_deref().is_some_and(|id| !id.trim().is_empty());
|
||||
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql, allow_database_switch)
|
||||
.await?;
|
||||
|
||||
let execution_id = req.execution_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
|
||||
let registered = state.app.running_queries.register_task_for_terminal_confirmation(
|
||||
execution_id.clone(),
|
||||
RunningTaskMetadata::query(req.connection_id.clone(), req.database.clone(), req.client_session_id.clone()),
|
||||
);
|
||||
let cancel_token = registered.token();
|
||||
let response_timeout = dbx_core::query::query_timeout_duration(req.timeout_secs);
|
||||
let app = state.app.clone();
|
||||
let (result_tx, result_rx) = oneshot::channel();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let result = dbx_core::query::execute_sql_statement_with_options_typed(
|
||||
&app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.sql,
|
||||
req.schema.as_deref(),
|
||||
Some(cancel_token),
|
||||
dbx_core::query::QueryExecutionOptions {
|
||||
max_rows: req.max_rows,
|
||||
fetch_size: req.fetch_size,
|
||||
page_size: req.page_size,
|
||||
row_offset: req.row_offset,
|
||||
max_result_bytes: req.max_result_bytes,
|
||||
result_key_columns: req.result_key_columns,
|
||||
table_data_preview: req.table_data_preview,
|
||||
catalog: req.catalog,
|
||||
result_session_id: req.result_session_id,
|
||||
client_session_id: req.client_session_id,
|
||||
// The outer response timeout reports uncertainty to the client
|
||||
// without dropping this task. The task remains reachable for a
|
||||
// later cancel and only releases its registration on terminal
|
||||
// completion.
|
||||
timeout_secs: Some(0),
|
||||
await_cancel_completion: true,
|
||||
execution_id: Some(execution_id),
|
||||
use_transaction: req.use_transaction,
|
||||
execution_mode: req.execution_mode.unwrap_or_default(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await;
|
||||
let _ = result_tx.send(result);
|
||||
drop(registered);
|
||||
});
|
||||
|
||||
let result = match response_timeout {
|
||||
Some(timeout) => match tokio::time::timeout(timeout, result_rx).await {
|
||||
Ok(Ok(result)) => result,
|
||||
Ok(Err(_)) => return Err(AppError::internal("Conditional update execution task stopped unexpectedly")),
|
||||
Err(_) => {
|
||||
return Err(AppError::from(
|
||||
dbx_core::query::QueryExecutionError::Timeout(format!(
|
||||
"Query timed out after {} seconds",
|
||||
timeout.as_secs().max(1)
|
||||
))
|
||||
.into_backend_error(),
|
||||
));
|
||||
}
|
||||
},
|
||||
None => match result_rx.await {
|
||||
Ok(result) => result,
|
||||
Err(_) => return Err(AppError::internal("Conditional update execution task stopped unexpectedly")),
|
||||
},
|
||||
};
|
||||
result.map(Json).map_err(|error| AppError::from(error.into_backend_error()))
|
||||
}
|
||||
|
||||
pub async fn execute_multi(
|
||||
State(state): State<Arc<WebState>>,
|
||||
headers: HeaderMap,
|
||||
@@ -441,6 +528,7 @@ pub async fn execute_multi(
|
||||
result_session_id: req.result_session_id,
|
||||
client_session_id: req.client_session_id,
|
||||
timeout_secs: req.timeout_secs,
|
||||
await_cancel_completion: false,
|
||||
execution_id: Some(execution_id),
|
||||
use_transaction: req.use_transaction,
|
||||
continue_on_error: req.continue_on_error.unwrap_or(false),
|
||||
@@ -507,6 +595,13 @@ pub async fn cancel_query(
|
||||
Json(serde_json::json!({ "cancelled": cancelled }))
|
||||
}
|
||||
|
||||
pub async fn cancel_conditional_update(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<CancelRequest>,
|
||||
) -> Json<dbx_core::query_cancel::CancellationWaitResult> {
|
||||
Json(state.app.running_queries.cancel_and_wait(&req.execution_id, CONDITIONAL_UPDATE_CANCEL_WAIT).await)
|
||||
}
|
||||
|
||||
pub async fn close_query_session(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<CloseSessionRequest>,
|
||||
@@ -940,6 +1035,12 @@ pub async fn build_data_grid_count_sql(Json(req): Json<BuildDataGridCountSqlRequ
|
||||
Json(dbx_core::data_grid_sql::build_data_grid_count_sql(req.options))
|
||||
}
|
||||
|
||||
pub async fn build_data_grid_conditional_update_sql(
|
||||
Json(req): Json<BuildDataGridConditionalUpdateSqlRequest>,
|
||||
) -> Json<Option<String>> {
|
||||
Json(dbx_core::data_grid_sql::build_data_grid_conditional_update_sql(req.options))
|
||||
}
|
||||
|
||||
pub async fn build_hive_table_properties_sql(Json(req): Json<BuildHiveTablePropertiesSqlRequest>) -> Json<String> {
|
||||
Json(dbx_core::data_grid_sql::build_hive_table_properties_sql(req.options))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user