Skip to content

Commit 7ffb0fe

Browse files
committed
refactor: query loading
1 parent d30b9ed commit 7ffb0fe

File tree

12 files changed

+160
-115
lines changed

12 files changed

+160
-115
lines changed

src-tauri/src/drivers/pgsql.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,43 @@ use crate::{utils::reflective_get, AppState};
1313
#[tauri::command(rename_all = "snake_case")]
1414
pub async fn pgsql_connector(
1515
project_id: &str,
16-
key: &str,
16+
key: Option<&str>,
1717
app: AppHandle,
1818
) -> Result<ProjectConnectionStatus> {
1919
let app_state = app.state::<AppState>();
20-
let connection = tokio_time::timeout(tokio_time::Duration::from_secs(10), connect(key, NoTls))
20+
let mut clients = app_state.client.lock().await;
21+
22+
// check if connection already exists
23+
if clients.as_ref().unwrap().contains_key(project_id) {
24+
tracing::info!("Postgres connection already exists!");
25+
return Ok(ProjectConnectionStatus::Connected);
26+
}
27+
28+
let key = match key {
29+
Some(key) => key.to_string(),
30+
None => {
31+
let projects_db = app_state.project_db.lock().await;
32+
let projects_db = projects_db.as_ref().unwrap();
33+
let project_details = projects_db.get(project_id).unwrap().unwrap().to_vec();
34+
let project_details = String::from_utf8(project_details).unwrap();
35+
let project_details = project_details.split(":").collect::<Vec<&str>>();
36+
let project_details = project_details
37+
.into_iter()
38+
.skip(1)
39+
.map(|s| {
40+
let kv = s.split('=').collect::<Vec<&str>>();
41+
kv[1].to_owned()
42+
})
43+
.collect::<Vec<String>>();
44+
let project_details = format!(
45+
"user={} password={} host={} port={}",
46+
project_details[0], project_details[1], project_details[2], project_details[3]
47+
);
48+
project_details
49+
}
50+
};
51+
52+
let connection = tokio_time::timeout(tokio_time::Duration::from_secs(10), connect(&key, NoTls))
2153
.await
2254
.map_err(|_| PostgresqlError::ConnectionTimeout);
2355

@@ -52,7 +84,6 @@ pub async fn pgsql_connector(
5284
return Ok(ProjectConnectionStatus::Failed);
5385
}
5486

55-
let mut clients = app_state.client.lock().await;
5687
let clients = clients.as_mut().unwrap();
5788
clients.insert(project_id.to_string(), client);
5889

src/dashboard/query_editor.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn QueryEditor(index: usize) -> impl IntoView {
6868
let run_query = create_action(move |tabs_store: &Arc<Mutex<TabsStore>>| {
6969
let tabs_store = tabs_store.clone();
7070
async move {
71-
tabs_store.lock().await.run_query().await.unwrap();
71+
tabs_store.lock().await.run_query().await;
7272
}
7373
});
7474

@@ -84,31 +84,34 @@ pub fn QueryEditor(index: usize) -> impl IntoView {
8484

8585
view! {
8686
<div _ref=node_ref class="border-b-1 border-neutral-200 h-72 sticky">
87-
<AddCustomQuery show=show/>
8887
<div class="absolute bottom-0 items-center text-xs flex justify-between px-4 left-0 w-full h-10 bg-gray-50">
8988
<Show when=move || active_project().is_some() fallback=|| view! { <div></div> }>
89+
<AddCustomQuery show=show project_id=active_project().unwrap()/>
9090
<div class="appearance-auto py-1 px-2 border-1 border-neutral-200 bg-white hover:bg-neutral-200 rounded-md">
9191
{active_project}
9292
</div>
9393
</Show>
94-
<div class="flex flex-row gap-2">
95-
<button
96-
class="p-1 border-1 border-neutral-200 bg-white hover:bg-neutral-200 rounded-md"
97-
on:click=move |_| show.set(true)
98-
>
99-
"Save Query"
100-
</button>
101-
<button
102-
class="p-1 border-1 border-neutral-200 bg-white hover:bg-neutral-200 rounded-md"
103-
on:click={
104-
let tabs_store = tabs_store_arc.clone();
105-
move |_| run_query.dispatch(tabs_store.clone())
106-
}
107-
>
94+
<Show when=move || active_project().is_some() fallback=|| view! { <div></div> }>
95+
96+
<div class="flex flex-row gap-2">
97+
<button
98+
class="p-1 border-1 border-neutral-200 bg-white hover:bg-neutral-200 rounded-md"
99+
on:click=move |_| show.set(true)
100+
>
101+
"Save Query"
102+
</button>
103+
<button
104+
class="p-1 border-1 border-neutral-200 bg-white hover:bg-neutral-200 rounded-md"
105+
on:click={
106+
let tabs_store = tabs_store_arc.clone();
107+
move |_| run_query.dispatch(tabs_store.clone())
108+
}
109+
>
108110

109-
"Query"
110-
</button>
111-
</div>
111+
"Query"
112+
</button>
113+
</div>
114+
</Show>
112115
</div>
113116
</div>
114117
}

src/databases/pgsql/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a> Pgsql<'a> {
5353
Invoke::PgsqlConnector.as_ref(),
5454
&InvokePgsqlConnectorArgs {
5555
project_id: &self.project_id.get(),
56-
key: connection_string.as_str(),
56+
key: Some(&connection_string),
5757
},
5858
)
5959
.await

src/databases/pgsql/index.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::rc::Rc;
2+
13
use leptos::*;
24
use leptos_icons::*;
35
use leptos_toaster::{Toast, ToastId, ToastVariant, Toasts};
@@ -8,6 +10,7 @@ use common::enums::ProjectConnectionStatus;
810

911
#[component]
1012
pub fn Pgsql(project_id: String) -> impl IntoView {
13+
let project_id = Rc::new(project_id);
1114
let tabs_store = expect_context::<TabsStore>();
1215
let projects_store = expect_context::<ProjectsStore>();
1316
let project_details = projects_store.select_project_by_name(&project_id).unwrap();
@@ -25,7 +28,7 @@ pub fn Pgsql(project_id: String) -> impl IntoView {
2528
.collect::<Vec<String>>();
2629
let connection_params = Box::leak(connection_params.into_boxed_slice());
2730
// [user, password, host, port]
28-
let mut pgsql = Pgsql::new(project_id.clone());
31+
let mut pgsql = Pgsql::new(project_id.clone().to_string());
2932
{
3033
pgsql.load_connection_details(
3134
&connection_params[0],
@@ -133,7 +136,8 @@ pub fn Pgsql(project_id: String) -> impl IntoView {
133136
on:click={
134137
let project_id = project_id.clone();
135138
move |_| {
136-
delete_project.dispatch((projects_store, project_id.clone()));
139+
delete_project
140+
.dispatch((projects_store, project_id.clone().to_string()));
137141
}
138142
}
139143
>

src/databases/pgsql/schema.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ pub fn Schema(schema: String) -> impl IntoView {
2828
let schema = schema.clone();
2929
move |_| {
3030
set_is_loading(true);
31-
let schema = (*schema).clone();
32-
load_tables.dispatch(schema.clone());
31+
let schema = schema.clone();
32+
load_tables.dispatch(schema.clone().to_string());
3333
}
3434
}
3535
>
@@ -43,15 +43,15 @@ pub fn Schema(schema: String) -> impl IntoView {
4343
/>
4444
</Show>
4545

46-
{(*schema).clone()}
46+
{&*schema}
4747
</button>
4848
<div class="pl-2">
4949
<Show when=show fallback=|| view! {}>
5050
<For
5151
each={
5252
let schema = schema.clone();
5353
move || {
54-
let schema = (*schema).clone();
54+
let schema = schema.clone();
5555
pgsql.select_tables_by_schema(&schema).unwrap()
5656
}
5757
}

src/databases/pgsql/table.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
use std::rc::Rc;
2+
13
use leptos::*;
24
use leptos_icons::*;
35

46
use crate::databases::pgsql::driver::Pgsql;
57

68
#[component]
79
pub fn Table(table: (String, String), schema: String) -> impl IntoView {
10+
let table = Rc::new(table);
11+
let schema = Rc::new(schema);
812
let pgsql = expect_context::<Pgsql>();
913
let query = create_action(move |(schema, table, pgsql): &(String, String, Pgsql)| {
1014
let pgsql = *pgsql;
@@ -23,15 +27,15 @@ pub fn Table(table: (String, String), schema: String) -> impl IntoView {
2327
class="flex flex-row justify-between items-center hover:font-semibold cursor-pointer"
2428
on:click={
2529
let table = table.clone();
26-
move |_| { query.dispatch((schema.clone(), table.0.clone(), pgsql)) }
30+
move |_| { query.dispatch((schema.clone().to_string(), table.0.clone(), pgsql)) }
2731
}
2832
>
2933

3034
<div class="flex flex-row items-center gap-1">
3135
<Icon icon=icondata::HiTableCellsOutlineLg width="12" height="12"/>
32-
<p>{table.0}</p>
36+
<p>{table.0.to_string()}</p>
3337
</div>
34-
<p>{table.1}</p>
38+
<p>{table.1.to_string()}</p>
3539
</div>
3640
}
3741
}

src/invoke.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl AsRef<str> for Invoke {
6262
#[derive(Serialize, Deserialize)]
6363
pub struct InvokePgsqlConnectorArgs<'a> {
6464
pub project_id: &'a str,
65-
pub key: &'a str,
65+
pub key: Option<&'a str>,
6666
}
6767

6868
#[derive(Serialize, Deserialize)]

src/modals/add_custom_query.rs

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,50 @@
1+
use std::rc::Rc;
2+
13
use leptos::*;
24
use thaw::{Modal, ModalFooter};
35

4-
use crate::store::{projects::ProjectsStore, queries::QueriesStore};
6+
use crate::store::queries::QueriesStore;
57

68
#[component]
7-
pub fn AddCustomQuery(show: RwSignal<bool>) -> impl IntoView {
8-
let projects_store = expect_context::<ProjectsStore>();
9+
pub fn AddCustomQuery(show: RwSignal<bool>, project_id: String) -> impl IntoView {
10+
let project_id = Rc::new(project_id);
11+
let project_id_clone = project_id.clone();
912
let query_store = expect_context::<QueriesStore>();
10-
let (query_title, set_query_title) = create_signal(String::new());
11-
//let projects = create_memo(move |_| projects_store.get_projects().unwrap());
12-
13-
let (project_name, set_project_name) = create_signal("".to_string());
14-
// create_effect(move |_| {
15-
// if !projects.get().is_empty() {
16-
// set_project_name(projects.get()[0].clone());
17-
// }
18-
// });
19-
13+
let (title, set_title) = create_signal(String::new());
2014
let insert_query = create_action(
21-
move |(query_db, key, project_name): &(QueriesStore, String, String)| {
15+
move |(query_db, project_id, title): &(QueriesStore, String, String)| {
2216
let query_db_clone = *query_db;
23-
let key = key.clone();
24-
let project_name = project_name.clone();
17+
let project_id = project_id.clone();
18+
let title = title.clone();
2519
async move {
26-
query_db_clone.insert_query(&key, &project_name).await;
20+
query_db_clone.insert_query(&project_id, &title).await;
2721
}
2822
},
2923
);
3024

3125
view! {
3226
<Modal show=show title="Save query!">
3327
<div class="flex flex-col gap-2">
34-
<select
35-
class="border-1 border-neutral-200 p-1 rounded-md w-full bg-white appearance-none"
36-
value=project_name
37-
default_value="teszt"
38-
placeholder="Select project.."
39-
>// <For
40-
// each=move || projects.get()
41-
// key=|project| project.clone()
42-
// children=move |p| {
43-
// view! {
44-
// <option value=&p selected=project_name() == p>
45-
// {p}
46-
// </option>
47-
// }
48-
// }
49-
// />
50-
51-
</select>
28+
<p>Project: {&*project_id_clone}</p>
5229
<input
5330
class="border-1 border-neutral-200 p-1 rounded-md w-full"
5431
type="text"
5532
placeholder="Add query name.."
56-
value=query_title
57-
on:input=move |e| set_query_title(event_target_value(&e))
33+
value=title
34+
on:input=move |e| set_title(event_target_value(&e))
5835
/>
5936
</div>
6037

6138
<ModalFooter slot>
6239
<div class="flex gap-2 justify-end">
6340
<button
6441
class="px-4 py-2 border-1 border-neutral-200 hover:bg-neutral-200 rounded-md"
65-
on:click=move |_| {
66-
insert_query.dispatch((query_store, query_title(), project_name()));
67-
show.set(false);
42+
on:click={
43+
let project_id = project_id.clone();
44+
move |_| {
45+
insert_query.dispatch((query_store, project_id.to_string(), title()));
46+
show.set(false);
47+
}
6848
}
6949
>
7050

src/sidebar/index.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use leptos::*;
22
use leptos_use::{use_document, use_event_listener};
33

44
use crate::{
5-
databases::pgsql::index::Pgsql, modals::add_pgsql_connection::AddPgsqlConnection,
6-
store::projects::ProjectsStore,
5+
databases::pgsql::index::Pgsql,
6+
modals::add_pgsql_connection::AddPgsqlConnection,
7+
store::{projects::ProjectsStore, queries::QueriesStore},
78
};
89
use common::enums::Drivers;
910

@@ -12,6 +13,7 @@ use super::queries::Queries;
1213
#[component]
1314
pub fn Sidebar() -> impl IntoView {
1415
let projects_store = expect_context::<ProjectsStore>();
16+
let queries_store = expect_context::<QueriesStore>();
1517
let show = create_rw_signal(false);
1618
let _ = use_event_listener(use_document(), ev::keydown, move |event| {
1719
if event.key() == "Escape" {
@@ -22,6 +24,7 @@ pub fn Sidebar() -> impl IntoView {
2224
|| {},
2325
move |_| async move {
2426
projects_store.load_projects().await;
27+
queries_store.load_queries().await;
2528
},
2629
);
2730

@@ -55,12 +58,14 @@ pub fn Sidebar() -> impl IntoView {
5558
/>
5659

5760
</div>
58-
<div class="py-2">
59-
<p class="font-semibold text-lg">Saved Queries</p>
60-
<div class="text-sm">
61-
<Queries/>
61+
<Show when=move || !queries_store.0.get().is_empty() fallback=|| view! { <div></div> }>
62+
<div class="py-2">
63+
<p class="font-semibold text-lg">Saved Queries</p>
64+
<div class="text-sm">
65+
<Queries/>
66+
</div>
6267
</div>
63-
</div>
68+
</Show>
6469
</div>
6570
}
6671
}

src/sidebar/queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub fn Queries() -> impl IntoView {
1616
view! {
1717
<For
1818
each=move || queries_store.0.get()
19-
key=|(key, _)| key.clone()
20-
children=move |(key, _)| view! { <Query key=key/> }
19+
key=|(query_id, _)| query_id.clone()
20+
children=move |(query_id, sql)| view! { <Query query_id sql/> }
2121
/>
2222
}
2323
}

0 commit comments

Comments
 (0)