11use super :: error:: { ConnectionError , ConnectionResult } ;
22use super :: id:: { ConnectionId , DriverKind } ;
3+ use super :: postgres:: PostgresSession ;
34use super :: query:: QueryEvent ;
45use super :: sqlite:: SqliteSession ;
56use serde:: { Deserialize , Serialize } ;
@@ -19,19 +20,27 @@ pub enum ConnectionConfig {
1920 #[ serde( default ) ]
2021 id : Option < String > ,
2122 } ,
22- /// Stub until the Postgres adapter epic wires connect.
2323 Postgres {
2424 host : String ,
2525 port : u16 ,
2626 database : String ,
2727 user : String ,
28+ /// Session-only secret — never written to ProfileStore (keyring: #1878).
29+ #[ serde( default ) ]
30+ password : Option < String > ,
31+ #[ serde( default ) ]
32+ id : Option < String > ,
2833 } ,
2934 /// Stub until the MySQL adapter epic wires connect.
3035 Mysql {
3136 host : String ,
3237 port : u16 ,
3338 database : String ,
3439 user : String ,
40+ #[ serde( default ) ]
41+ password : Option < String > ,
42+ #[ serde( default ) ]
43+ id : Option < String > ,
3544 } ,
3645}
3746
@@ -82,7 +91,7 @@ impl ConnectionRegistry {
8291 Self :: with_builtin_drivers ( )
8392 }
8493
85- /// Registry with SQLite opener wired (PG/ MySQL return UnsupportedDriver).
94+ /// Registry with SQLite + Postgres openers ( MySQL still UnsupportedDriver).
8695 pub fn with_builtin_drivers ( ) -> Self {
8796 Self :: with_opener ( |config| match config {
8897 ConnectionConfig :: Sqlite { path, id } => {
@@ -94,6 +103,24 @@ impl ConnectionRegistry {
94103 } ?;
95104 Ok ( Arc :: new ( session) as SessionArc )
96105 }
106+ ConnectionConfig :: Postgres {
107+ host,
108+ port,
109+ database,
110+ user,
111+ password,
112+ id,
113+ } => {
114+ let session = super :: postgres:: open_postgres ( super :: postgres:: PostgresConnectParams {
115+ host,
116+ port,
117+ database,
118+ user,
119+ password,
120+ id : id. filter ( |s| !s. trim ( ) . is_empty ( ) ) . map ( ConnectionId :: from) ,
121+ } ) ?;
122+ Ok ( Arc :: new ( session) as SessionArc )
123+ }
97124 other => Err ( ConnectionError :: UnsupportedDriver {
98125 driver : other. driver_kind ( ) ,
99126 } ) ,
@@ -153,6 +180,39 @@ impl ConnectionRegistry {
153180 self . with_sqlite ( id, |s| s. execute_streaming ( sql, limit, emit) )
154181 }
155182
183+ pub fn with_postgres < T > (
184+ & self ,
185+ id : & ConnectionId ,
186+ f : impl FnOnce ( & PostgresSession ) -> ConnectionResult < T > ,
187+ ) -> ConnectionResult < T > {
188+ let session = self . session_arc ( id) ?;
189+ let pg = session
190+ . as_any ( )
191+ . downcast_ref :: < PostgresSession > ( )
192+ . ok_or ( ConnectionError :: UnsupportedDriver {
193+ driver : session. driver ( ) ,
194+ } ) ?;
195+ f ( pg)
196+ }
197+
198+ /// Driver-aware query stream (SQLite + Postgres).
199+ pub fn execute_query (
200+ & self ,
201+ id : & ConnectionId ,
202+ sql : & str ,
203+ limit : Option < usize > ,
204+ emit : impl FnMut ( QueryEvent ) -> ConnectionResult < ( ) > ,
205+ ) -> ConnectionResult < ( ) > {
206+ match self . driver_of ( id) {
207+ Some ( DriverKind :: Sqlite ) => self . execute_sqlite_query ( id, sql, limit, emit) ,
208+ Some ( DriverKind :: Postgres ) => {
209+ self . with_postgres ( id, |s| s. execute_streaming ( sql, limit, emit) )
210+ }
211+ Some ( other) => Err ( ConnectionError :: UnsupportedDriver { driver : other } ) ,
212+ None => Err ( ConnectionError :: NotFound { id : id. clone ( ) } ) ,
213+ }
214+ }
215+
156216 /// Driver-agnostic object tree for a live connection (SQLite first).
157217 pub fn object_tree (
158218 & self ,
@@ -300,24 +360,42 @@ mod tests {
300360 }
301361
302362 #[ test]
303- fn registry_postgres_is_unsupported_for_now ( ) {
363+ fn registry_mysql_is_unsupported_for_now ( ) {
304364 let reg = ConnectionRegistry :: with_builtin_drivers ( ) ;
305365 let err = reg
306- . connect ( ConnectionConfig :: Postgres {
366+ . connect ( ConnectionConfig :: Mysql {
307367 host : "localhost" . into ( ) ,
308- port : 5432 ,
368+ port : 3306 ,
309369 database : "db" . into ( ) ,
310370 user : "u" . into ( ) ,
371+ password : None ,
372+ id : None ,
311373 } )
312374 . unwrap_err ( ) ;
313375 assert ! ( matches!(
314376 err,
315377 ConnectionError :: UnsupportedDriver {
316- driver: DriverKind :: Postgres
378+ driver: DriverKind :: Mysql
317379 }
318380 ) ) ;
319381 }
320382
383+ #[ test]
384+ fn registry_postgres_rejects_empty_host ( ) {
385+ let reg = ConnectionRegistry :: with_builtin_drivers ( ) ;
386+ let err = reg
387+ . connect ( ConnectionConfig :: Postgres {
388+ host : " " . into ( ) ,
389+ port : 5432 ,
390+ database : "querya" . into ( ) ,
391+ user : "querya" . into ( ) ,
392+ password : Some ( "querya" . into ( ) ) ,
393+ id : None ,
394+ } )
395+ . unwrap_err ( ) ;
396+ assert ! ( matches!( err, ConnectionError :: InvalidConfig { .. } ) ) ;
397+ }
398+
321399 #[ test]
322400 fn execute_sqlite_query_streams_via_session ( ) {
323401 let path = std:: env:: temp_dir ( ) . join ( format ! (
0 commit comments