diff --git a/src-tauri/src/database/connections.rs b/src-tauri/src/database/connections.rs index 032aeea..a2c5c3d 100644 --- a/src-tauri/src/database/connections.rs +++ b/src-tauri/src/database/connections.rs @@ -156,6 +156,20 @@ impl ConnectedConnection { Scheme::Sqlite(_) => todo!(), } } + pub async fn ping(&self) -> Result<()> { + match &self.pool { + Pool::Mysql(pool) => { + sqlx::query("SELECT 1").execute(pool).await?; + } + Pool::Postgres(pool) => { + sqlx::query("SELECT 1").execute(pool).await?; + } + Pool::Sqlite(pool) => { + sqlx::query("SELECT 1").execute(pool).await?; + } + } + Ok(()) + } } pub fn add_connection(db: &Connection, conn: &ConnectionConfig) -> Result<()> { diff --git a/src-tauri/src/handlers/queries.rs b/src-tauri/src/handlers/queries.rs index 277f41c..d4ed562 100644 --- a/src-tauri/src/handlers/queries.rs +++ b/src-tauri/src/handlers/queries.rs @@ -1,4 +1,4 @@ -use crate::{database::connections::ConnectionConfig, utils::error::CommandResult}; +use crate::{database::connections::ConnectionConfig, utils::error::CommandResult, state::ServiceAccess}; use log::info; use tauri::{command, AppHandle}; @@ -10,7 +10,9 @@ pub fn execute_query(_app_handle: AppHandle, query: String) -> CommandResult<()> } #[command] -pub fn ping_db(_app_handle: AppHandle, _conn: ConnectionConfig) -> CommandResult<()> { +pub async fn ping_db(app_handle: AppHandle, conn: ConnectionConfig) -> CommandResult<()> { + let res = app_handle.acquire_connection(conn.id.to_string()).ping().await; + info!("ping_db: {:?}", res); Ok(()) } diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs index 50bdd8f..8962f35 100644 --- a/src-tauri/src/state.rs +++ b/src-tauri/src/state.rs @@ -1,12 +1,13 @@ +use anyhow::Result; use rusqlite::Connection; -use std::sync::Mutex; +use std::{collections::HashMap, sync::Mutex}; use tauri::{AppHandle, Manager, State}; use crate::database::connections::ConnectedConnection; pub struct AppState { pub db: Mutex>, - pub connections: Mutex>, + pub connections: Mutex>, } pub trait ServiceAccess { @@ -18,7 +19,8 @@ pub trait ServiceAccess { where F: FnOnce(&mut Connection) -> TResult; - fn connections(&self) -> Mutex>; + fn acquire_connection(&self, conn_id: String) -> ConnectedConnection; + fn add_connection(&mut self, conn: ConnectedConnection) -> Result<()>; } impl ServiceAccess for AppHandle { @@ -44,10 +46,21 @@ impl ServiceAccess for AppHandle { operation(db) } - fn connections(&self) -> Mutex> { + fn acquire_connection(&self, conn_id: String) -> ConnectedConnection { let app_state: State = self.state(); - let connections_guard = app_state.connections.lock().unwrap(); + let binding = app_state.connections.lock(); + let connection_guard = binding.as_ref(); + let connection = connection_guard.unwrap().get(&conn_id).unwrap(); - connections_guard.clone().into() + return connection.clone(); + } + + fn add_connection(&mut self, conn: ConnectedConnection) -> Result<()> { + let app_state: State = self.state(); + let mut binding = app_state.connections.lock(); + let connection_guard = binding.as_mut(); + connection_guard.unwrap().insert(conn.config.id.to_string().clone(), conn); + + Ok(()) } }