Skip to content

Commit 5da4f8b

Browse files
authored
Merge pull request #6 from dancixx/feat/improve-error-handling
refactor: improve performance for beta
2 parents 820cfa8 + e5a8334 commit 5da4f8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1765
-1500
lines changed

Cargo.toml

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
[package]
2-
name = "rust-sql-gui-ui"
3-
version = "1.0.0-alpha.9"
2+
name = "rsql"
3+
version = "1.0.0-beta.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77
[dependencies]
8-
leptos = { version = "0.6.8", features = ["csr", "nightly"] }
8+
leptos = { version = "0.6.11", features = ["csr", "nightly"] }
99
leptos_devtools = { git = "https://github.com/luoxiaozero/leptos-devtools" }
1010
serde = { version = "1.0.192", features = ["derive"] }
1111
serde-wasm-bindgen = "0.6.3"
1212
wasm-bindgen = { version ="0.2.91", features = ["serde-serialize"] }
1313
js-sys = "0.3.68"
14-
leptos-use = { version = "0.10.9", features = ["serde", "serde_json"]}
14+
leptos-use = { version = "0.10.10", features = ["serde", "serde_json"]}
1515
leptos_icons = "0.3.0" # https://carlosted.github.io/icondata/
1616
serde_json = "1.0.113"
1717
wasm-bindgen-futures = "0.4.39"
@@ -22,6 +22,16 @@ common = { path = "common" }
2222
futures = "0.3.30"
2323
async-stream = "0.3.5"
2424
icondata = "0.3.0"
25+
ahash = { version = "0.8.11", features = ["serde"] }
26+
leptos_toaster = { version = "0.1.6", features = ["builtin_toast"] }
27+
proc-macro2 = "1.0.82"
28+
quote = "1.0.36"
29+
syn = { version = "2.0.64", features = ["full"] }
30+
chrono = "0.4.38"
31+
2532

2633
[workspace]
2734
members = ["src-tauri", "common"]
35+
36+
[lib]
37+
proc-macro = true

common/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "common"
3-
version = "1.0.0-alpha.9"
3+
version = "1.0.0-beta.0"
44
edition = "2021"
55

66
[dependencies]

common/src/drivers/mod.rs

-1
This file was deleted.

common/src/drivers/postgresql.rs

-20
This file was deleted.

common/src/enums.rs

+49-10
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,68 @@ use std::fmt::Display;
22

33
use serde::{Deserialize, Serialize};
44

5-
use super::projects::postgresql::Postgresql;
6-
7-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
8-
pub enum Project {
9-
POSTGRESQL(Postgresql),
10-
}
11-
12-
#[derive(Clone, Serialize, Deserialize)]
5+
#[derive(Clone, Copy, Serialize, Deserialize, Default)]
136
pub enum Drivers {
14-
POSTGRESQL,
7+
#[default]
8+
PGSQL,
159
}
1610

1711
impl Display for Drivers {
1812
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1913
match self {
20-
Drivers::POSTGRESQL => write!(f, "POSTGRESQL"),
14+
Drivers::PGSQL => write!(f, "PGSQL"),
15+
}
16+
}
17+
}
18+
19+
impl AsRef<str> for Drivers {
20+
fn as_ref(&self) -> &str {
21+
match self {
22+
Drivers::PGSQL => "PGSQL",
2123
}
2224
}
2325
}
2426

2527
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
2628
pub enum ProjectConnectionStatus {
2729
Connected,
30+
Connecting,
2831
#[default]
2932
Disconnected,
33+
Failed,
3034
}
35+
36+
impl std::error::Error for ProjectConnectionStatus {}
37+
impl Display for ProjectConnectionStatus {
38+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39+
match self {
40+
ProjectConnectionStatus::Connected => write!(f, "Connected"),
41+
ProjectConnectionStatus::Connecting => write!(f, "Connecting"),
42+
ProjectConnectionStatus::Disconnected => write!(f, "Disconnected"),
43+
ProjectConnectionStatus::Failed => write!(f, "Failed"),
44+
}
45+
}
46+
}
47+
48+
use std::fmt;
49+
50+
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
51+
pub enum PostgresqlError {
52+
ConnectionTimeout,
53+
ConnectionError,
54+
QueryTimeout,
55+
QueryError,
56+
}
57+
58+
impl std::error::Error for PostgresqlError {}
59+
impl fmt::Display for PostgresqlError {
60+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61+
match *self {
62+
PostgresqlError::ConnectionTimeout => write!(f, "ConnectionTimeout"),
63+
PostgresqlError::ConnectionError => write!(f, "ConnectionError"),
64+
PostgresqlError::QueryTimeout => write!(f, "QueryTimeout"),
65+
PostgresqlError::QueryError => write!(f, "QueryError"),
66+
}
67+
}
68+
}
69+

common/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pub mod drivers;
21
pub mod enums;
3-
pub mod projects;
2+
pub mod types;
3+

common/src/projects/mod.rs

-1
This file was deleted.

common/src/projects/postgresql.rs

-36
This file was deleted.

common/src/types/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod pgsql;
2+

common/src/types/pgsql.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub type PgsqlLoadSchemas = Vec<String>;
2+
pub type PgsqlLoadTables = Vec<(String, String)>;
3+
pub type PgsqlRunQuery = (Vec<String>, Vec<Vec<String>>, f32);
4+

src-tauri/Cargo.toml

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
2-
name = "rust-sql-gui"
3-
version = "1.0.0-alpha.9"
2+
name = "rsql_tauri"
3+
version = "1.0.0-beta.0"
44
description = "PostgreSQL GUI written in Rust"
55
authors = ["Daniel Boros"]
66
license = ""
@@ -10,17 +10,21 @@ edition = "2021"
1010
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1111

1212
[build-dependencies]
13-
tauri-build = { version = "1.5.1", features = [] }
13+
tauri-build = { version = "1.5.2", features = [] }
1414

1515
[dependencies]
1616
common = { path = "../common" }
17-
tauri = { version = "1.6.0", features = [ "shell-open", "fs-all"] }
17+
tauri = { version = "1.6.5", features = ["shell-open", "fs-all"] }
1818
serde = { version = "1.0.193", features = ["derive"] }
1919
serde_json = "1.0.108"
20-
tokio = "1.36.0"
20+
tokio = "1.37.0"
2121
tokio-postgres = "0.7.10"
2222
chrono = "0.4.31"
2323
sled = "0.34.7"
24+
anyhow = "1.0.83"
25+
tracing = "0.1.40"
26+
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
27+
ahash = { version = "0.8.11", features = ["serde"] }
2428

2529

2630

src-tauri/src/dbs/project.rs

+27-58
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,49 @@
1-
use common::{
2-
drivers::postgresql::Postgresql as PostgresqlDriver,
3-
enums::{Drivers, Project},
4-
projects::postgresql::Postgresql,
5-
};
1+
use std::collections::BTreeMap;
2+
63
use tauri::{Result, State};
74

85
use crate::AppState;
96

107
#[tauri::command(rename_all = "snake_case")]
11-
pub async fn select_projects(app_state: State<'_, AppState>) -> Result<Vec<(String, Project)>> {
8+
pub async fn project_db_select(app_state: State<'_, AppState>) -> Result<BTreeMap<String, String>> {
129
let project_db = app_state.project_db.lock().await;
13-
let mut projects = project_db
14-
.clone()
15-
.unwrap()
16-
.iter()
17-
.map(|r| {
18-
let (project, connection_string) = r.unwrap();
19-
let project = String::from_utf8(project.to_vec()).unwrap();
20-
let connection_string = String::from_utf8(connection_string.to_vec()).unwrap();
21-
let connection_string = connection_string.split(':').collect::<Vec<&str>>();
22-
let _driver = connection_string[0].to_string();
23-
let _driver = _driver.split('=').collect::<Vec<&str>>()[1];
24-
let project_details = match _driver {
25-
d if d == Drivers::POSTGRESQL.to_string() => {
26-
let mut driver = PostgresqlDriver::default();
10+
let db = project_db.clone().unwrap();
11+
let mut projects = BTreeMap::new();
2712

28-
for c in connection_string[1..].iter() {
29-
let c = c.split('=').collect::<Vec<&str>>();
30-
let key = c[0];
31-
let value = c[1];
13+
if db.is_empty() {
14+
tracing::info!("No projects found in the database");
15+
return Ok(projects);
16+
}
3217

33-
match key {
34-
"user" => driver.user = value.to_string(),
35-
"password" => driver.password = value.to_string(),
36-
"host" => driver.host = value.to_string(),
37-
"port" => driver.port = value.to_string(),
38-
_ => (),
39-
}
40-
}
18+
for p in db.iter() {
19+
let project = p.unwrap();
4120

42-
Project::POSTGRESQL(Postgresql {
43-
name: project.clone(),
44-
driver,
45-
..Postgresql::default()
46-
})
47-
}
48-
_ => unreachable!(),
49-
};
50-
(project, project_details)
51-
})
52-
.collect::<Vec<(String, Project)>>();
53-
projects.sort_by(|a, b| a.0.cmp(&b.0));
21+
let project = (
22+
String::from_utf8(project.0.to_vec()).unwrap(),
23+
String::from_utf8(project.1.to_vec()).unwrap(),
24+
);
25+
projects.insert(project.0, project.1);
26+
}
5427
Ok(projects)
5528
}
5629

5730
#[tauri::command(rename_all = "snake_case")]
58-
pub async fn insert_project(project: Project, app_state: State<'_, AppState>) -> Result<Project> {
31+
pub async fn project_db_insert(
32+
project_id: &str,
33+
project_details: &str,
34+
app_state: State<'_, AppState>,
35+
) -> Result<()> {
5936
let project_db = app_state.project_db.lock().await;
6037
let db = project_db.clone().unwrap();
61-
match project {
62-
Project::POSTGRESQL(project) => {
63-
let driver = &project.driver;
64-
let connection_string = format!(
65-
"driver=POSTGRESQL:user={}:password={}:host={}:port={}",
66-
driver.user, driver.password, driver.host, driver.port,
67-
);
68-
db.insert(&project.name, &*connection_string).unwrap();
69-
Ok(Project::POSTGRESQL(project))
70-
}
71-
}
38+
db.insert(project_id, project_details).unwrap();
39+
Ok(())
7240
}
7341

7442
#[tauri::command(rename_all = "snake_case")]
75-
pub async fn delete_project(project_name: &str, app_state: State<'_, AppState>) -> Result<()> {
43+
pub async fn project_db_delete(project_id: &str, app_state: State<'_, AppState>) -> Result<()> {
7644
let db = app_state.project_db.lock().await;
7745
let db = db.clone().unwrap();
78-
db.remove(project_name).unwrap();
46+
db.remove(project_id).unwrap();
7947
Ok(())
8048
}
49+

src-tauri/src/dbs/query.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@ use tauri::{AppHandle, Manager, Result, State};
55
use crate::AppState;
66

77
#[tauri::command(rename_all = "snake_case")]
8-
pub async fn insert_query(key: &str, sql: &str, app: AppHandle) -> Result<()> {
9-
let app_state = app.state::<AppState>();
10-
let db = app_state.query_db.lock().await;
11-
if let Some(ref db_instance) = *db {
12-
db_instance.insert(key, sql).unwrap();
13-
}
14-
Ok(())
15-
}
16-
17-
#[tauri::command(rename_all = "snake_case")]
18-
pub async fn select_queries(app_state: State<'_, AppState>) -> Result<BTreeMap<String, String>> {
8+
pub async fn query_db_select(app_state: State<'_, AppState>) -> Result<BTreeMap<String, String>> {
199
let query_db = app_state.query_db.lock().await;
2010
let mut queries = BTreeMap::new();
2111
if let Some(ref query_db) = *query_db {
@@ -30,10 +20,21 @@ pub async fn select_queries(app_state: State<'_, AppState>) -> Result<BTreeMap<S
3020
}
3121

3222
#[tauri::command(rename_all = "snake_case")]
33-
pub async fn delete_query(key: &str, app_state: State<'_, AppState>) -> Result<()> {
23+
pub async fn query_db_insert(query_id: &str, sql: &str, app: AppHandle) -> Result<()> {
24+
let app_state = app.state::<AppState>();
25+
let db = app_state.query_db.lock().await;
26+
if let Some(ref db_instance) = *db {
27+
db_instance.insert(query_id, sql).unwrap();
28+
}
29+
Ok(())
30+
}
31+
32+
#[tauri::command(rename_all = "snake_case")]
33+
pub async fn query_db_delete(query_id: &str, app_state: State<'_, AppState>) -> Result<()> {
3434
let query_db = app_state.query_db.lock().await;
3535
if let Some(ref query_db) = *query_db {
36-
query_db.remove(key).unwrap();
36+
query_db.remove(query_id).unwrap();
3737
};
3838
Ok(())
3939
}
40+

src-tauri/src/drivers/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pub mod postgresql;
1+
pub mod pgsql;
2+

0 commit comments

Comments
 (0)