diff --git a/crates/dbx-core/src/sql.rs b/crates/dbx-core/src/sql.rs index 25025aea02..b615fb4e13 100644 --- a/crates/dbx-core/src/sql.rs +++ b/crates/dbx-core/src/sql.rs @@ -39,6 +39,20 @@ pub enum SqlFileStatementAction { Skip, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SqlFileImportStatementKind { + Execute, + Skip, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SqlFileImportStatement { + pub kind: SqlFileImportStatementKind, + pub sql: String, + pub source_sqls: Vec, + pub source_statement_count: usize, +} + #[derive(Debug, Clone, Copy, Default)] pub struct SqlParsingOptions { pub supports_hash_line_comments: bool, @@ -685,6 +699,343 @@ pub fn prepare_sql_file_statement( SqlFileStatementAction::Execute(body.to_string()) } +pub fn optimize_sql_file_import_statements( + statements: &[String], + db_type: Option, + driver_profile: Option<&str>, +) -> Vec { + let mut optimized = Vec::new(); + let mut pending_insert: Option = None; + + for statement in statements { + let action = db_type + .as_ref() + .map(|db_type| prepare_sql_file_statement(statement, db_type, driver_profile)) + .unwrap_or_else(|| SqlFileStatementAction::Execute(statement.trim().to_string())); + + match action { + SqlFileStatementAction::Skip => { + flush_pending_insert(&mut optimized, &mut pending_insert); + optimized.push(SqlFileImportStatement { + kind: SqlFileImportStatementKind::Skip, + sql: statement.trim().to_string(), + source_sqls: vec![statement.trim().to_string()], + source_statement_count: 1, + }); + } + SqlFileStatementAction::Execute(sql) => { + let options = db_type.map(SqlParsingOptions::for_database_type).unwrap_or_default(); + if let Some(insert) = parse_mergeable_insert(&sql, options) { + match pending_insert.as_mut() { + Some(batch) if batch.can_accept(&insert) => batch.push(insert), + Some(_) => { + flush_pending_insert(&mut optimized, &mut pending_insert); + pending_insert = Some(PendingInsertBatch::new(insert)); + } + None => { + pending_insert = Some(PendingInsertBatch::new(insert)); + } + } + } else { + flush_pending_insert(&mut optimized, &mut pending_insert); + optimized.push(SqlFileImportStatement { + kind: SqlFileImportStatementKind::Execute, + sql: sql.clone(), + source_sqls: vec![sql], + source_statement_count: 1, + }); + } + } + } + } + + flush_pending_insert(&mut optimized, &mut pending_insert); + optimized +} + +fn flush_pending_insert(optimized: &mut Vec, pending_insert: &mut Option) { + if let Some(batch) = pending_insert.take() { + optimized.push(SqlFileImportStatement { + kind: SqlFileImportStatementKind::Execute, + sql: batch.to_sql(), + source_sqls: batch.source_sqls, + source_statement_count: batch.source_statement_count, + }); + } +} + +const SQL_FILE_INSERT_BATCH_MAX_STATEMENTS: usize = 500; +const SQL_FILE_INSERT_BATCH_MAX_BYTES: usize = 4 * 1024 * 1024; + +#[derive(Debug, Clone)] +struct MergeableInsert { + prefix: String, + prefix_key: String, + values: String, + sql: String, +} + +#[derive(Debug, Clone)] +struct PendingInsertBatch { + prefix: String, + prefix_key: String, + values: Vec, + source_sqls: Vec, + source_statement_count: usize, + byte_len: usize, +} + +impl PendingInsertBatch { + fn new(insert: MergeableInsert) -> Self { + let byte_len = insert.prefix.len() + insert.values.len() + 16; + Self { + prefix: insert.prefix, + prefix_key: insert.prefix_key, + values: vec![insert.values], + source_sqls: vec![insert.sql], + source_statement_count: 1, + byte_len, + } + } + + fn can_accept(&self, insert: &MergeableInsert) -> bool { + self.prefix_key == insert.prefix_key + && self.source_statement_count < SQL_FILE_INSERT_BATCH_MAX_STATEMENTS + && self.byte_len + insert.values.len() + 3 <= SQL_FILE_INSERT_BATCH_MAX_BYTES + } + + fn push(&mut self, insert: MergeableInsert) { + self.byte_len += insert.values.len() + 3; + self.values.push(insert.values); + self.source_sqls.push(insert.sql); + self.source_statement_count += 1; + } + + fn to_sql(&self) -> String { + if self.source_statement_count == 1 { + return self.source_sqls.first().cloned().unwrap_or_default(); + } + format!("{} VALUES\n{}", self.prefix, self.values.join(",\n")) + } +} + +fn parse_mergeable_insert(sql: &str, options: SqlParsingOptions) -> Option { + let executable = leading_executable_sql_with_options(sql, options).trim().trim_end_matches(';').trim(); + if !starts_with_keyword(executable, "insert") { + return None; + } + + let (values_start, values_end) = find_top_level_values_keyword(executable)?; + let prefix_without_values = executable[..values_start].trim_end(); + let values = executable[values_end..].trim(); + let values = parse_insert_values_tail(values)?; + + let prefix = prefix_without_values.to_string(); + Some(MergeableInsert { + prefix_key: normalize_insert_prefix_key(&prefix), + prefix, + values, + sql: executable.to_string(), + }) +} + +fn find_top_level_values_keyword(sql: &str) -> Option<(usize, usize)> { + let mut scanner = SqlScanner::default(); + let mut depth = 0usize; + + for (idx, ch) in sql.char_indices() { + scanner.step(sql, idx, ch); + if scanner.is_masked() { + continue; + } + + match ch { + '(' => depth += 1, + ')' => depth = depth.saturating_sub(1), + _ if depth == 0 => { + for keyword in ["values", "value"] { + if keyword_at(sql, idx, keyword) { + return Some((idx, idx + keyword.len())); + } + } + } + _ => {} + } + } + + None +} + +fn parse_insert_values_tail(tail: &str) -> Option { + let tail = tail.trim().trim_end_matches(';').trim(); + if tail.is_empty() { + return None; + } + + let mut scanner = SqlScanner::default(); + let mut depth = 0usize; + let mut saw_tuple = false; + let mut expecting_tuple = true; + + for (idx, ch) in tail.char_indices() { + scanner.step(tail, idx, ch); + if scanner.is_masked() { + continue; + } + + if expecting_tuple { + if ch.is_whitespace() || (saw_tuple && ch == ',') { + continue; + } + if ch != '(' { + return None; + } + expecting_tuple = false; + saw_tuple = true; + depth = 1; + continue; + } + + match ch { + '(' => depth += 1, + ')' => { + depth = depth.checked_sub(1)?; + if depth == 0 { + expecting_tuple = true; + } + } + ',' if depth == 0 => {} + _ if depth == 0 && !ch.is_whitespace() => return None, + _ => {} + } + } + + if saw_tuple && depth == 0 { + Some(tail.to_string()) + } else { + None + } +} + +#[derive(Default)] +struct SqlScanner { + in_single_quote: bool, + in_double_quote: bool, + in_backtick: bool, + in_line_comment: bool, + in_block_comment: bool, + dollar_quote_tag: Option, + previous: Option, +} + +impl SqlScanner { + fn step(&mut self, sql: &str, idx: usize, ch: char) { + if let Some(tag) = self.dollar_quote_tag.clone() { + if sql[idx..].starts_with(&tag) { + self.dollar_quote_tag = None; + } + self.previous = Some(ch); + return; + } + + let next = next_char_at(sql, idx + ch.len_utf8()); + if self.in_line_comment { + if ch == '\n' { + self.in_line_comment = false; + } + self.previous = Some(ch); + return; + } + if self.in_block_comment { + if self.previous == Some('*') && ch == '/' { + self.in_block_comment = false; + } + self.previous = Some(ch); + return; + } + + if !self.in_single_quote && !self.in_double_quote && !self.in_backtick { + if ch == '-' && next == Some('-') { + self.in_line_comment = true; + } else if ch == '/' && next == Some('*') { + self.in_block_comment = true; + } else if let Some(tag) = dollar_quote_tag_at_str(sql, idx) { + self.dollar_quote_tag = Some(tag); + } + } + + match ch { + '\'' if !self.in_double_quote && !self.in_backtick && self.previous != Some('\\') => { + self.in_single_quote = !self.in_single_quote; + } + '"' if !self.in_single_quote && !self.in_backtick && self.previous != Some('\\') => { + self.in_double_quote = !self.in_double_quote; + } + '`' if !self.in_single_quote && !self.in_double_quote => { + self.in_backtick = !self.in_backtick; + } + _ => {} + } + self.previous = Some(ch); + } + + fn is_masked(&self) -> bool { + self.in_single_quote + || self.in_double_quote + || self.in_backtick + || self.in_line_comment + || self.in_block_comment + || self.dollar_quote_tag.is_some() + } +} + +fn keyword_at(sql: &str, idx: usize, keyword: &str) -> bool { + let end = idx + keyword.len(); + sql.get(idx..end).is_some_and(|candidate| candidate.eq_ignore_ascii_case(keyword)) + && sql[..idx].chars().next_back().is_none_or(|ch| !is_sql_ident_char(ch)) + && sql.get(end..).and_then(|tail| tail.chars().next()).is_none_or(|ch| !is_sql_ident_char(ch)) +} + +fn starts_with_keyword(sql: &str, keyword: &str) -> bool { + sql.get(..keyword.len()).is_some_and(|candidate| candidate.eq_ignore_ascii_case(keyword)) + && sql.get(keyword.len()..).and_then(|tail| tail.chars().next()).is_none_or(|ch| !is_sql_ident_char(ch)) +} + +fn is_sql_ident_char(ch: char) -> bool { + ch == '_' || ch == '$' || ch.is_ascii_alphanumeric() +} + +fn normalize_insert_prefix_key(prefix: &str) -> String { + let mut scanner = SqlScanner::default(); + let mut key = String::with_capacity(prefix.len()); + let mut previous_space = false; + + for (idx, ch) in prefix.char_indices() { + scanner.step(prefix, idx, ch); + if scanner.in_single_quote + || scanner.in_double_quote + || scanner.in_backtick + || scanner.dollar_quote_tag.is_some() + { + key.push(ch); + previous_space = false; + continue; + } + + if ch.is_whitespace() { + if !previous_space { + key.push(' '); + } + previous_space = true; + } else { + key.push(ch.to_ascii_lowercase()); + previous_space = false; + } + } + + key.trim().to_string() +} + pub fn starts_with_executable_sql_keyword(sql: &str, keywords: &[&str]) -> bool { starts_with_executable_sql_keyword_with_options(sql, keywords, SqlParsingOptions::default()) } @@ -1041,9 +1392,10 @@ mod tests { use crate::models::connection::DatabaseType; use super::{ - decode_sql_file_bytes, find_statement_at_cursor_for_database, prepare_sql_file_statement, split_sql_script, - split_sql_statements_for_database, starts_with_executable_sql_keyword, - starts_with_executable_sql_keyword_for_database, SqlFileStatementAction, SqlStatementSplitter, + decode_sql_file_bytes, find_statement_at_cursor_for_database, optimize_sql_file_import_statements, + prepare_sql_file_statement, split_sql_script, split_sql_statements_for_database, + starts_with_executable_sql_keyword, starts_with_executable_sql_keyword_for_database, SqlFileStatementAction, + SqlStatementSplitter, }; #[test] @@ -1271,6 +1623,53 @@ mod tests { ); } + #[test] + fn optimizes_adjacent_single_row_inserts_into_multi_row_insert() { + let statements = vec![ + "INSERT INTO users (id, name) VALUES (1, 'Ada')".to_string(), + "insert into users (id, name) values (2, 'Linus')".to_string(), + "SELECT 1".to_string(), + ]; + + let optimized = optimize_sql_file_import_statements(&statements, Some(DatabaseType::Mysql), None); + + assert_eq!(optimized.len(), 2); + assert_eq!(optimized[0].source_statement_count, 2); + assert_eq!(optimized[0].sql, "INSERT INTO users (id, name) VALUES\n(1, 'Ada'),\n(2, 'Linus')"); + assert_eq!(optimized[1].sql, "SELECT 1"); + } + + #[test] + fn keeps_insert_batches_separate_for_different_targets_or_suffixes() { + let statements = vec![ + "INSERT INTO users (id) VALUES (1)".to_string(), + "INSERT INTO teams (id) VALUES (1)".to_string(), + "INSERT INTO users (id) VALUES (2) RETURNING id".to_string(), + ]; + + let optimized = optimize_sql_file_import_statements(&statements, Some(DatabaseType::Postgres), None); + + assert_eq!(optimized.len(), 3); + assert!(optimized.iter().all(|statement| statement.source_statement_count == 1)); + } + + #[test] + fn optimized_sql_file_import_keeps_skipped_mysql_dump_statements() { + let statements = vec![ + "LOCK TABLES `users` WRITE".to_string(), + "INSERT INTO `users` VALUES (1)".to_string(), + "INSERT INTO `users` VALUES (2)".to_string(), + "UNLOCK TABLES".to_string(), + ]; + + let optimized = optimize_sql_file_import_statements(&statements, Some(DatabaseType::Mysql), None); + + assert_eq!(optimized.len(), 3); + assert_eq!(optimized[0].kind, super::SqlFileImportStatementKind::Skip); + assert_eq!(optimized[1].source_statement_count, 2); + assert_eq!(optimized[2].kind, super::SqlFileImportStatementKind::Skip); + } + #[test] fn split_batches_by_go() { assert_eq!(super::split_sql_batches("SELECT 1\nGO\nSELECT 2"), vec!["SELECT 1", "SELECT 2"]); diff --git a/crates/dbx-web/src/routes/sql_file.rs b/crates/dbx-web/src/routes/sql_file.rs index d1d6d5e64a..cf6a9540ee 100644 --- a/crates/dbx-web/src/routes/sql_file.rs +++ b/crates/dbx-web/src/routes/sql_file.rs @@ -6,7 +6,7 @@ use axum::response::sse::{Event, Sse}; use axum::Json; use dbx_core::query; use dbx_core::sql; -use dbx_core::sql::SqlFileStatementAction; +use dbx_core::sql::{SqlFileImportStatement, SqlFileImportStatementKind}; use futures::stream::Stream; use serde::Deserialize; @@ -166,31 +166,26 @@ pub async fn execute_sql_file( .as_ref() .map(|(db_type, _)| sql::split_sql_statements_for_database(&file_content, *db_type)) .unwrap_or_else(|| sql::split_sql_statements(&file_content)); + let planned_statements = sql::optimize_sql_file_import_statements( + &statements, + import_target.as_ref().map(|(db_type, _)| *db_type), + import_target.as_ref().and_then(|(_, driver_profile)| driver_profile.as_deref()), + ); let start = std::time::Instant::now(); let mut success_count = 0usize; let mut failure_count = 0usize; let mut total_affected: u64 = 0; + let mut statement_index = 0usize; - for (i, stmt) in statements.iter().enumerate() { - let statement_action = import_target - .as_ref() - .map(|(db_type, driver_profile)| { - sql::prepare_sql_file_statement(stmt, db_type, driver_profile.as_deref()) - }) - .unwrap_or_else(|| SqlFileStatementAction::Execute(stmt.to_string())); - let (stmt_to_execute, summary, should_execute) = match statement_action { - SqlFileStatementAction::Execute(statement) => { - let summary = sql::statement_summary(&statement); - (statement, summary, true) - } - SqlFileStatementAction::Skip => (String::new(), sql::statement_summary(stmt), false), - }; + for planned_statement in planned_statements { + let next_statement_index = statement_index + planned_statement.source_statement_count; + let summary = sql::statement_summary(&planned_statement.sql); // Send running let running = dbx_core::sql::SqlFileProgress { execution_id: req.execution_id.clone(), status: dbx_core::sql::SqlFileStatus::Running, - statement_index: i, + statement_index: next_statement_index, success_count, failure_count, affected_rows: total_affected, @@ -202,12 +197,12 @@ pub async fn execute_sql_file( let _ = tx.send(json); } - if !should_execute { - success_count += 1; + if planned_statement.kind == SqlFileImportStatementKind::Skip { + success_count += planned_statement.source_statement_count; let done = dbx_core::sql::SqlFileProgress { execution_id: req.execution_id.clone(), status: dbx_core::sql::SqlFileStatus::StatementDone, - statement_index: i, + statement_index: next_statement_index, success_count, failure_count, affected_rows: total_affected, @@ -218,19 +213,27 @@ pub async fn execute_sql_file( if let Ok(json) = serde_json::to_string(&done) { let _ = tx.send(json); } + statement_index = next_statement_index; continue; } - match query::execute_sql_statement(&app, &req.connection_id, &req.database, &stmt_to_execute, None, None) - .await + match query::execute_sql_statement( + &app, + &req.connection_id, + &req.database, + &planned_statement.sql, + None, + None, + ) + .await { Ok(result) => { - success_count += 1; + success_count += planned_statement.source_statement_count; total_affected += result.affected_rows; let done = dbx_core::sql::SqlFileProgress { execution_id: req.execution_id.clone(), status: dbx_core::sql::SqlFileStatus::StatementDone, - statement_index: i, + statement_index: next_statement_index, success_count, failure_count, affected_rows: total_affected, @@ -243,33 +246,32 @@ pub async fn execute_sql_file( } } Err(e) => { - failure_count += 1; - let failed = dbx_core::sql::SqlFileProgress { - execution_id: req.execution_id.clone(), - status: dbx_core::sql::SqlFileStatus::StatementFailed, - statement_index: i, - success_count, - failure_count, - affected_rows: total_affected, - elapsed_ms: start.elapsed().as_millis(), - statement_summary: summary, - error: Some(e), - }; - if let Ok(json) = serde_json::to_string(&failed) { - let _ = tx.send(json); - } - if !req.continue_on_error { + let stopped = execute_web_merged_statement_fallback( + &app, + &req, + &tx, + start, + statement_index + 1, + &planned_statement, + &mut success_count, + &mut failure_count, + &mut total_affected, + e, + ) + .await; + if stopped || !req.continue_on_error { break; } } } + statement_index = next_statement_index; } // Send final done let final_done = dbx_core::sql::SqlFileProgress { execution_id: req.execution_id.clone(), status: dbx_core::sql::SqlFileStatus::Done, - statement_index: statements.len(), + statement_index, success_count, failure_count, affected_rows: total_affected, @@ -287,6 +289,101 @@ pub async fn execute_sql_file( Ok(Json(serde_json::json!({ "executionId": execution_id }))) } +async fn execute_web_merged_statement_fallback( + app: &Arc, + req: &SqlFileExecuteRequest, + tx: &tokio::sync::broadcast::Sender, + start: std::time::Instant, + first_statement_index: usize, + statement: &SqlFileImportStatement, + success_count: &mut usize, + failure_count: &mut usize, + total_affected: &mut u64, + batch_error: String, +) -> bool { + if statement.source_statement_count <= 1 { + let summary = sql::statement_summary(&statement.sql); + *failure_count += 1; + let failed = dbx_core::sql::SqlFileProgress { + execution_id: req.execution_id.clone(), + status: dbx_core::sql::SqlFileStatus::StatementFailed, + statement_index: first_statement_index, + success_count: *success_count, + failure_count: *failure_count, + affected_rows: *total_affected, + elapsed_ms: start.elapsed().as_millis(), + statement_summary: summary, + error: Some(batch_error), + }; + if let Ok(json) = serde_json::to_string(&failed) { + let _ = tx.send(json); + } + return false; + } + + for (offset, source_sql) in statement.source_sqls.iter().enumerate() { + let statement_index = first_statement_index + offset; + let summary = sql::statement_summary(source_sql); + let running = dbx_core::sql::SqlFileProgress { + execution_id: req.execution_id.clone(), + status: dbx_core::sql::SqlFileStatus::Running, + statement_index, + success_count: *success_count, + failure_count: *failure_count, + affected_rows: *total_affected, + elapsed_ms: start.elapsed().as_millis(), + statement_summary: summary.clone(), + error: None, + }; + if let Ok(json) = serde_json::to_string(&running) { + let _ = tx.send(json); + } + + match query::execute_sql_statement(app, &req.connection_id, &req.database, source_sql, None, None).await { + Ok(result) => { + *success_count += 1; + *total_affected += result.affected_rows; + let done = dbx_core::sql::SqlFileProgress { + execution_id: req.execution_id.clone(), + status: dbx_core::sql::SqlFileStatus::StatementDone, + statement_index, + success_count: *success_count, + failure_count: *failure_count, + affected_rows: *total_affected, + elapsed_ms: start.elapsed().as_millis(), + statement_summary: summary, + error: None, + }; + if let Ok(json) = serde_json::to_string(&done) { + let _ = tx.send(json); + } + } + Err(error) => { + *failure_count += 1; + let failed = dbx_core::sql::SqlFileProgress { + execution_id: req.execution_id.clone(), + status: dbx_core::sql::SqlFileStatus::StatementFailed, + statement_index, + success_count: *success_count, + failure_count: *failure_count, + affected_rows: *total_affected, + elapsed_ms: start.elapsed().as_millis(), + statement_summary: summary, + error: Some(error), + }; + if let Ok(json) = serde_json::to_string(&failed) { + let _ = tx.send(json); + } + if !req.continue_on_error { + return true; + } + } + } + } + + false +} + fn safe_uploaded_sql_path(tmp_dir: &Path, file_name: &str) -> Result { let base_name = file_name.rsplit(|ch| ch == '/' || ch == '\\').find(|part| !part.is_empty()).unwrap_or("upload.sql").trim(); diff --git a/src-tauri/src/commands/sql_file.rs b/src-tauri/src/commands/sql_file.rs index 614c2b2693..d7ec967feb 100644 --- a/src-tauri/src/commands/sql_file.rs +++ b/src-tauri/src/commands/sql_file.rs @@ -12,8 +12,9 @@ use crate::commands::query::execute_sql_statement; use dbx_core::models::connection::DatabaseType; pub use dbx_core::sql::{ - decode_sql_file_bytes, prepare_sql_file_statement, statement_summary, SqlFilePreview, SqlFileProgress, - SqlFileRequest, SqlFileStatementAction, SqlFileStatus, SqlParsingOptions, SqlStatementSplitter, + decode_sql_file_bytes, optimize_sql_file_import_statements, statement_summary, SqlFileImportStatement, + SqlFileImportStatementKind, SqlFilePreview, SqlFileProgress, SqlFileRequest, SqlFileStatus, SqlParsingOptions, + SqlStatementSplitter, }; static SQL_FILE_EXECUTIONS: std::sync::LazyLock>> = @@ -142,7 +143,13 @@ async fn execute_sql_file_inner( let mut statements = splitter.push_chunk(&file_content); statements.extend(splitter.finish()); - for statement in statements { + let planned_statements = optimize_sql_file_import_statements( + &statements, + import_target.as_ref().map(|target| target.db_type), + import_target.as_ref().and_then(|target| target.driver_profile.as_deref()), + ); + + for planned_statement in planned_statements { if token.is_cancelled() { emit_progress( app, @@ -159,16 +166,15 @@ async fn execute_sql_file_inner( return Ok(()); } - statement_index += 1; + let next_statement_index = statement_index + planned_statement.source_statement_count; if execute_statement_with_progress( app, state, request, &token, started_at, - statement_index, - &statement, - import_target.as_ref(), + next_statement_index, + &planned_statement, &mut success_count, &mut failure_count, &mut affected_rows, @@ -177,6 +183,7 @@ async fn execute_sql_file_inner( { return Ok(()); } + statement_index = next_statement_index; } emit_progress( @@ -208,14 +215,13 @@ async fn execute_statement_with_progress( token: &CancellationToken, started_at: Instant, statement_index: usize, - statement: &str, - import_target: Option<&SqlFileImportTarget>, + statement: &SqlFileImportStatement, success_count: &mut usize, failure_count: &mut usize, affected_rows: &mut u64, ) -> Result { if token.is_cancelled() { - let summary = statement_summary(statement); + let summary = statement_summary(&statement.sql); emit_progress( app, &request.execution_id, @@ -231,42 +237,37 @@ async fn execute_statement_with_progress( return Ok(true); } - let statement_action = import_target - .map(|target| prepare_sql_file_statement(statement, &target.db_type, target.driver_profile.as_deref())) - .unwrap_or_else(|| SqlFileStatementAction::Execute(statement.to_string())); - let executable_statement = match statement_action { - SqlFileStatementAction::Execute(statement) => statement, - SqlFileStatementAction::Skip => { - let summary = statement_summary(statement); - emit_progress( - app, - &request.execution_id, - SqlFileStatus::Running, - statement_index, - *success_count, - *failure_count, - *affected_rows, - started_at, - &summary, - None, - ); - *success_count += 1; - emit_progress( - app, - &request.execution_id, - SqlFileStatus::StatementDone, - statement_index, - *success_count, - *failure_count, - *affected_rows, - started_at, - &summary, - None, - ); - return Ok(false); - } - }; - let summary = statement_summary(&executable_statement); + if statement.kind == SqlFileImportStatementKind::Skip { + let summary = statement_summary(&statement.sql); + emit_progress( + app, + &request.execution_id, + SqlFileStatus::Running, + statement_index, + *success_count, + *failure_count, + *affected_rows, + started_at, + &summary, + None, + ); + *success_count += statement.source_statement_count; + emit_progress( + app, + &request.execution_id, + SqlFileStatus::StatementDone, + statement_index, + *success_count, + *failure_count, + *affected_rows, + started_at, + &summary, + None, + ); + return Ok(false); + } + + let summary = statement_summary(&statement.sql); emit_progress( app, @@ -285,14 +286,14 @@ async fn execute_statement_with_progress( state.inner().as_ref(), &request.connection_id, &request.database, - &executable_statement, + &statement.sql, None, Some(token.clone()), ) .await { Ok(result) => { - *success_count += 1; + *success_count += statement.source_statement_count; *affected_rows += result.affected_rows; emit_progress( app, @@ -309,6 +310,22 @@ async fn execute_statement_with_progress( Ok(false) } Err(error) => { + if statement.source_statement_count > 1 && !token.is_cancelled() { + return execute_merged_statement_fallback_with_progress( + app, + state, + request, + token, + started_at, + statement_index + 1 - statement.source_statement_count, + statement, + success_count, + failure_count, + affected_rows, + ) + .await; + } + let decision = statement_error_decision( &request.execution_id, token, @@ -331,6 +348,104 @@ async fn execute_statement_with_progress( } } +async fn execute_merged_statement_fallback_with_progress( + app: &AppHandle, + state: &State<'_, Arc>, + request: &SqlFileRequest, + token: &CancellationToken, + started_at: Instant, + first_statement_index: usize, + statement: &SqlFileImportStatement, + success_count: &mut usize, + failure_count: &mut usize, + affected_rows: &mut u64, +) -> Result { + for (offset, source_sql) in statement.source_sqls.iter().enumerate() { + let statement_index = first_statement_index + offset; + if token.is_cancelled() { + emit_progress( + app, + &request.execution_id, + SqlFileStatus::Cancelled, + statement_index, + *success_count, + *failure_count, + *affected_rows, + started_at, + &statement_summary(source_sql), + None, + ); + return Ok(true); + } + + let summary = statement_summary(source_sql); + emit_progress( + app, + &request.execution_id, + SqlFileStatus::Running, + statement_index, + *success_count, + *failure_count, + *affected_rows, + started_at, + &summary, + None, + ); + + match execute_sql_statement( + state.inner().as_ref(), + &request.connection_id, + &request.database, + source_sql, + None, + Some(token.clone()), + ) + .await + { + Ok(result) => { + *success_count += 1; + *affected_rows += result.affected_rows; + emit_progress( + app, + &request.execution_id, + SqlFileStatus::StatementDone, + statement_index, + *success_count, + *failure_count, + *affected_rows, + started_at, + &summary, + None, + ); + } + Err(error) => { + let decision = statement_error_decision( + &request.execution_id, + token, + request.continue_on_error, + started_at, + statement_index, + *success_count, + *failure_count, + *affected_rows, + &summary, + error, + ); + + *failure_count = decision.failure_count; + for progress in decision.progress { + let _ = app.emit("sql-file-progress", progress); + } + if decision.result? { + return Ok(true); + } + } + } + } + + Ok(false) +} + fn register_sql_file_execution( executions: &mut HashMap, execution_id: String,