|
| 1 | +//! IPC payload: rebuild a [`StagingBuffer`] from the frontend snapshot and apply. |
| 2 | +
|
| 3 | +use super::apply::{ |
| 4 | + apply_staging_mysql, apply_staging_postgres, apply_staging_sqlite, ApplyError, ApplyResult, |
| 5 | +}; |
| 6 | +use super::StagingBuffer; |
| 7 | +use crate::connection::{ |
| 8 | + ConnectionError, ConnectionId, ConnectionManager, ConnectionRegistry, DriverKind, |
| 9 | +}; |
| 10 | +use serde::Deserialize; |
| 11 | +use std::collections::BTreeSet; |
| 12 | + |
| 13 | +/// One staged cell edit (JSON-friendly; Maps don't round-trip cleanly). |
| 14 | +#[derive(Debug, Clone, Deserialize)] |
| 15 | +#[serde(rename_all = "camelCase")] |
| 16 | +pub struct StagedCellEdit { |
| 17 | + pub row: usize, |
| 18 | + pub col: usize, |
| 19 | + pub value: String, |
| 20 | +} |
| 21 | + |
| 22 | +/// Frontend staging snapshot for apply (#1891). |
| 23 | +#[derive(Debug, Clone, Deserialize)] |
| 24 | +#[serde(rename_all = "camelCase")] |
| 25 | +pub struct StagingApplyRequest { |
| 26 | + pub connection_id: ConnectionId, |
| 27 | + pub table_name: String, |
| 28 | + pub schema: Option<String>, |
| 29 | + pub columns: Vec<String>, |
| 30 | + pub primary_keys: Vec<String>, |
| 31 | + pub original_rows: Vec<Vec<String>>, |
| 32 | + pub modified_cells: Vec<StagedCellEdit>, |
| 33 | + pub inserted_rows: Vec<Vec<String>>, |
| 34 | + pub deleted_row_indices: Vec<usize>, |
| 35 | +} |
| 36 | + |
| 37 | +impl StagingApplyRequest { |
| 38 | + pub fn into_buffer(self) -> StagingBuffer { |
| 39 | + let mut buf = StagingBuffer::new( |
| 40 | + self.columns, |
| 41 | + self.original_rows, |
| 42 | + self.primary_keys, |
| 43 | + ); |
| 44 | + for edit in self.modified_cells { |
| 45 | + buf.set_cell(edit.row, edit.col, edit.value); |
| 46 | + } |
| 47 | + for row in self.inserted_rows { |
| 48 | + buf.add_row(Some(row)); |
| 49 | + } |
| 50 | + let deleted: BTreeSet<usize> = self.deleted_row_indices.into_iter().collect(); |
| 51 | + for idx in deleted { |
| 52 | + buf.toggle_delete_row(idx); |
| 53 | + } |
| 54 | + buf |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Apply staged edits on the live connection; clears buffer only on success (Rust side). |
| 59 | +pub fn apply_staging_request( |
| 60 | + registry: &ConnectionRegistry, |
| 61 | + req: StagingApplyRequest, |
| 62 | +) -> Result<ApplyResult, ConnectionError> { |
| 63 | + let id = req.connection_id.clone(); |
| 64 | + let table = req.table_name.clone(); |
| 65 | + let schema = req.schema.clone(); |
| 66 | + let mut buffer = req.into_buffer(); |
| 67 | + |
| 68 | + let driver = registry |
| 69 | + .driver_of(&id) |
| 70 | + .ok_or_else(|| ConnectionError::NotFound { id: id.clone() })?; |
| 71 | + |
| 72 | + let result = match driver { |
| 73 | + DriverKind::Sqlite => registry.with_sqlite(&id, |s| { |
| 74 | + s.with_connection(|conn| { |
| 75 | + apply_staging_sqlite(conn, &mut buffer, &table, schema.as_deref()) |
| 76 | + .map_err(map_apply_error) |
| 77 | + }) |
| 78 | + }), |
| 79 | + DriverKind::Postgres => registry.with_postgres(&id, |s| { |
| 80 | + s.with_pool(|pool| { |
| 81 | + apply_staging_postgres(pool, &mut buffer, &table, schema.as_deref()) |
| 82 | + .map_err(map_apply_error) |
| 83 | + }) |
| 84 | + }), |
| 85 | + DriverKind::Mysql => registry.with_mysql(&id, |s| { |
| 86 | + s.with_pool(|pool| { |
| 87 | + apply_staging_mysql(pool, &mut buffer, &table, schema.as_deref()) |
| 88 | + .map_err(map_apply_error) |
| 89 | + }) |
| 90 | + }), |
| 91 | + }?; |
| 92 | + |
| 93 | + Ok(result) |
| 94 | +} |
| 95 | + |
| 96 | +fn map_apply_error(err: ApplyError) -> ConnectionError { |
| 97 | + ConnectionError::io(err.to_string()) |
| 98 | +} |
0 commit comments