Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions apps/tauri/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 26 additions & 7 deletions apps/tauri/src-tauri/src/connection/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,31 @@ pub trait ConnectionManager: Send + Sync {
type SessionBox = Box<dyn ConnectionSession>;

/// In-memory registry used by Tauri commands. Drivers register factories later.
#[derive(Default)]
pub struct ConnectionRegistry {
sessions: Mutex<HashMap<ConnectionId, SessionBox>>,
/// Optional opener for tests / driver wiring. Returns a live session.
opener: Option<Arc<dyn Fn(ConnectionConfig) -> ConnectionResult<SessionBox> + 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<F>(opener: F) -> Self
Expand Down Expand Up @@ -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
}
));
}
Expand Down
5 changes: 5 additions & 0 deletions apps/tauri/src-tauri/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
142 changes: 142 additions & 0 deletions apps/tauri/src-tauri/src/connection/sqlite.rs
Original file line number Diff line number Diff line change
@@ -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<Option<Connection>>,
}

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<T>(
&self,
f: impl FnOnce(&Connection) -> ConnectionResult<T>,
) -> ConnectionResult<T> {
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<str>) -> ConnectionResult<Box<dyn ConnectionSession>> {
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);
}
}
Loading