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
11 changes: 10 additions & 1 deletion 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, SchemaObjectKind, SqlObjectKind, SqlObjectNode, TableSchema,
SchemaObject, SchemaObjectKind, SqlObjectKind, SqlObjectNode, SqliteOverview, TableSchema,
};
use tauri::ipc::Channel;
use tauri::State;
Expand Down Expand Up @@ -98,6 +98,15 @@ pub fn db_list_columns(
registry.with_sqlite(&id, |s| s.list_columns(&table))
}

/// SQLite Overview / Database Info (Flutter `SqliteOverviewTab`).
#[tauri::command]
pub fn db_sqlite_overview(
id: ConnectionId,
registry: State<'_, ConnectionRegistry>,
) -> Result<SqliteOverview, ConnectionError> {
registry.with_sqlite(&id, |s| s.database_overview())
}

/// Execute SQL and stream `QueryEvent` batches over `on_event`.
/// Failures return as the invoke `Err` only (no dual channel `Error` event).
#[tauri::command]
Expand Down
1 change: 1 addition & 0 deletions apps/tauri/src-tauri/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ pub use introspect::{ColumnMeta, SchemaObject, SchemaObjectKind, TableSchema};
pub use manager::{ConnectionConfig, ConnectionManager, ConnectionRegistry};
pub use object_tree::{ObjectTreeProvider, SqlObjectKind, SqlObjectNode};
pub use query::QueryEvent;
pub use sqlite_introspect::SqliteOverview;
9 changes: 9 additions & 0 deletions apps/tauri/src-tauri/src/connection/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ impl SqliteSession {
self.with_connection(|c| super::sqlite_introspect::list_columns(c, table))
}

/// Flutter `databaseOverview` for the Overview / Database Info tab.
pub fn database_overview(
&self,
) -> ConnectionResult<super::sqlite_introspect::SqliteOverview> {
self.with_connection(|c| {
super::sqlite_introspect::database_overview(c, &self.path)
})
}

pub fn execute_streaming(
&self,
sql: &str,
Expand Down
55 changes: 55 additions & 0 deletions apps/tauri/src-tauri/src/connection/sqlite_introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,61 @@ use super::error::{ConnectionError, ConnectionResult};
use super::id::DriverKind;
use super::introspect::{ColumnMeta, SchemaObject, SchemaObjectKind, TableSchema};
use rusqlite::Connection;
use serde::Serialize;

/// Flutter `SqliteConnection.databaseOverview` (+ on-disk file size).
#[derive(Debug, Clone, Serialize)]
pub struct SqliteOverview {
pub version: String,
pub page_count: i64,
pub page_size: i64,
pub journal_mode: String,
pub table_count: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub file_size_bytes: Option<u64>,
}

pub fn database_overview(
conn: &Connection,
path: &str,
) -> ConnectionResult<SqliteOverview> {
let version: String = conn
.query_row("SELECT sqlite_version()", [], |r| r.get(0))
.unwrap_or_default();
let page_count: i64 = conn
.query_row("PRAGMA page_count", [], |r| r.get(0))
.unwrap_or(0);
let page_size: i64 = conn
.query_row("PRAGMA page_size", [], |r| r.get(0))
.unwrap_or(0);
let journal_mode: String = conn
.query_row("PRAGMA journal_mode", [], |r| r.get::<_, String>(0))
.map(|s| s.to_uppercase())
.unwrap_or_else(|_| "—".into());
let table_count: i64 = conn
.query_row(
"SELECT count(*) FROM sqlite_master \
WHERE type='table' AND name NOT LIKE 'sqlite_%'",
[],
|r| r.get(0),
)
.unwrap_or(0);

let file_size_bytes = if path.is_empty() || path == ":memory:" {
None
} else {
std::fs::metadata(path).ok().map(|m| m.len())
};

Ok(SqliteOverview {
version,
page_count,
page_size,
journal_mode,
table_count,
file_size_bytes,
})
}

/// SQLite has no real schemas; expose the default attached DB name.
pub fn list_schemas(_conn: &Connection) -> ConnectionResult<Vec<String>> {
Expand Down
5 changes: 3 additions & 2 deletions apps/tauri/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ mod connection;
pub use connection::{
ColumnMeta, ConnectionConfig, ConnectionError, ConnectionId, ConnectionManager,
ConnectionRegistry, DriverKind, ObjectTreeProvider, QueryEvent, SchemaObject, SchemaObjectKind,
SqlObjectKind, SqlObjectNode, TableSchema,
SqlObjectKind, SqlObjectNode, SqliteOverview, TableSchema,
};

use commands::{
db_connect_sqlite, db_describe_object, db_disconnect, db_execute_query, db_is_connected,
db_list_children, db_list_columns, db_list_schemas, db_list_tables,
db_list_children, db_list_columns, db_list_schemas, db_list_tables, db_sqlite_overview,
};

#[tauri::command]
Expand Down Expand Up @@ -50,6 +50,7 @@ pub fn run() {
db_list_columns,
db_list_children,
db_describe_object,
db_sqlite_overview,
db_execute_query,
])
.run(tauri::generate_context!())
Expand Down
14 changes: 14 additions & 0 deletions apps/tauri/src/api/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ export async function dbListColumns(
return invoke<TableSchema>("db_list_columns", { id, table });
}

/** Flutter `SqliteConnection.databaseOverview`. */
export type SqliteOverview = {
version: string;
page_count: number;
page_size: number;
journal_mode: string;
table_count: number;
file_size_bytes?: number | null;
};

export async function dbSqliteOverview(id: string): Promise<SqliteOverview> {
return invoke<SqliteOverview>("db_sqlite_overview", { id });
}

export function formatConnectionError(err: unknown): string {
if (typeof err === "string") return err;
if (err && typeof err === "object") {
Expand Down
Loading
Loading