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
691 changes: 686 additions & 5 deletions apps/tauri/src-tauri/Cargo.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions apps/tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ serde_json = "1"
uuid = { version = "1", features = ["v4", "serde"] }
rusqlite = { version = "0.32", features = ["bundled"] }
dirs = "5"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
sqlx = { version = "0.8", default-features = false, features = [
"runtime-tokio",
"postgres",
"json",
"chrono",
] }
futures-util = "0.3"
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }

[profile.release]
codegen-units = 1
Expand Down
22 changes: 21 additions & 1 deletion apps/tauri/src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ pub fn db_connect_sqlite(
registry.connect(ConnectionConfig::Sqlite { path, id })
}

#[tauri::command]
pub fn db_connect_postgres(
host: String,
port: u16,
database: String,
user: String,
password: Option<String>,
id: Option<String>,
registry: State<'_, ConnectionRegistry>,
) -> Result<ConnectionId, ConnectionError> {
registry.connect(ConnectionConfig::Postgres {
host,
port,
database,
user,
password,
id,
})
}

#[tauri::command]
pub fn profiles_list(
store: State<'_, ProfileStore>,
Expand Down Expand Up @@ -208,7 +228,7 @@ pub fn db_execute_query(
registry: State<'_, ConnectionRegistry>,
) -> Result<(), ConnectionError> {
let limit = limit.map(|n| n as usize);
registry.execute_sqlite_query(&id, &sql, limit, |ev| {
registry.execute_query(&id, &sql, limit, |ev| {
on_event
.send(ev)
.map_err(|e| ConnectionError::io(format!("query channel closed: {e}")))
Expand Down
90 changes: 84 additions & 6 deletions apps/tauri/src-tauri/src/connection/manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::error::{ConnectionError, ConnectionResult};
use super::id::{ConnectionId, DriverKind};
use super::postgres::PostgresSession;
use super::query::QueryEvent;
use super::sqlite::SqliteSession;
use serde::{Deserialize, Serialize};
Expand All @@ -19,19 +20,27 @@ pub enum ConnectionConfig {
#[serde(default)]
id: Option<String>,
},
/// Stub until the Postgres adapter epic wires connect.
Postgres {
host: String,
port: u16,
database: String,
user: String,
/// Session-only secret — never written to ProfileStore (keyring: #1878).
#[serde(default)]
password: Option<String>,
#[serde(default)]
id: Option<String>,
},
/// Stub until the MySQL adapter epic wires connect.
Mysql {
host: String,
port: u16,
database: String,
user: String,
#[serde(default)]
password: Option<String>,
#[serde(default)]
id: Option<String>,
},
}

Expand Down Expand Up @@ -82,7 +91,7 @@ impl ConnectionRegistry {
Self::with_builtin_drivers()
}

/// Registry with SQLite opener wired (PG/MySQL return UnsupportedDriver).
/// Registry with SQLite + Postgres openers (MySQL still UnsupportedDriver).
pub fn with_builtin_drivers() -> Self {
Self::with_opener(|config| match config {
ConnectionConfig::Sqlite { path, id } => {
Expand All @@ -94,6 +103,24 @@ impl ConnectionRegistry {
}?;
Ok(Arc::new(session) as SessionArc)
}
ConnectionConfig::Postgres {
host,
port,
database,
user,
password,
id,
} => {
let session = super::postgres::open_postgres(super::postgres::PostgresConnectParams {
host,
port,
database,
user,
password,
id: id.filter(|s| !s.trim().is_empty()).map(ConnectionId::from),
})?;
Ok(Arc::new(session) as SessionArc)
}
other => Err(ConnectionError::UnsupportedDriver {
driver: other.driver_kind(),
}),
Expand Down Expand Up @@ -153,6 +180,39 @@ impl ConnectionRegistry {
self.with_sqlite(id, |s| s.execute_streaming(sql, limit, emit))
}

pub fn with_postgres<T>(
&self,
id: &ConnectionId,
f: impl FnOnce(&PostgresSession) -> ConnectionResult<T>,
) -> ConnectionResult<T> {
let session = self.session_arc(id)?;
let pg = session
.as_any()
.downcast_ref::<PostgresSession>()
.ok_or(ConnectionError::UnsupportedDriver {
driver: session.driver(),
})?;
f(pg)
}

/// Driver-aware query stream (SQLite + Postgres).
pub fn execute_query(
&self,
id: &ConnectionId,
sql: &str,
limit: Option<usize>,
emit: impl FnMut(QueryEvent) -> ConnectionResult<()>,
) -> ConnectionResult<()> {
match self.driver_of(id) {
Some(DriverKind::Sqlite) => self.execute_sqlite_query(id, sql, limit, emit),
Some(DriverKind::Postgres) => {
self.with_postgres(id, |s| s.execute_streaming(sql, limit, emit))
}
Some(other) => Err(ConnectionError::UnsupportedDriver { driver: other }),
None => Err(ConnectionError::NotFound { id: id.clone() }),
}
}

/// Driver-agnostic object tree for a live connection (SQLite first).
pub fn object_tree(
&self,
Expand Down Expand Up @@ -300,24 +360,42 @@ mod tests {
}

#[test]
fn registry_postgres_is_unsupported_for_now() {
fn registry_mysql_is_unsupported_for_now() {
let reg = ConnectionRegistry::with_builtin_drivers();
let err = reg
.connect(ConnectionConfig::Postgres {
.connect(ConnectionConfig::Mysql {
host: "localhost".into(),
port: 5432,
port: 3306,
database: "db".into(),
user: "u".into(),
password: None,
id: None,
})
.unwrap_err();
assert!(matches!(
err,
ConnectionError::UnsupportedDriver {
driver: DriverKind::Postgres
driver: DriverKind::Mysql
}
));
}

#[test]
fn registry_postgres_rejects_empty_host() {
let reg = ConnectionRegistry::with_builtin_drivers();
let err = reg
.connect(ConnectionConfig::Postgres {
host: " ".into(),
port: 5432,
database: "querya".into(),
user: "querya".into(),
password: Some("querya".into()),
id: None,
})
.unwrap_err();
assert!(matches!(err, ConnectionError::InvalidConfig { .. }));
}

#[test]
fn execute_sqlite_query_streams_via_session() {
let path = std::env::temp_dir().join(format!(
Expand Down
3 changes: 3 additions & 0 deletions apps/tauri/src-tauri/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ mod id;
mod introspect;
mod manager;
mod object_tree;
mod postgres;
pub(crate) mod postgres_query;
mod query;
mod runtime;
mod smoke_slice;
mod sqlite;
mod sqlite_introspect;
Expand Down
Loading
Loading