1- use std:: { borrow :: Borrow , collections:: BTreeMap } ;
1+ use std:: collections:: BTreeMap ;
22
3- use common:: { enums:: Project , utils:: project_matcher} ;
3+ use common:: {
4+ enums:: { Project , ProjectConnectionStatus } ,
5+ utils:: project_matcher,
6+ } ;
47use leptos:: {
58 create_rw_signal, error:: Result , RwSignal , SignalGet , SignalGetUntracked , SignalUpdate ,
69} ;
@@ -27,7 +30,7 @@ impl ProjectsStore {
2730 pub fn set_projects ( & self , projects : Vec < Project > ) -> Result < BTreeMap < String , Project > > {
2831 let projects = projects
2932 . into_iter ( )
30- . map ( |project| project_matcher ( project ) )
33+ . map ( project_matcher)
3134 . collect :: < BTreeMap < String , Project > > ( ) ;
3235 self . 0 . update ( |prev| {
3336 * prev = projects;
@@ -39,7 +42,6 @@ impl ProjectsStore {
3942 self . 0 . update ( |prev| {
4043 let project = match project {
4144 Project :: POSTGRESQL ( project) => ( project. name . clone ( ) , Project :: POSTGRESQL ( project) ) ,
42- _ => unreachable ! ( ) ,
4345 } ;
4446 prev. insert ( project. 0 , project. 1 ) ;
4547 } ) ;
@@ -52,77 +54,93 @@ impl ProjectsStore {
5254 Ok ( projects)
5355 }
5456
55- pub fn create_project_connection_string ( & self , project_key : & str ) -> String {
57+ pub fn create_project_connection_string ( & self , project_name : & str ) -> String {
5658 let projects = self . 0 . get_untracked ( ) ;
57- let ( _, project) = projects. get_key_value ( project_key ) . unwrap ( ) ;
59+ let ( _, project) = projects. get_key_value ( project_name ) . unwrap ( ) ;
5860
59- format ! (
60- "user={} password={} host={} port={}" ,
61- project. user, project. password, project. host, project. port,
62- )
61+ match project {
62+ Project :: POSTGRESQL ( project) => {
63+ let driver = project. driver . clone ( ) ;
64+ format ! (
65+ "user={}:password={}:host={}:port={}" ,
66+ driver. user, driver. password, driver. host, driver. port,
67+ )
68+ }
69+ }
6370 }
6471
65- pub async fn connect ( & self , project : & str ) -> Result < Vec < String > > {
72+ pub async fn connect ( & self , project_name : & str ) -> Result < Vec < String > > {
6673 let projects = self . 0 ;
74+ let _projects = projects. get_untracked ( ) ;
75+ let project = _projects. get ( project_name) . unwrap ( ) ;
76+
77+ match project {
78+ Project :: POSTGRESQL ( project) => {
79+ if project. connection_status == ProjectConnectionStatus :: Connected {
80+ return Ok ( project. schmemas . clone ( ) . unwrap ( ) ) ;
81+ }
6782
68- if let Some ( project) = projects. get_untracked ( ) . get ( project) {
69- if !project. schemas . is_empty ( ) {
70- return Ok ( project. schemas . clone ( ) ) ;
83+ let schemas = self . postgresql_schema_selector ( & project. name ) . await ?;
84+ projects. update ( |prev| {
85+ let project = prev. get_mut ( project_name) . unwrap ( ) ;
86+ match project {
87+ Project :: POSTGRESQL ( project) => {
88+ project. schmemas = Some ( schemas. clone ( ) ) ;
89+ project. connection_status = ProjectConnectionStatus :: Connected ;
90+ }
91+ }
92+ } ) ;
93+ Ok ( schemas)
7194 }
7295 }
73-
74- let connection_string = self . create_project_connection_string ( project) ;
75- let args = serde_wasm_bindgen:: to_value ( & InvokePostgresConnectionArgs {
76- project : project. to_string ( ) ,
77- key : connection_string,
78- } )
79- . unwrap ( ) ;
80- let schemas = invoke ( & Invoke :: postgresql_connector. to_string ( ) , args) . await ;
81- let mut schemas = serde_wasm_bindgen:: from_value :: < Vec < String > > ( schemas) . unwrap ( ) ;
82- schemas. sort ( ) ;
83- projects. update ( |prev| {
84- let project = prev. get_mut ( project) . unwrap ( ) ;
85- project. schemas = schemas;
86- project. status = ProjectConnectionStatus :: Connected ;
87- } ) ;
88- let schemas = self . 0 . get_untracked ( ) . get ( project) . unwrap ( ) . schemas . clone ( ) ;
89- Ok ( schemas)
9096 }
9197
9298 pub async fn retrieve_tables (
9399 & self ,
94- project : & str ,
100+ project_name : & str ,
95101 schema : & str ,
96102 ) -> Result < Vec < ( String , String ) > > {
97103 let projects = self . 0 ;
98- let p = projects. borrow ( ) . get_untracked ( ) ;
99- let p = p. get ( project) . unwrap ( ) ;
100- if let Some ( tables) = p. tables . get ( schema) {
101- if !tables. is_empty ( ) {
102- return Ok ( tables. clone ( ) ) ;
104+ let _projects = projects. get_untracked ( ) ;
105+ let project = _projects. get ( project_name) . unwrap ( ) ;
106+
107+ match project {
108+ Project :: POSTGRESQL ( project) => {
109+ if let Some ( tables) = project. tables . as_ref ( ) . unwrap ( ) . get ( schema) {
110+ if !tables. is_empty ( ) {
111+ return Ok ( tables. clone ( ) ) ;
112+ }
113+ }
114+
115+ let tables = self
116+ . postgresql_table_selector ( & project. name , schema)
117+ . await
118+ . unwrap ( ) ;
119+
120+ projects. update ( |prev| {
121+ let project = prev. get_mut ( project_name) . unwrap ( ) ;
122+ match project {
123+ Project :: POSTGRESQL ( project) => {
124+ let _tables = project. tables . as_mut ( ) . unwrap ( ) ;
125+ _tables. insert ( schema. to_string ( ) , tables. clone ( ) ) ;
126+ project. tables = Some ( _tables. clone ( ) ) ;
127+ }
128+ }
129+ } ) ;
130+
131+ let schemas = self . postgresql_schema_selector ( & project. name ) . await ?;
132+ projects. update ( |prev| {
133+ let project = prev. get_mut ( project_name) . unwrap ( ) ;
134+ match project {
135+ Project :: POSTGRESQL ( project) => {
136+ project. schmemas = Some ( schemas. clone ( ) ) ;
137+ }
138+ }
139+ } ) ;
140+
141+ Ok ( tables)
103142 }
104143 }
105- let args = serde_wasm_bindgen:: to_value ( & InvokeSchemaTablesArgs {
106- project : project. to_string ( ) ,
107- schema : schema. to_string ( ) ,
108- } )
109- . unwrap ( ) ;
110- let tables = invoke ( & Invoke :: select_schema_tables. to_string ( ) , args) . await ;
111- let tables = serde_wasm_bindgen:: from_value :: < Vec < ( String , String ) > > ( tables) . unwrap ( ) ;
112- projects. update ( |prev| {
113- let project = prev. get_mut ( project) . unwrap ( ) ;
114- project. tables . insert ( schema. to_string ( ) , tables. clone ( ) ) ;
115- } ) ;
116- let tables = self
117- . 0
118- . get_untracked ( )
119- . get ( project)
120- . unwrap ( )
121- . tables
122- . get ( schema)
123- . unwrap ( )
124- . clone ( ) ;
125- Ok ( tables)
126144 }
127145
128146 pub async fn delete_project ( & self , project : & str ) -> Result < ( ) > {
@@ -137,4 +155,32 @@ impl ProjectsStore {
137155 } ) ;
138156 Ok ( ( ) )
139157 }
158+
159+ async fn postgresql_schema_selector ( & self , project_name : & str ) -> Result < Vec < String > > {
160+ let connection_string = self . create_project_connection_string ( project_name) ;
161+ let args = serde_wasm_bindgen:: to_value ( & InvokePostgresConnectionArgs {
162+ project_name,
163+ key : & connection_string,
164+ } )
165+ . unwrap ( ) ;
166+ let schemas = invoke ( & Invoke :: postgresql_connector. to_string ( ) , args) . await ;
167+ let mut schemas = serde_wasm_bindgen:: from_value :: < Vec < String > > ( schemas) . unwrap ( ) ;
168+ schemas. sort ( ) ;
169+ Ok ( schemas)
170+ }
171+
172+ async fn postgresql_table_selector (
173+ & self ,
174+ project_name : & str ,
175+ schema : & str ,
176+ ) -> Result < Vec < ( String , String ) > > {
177+ let args = serde_wasm_bindgen:: to_value ( & InvokeSchemaTablesArgs {
178+ project_name,
179+ schema,
180+ } )
181+ . unwrap ( ) ;
182+ let tables = invoke ( & Invoke :: select_schema_tables. to_string ( ) , args) . await ;
183+ let tables = serde_wasm_bindgen:: from_value :: < Vec < ( String , String ) > > ( tables) . unwrap ( ) ;
184+ Ok ( tables)
185+ }
140186}
0 commit comments