11use deadpool_postgres:: { Manager , ManagerConfig , Pool , RecyclingMethod } ;
22use pyo3:: { pyclass, pymethods, PyAny , Python } ;
3- use std:: { sync:: Arc , vec} ;
3+ use std:: { str :: FromStr , sync:: Arc , vec} ;
44use tokio_postgres:: { types:: ToSql , NoTls } ;
55
66use crate :: {
@@ -16,6 +16,7 @@ use super::connection::Connection;
1616///
1717/// It is not exposed to python.
1818pub struct RustPSQLPool {
19+ dsn : Option < String > ,
1920 username : Option < String > ,
2021 password : Option < String > ,
2122 host : Option < String > ,
@@ -29,6 +30,7 @@ impl RustPSQLPool {
2930 /// Create new `RustPSQLPool`.
3031 #[ must_use]
3132 pub fn new (
33+ dsn : Option < String > ,
3234 username : Option < String > ,
3335 password : Option < String > ,
3436 host : Option < String > ,
@@ -37,6 +39,7 @@ impl RustPSQLPool {
3739 max_db_pool_size : Option < usize > ,
3840 ) -> Self {
3941 RustPSQLPool {
42+ dsn,
4043 username,
4144 password,
4245 host,
@@ -115,6 +118,7 @@ impl RustPSQLPool {
115118 /// `max_db_pool_size` is less than 2 or it's impossible to build db pool.
116119 pub async fn inner_startup ( & self ) -> RustPSQLDriverPyResult < ( ) > {
117120 let db_pool_arc = self . db_pool . clone ( ) ;
121+ let dsn = self . dsn . clone ( ) ;
118122 let password = self . password . clone ( ) ;
119123 let username = self . username . clone ( ) ;
120124 let db_host = self . host . clone ( ) ;
@@ -137,22 +141,26 @@ impl RustPSQLPool {
137141 }
138142 }
139143
140- let mut pg_config = tokio_postgres:: Config :: new ( ) ;
141-
142- if let ( Some ( password) , Some ( username) ) = ( password, username) {
143- pg_config. password ( & password) ;
144- pg_config. user ( & username) ;
145- }
146- if let Some ( db_host) = db_host {
147- pg_config. host ( & db_host) ;
148- }
144+ let mut pg_config: tokio_postgres:: Config ;
145+ if let Some ( dsn_string) = dsn {
146+ pg_config = tokio_postgres:: Config :: from_str ( & dsn_string) ?;
147+ } else {
148+ pg_config = tokio_postgres:: Config :: new ( ) ;
149+ if let ( Some ( password) , Some ( username) ) = ( password, username) {
150+ pg_config. password ( & password) ;
151+ pg_config. user ( & username) ;
152+ }
153+ if let Some ( db_host) = db_host {
154+ pg_config. host ( & db_host) ;
155+ }
149156
150- if let Some ( db_port) = db_port {
151- pg_config. port ( db_port) ;
152- }
157+ if let Some ( db_port) = db_port {
158+ pg_config. port ( db_port) ;
159+ }
153160
154- if let Some ( db_name) = db_name {
155- pg_config. dbname ( & db_name) ;
161+ if let Some ( db_name) = db_name {
162+ pg_config. dbname ( & db_name) ;
163+ }
156164 }
157165
158166 let mgr_config = ManagerConfig {
@@ -180,6 +188,7 @@ impl PSQLPool {
180188 #[ new]
181189 #[ must_use]
182190 pub fn new (
191+ dsn : Option < String > ,
183192 username : Option < String > ,
184193 password : Option < String > ,
185194 host : Option < String > ,
@@ -189,6 +198,7 @@ impl PSQLPool {
189198 ) -> Self {
190199 PSQLPool {
191200 rust_psql_pool : Arc :: new ( tokio:: sync:: RwLock :: new ( RustPSQLPool {
201+ dsn,
192202 username,
193203 password,
194204 host,
0 commit comments