CodeIgniter Forums
CI_DB_postgre_driver->db_connect (and db_pconnect): new connection string logic - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: CI_DB_postgre_driver->db_connect (and db_pconnect): new connection string logic (/showthread.php?tid=9581)



CI_DB_postgre_driver->db_connect (and db_pconnect): new connection string logic - El Forum - 06-30-2008

[eluser]dathan[/eluser]
The connection logic in db_connect and db_pconnect doesn't give a lot of flexibility in terms of creating the connection string. So I modified it to only use whichever DB connection parameters the user actually supplied to create the connection string (motivated by my server config, in which I just have to give the dbname parameter and the apache account is authenticated by the system with no additional credentials). I've only tested it with database settings set in the application/config/database.php file.

Here's a unified DIFF of the database/drivers/postgre/postgre_driver.php file:

-----===== Removed for brevity. See next post for updated version. =====-----


CI_DB_postgre_driver->db_connect (and db_pconnect): new connection string logic - El Forum - 07-01-2008

[eluser]dathan[/eluser]
Here's a fix to the the previous patch. Apparently when I posted it it had a couple syntax errors.
Code:
Index: postgre_driver.php
===================================================================
--- postgre_driver.php    (revision 1245)
+++ postgre_driver.php    (working copy)
@@ -46,9 +46,18 @@
      */    
     function db_connect()
     {
-        $port = ($this->port == '') ? '' : " port=".$this->port;
-        
-        return @pg_connect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
+        $params = array();
+        if ($this->port !== '')
+            $params[] = 'port='.$this->port;
+        if ($this->hostname !== '')
+            $params[] = 'host='.$this->hostname;
+        if ($this->database !== '')
+            $params[] = 'dbname='.$this->database;
+        if ($this->username !== '')
+            $params[] = 'user='.$this->username;
+        if ($this->password !== '')
+            $params[] = 'password'.$this->password;
+        return @pg_connect(implode(" ", $params));
     }

     // --------------------------------------------------------------------
@@ -61,9 +70,18 @@
      */    
     function db_pconnect()
     {
-        $port = ($this->port == '') ? '' : " port=".$this->port;
-
-        return @pg_pconnect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
+        $params = array();
+        if ($this->port !== '')
+            $params[] = 'port='.$this->port;
+        if ($this->hostname !== '')
+            $params[] = 'host='.$this->hostname;
+        if ($this->database !== '')
+            $params[] = 'dbname='.$this->database;
+        if ($this->username !== '')
+            $params[] = 'user='.$this->username;
+        if ($this->password !== '')
+            $params[] = 'password'.$this->password;
+        return @pg_pconnect(implode(" ", $params));
     }
    
     // --------------------------------------------------------------------

Mod edit: stuck code inside [code][/code] tags for legibility