From 2c3704a365ef4a814f3db18dbf780507a6115a6c Mon Sep 17 00:00:00 2001 From: Illuminated2020 <2357303264@qq.com> Date: Sun, 10 May 2026 19:52:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(query):=20=E7=BB=9F=E4=B8=80=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=85=A5=E5=8F=A3=E7=BB=93=E6=9E=9C=E8=A1=8C=E6=95=B0?= =?UTF-8?q?=E6=88=AA=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 execute_sql_statement_with_row_limit 成功返回前统一应用 row_limit - 补充 truncate_result_with_row_limit 的截断、保留和零值限制单测 --- crates/dbx-core/src/query.rs | 42 ++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/crates/dbx-core/src/query.rs b/crates/dbx-core/src/query.rs index df0807c41..41663707e 100644 --- a/crates/dbx-core/src/query.rs +++ b/crates/dbx-core/src/query.rs @@ -365,14 +365,16 @@ pub async fn execute_sql_statement_with_row_limit( let result = do_execute_with_row_limit(state, &pool_key, sql, schema, cancel_token.clone(), row_limit).await; - match &result { + let result = match &result { Err(e) if is_connection_error(e) && !is_canceled(&cancel_token) => { let db_opt = if database.is_empty() { None } else { Some(database) }; let new_key = state.reconnect_pool(connection_id, db_opt).await?; do_execute_with_row_limit(state, &new_key, sql, schema, cancel_token, row_limit).await } _ => result, - } + }; + + result.map(|result| truncate_result_with_row_limit(result, row_limit)) } pub async fn execute_multi_core( @@ -796,6 +798,42 @@ mod tests { use crate::models::connection::{ConnectionConfig, DatabaseType}; use crate::storage::Storage; + fn query_result_with_rows(row_count: usize, truncated: bool) -> db::QueryResult { + db::QueryResult { + columns: vec!["id".to_string()], + rows: (1..=row_count).map(|id| vec![serde_json::Value::Number(id.into())]).collect(), + affected_rows: 0, + execution_time_ms: 0, + truncated, + } + } + + #[test] + fn truncate_result_with_row_limit_truncates_rows_and_marks_result() { + let result = truncate_result_with_row_limit(query_result_with_rows(5, false), 3); + + assert_eq!(result.rows.len(), 3); + assert!(result.truncated); + assert_eq!(result.rows[0][0], serde_json::Value::Number(1.into())); + assert_eq!(result.rows[2][0], serde_json::Value::Number(3.into())); + } + + #[test] + fn truncate_result_with_row_limit_keeps_untruncated_result_when_within_limit() { + let result = truncate_result_with_row_limit(query_result_with_rows(3, false), 3); + + assert_eq!(result.rows.len(), 3); + assert!(!result.truncated); + } + + #[test] + fn truncate_result_with_row_limit_treats_zero_limit_as_one() { + let result = truncate_result_with_row_limit(query_result_with_rows(2, false), 0); + + assert_eq!(result.rows.len(), 1); + assert!(result.truncated); + } + #[tokio::test] async fn wait_for_query_returns_cancelled_when_token_is_cancelled() { let token = CancellationToken::new();