Skip to content

Commit

Permalink
feat: change connections vector to hashmap and add ping method
Browse files Browse the repository at this point in the history
  • Loading branch information
invm committed Jul 11, 2023
1 parent 2e370c9 commit f3a72cf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src-tauri/src/database/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down
6 changes: 4 additions & 2 deletions src-tauri/src/handlers/queries.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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(())
}

Expand Down
25 changes: 19 additions & 6 deletions src-tauri/src/state.rs
Original file line number Diff line number Diff line change
@@ -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<Option<Connection>>,
pub connections: Mutex<Vec<ConnectedConnection>>,
pub connections: Mutex<HashMap<String, ConnectedConnection>>,
}

pub trait ServiceAccess {
Expand All @@ -18,7 +19,8 @@ pub trait ServiceAccess {
where
F: FnOnce(&mut Connection) -> TResult;

fn connections<F, TResult>(&self) -> Mutex<Vec<ConnectedConnection>>;
fn acquire_connection(&self, conn_id: String) -> ConnectedConnection;
fn add_connection(&mut self, conn: ConnectedConnection) -> Result<()>;
}

impl ServiceAccess for AppHandle {
Expand All @@ -44,10 +46,21 @@ impl ServiceAccess for AppHandle {
operation(db)
}

fn connections<F, TResult>(&self) -> Mutex<Vec<ConnectedConnection>> {
fn acquire_connection(&self, conn_id: String) -> ConnectedConnection {
let app_state: State<AppState> = 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<AppState> = 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(())
}
}

0 comments on commit f3a72cf

Please sign in to comment.