Skip to content

Commit 4d1ee00

Browse files
authored
Merge pull request #2 from dancixx/feat/migrate-to-sqlite
feat: migrate to sqlite
2 parents 6ee08d5 + 2e63671 commit 4d1ee00

27 files changed

+390
-302
lines changed

Cargo.toml

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

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -20,5 +20,6 @@ thaw = "0.1.3"
2020
common = { path = "common" }
2121

2222

23+
2324
[workspace]
2425
members = ["src-tauri", "common"]

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.3"
3+
version = "1.0.0-alpha.4"
44
edition = "2021"
55

66
[dependencies]

common/src/drivers/mod.rs

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

common/src/drivers/postgresql.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
4+
pub struct Postgresql {
5+
pub user: String,
6+
pub password: String,
7+
pub host: String,
8+
pub port: String,
9+
}
10+
11+
impl Postgresql {
12+
pub fn new(user: String, password: String, host: String, port: String) -> Self {
13+
Self {
14+
user,
15+
password,
16+
host,
17+
port,
18+
}
19+
}
20+
}

common/src/enums.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::fmt::Display;
2+
3+
use serde::{Deserialize, Serialize};
4+
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)]
13+
pub enum Drivers {
14+
POSTGRESQL,
15+
}
16+
17+
impl Display for Drivers {
18+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19+
match self {
20+
Drivers::POSTGRESQL => write!(f, "POSTGRESQL"),
21+
}
22+
}
23+
}
24+
25+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
26+
pub enum ProjectConnectionStatus {
27+
Connected,
28+
#[default]
29+
Disconnected,
30+
}

common/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
pub mod project;
1+
pub mod drivers;
2+
pub mod enums;
3+
pub mod projects;
4+
pub mod utils;

common/src/project.rs

-10
This file was deleted.

common/src/projects/mod.rs

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

common/src/projects/postgresql.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::collections::BTreeMap;
3+
4+
use crate::{drivers::postgresql::Postgresql as PostgresqlDriver, enums::ProjectConnectionStatus};
5+
6+
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
7+
pub struct Postgresql {
8+
pub name: String,
9+
pub driver: PostgresqlDriver,
10+
pub schemas: Option<Vec<String>>,
11+
pub tables: Option<BTreeMap<String, Vec<(String, String)>>>,
12+
pub connection_status: ProjectConnectionStatus,
13+
}
14+
15+
impl Default for Postgresql {
16+
fn default() -> Self {
17+
Self {
18+
name: String::default(),
19+
driver: PostgresqlDriver::default(),
20+
schemas: None,
21+
tables: None,
22+
connection_status: ProjectConnectionStatus::Disconnected,
23+
}
24+
}
25+
}

common/src/utils.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use crate::enums::Project;
2+
3+
pub fn project_matcher(project: Project) -> (String, Project) {
4+
match project {
5+
Project::POSTGRESQL(project) => (project.name.clone(), Project::POSTGRESQL(project)),
6+
}
7+
}

src-tauri/Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "rust-sql-gui"
3-
version = "1.0.0-alpha.3"
3+
version = "1.0.0-alpha.4"
44
description = "PostgreSQL GUI written in Rust"
5-
authors = ["you"]
5+
authors = ["Daniel Boros"]
66
license = ""
77
repository = ""
88
edition = "2021"
@@ -13,14 +13,16 @@ edition = "2021"
1313
tauri-build = { version = "1.5", features = [] }
1414

1515
[dependencies]
16+
common = { path = "../common" }
1617
tauri = { version = "1.5.2", features = [ "shell-open", "fs-all"] }
1718
serde = { version = "1.0.193", features = ["derive"] }
1819
serde_json = "1.0.108"
1920
tokio = "1.34.0"
2021
tokio-postgres = "0.7.10"
2122
chrono = "0.4.31"
2223
sled = "0.34.7"
23-
common = { path = "../common" }
24+
strum = "0.25.0"
25+
strum_macros = "0.25.3"
2426

2527

2628

src-tauri/src/constant.rs

-2
This file was deleted.

src-tauri/src/dbs/mod.rs

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

src-tauri/src/dbs/project.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use common::{
2+
drivers::postgresql::Postgresql as PostgresqlDriver,
3+
enums::{Drivers, Project},
4+
projects::postgresql::Postgresql,
5+
};
6+
use tauri::{Result, State};
7+
8+
use crate::AppState;
9+
10+
#[tauri::command(rename_all = "snake_case")]
11+
pub async fn select_projects(app_state: State<'_, AppState>) -> Result<Vec<(String, Project)>> {
12+
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();
27+
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];
32+
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+
}
41+
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));
54+
Ok(projects)
55+
}
56+
57+
#[tauri::command(rename_all = "snake_case")]
58+
pub async fn insert_project(project: Project, app_state: State<'_, AppState>) -> Result<Project> {
59+
let project_db = app_state.project_db.lock().await;
60+
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+
}
72+
}
73+
74+
#[tauri::command(rename_all = "snake_case")]
75+
pub async fn delete_project(project_name: &str, app_state: State<'_, AppState>) -> Result<String> {
76+
let db = app_state.project_db.lock().await;
77+
let db = db.clone().unwrap();
78+
db.remove(project_name).unwrap();
79+
Ok(project_name.to_string())
80+
}

src-tauri/src/query_db.rs renamed to src-tauri/src/dbs/query.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tauri::{AppHandle, Manager, Result, State};
44

55
use crate::AppState;
66

7-
#[tauri::command]
7+
#[tauri::command(rename_all = "snake_case")]
88
pub async fn insert_query(key: &str, sql: &str, app: AppHandle) -> Result<()> {
99
let app_state = app.state::<AppState>();
1010
let db = app_state.query_db.lock().await;
@@ -14,7 +14,7 @@ pub async fn insert_query(key: &str, sql: &str, app: AppHandle) -> Result<()> {
1414
Ok(())
1515
}
1616

17-
#[tauri::command]
17+
#[tauri::command(rename_all = "snake_case")]
1818
pub async fn select_queries(app_state: State<'_, AppState>) -> Result<BTreeMap<String, String>> {
1919
let query_db = app_state.query_db.lock().await;
2020
let mut queries = BTreeMap::new();
@@ -29,7 +29,7 @@ pub async fn select_queries(app_state: State<'_, AppState>) -> Result<BTreeMap<S
2929
Ok(queries)
3030
}
3131

32-
#[tauri::command]
32+
#[tauri::command(rename_all = "snake_case")]
3333
pub async fn delete_query(key: &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 {

src-tauri/src/drivers/mod.rs

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

src-tauri/src/postgres.rs renamed to src-tauri/src/drivers/postgresql.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ use tokio_postgres::{connect, NoTls};
33

44
use crate::{utils::reflective_get, AppState};
55

6-
#[tauri::command]
7-
pub async fn pg_connector(project: &str, key: &str, app: AppHandle) -> Result<Vec<String>> {
6+
#[tauri::command(rename_all = "snake_case")]
7+
pub async fn postgresql_connector(
8+
project_name: &str,
9+
key: &str,
10+
app: AppHandle,
11+
) -> Result<Vec<String>> {
812
let app_state = app.state::<AppState>();
9-
let mut db = app_state.project_db.lock().await;
10-
if let Some(ref mut db_instance) = *db {
11-
db_instance.insert(project, key).unwrap();
12-
}
13-
1413
let (client, connection) = connect(key, NoTls).await.expect("connection error");
1514
tokio::spawn(async move {
1615
if let Err(e) = connection.await {
@@ -32,19 +31,18 @@ pub async fn pg_connector(project: &str, key: &str, app: AppHandle) -> Result<Ve
3231
let schemas = schemas.iter().map(|r| r.get(0)).collect();
3332
let mut clients = app_state.client.lock().await;
3433
let clients = clients.as_mut().unwrap();
35-
clients.insert(project.to_string(), client);
36-
34+
clients.insert(project_name.to_string(), client);
3735
Ok(schemas)
3836
}
3937

40-
#[tauri::command]
38+
#[tauri::command(rename_all = "snake_case")]
4139
pub async fn select_schema_tables(
42-
project: &str,
40+
project_name: &str,
4341
schema: &str,
4442
app_state: State<'_, AppState>,
4543
) -> Result<Vec<(String, String)>> {
4644
let clients = app_state.client.lock().await;
47-
let client = clients.as_ref().unwrap().get(project).unwrap();
45+
let client = clients.as_ref().unwrap().get(project_name).unwrap();
4846
let tables = client
4947
.query(
5048
r#"
@@ -69,14 +67,14 @@ pub async fn select_schema_tables(
6967
Ok(tables)
7068
}
7169

72-
#[tauri::command]
70+
#[tauri::command(rename_all = "snake_case")]
7371
pub async fn select_sql_result(
74-
project: &str,
72+
project_name: &str,
7573
sql: String,
7674
app_state: State<'_, AppState>,
7775
) -> Result<(Vec<String>, Vec<Vec<String>>)> {
7876
let clients = app_state.client.lock().await;
79-
let client = clients.as_ref().unwrap().get(project).unwrap();
77+
let client = clients.as_ref().unwrap().get(project_name).unwrap();
8078
let rows = client.query(sql.as_str(), &[]).await.unwrap();
8179

8280
if rows.is_empty() {

src-tauri/src/main.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
33

4-
mod constant;
5-
mod postgres;
6-
mod project_db;
7-
mod query_db;
4+
mod dbs;
5+
mod drivers;
86
mod utils;
97

10-
use constant::{PROJECT_DB_PATH, QUERY_DB_PATH};
11-
use postgres::{pg_connector, select_schema_tables, select_sql_result};
12-
use project_db::{delete_project, insert_project, select_projects};
13-
use query_db::{delete_query, insert_query, select_queries};
8+
const PROJECT_DB_PATH: &str = "project_db";
9+
const QUERY_DB_PATH: &str = "query_db";
10+
1411
use sled::Db;
1512
use std::{collections::BTreeMap, sync::Arc};
1613
use tauri::Manager;
@@ -60,15 +57,15 @@ fn main() {
6057
Ok(())
6158
})
6259
.invoke_handler(tauri::generate_handler![
63-
delete_project,
64-
delete_query,
65-
insert_project,
66-
insert_query,
67-
pg_connector,
68-
select_projects,
69-
select_queries,
70-
select_schema_tables,
71-
select_sql_result,
60+
dbs::project::delete_project,
61+
dbs::project::insert_project,
62+
dbs::project::select_projects,
63+
dbs::query::delete_query,
64+
dbs::query::insert_query,
65+
dbs::query::select_queries,
66+
drivers::postgresql::postgresql_connector,
67+
drivers::postgresql::select_schema_tables,
68+
drivers::postgresql::select_sql_result,
7269
])
7370
.run(tauri::generate_context!())
7471
.expect("error while running tauri application");

0 commit comments

Comments
 (0)