@@ -64,14 +64,11 @@ public function connect(bool $persistent = false)
6464 $ this ->buildDSN ();
6565 }
6666
67- // Strip pgsql if exists
67+ // Convert DSN string
6868 if (mb_strpos ($ this ->DSN , 'pgsql: ' ) === 0 ) {
69- $ this ->DSN = mb_substr ( $ this -> DSN , 6 );
69+ $ this ->convertDSN ( );
7070 }
7171
72- // Convert semicolons to spaces.
73- $ this ->DSN = str_replace ('; ' , ' ' , $ this ->DSN );
74-
7572 $ this ->connID = $ persistent === true ? pg_pconnect ($ this ->DSN ) : pg_connect ($ this ->DSN );
7673
7774 if ($ this ->connID !== false ) {
@@ -92,6 +89,44 @@ public function connect(bool $persistent = false)
9289 return $ this ->connID ;
9390 }
9491
92+ /**
93+ * Converts the DSN with semicolon syntax.
94+ */
95+ private function convertDSN ()
96+ {
97+ // Strip pgsql
98+ $ this ->DSN = mb_substr ($ this ->DSN , 6 );
99+
100+ // Convert semicolons to spaces in DSN format like:
101+ // pgsql:host=localhost;port=5432;dbname=database_name
102+ // https://www.php.net/manual/en/function.pg-connect.php
103+ $ allowedParams = ['host ' , 'port ' , 'dbname ' , 'user ' , 'password ' , 'connect_timeout ' , 'options ' , 'sslmode ' , 'service ' ];
104+
105+ $ parameters = explode ('; ' , $ this ->DSN );
106+
107+ $ output = '' ;
108+ $ previousParameter = '' ;
109+
110+ foreach ($ parameters as $ parameter ) {
111+ [$ key , $ value ] = explode ('= ' , $ parameter , 2 );
112+ if (in_array ($ key , $ allowedParams , true )) {
113+ if ($ previousParameter !== '' ) {
114+ if (array_search ($ key , $ allowedParams , true ) < array_search ($ previousParameter , $ allowedParams , true )) {
115+ $ output .= '; ' ;
116+ } else {
117+ $ output .= ' ' ;
118+ }
119+ }
120+ $ output .= $ parameter ;
121+ $ previousParameter = $ key ;
122+ } else {
123+ $ output .= '; ' . $ parameter ;
124+ }
125+ }
126+
127+ $ this ->DSN = $ output ;
128+ }
129+
95130 /**
96131 * Keep or establish the connection if no queries have been sent for
97132 * a length of time exceeding the server's idle timeout.
0 commit comments