• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI_DB_postgre_driver->db_connect (and db_pconnect): new connection string logic

#1
[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. =====-----

#2
[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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.