1111
1212from . import pq
1313from ._tz import get_tzinfo
14- from .conninfo import make_conninfo
1514
1615
1716class ConnectionInfo :
@@ -72,26 +71,74 @@ def get_parameters(self) -> dict[str, str]:
7271 either from the connection string and parameters passed to
7372 `~Connection.connect()` or from environment variables. The password
7473 is never returned (you can read it using the `password` attribute).
74+
75+ Note:
76+ GaussDB does not support PGconn.info attribute, uses fallback method.
7577 """
7678 pyenc = self .encoding
7779
78- # Get the known defaults to avoid reporting them
79- defaults = {
80- i .keyword : i .compiled
81- for i in pq .Conninfo .get_defaults ()
82- if i .compiled is not None
83- }
84- # Not returned by the libq. Bug? Bet we're using SSH.
85- defaults .setdefault (b"channel_binding" , b"prefer" )
86- defaults [b"passfile" ] = str (Path .home () / ".pgpass" ).encode ()
87-
88- return {
89- i .keyword .decode (pyenc ): i .val .decode (pyenc )
90- for i in self .pgconn .info
91- if i .val is not None
92- and i .keyword != b"password"
93- and i .val != defaults .get (i .keyword )
94- }
80+ # Check if info attribute is supported (GaussDB does not support)
81+ try :
82+ info = self .pgconn .info
83+ if info is None :
84+ return self ._get_parameters_fallback ()
85+ except (AttributeError , NotImplementedError ):
86+ return self ._get_parameters_fallback ()
87+
88+ # PostgreSQL normal path
89+ try :
90+ # Get the known defaults to avoid reporting them
91+ defaults = {
92+ i .keyword : i .compiled
93+ for i in pq .Conninfo .get_defaults ()
94+ if i .compiled is not None
95+ }
96+ # Not returned by the libq. Bug? Bet we're using SSH.
97+ defaults .setdefault (b"channel_binding" , b"prefer" )
98+ defaults [b"passfile" ] = str (Path .home () / ".pgpass" ).encode ()
99+
100+ return {
101+ i .keyword .decode (pyenc ): i .val .decode (pyenc )
102+ for i in info
103+ if i .val is not None
104+ and i .keyword != b"password"
105+ and i .val != defaults .get (i .keyword )
106+ }
107+ except Exception :
108+ # Use fallback on error
109+ return self ._get_parameters_fallback ()
110+
111+ def _get_parameters_fallback (self ) -> dict [str , str ]:
112+ """Fallback method for getting connection parameters.
113+
114+ When PGconn.info is not available (e.g., GaussDB),
115+ retrieve basic connection information from other sources.
116+ """
117+ params = {}
118+
119+ # Get available information from pgconn attributes
120+ if self .pgconn .host :
121+ params ["host" ] = self .pgconn .host .decode (self .encoding , errors = "replace" )
122+
123+ if self .pgconn .port :
124+ params ["port" ] = self .pgconn .port .decode (self .encoding , errors = "replace" )
125+
126+ if self .pgconn .db :
127+ params ["dbname" ] = self .pgconn .db .decode (self .encoding , errors = "replace" )
128+
129+ if self .pgconn .user :
130+ params ["user" ] = self .pgconn .user .decode (self .encoding , errors = "replace" )
131+
132+ # Get other available parameters
133+ try :
134+ if hasattr (self .pgconn , "options" ) and self .pgconn .options :
135+ params ["options" ] = self .pgconn .options .decode (
136+ self .encoding , errors = "replace"
137+ )
138+ except Exception :
139+ pass
140+
141+ return params
95142
96143 @property
97144 def dsn (self ) -> str :
@@ -103,7 +150,25 @@ def dsn(self) -> str:
103150 password is never returned (you can read it using the `password`
104151 attribute).
105152 """
106- return make_conninfo (** self .get_parameters ())
153+ try :
154+ params = self .get_parameters ()
155+ except Exception :
156+ params = self ._get_parameters_fallback ()
157+
158+ if not params :
159+ return ""
160+
161+ # Build DSN string
162+ parts = []
163+ for key , value in params .items ():
164+ if key == "password" :
165+ continue # Do not include password
166+ # Escape values
167+ if " " in value or "=" in value or "'" in value :
168+ value = "'" + value .replace ("'" , "\\ '" ) + "'"
169+ parts .append (f"{ key } ={ value } " )
170+
171+ return " " .join (parts )
107172
108173 @property
109174 def status (self ) -> pq .ConnStatus :
0 commit comments