1
- use std:: { borrow :: Borrow , collections:: BTreeMap } ;
1
+ use std:: collections:: BTreeMap ;
2
2
3
- use common:: { enums:: Project , utils:: project_matcher} ;
3
+ use common:: {
4
+ enums:: { Project , ProjectConnectionStatus } ,
5
+ utils:: project_matcher,
6
+ } ;
4
7
use leptos:: {
5
8
create_rw_signal, error:: Result , RwSignal , SignalGet , SignalGetUntracked , SignalUpdate ,
6
9
} ;
@@ -27,7 +30,7 @@ impl ProjectsStore {
27
30
pub fn set_projects ( & self , projects : Vec < Project > ) -> Result < BTreeMap < String , Project > > {
28
31
let projects = projects
29
32
. into_iter ( )
30
- . map ( |project| project_matcher ( project ) )
33
+ . map ( project_matcher)
31
34
. collect :: < BTreeMap < String , Project > > ( ) ;
32
35
self . 0 . update ( |prev| {
33
36
* prev = projects;
@@ -39,7 +42,6 @@ impl ProjectsStore {
39
42
self . 0 . update ( |prev| {
40
43
let project = match project {
41
44
Project :: POSTGRESQL ( project) => ( project. name . clone ( ) , Project :: POSTGRESQL ( project) ) ,
42
- _ => unreachable ! ( ) ,
43
45
} ;
44
46
prev. insert ( project. 0 , project. 1 ) ;
45
47
} ) ;
@@ -52,77 +54,93 @@ impl ProjectsStore {
52
54
Ok ( projects)
53
55
}
54
56
55
- pub fn create_project_connection_string ( & self , project_key : & str ) -> String {
57
+ pub fn create_project_connection_string ( & self , project_name : & str ) -> String {
56
58
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 ( ) ;
58
60
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
+ }
63
70
}
64
71
65
- pub async fn connect ( & self , project : & str ) -> Result < Vec < String > > {
72
+ pub async fn connect ( & self , project_name : & str ) -> Result < Vec < String > > {
66
73
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
+ }
67
82
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)
71
94
}
72
95
}
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)
90
96
}
91
97
92
98
pub async fn retrieve_tables (
93
99
& self ,
94
- project : & str ,
100
+ project_name : & str ,
95
101
schema : & str ,
96
102
) -> Result < Vec < ( String , String ) > > {
97
103
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)
103
142
}
104
143
}
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)
126
144
}
127
145
128
146
pub async fn delete_project ( & self , project : & str ) -> Result < ( ) > {
@@ -137,4 +155,32 @@ impl ProjectsStore {
137
155
} ) ;
138
156
Ok ( ( ) )
139
157
}
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
+ }
140
186
}
0 commit comments