diff --git a/apps/tauri/src-tauri/Cargo.lock b/apps/tauri/src-tauri/Cargo.lock index 9784e3ea..47211644 100644 --- a/apps/tauri/src-tauri/Cargo.lock +++ b/apps/tauri/src-tauri/Cargo.lock @@ -8,6 +8,18 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.5" @@ -950,6 +962,18 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fastrand" version = "2.5.0" @@ -1410,12 +1434,30 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", +] + [[package]] name = "hashbrown" version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +[[package]] +name = "hashlink" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" +dependencies = [ + "hashbrown 0.14.5", +] + [[package]] name = "heck" version = "0.4.1" @@ -1970,6 +2012,17 @@ dependencies = [ "libc", ] +[[package]] +name = "libsqlite3-sys" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + [[package]] name = "link-section" version = "0.19.4" @@ -2731,6 +2784,7 @@ dependencies = [ name = "querya_desktop_tauri" version = "0.1.0" dependencies = [ + "rusqlite", "serde", "serde_json", "tauri", @@ -2877,6 +2931,20 @@ dependencies = [ "web-sys", ] +[[package]] +name = "rusqlite" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7753b721174eb8ff87a9a0e799e2d7bc3749323e773db92e0984debb00019d6e" +dependencies = [ + "bitflags 2.13.2", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", +] + [[package]] name = "rustc-hash" version = "2.1.3" @@ -4173,6 +4241,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version-compare" version = "0.2.1" @@ -4867,6 +4941,26 @@ dependencies = [ "serde", ] +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "zerofrom" version = "0.1.8" diff --git a/apps/tauri/src-tauri/Cargo.toml b/apps/tauri/src-tauri/Cargo.toml index c43ac14b..28fc52f3 100644 --- a/apps/tauri/src-tauri/Cargo.toml +++ b/apps/tauri/src-tauri/Cargo.toml @@ -18,6 +18,7 @@ tauri-plugin-opener = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" uuid = { version = "1", features = ["v4", "serde"] } +rusqlite = { version = "0.32", features = ["bundled"] } [profile.release] codegen-units = 1 diff --git a/apps/tauri/src-tauri/src/connection/manager.rs b/apps/tauri/src-tauri/src/connection/manager.rs index 98fe494b..57d97c0a 100644 --- a/apps/tauri/src-tauri/src/connection/manager.rs +++ b/apps/tauri/src-tauri/src/connection/manager.rs @@ -57,16 +57,31 @@ pub trait ConnectionManager: Send + Sync { type SessionBox = Box; /// In-memory registry used by Tauri commands. Drivers register factories later. -#[derive(Default)] pub struct ConnectionRegistry { sessions: Mutex>, /// Optional opener for tests / driver wiring. Returns a live session. opener: Option ConnectionResult + Send + Sync>>, } +impl Default for ConnectionRegistry { + fn default() -> Self { + Self::with_builtin_drivers() + } +} + impl ConnectionRegistry { pub fn new() -> Self { - Self::default() + Self::with_builtin_drivers() + } + + /// Registry with SQLite opener wired (PG/MySQL return UnsupportedDriver). + pub fn with_builtin_drivers() -> Self { + Self::with_opener(|config| match config { + ConnectionConfig::Sqlite { path } => super::sqlite::open_sqlite(path), + other => Err(ConnectionError::UnsupportedDriver { + driver: other.driver_kind(), + }), + }) } pub fn with_opener(opener: F) -> Self @@ -173,17 +188,21 @@ mod tests { } #[test] - fn registry_without_opener_is_unsupported() { - let reg = ConnectionRegistry::new(); + fn registry_postgres_is_unsupported_for_now() { + let reg = ConnectionRegistry::with_builtin_drivers(); let err = reg - .connect(ConnectionConfig::Sqlite { - path: "a.db".into(), + .connect(ConnectionConfig::Postgres { + host: "localhost".into(), + port: 5432, + database: "db".into(), + user: "u".into(), + password: None, }) .unwrap_err(); assert!(matches!( err, ConnectionError::UnsupportedDriver { - driver: DriverKind::Sqlite + driver: DriverKind::Postgres } )); } diff --git a/apps/tauri/src-tauri/src/connection/mod.rs b/apps/tauri/src-tauri/src/connection/mod.rs index d2025a23..3f75adb2 100644 --- a/apps/tauri/src-tauri/src/connection/mod.rs +++ b/apps/tauri/src-tauri/src/connection/mod.rs @@ -3,7 +3,12 @@ mod error; mod id; mod manager; +mod sqlite; pub use error::ConnectionError; pub use id::{ConnectionId, DriverKind}; pub use manager::{ConnectionConfig, ConnectionManager, ConnectionRegistry}; +#[allow(unused_imports)] // public API for later IPC / introspection issues +pub use manager::ConnectionSession; +#[allow(unused_imports)] // public API for later IPC / introspection issues +pub use sqlite::{open_sqlite, SqliteSession}; diff --git a/apps/tauri/src-tauri/src/connection/sqlite.rs b/apps/tauri/src-tauri/src/connection/sqlite.rs new file mode 100644 index 00000000..c99a3fd7 --- /dev/null +++ b/apps/tauri/src-tauri/src/connection/sqlite.rs @@ -0,0 +1,142 @@ +//! SQLite driver via `rusqlite` (bundled libsqlite3). + +use super::error::{ConnectionError, ConnectionResult}; +use super::id::{ConnectionId, DriverKind}; +use super::manager::ConnectionSession; +use rusqlite::Connection; +use std::path::Path; +use std::sync::Mutex; + +pub struct SqliteSession { + id: ConnectionId, + path: String, + /// rusqlite::Connection is !Sync — guard for Tauri/registry threads. + conn: Mutex>, +} + +impl SqliteSession { + #[allow(dead_code)] // used by introspection / query issues next + pub fn path(&self) -> &str { + &self.path + } + + #[allow(dead_code)] // used by execute_query / tree issues next + pub fn with_connection( + &self, + f: impl FnOnce(&Connection) -> ConnectionResult, + ) -> ConnectionResult { + let guard = self + .conn + .lock() + .map_err(|_| ConnectionError::io("sqlite connection lock poisoned"))?; + let conn = guard + .as_ref() + .ok_or_else(|| ConnectionError::Disconnected { + id: self.id.clone(), + })?; + f(conn) + } +} + +impl ConnectionSession for SqliteSession { + fn id(&self) -> &ConnectionId { + &self.id + } + + fn driver(&self) -> DriverKind { + DriverKind::Sqlite + } + + fn disconnect(&mut self) -> ConnectionResult<()> { + let guard = self + .conn + .get_mut() + .map_err(|_| ConnectionError::io("sqlite connection lock poisoned"))?; + if let Some(conn) = guard.take() { + conn.close() + .map_err(|(_, e)| ConnectionError::driver(DriverKind::Sqlite, e.to_string()))?; + } + Ok(()) + } +} + +/// Open a SQLite database file and return a live session. +pub fn open_sqlite(path: impl AsRef) -> ConnectionResult> { + let path = path.as_ref().trim(); + if path.is_empty() { + return Err(ConnectionError::invalid_config("sqlite path is empty")); + } + + let p = Path::new(path); + if let Some(parent) = p.parent() { + if !parent.as_os_str().is_empty() && !parent.exists() { + return Err(ConnectionError::io(format!( + "parent directory does not exist: {}", + parent.display() + ))); + } + } + + let conn = Connection::open(path) + .map_err(|e| ConnectionError::driver(DriverKind::Sqlite, e.to_string()))?; + + conn.query_row("SELECT 1", [], |_| Ok(())) + .map_err(|e| ConnectionError::driver(DriverKind::Sqlite, e.to_string()))?; + + Ok(Box::new(SqliteSession { + id: ConnectionId::new(), + path: path.to_owned(), + conn: Mutex::new(Some(conn)), + })) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::connection::manager::{ConnectionConfig, ConnectionManager, ConnectionRegistry}; + use std::time::{SystemTime, UNIX_EPOCH}; + + fn temp_db() -> std::path::PathBuf { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + std::env::temp_dir().join(format!("querya_sqlite_test_{nanos}.db")) + } + + #[test] + fn open_and_disconnect_temp_db() { + let path = temp_db(); + let path_str = path.to_string_lossy().to_string(); + let mut session = open_sqlite(&path_str).expect("open"); + assert_eq!(session.driver(), DriverKind::Sqlite); + session.disconnect().expect("disconnect"); + let _ = std::fs::remove_file(&path); + } + + #[test] + fn empty_path_is_invalid_config() { + match open_sqlite(" ") { + Err(ConnectionError::InvalidConfig { .. }) => {} + Err(e) => panic!("expected InvalidConfig, got {e}"), + Ok(_) => panic!("expected error for empty path"), + } + } + + #[test] + fn registry_sqlite_round_trip() { + let path = temp_db(); + let path_str = path.to_string_lossy().to_string(); + let reg = ConnectionRegistry::with_builtin_drivers(); + let id = reg + .connect(ConnectionConfig::Sqlite { + path: path_str.clone(), + }) + .expect("connect"); + assert!(reg.is_connected(&id)); + assert_eq!(reg.driver_of(&id), Some(DriverKind::Sqlite)); + reg.disconnect(&id).expect("disconnect"); + assert!(!reg.is_connected(&id)); + let _ = std::fs::remove_file(&path); + } +}