diff --git a/apps/tauri/src-tauri/src/commands.rs b/apps/tauri/src-tauri/src/commands.rs index d4f5c72e..f1d17b3b 100644 --- a/apps/tauri/src-tauri/src/commands.rs +++ b/apps/tauri/src-tauri/src/commands.rs @@ -1,7 +1,8 @@ use crate::connection::{ ConnectionConfig, ConnectionError, ConnectionId, ConnectionManager, ConnectionRegistry, - SchemaObject, TableSchema, + QueryEvent, SchemaObject, TableSchema, }; +use tauri::ipc::Channel; use tauri::State; #[tauri::command] @@ -49,3 +50,36 @@ pub fn db_list_columns( ) -> Result { registry.with_sqlite(&id, |s| s.list_columns(&table)) } + +/// Execute SQL and stream `QueryEvent` batches over `on_event` (not one giant IPC payload). +#[tauri::command] +pub fn db_execute_query( + id: ConnectionId, + sql: String, + limit: Option, + on_event: Channel, + registry: State<'_, ConnectionRegistry>, +) -> Result<(), ConnectionError> { + let conn_arc = registry.sqlite_connection_arc(&id)?; + let guard = conn_arc + .lock() + .map_err(|_| ConnectionError::io("sqlite connection lock poisoned"))?; + let conn = guard + .as_ref() + .ok_or_else(|| ConnectionError::Disconnected { id: id.clone() })?; + + let limit = limit.map(|n| n as usize); + match crate::connection::sqlite_query::execute_streaming(conn, &sql, limit, None, |ev| { + on_event + .send(ev) + .map_err(|e| ConnectionError::io(format!("query channel closed: {e}"))) + }) { + Ok(()) => Ok(()), + Err(e) => { + let _ = on_event.send(QueryEvent::Error { + message: e.to_string(), + }); + Err(e) + } + } +} diff --git a/apps/tauri/src-tauri/src/connection/manager.rs b/apps/tauri/src-tauri/src/connection/manager.rs index bd99ab13..60f7115e 100644 --- a/apps/tauri/src-tauri/src/connection/manager.rs +++ b/apps/tauri/src-tauri/src/connection/manager.rs @@ -124,6 +124,14 @@ impl ConnectionRegistry { })?; f(sqlite) } + + /// Clone the SQLite connection Arc so callers can drop the registry lock. + pub fn sqlite_connection_arc( + &self, + id: &ConnectionId, + ) -> ConnectionResult>>> { + self.with_sqlite(id, |s| Ok(s.connection_arc())) + } } impl ConnectionManager for ConnectionRegistry { diff --git a/apps/tauri/src-tauri/src/connection/mod.rs b/apps/tauri/src-tauri/src/connection/mod.rs index 6ba58642..58508836 100644 --- a/apps/tauri/src-tauri/src/connection/mod.rs +++ b/apps/tauri/src-tauri/src/connection/mod.rs @@ -4,8 +4,10 @@ mod error; mod id; mod introspect; mod manager; +mod query; mod sqlite; mod sqlite_introspect; +pub(crate) mod sqlite_query; pub use error::ConnectionError; pub use id::{ConnectionId, DriverKind}; @@ -13,5 +15,6 @@ pub use introspect::{ColumnMeta, SchemaObject, SchemaObjectKind, TableSchema}; pub use manager::{ConnectionConfig, ConnectionManager, ConnectionRegistry}; #[allow(unused_imports)] // public API for later IPC / frontend issues pub use manager::ConnectionSession; +pub use query::QueryEvent; #[allow(unused_imports)] // public API for later IPC / frontend issues pub use sqlite::{open_sqlite, SqliteSession}; diff --git a/apps/tauri/src-tauri/src/connection/query.rs b/apps/tauri/src-tauri/src/connection/query.rs new file mode 100644 index 00000000..45453cd6 --- /dev/null +++ b/apps/tauri/src-tauri/src/connection/query.rs @@ -0,0 +1,26 @@ +//! Query result streaming events (shared IPC shape). + +use serde::Serialize; + +/// Events pushed over a Tauri [`tauri::ipc::Channel`] during `db_execute_query`. +#[derive(Debug, Clone, Serialize)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum QueryEvent { + Columns { + names: Vec, + }, + Rows { + rows: Vec>, + offset: u64, + }, + Done { + row_count: u64, + truncated: bool, + #[serde(skip_serializing_if = "Option::is_none")] + affected_rows: Option, + elapsed_ms: u64, + }, + Error { + message: String, + }, +} diff --git a/apps/tauri/src-tauri/src/connection/sqlite.rs b/apps/tauri/src-tauri/src/connection/sqlite.rs index e5d0c42d..0bca1460 100644 --- a/apps/tauri/src-tauri/src/connection/sqlite.rs +++ b/apps/tauri/src-tauri/src/connection/sqlite.rs @@ -3,15 +3,16 @@ use super::error::{ConnectionError, ConnectionResult}; use super::id::{ConnectionId, DriverKind}; use super::manager::ConnectionSession; +use super::query::QueryEvent; use rusqlite::Connection; use std::path::Path; -use std::sync::Mutex; +use std::sync::{Arc, Mutex}; pub struct SqliteSession { id: ConnectionId, path: String, - /// rusqlite::Connection is !Sync — guard for Tauri/registry threads. - conn: Mutex>, + /// Shared so query streaming can drop the registry lock. + conn: Arc>>, } impl SqliteSession { @@ -19,6 +20,10 @@ impl SqliteSession { &self.path } + pub fn connection_arc(&self) -> Arc>> { + Arc::clone(&self.conn) + } + pub fn with_connection( &self, f: impl FnOnce(&Connection) -> ConnectionResult, @@ -49,6 +54,17 @@ impl SqliteSession { ) -> ConnectionResult { self.with_connection(|c| super::sqlite_introspect::list_columns(c, table)) } + + pub fn execute_streaming( + &self, + sql: &str, + limit: Option, + emit: impl FnMut(QueryEvent) -> ConnectionResult<()>, + ) -> ConnectionResult<()> { + self.with_connection(|c| { + super::sqlite_query::execute_streaming(c, sql, limit, None, emit) + }) + } } impl ConnectionSession for SqliteSession { @@ -61,9 +77,9 @@ impl ConnectionSession for SqliteSession { } fn disconnect(&mut self) -> ConnectionResult<()> { - let guard = self + let mut guard = self .conn - .get_mut() + .lock() .map_err(|_| ConnectionError::io("sqlite connection lock poisoned"))?; if let Some(conn) = guard.take() { conn.close() @@ -103,7 +119,7 @@ pub fn open_sqlite(path: impl AsRef) -> ConnectionResult, + batch_size: Option, + mut emit: impl FnMut(QueryEvent) -> ConnectionResult<()>, +) -> ConnectionResult<()> { + let sql = sql.trim(); + if sql.is_empty() { + return Err(ConnectionError::invalid_config("sql is empty")); + } + + let limit = limit.unwrap_or(DEFAULT_LIMIT); + let batch_size = batch_size.unwrap_or(DEFAULT_BATCH).max(1); + let started = Instant::now(); + + let mut stmt = conn + .prepare(sql) + .map_err(|e| ConnectionError::driver(DriverKind::Sqlite, e.to_string()))?; + + let col_count = stmt.column_count(); + if col_count == 0 { + // Non-SELECT (DDL/DML): execute and report changes. + let affected = stmt + .execute([]) + .map_err(|e| ConnectionError::driver(DriverKind::Sqlite, e.to_string()))? + as u64; + emit(QueryEvent::Done { + row_count: 0, + truncated: false, + affected_rows: Some(affected), + elapsed_ms: elapsed_ms(started), + })?; + return Ok(()); + } + + let names: Vec = (0..col_count) + .map(|i| stmt.column_name(i).unwrap_or("").to_owned()) + .collect(); + emit(QueryEvent::Columns { names })?; + + stream_rows(&mut stmt, col_count, limit, batch_size, started, &mut emit) +} + +fn stream_rows( + stmt: &mut Statement<'_>, + col_count: usize, + limit: usize, + batch_size: usize, + started: Instant, + emit: &mut impl FnMut(QueryEvent) -> ConnectionResult<()>, +) -> ConnectionResult<()> { + let mut rows = stmt + .query([]) + .map_err(|e| ConnectionError::driver(DriverKind::Sqlite, e.to_string()))?; + + let mut batch: Vec> = Vec::with_capacity(batch_size); + let mut offset: u64 = 0; + let mut row_count: u64 = 0; + let mut truncated = false; + + loop { + if row_count as usize >= limit { + truncated = true; + break; + } + + let row = match rows.next() { + Ok(Some(r)) => r, + Ok(None) => break, + Err(e) => { + return Err(ConnectionError::driver(DriverKind::Sqlite, e.to_string())); + } + }; + + let mut cells = Vec::with_capacity(col_count); + for i in 0..col_count { + let v = row + .get_ref(i) + .map_err(|e| ConnectionError::driver(DriverKind::Sqlite, e.to_string()))?; + cells.push(value_ref_to_json(v)); + } + batch.push(cells); + row_count += 1; + + if batch.len() >= batch_size { + let rows_out = std::mem::take(&mut batch); + let n = rows_out.len() as u64; + emit(QueryEvent::Rows { + rows: rows_out, + offset, + })?; + offset += n; + } + } + + if !batch.is_empty() { + let n = batch.len() as u64; + emit(QueryEvent::Rows { + rows: batch, + offset, + })?; + offset += n; + let _ = offset; + } + + emit(QueryEvent::Done { + row_count, + truncated, + affected_rows: None, + elapsed_ms: elapsed_ms(started), + })?; + Ok(()) +} + +fn elapsed_ms(started: Instant) -> u64 { + started.elapsed().as_millis() as u64 +} + +fn value_ref_to_json(v: ValueRef<'_>) -> serde_json::Value { + match v { + ValueRef::Null => serde_json::Value::Null, + ValueRef::Integer(i) => serde_json::json!(i), + ValueRef::Real(f) => { + if f.is_finite() { + serde_json::json!(f) + } else { + serde_json::Value::String(f.to_string()) + } + } + ValueRef::Text(bytes) => { + let s = String::from_utf8_lossy(bytes); + serde_json::Value::String(s.into_owned()) + } + ValueRef::Blob(bytes) => { + let mut hex = String::with_capacity(2 + bytes.len() * 2); + hex.push_str("X'"); + for b in bytes { + use std::fmt::Write; + let _ = write!(hex, "{b:02x}"); + } + hex.push('\''); + serde_json::Value::String(hex) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use rusqlite::Connection; + + fn seed_nums() -> Connection { + let conn = Connection::open_in_memory().unwrap(); + conn.execute_batch("CREATE TABLE nums (n INTEGER);") + .unwrap(); + for i in 1..=10 { + conn.execute("INSERT INTO nums(n) VALUES (?1)", [i]) + .unwrap(); + } + conn + } + + fn collect(sql: &str, limit: Option) -> (Vec, bool) { + let conn = seed_nums(); + let mut events = Vec::new(); + let result = execute_streaming(&conn, sql, limit, Some(3), |ev| { + events.push(ev); + Ok(()) + }); + (events, result.is_ok()) + } + + #[test] + fn streams_columns_batches_done() { + let (events, ok) = collect("SELECT n FROM nums ORDER BY n", None); + assert!(ok); + assert!(matches!(events.first(), Some(QueryEvent::Columns { .. }))); + let row_batches: Vec<_> = events + .iter() + .filter(|e| matches!(e, QueryEvent::Rows { .. })) + .collect(); + assert!(row_batches.len() >= 3); // 10 rows / batch 3 + match events.last() { + Some(QueryEvent::Done { + row_count, + truncated, + .. + }) => { + assert_eq!(*row_count, 10); + assert!(!*truncated); + } + other => panic!("expected Done, got {other:?}"), + } + } + + #[test] + fn respects_limit_truncation() { + let (events, ok) = collect("SELECT n FROM nums ORDER BY n", Some(4)); + assert!(ok); + match events.last() { + Some(QueryEvent::Done { + row_count, + truncated, + .. + }) => { + assert_eq!(*row_count, 4); + assert!(*truncated); + } + other => panic!("expected Done, got {other:?}"), + } + } + + #[test] + fn dml_reports_affected() { + let conn = Connection::open_in_memory().unwrap(); + conn.execute_batch("CREATE TABLE t(id INTEGER); INSERT INTO t VALUES (1),(2);") + .unwrap(); + let mut events = Vec::new(); + execute_streaming(&conn, "DELETE FROM t", None, None, |ev| { + events.push(ev); + Ok(()) + }) + .unwrap(); + match events.as_slice() { + [QueryEvent::Done { + affected_rows: Some(2), + row_count: 0, + .. + }] => {} + other => panic!("unexpected {other:?}"), + } + } + + #[test] + fn never_buffers_all_rows_in_one_event() { + let (events, ok) = collect("SELECT n FROM nums ORDER BY n", None); + assert!(ok); + for ev in &events { + if let QueryEvent::Rows { rows, .. } = ev { + assert!(rows.len() <= 3); + } + } + } +} diff --git a/apps/tauri/src-tauri/src/lib.rs b/apps/tauri/src-tauri/src/lib.rs index d896f905..5f785d32 100644 --- a/apps/tauri/src-tauri/src/lib.rs +++ b/apps/tauri/src-tauri/src/lib.rs @@ -3,12 +3,12 @@ mod connection; pub use connection::{ ColumnMeta, ConnectionConfig, ConnectionError, ConnectionId, ConnectionManager, - ConnectionRegistry, DriverKind, SchemaObject, SchemaObjectKind, TableSchema, + ConnectionRegistry, DriverKind, QueryEvent, SchemaObject, SchemaObjectKind, TableSchema, }; use commands::{ - db_connect_sqlite, db_disconnect, db_is_connected, db_list_columns, db_list_schemas, - db_list_tables, + db_connect_sqlite, db_disconnect, db_execute_query, db_is_connected, db_list_columns, + db_list_schemas, db_list_tables, }; #[tauri::command] @@ -47,6 +47,7 @@ pub fn run() { db_list_schemas, db_list_tables, db_list_columns, + db_execute_query, ]) .run(tauri::generate_context!()) .expect("error while running tauri application");