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
53 changes: 50 additions & 3 deletions apps/tauri/src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::connection::{
ConnectionConfig, ConnectionError, ConnectionId, ConnectionManager, ConnectionRegistry,
SchemaObject, TableSchema,
SchemaObject, SchemaObjectKind, SqlObjectKind, SqlObjectNode, TableSchema,
};
use tauri::ipc::Channel;
use tauri::State;
Expand All @@ -26,22 +26,69 @@ pub fn db_is_connected(id: ConnectionId, registry: State<'_, ConnectionRegistry>
registry.is_connected(&id)
}

/// Universal object tree: `parent_id = null` → schemas; schema → relations; table/view → columns.
#[tauri::command]
pub fn db_list_children(
id: ConnectionId,
parent_id: Option<String>,
registry: State<'_, ConnectionRegistry>,
) -> Result<Vec<SqlObjectNode>, ConnectionError> {
registry.list_children(&id, parent_id.as_deref())
}

/// Describe a table/view by stable object id (`sqlite:table:main:users`).
#[tauri::command]
pub fn db_describe_object(
id: ConnectionId,
object_id: String,
registry: State<'_, ConnectionRegistry>,
) -> Result<TableSchema, ConnectionError> {
registry.describe_object(&id, &object_id)
}

/// Shim: schema names via object tree roots.
#[tauri::command]
pub fn db_list_schemas(
id: ConnectionId,
registry: State<'_, ConnectionRegistry>,
) -> Result<Vec<String>, ConnectionError> {
registry.with_sqlite(&id, |s| s.list_schemas())
Ok(registry
.list_children(&id, None)?
.into_iter()
.map(|n| n.name)
.collect())
}

/// Shim: flat tables/views under `main` (legacy callers).
#[tauri::command]
pub fn db_list_tables(
id: ConnectionId,
registry: State<'_, ConnectionRegistry>,
) -> Result<Vec<SchemaObject>, ConnectionError> {
registry.with_sqlite(&id, |s| s.list_tables())
let main = registry
.list_children(&id, None)?
.into_iter()
.find(|n| n.name == "main")
.ok_or_else(|| ConnectionError::invalid_config("sqlite schema main missing"))?;
Ok(registry
.list_children(&id, Some(&main.id))?
.into_iter()
.filter_map(|n| {
let kind = match n.kind {
SqlObjectKind::Table => SchemaObjectKind::Table,
SqlObjectKind::View => SchemaObjectKind::View,
_ => return None,
};
Some(SchemaObject {
name: n.name,
kind,
schema: n.schema,
})
})
.collect())
}

/// Shim: columns by table/view name (legacy callers).
#[tauri::command]
pub fn db_list_columns(
id: ConnectionId,
Expand Down
38 changes: 38 additions & 0 deletions apps/tauri/src-tauri/src/connection/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,44 @@ impl ConnectionRegistry {
) -> ConnectionResult<()> {
self.with_sqlite(id, |s| s.execute_streaming(sql, limit, emit))
}

/// Driver-agnostic object tree for a live connection (SQLite first).
pub fn object_tree(
&self,
id: &ConnectionId,
) -> ConnectionResult<Arc<dyn super::object_tree::ObjectTreeProvider>> {
let session = self.session_arc(id)?;
match session.driver() {
DriverKind::Sqlite => {
let sqlite = session
.as_any()
.downcast_ref::<SqliteSession>()
.ok_or(ConnectionError::UnsupportedDriver {
driver: DriverKind::Sqlite,
})?;
Ok(Arc::new(super::sqlite_object_tree::SqliteObjectTree::new(
sqlite.clone_handle(),
)))
}
other => Err(ConnectionError::UnsupportedDriver { driver: other }),
}
}

pub fn list_children(
&self,
id: &ConnectionId,
parent_id: Option<&str>,
) -> ConnectionResult<Vec<super::object_tree::SqlObjectNode>> {
self.object_tree(id)?.list_children(parent_id)
}

pub fn describe_object(
&self,
id: &ConnectionId,
object_id: &str,
) -> ConnectionResult<super::introspect::TableSchema> {
self.object_tree(id)?.describe(object_id)
}
}

impl ConnectionManager for ConnectionRegistry {
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 @@ -4,14 +4,17 @@ mod error;
mod id;
mod introspect;
mod manager;
mod object_tree;
mod query;
mod smoke_slice;
mod sqlite;
mod sqlite_introspect;
mod sqlite_object_tree;
pub(crate) mod sqlite_query;

pub use error::ConnectionError;
pub use id::{ConnectionId, DriverKind};
pub use introspect::{ColumnMeta, SchemaObject, SchemaObjectKind, TableSchema};
pub use manager::{ConnectionConfig, ConnectionManager, ConnectionRegistry};
pub use object_tree::{ObjectTreeProvider, SqlObjectKind, SqlObjectNode};
pub use query::QueryEvent;
256 changes: 256 additions & 0 deletions apps/tauri/src-tauri/src/connection/object_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
//! Driver-agnostic SQL object tree (schemas → tables/views → columns).

use super::error::{ConnectionError, ConnectionResult};
use super::id::DriverKind;
use super::introspect::TableSchema;
use serde::{Deserialize, Serialize};

/// Catalog node kinds. Extra variants reserved for later driver epics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SqlObjectKind {
Schema,
Table,
View,
Column,
Index,
Trigger,
Function,
Procedure,
}

impl SqlObjectKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Schema => "schema",
Self::Table => "table",
Self::View => "view",
Self::Column => "column",
Self::Index => "index",
Self::Trigger => "trigger",
Self::Function => "function",
Self::Procedure => "procedure",
}
}

fn parse(s: &str) -> Option<Self> {
match s {
"schema" => Some(Self::Schema),
"table" => Some(Self::Table),
"view" => Some(Self::View),
"column" => Some(Self::Column),
"index" => Some(Self::Index),
"trigger" => Some(Self::Trigger),
"function" => Some(Self::Function),
"procedure" => Some(Self::Procedure),
_ => None,
}
}

pub fn is_expandable(self) -> bool {
matches!(
self,
Self::Schema | Self::Table | Self::View | Self::Index | Self::Function | Self::Procedure
)
}

pub fn is_relation(self) -> bool {
matches!(self, Self::Table | Self::View)
}
}

/// Stable tree node shared across IPC and UI.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct SqlObjectNode {
/// Durable id, e.g. `sqlite:table:main:users`.
pub id: String,
pub name: String,
pub kind: SqlObjectKind,
#[serde(skip_serializing_if = "Option::is_none")]
pub schema: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parent_id: Option<String>,
pub expandable: bool,
}

impl SqlObjectNode {
pub fn new(
id: String,
name: String,
kind: SqlObjectKind,
schema: Option<String>,
parent_id: Option<String>,
) -> Self {
Self {
id,
name,
kind,
schema,
parent_id,
expandable: kind.is_expandable(),
}
}
}

/// Per-connection object tree. Drivers implement; registry returns Arc.
pub trait ObjectTreeProvider: Send + Sync {
fn list_children(&self, parent_id: Option<&str>) -> ConnectionResult<Vec<SqlObjectNode>>;
fn describe(&self, object_id: &str) -> ConnectionResult<TableSchema>;
}

/// Encode a path segment so `:` / `%` in names do not break ids.
pub fn enc_segment(s: &str) -> String {
s.replace('%', "%25").replace(':', "%3A")
}

pub fn dec_segment(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
let hex = &s[i + 1..i + 3];
if let Ok(v) = u8::from_str_radix(hex, 16) {
out.push(v as char);
i += 3;
continue;
}
}
out.push(bytes[i] as char);
i += 1;
}
out
}

pub fn schema_id(driver: DriverKind, schema: &str) -> String {
format!("{}:schema:{}", driver.as_str(), enc_segment(schema))
}

pub fn relation_id(driver: DriverKind, kind: SqlObjectKind, schema: &str, name: &str) -> String {
debug_assert!(kind.is_relation());
format!(
"{}:{}:{}:{}",
driver.as_str(),
kind.as_str(),
enc_segment(schema),
enc_segment(name)
)
}

pub fn column_id(driver: DriverKind, schema: &str, table: &str, column: &str) -> String {
format!(
"{}:column:{}:{}:{}",
driver.as_str(),
enc_segment(schema),
enc_segment(table),
enc_segment(column)
)
}

/// Parsed object id.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedObjectId {
Schema {
driver: DriverKind,
schema: String,
},
Relation {
driver: DriverKind,
kind: SqlObjectKind,
schema: String,
name: String,
},
Column {
driver: DriverKind,
schema: String,
table: String,
column: String,
},
}

fn bad_id(msg: impl Into<String>) -> ConnectionError {
ConnectionError::invalid_config(msg)
}

pub fn parse_object_id(id: &str) -> ConnectionResult<ParsedObjectId> {
let parts: Vec<&str> = id.split(':').collect();
if parts.len() < 3 {
return Err(bad_id(format!("invalid object id: {id}")));
}
let driver = match parts[0] {
"sqlite" => DriverKind::Sqlite,
"postgres" => DriverKind::Postgres,
"mysql" => DriverKind::Mysql,
other => return Err(bad_id(format!("unknown driver in object id: {other}"))),
};
let kind = SqlObjectKind::parse(parts[1])
.ok_or_else(|| bad_id(format!("unknown object kind in id: {id}")))?;

match kind {
SqlObjectKind::Schema if parts.len() == 3 => Ok(ParsedObjectId::Schema {
driver,
schema: dec_segment(parts[2]),
}),
SqlObjectKind::Table | SqlObjectKind::View if parts.len() == 4 => {
Ok(ParsedObjectId::Relation {
driver,
kind,
schema: dec_segment(parts[2]),
name: dec_segment(parts[3]),
})
}
SqlObjectKind::Column if parts.len() == 5 => Ok(ParsedObjectId::Column {
driver,
schema: dec_segment(parts[2]),
table: dec_segment(parts[3]),
column: dec_segment(parts[4]),
}),
SqlObjectKind::Schema => Err(bad_id(format!("invalid schema id: {id}"))),
SqlObjectKind::Table | SqlObjectKind::View => {
Err(bad_id(format!("invalid relation id: {id}")))
}
SqlObjectKind::Column => Err(bad_id(format!("invalid column id: {id}"))),
other => Err(bad_id(format!(
"object kind {:?} not supported in ids yet",
other
))),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn round_trip_ids_with_colon_in_name() {
let id = relation_id(DriverKind::Sqlite, SqlObjectKind::Table, "main", "a:b");
assert_eq!(id, "sqlite:table:main:a%3Ab");
match parse_object_id(&id).unwrap() {
ParsedObjectId::Relation { name, schema, .. } => {
assert_eq!(schema, "main");
assert_eq!(name, "a:b");
}
other => panic!("unexpected {other:?}"),
}
}

#[test]
fn column_id_round_trip() {
let id = column_id(DriverKind::Sqlite, "main", "users", "id");
assert_eq!(id, "sqlite:column:main:users:id");
match parse_object_id(&id).unwrap() {
ParsedObjectId::Column {
schema,
table,
column,
..
} => {
assert_eq!(schema, "main");
assert_eq!(table, "users");
assert_eq!(column, "id");
}
other => panic!("unexpected {other:?}"),
}
}
}
Loading
Loading