@@ -2,8 +2,8 @@ use bytes::BytesMut;
22use deadpool_postgres:: Pool ;
33use futures_util:: pin_mut;
44use pyo3:: { buffer:: PyBuffer , pyclass, pymethods, Py , PyAny , PyErr , Python } ;
5- use std:: { collections:: HashSet , sync:: Arc } ;
6- use tokio_postgres:: binary_copy:: BinaryCopyInWriter ;
5+ use std:: { collections:: HashSet , net :: IpAddr , sync:: Arc } ;
6+ use tokio_postgres:: { binary_copy:: BinaryCopyInWriter , config :: Host , Config } ;
77
88use crate :: {
99 exceptions:: rust_errors:: { RustPSQLDriverError , RustPSQLDriverPyResult } ,
@@ -24,12 +24,21 @@ use super::{
2424pub struct Connection {
2525 db_client : Option < Arc < PsqlpyConnection > > ,
2626 db_pool : Option < Pool > ,
27+ pg_config : Arc < Config > ,
2728}
2829
2930impl Connection {
3031 #[ must_use]
31- pub fn new ( db_client : Option < Arc < PsqlpyConnection > > , db_pool : Option < Pool > ) -> Self {
32- Connection { db_client, db_pool }
32+ pub fn new (
33+ db_client : Option < Arc < PsqlpyConnection > > ,
34+ db_pool : Option < Pool > ,
35+ pg_config : Arc < Config > ,
36+ ) -> Self {
37+ Connection {
38+ db_client,
39+ db_pool,
40+ pg_config,
41+ }
3342 }
3443
3544 #[ must_use]
@@ -45,12 +54,70 @@ impl Connection {
4554
4655impl Default for Connection {
4756 fn default ( ) -> Self {
48- Connection :: new ( None , None )
57+ Connection :: new ( None , None , Arc :: new ( Config :: default ( ) ) )
4958 }
5059}
5160
5261#[ pymethods]
5362impl Connection {
63+ #[ getter]
64+ fn conn_dbname ( & self ) -> Option < & str > {
65+ self . pg_config . get_dbname ( )
66+ }
67+
68+ #[ getter]
69+ fn user ( & self ) -> Option < & str > {
70+ self . pg_config . get_user ( )
71+ }
72+
73+ #[ getter]
74+ fn host_addrs ( & self ) -> Vec < String > {
75+ let mut host_addrs_vec = vec ! [ ] ;
76+
77+ let host_addrs = self . pg_config . get_hostaddrs ( ) ;
78+ for ip_addr in host_addrs {
79+ match ip_addr {
80+ IpAddr :: V4 ( ipv4) => {
81+ host_addrs_vec. push ( ipv4. to_string ( ) ) ;
82+ }
83+ IpAddr :: V6 ( ipv6) => {
84+ host_addrs_vec. push ( ipv6. to_string ( ) ) ;
85+ }
86+ }
87+ }
88+
89+ host_addrs_vec
90+ }
91+
92+ #[ getter]
93+ fn hosts ( & self ) -> Vec < String > {
94+ let mut hosts_vec = vec ! [ ] ;
95+
96+ let hosts = self . pg_config . get_hosts ( ) ;
97+ for host in hosts {
98+ match host {
99+ Host :: Tcp ( host) => {
100+ hosts_vec. push ( host. to_string ( ) ) ;
101+ }
102+ Host :: Unix ( host) => {
103+ hosts_vec. push ( host. display ( ) . to_string ( ) ) ;
104+ }
105+ }
106+ }
107+
108+ hosts_vec
109+ }
110+
111+ #[ getter]
112+ fn ports ( & self ) -> Vec < & u16 > {
113+ return self . pg_config . get_ports ( ) . iter ( ) . collect :: < Vec < & u16 > > ( ) ;
114+ }
115+
116+ #[ getter]
117+ fn options ( & self ) -> Option < & str > {
118+ return self . pg_config . get_options ( ) ;
119+ }
120+
54121 async fn __aenter__ < ' a > ( self_ : Py < Self > ) -> RustPSQLDriverPyResult < Py < Self > > {
55122 let ( db_client, db_pool) = pyo3:: Python :: with_gil ( |gil| {
56123 let self_ = self_. borrow ( gil) ;
@@ -283,6 +350,7 @@ impl Connection {
283350 if let Some ( db_client) = & self . db_client {
284351 return Ok ( Transaction :: new (
285352 db_client. clone ( ) ,
353+ self . pg_config . clone ( ) ,
286354 false ,
287355 false ,
288356 isolation_level,
@@ -318,6 +386,7 @@ impl Connection {
318386 if let Some ( db_client) = & self . db_client {
319387 return Ok ( Cursor :: new (
320388 db_client. clone ( ) ,
389+ self . pg_config . clone ( ) ,
321390 querystring,
322391 parameters,
323392 "cur_name" . into ( ) ,
0 commit comments