From 831c7ee1b5102d13f1674ddf2bd71abd3762439e Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Fri, 6 Jan 2012 09:37:12 +0100 Subject: [PATCH 1/4] [bug #270] PostgreSQL backend try to connect throught TCP socket for local database --- htdocs/lib/databases/pgsql.lib.php | 40 ++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/htdocs/lib/databases/pgsql.lib.php b/htdocs/lib/databases/pgsql.lib.php index 173b0862f66..feec3454469 100644 --- a/htdocs/lib/databases/pgsql.lib.php +++ b/htdocs/lib/databases/pgsql.lib.php @@ -345,22 +345,42 @@ class DoliDb */ function connect($host, $login, $passwd, $name, $port=0) { - if (!$name){ - $name="postgres"; + // use pg_connect() instead of pg_pconnect(): + // To us persistent connection because this one cost 1ms, non persistent cost 30ms + + $this->db = false; + + // connections parameters must be protected (only \ and ' according to pg_connect() manual) + $host = str_replace(array("\\", "'"), array("\\\\", "\\'"), $host); + $login = str_replace(array("\\", "'"), array("\\\\", "\\'"), $login); + $passwd = str_replace(array("\\", "'"), array("\\\\", "\\'"), $passwd); + $name = str_replace(array("\\", "'"), array("\\\\", "\\'"), $name); + $port = str_replace(array("\\", "'"), array("\\\\", "\\'"), $port); + + if (! $name) $name="postgres"; + + // try first Unix domain socket (local) + if (! $host || $host == "" || $host == "localhost") + { + $con_string = "dbname='".$name."' user='".$login."' password='".$passwd."'"; + $this->db = pg_connect($con_string); } - if (!$port){ - $port=5432; + + // if local connection failed or not requested, use TCP/IP + if (! $this->db) + { + if (! $host) $host = "localhost"; + if (! $port) $port = 5432; + + $con_string = "host='".$host."' port='".$port."' dbname='".$name."' user='".$login."' password='".$passwd."'"; + $this->db = pg_connect($con_string); } - $con_string = "host=$host port=$port dbname=$name user=$login password=$passwd"; - //print 'xxx'.$con_string; - //$this->db = pg_pconnect($con_string); // To us persistent connection because this one cost 1ms, non ersisten cost 30ms - $this->db = pg_connect($con_string); - if ($this->db) + else { $this->database_name = $name; pg_set_error_verbosity($this->db, PGSQL_ERRORS_VERBOSE); // Set verbosity to max - } + return $this->db; } From 602e64282c8de8473495aefcac4fb3913fbdd215 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Fri, 6 Jan 2012 10:43:08 +0100 Subject: [PATCH 2/4] Fix: remove the default pgsql database name --- htdocs/lib/databases/pgsql.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/lib/databases/pgsql.lib.php b/htdocs/lib/databases/pgsql.lib.php index feec3454469..e808af1db5e 100644 --- a/htdocs/lib/databases/pgsql.lib.php +++ b/htdocs/lib/databases/pgsql.lib.php @@ -357,7 +357,7 @@ class DoliDb $name = str_replace(array("\\", "'"), array("\\\\", "\\'"), $name); $port = str_replace(array("\\", "'"), array("\\\\", "\\'"), $port); - if (! $name) $name="postgres"; + //if (! $name) $name="postgres"; // try first Unix domain socket (local) if (! $host || $host == "" || $host == "localhost") From c5a2d8baa9611c303bc31aaab4be7f1e47e3fef1 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Sat, 7 Jan 2012 01:12:17 +0800 Subject: [PATCH 3/4] Fix: add entity filter --- htdocs/categories/class/categorie.class.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index ec6c4b2fe73..d3ae422d22d 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -615,6 +615,8 @@ class Categorie */ function get_full_arbo($type,$markafterid=0) { + global $conf; + $this->cats = array(); // Charge tableau des meres @@ -643,6 +645,7 @@ class Categorie $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_association as ca"; $sql.= " ON c.rowid=ca.fk_categorie_mere"; $sql.= " WHERE c.type = ".$type; + $sql.= " AND c.entity = ".$conf->entity; $sql.= " ORDER BY c.label, c.rowid"; dol_syslog("Categorie::get_full_arbo get category list sql=".$sql, LOG_DEBUG); @@ -782,7 +785,10 @@ class Categorie */ function get_all_categories () { + global $conf; + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie"; + $sql.= " WHERE entity = ".$conf->entity; $res = $this->db->query($sql); if ($res) @@ -808,8 +814,12 @@ class Categorie */ function get_nb_categories () { + global $conf; + $sql = "SELECT count(rowid)"; $sql.= " FROM ".MAIN_DB_PREFIX."categorie"; + $sql.= " WHERE entity = ".$conf->entity; + $res = $this->db->query($sql); if ($res) { From 59a6e283154b5e83eb6445173287400f0f5abc50 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Sat, 7 Jan 2012 08:36:13 +0100 Subject: [PATCH 4/4] Fix: add database prefix filter --- htdocs/install/upgrade.php | 84 ++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/htdocs/install/upgrade.php b/htdocs/install/upgrade.php index 58e0bf1bc0f..271c5d8125b 100644 --- a/htdocs/install/upgrade.php +++ b/htdocs/install/upgrade.php @@ -238,45 +238,51 @@ if (! GETPOST("action") || preg_match('/upgrade/i',GETPOST('action'))) && versioncompare($versioncommande,$versionarray) <= 0) // Si mysql >= 4.0 { // Suppression vieilles contraintes sans noms et en doubles - // Les contraintes indesirables ont un nom qui commence par 0_ ou se termine par ibfk_999 - $listtables=array( 'llx_adherent_options', - 'llx_bank_class', - 'llx_c_ecotaxe', - 'llx_c_methode_commande_fournisseur', // table renamed - 'llx_c_input_method'); - - $listtables = $db->DDLListTables($conf->db->name,''); - foreach ($listtables as $val) - { - //print "x".$val."
"; - $sql = "SHOW CREATE TABLE ".$val; - $resql = $db->query($sql); - if ($resql) - { - $values=$db->fetch_array($resql); - $i=0; - $createsql=$values[1]; - while (preg_match('/CONSTRAINT `(0_[0-9a-zA-Z]+|[_0-9a-zA-Z]+_ibfk_[0-9]+)`/i',$createsql,$reg) && $i < 100) - { - $sqldrop="ALTER TABLE ".$val." DROP FOREIGN KEY ".$reg[1]; - $resqldrop = $db->query($sqldrop); - if ($resqldrop) - { - print ''.$sqldrop.";\n"; - } - $createsql=preg_replace('/CONSTRAINT `'.$reg[1].'`/i','XXX',$createsql); - $i++; - } - $db->free($resql); - } - else - { - if ($db->lasterrno() != 'DB_ERROR_NOSUCHTABLE') - { - print ''.$sql.' : '.$db->lasterror()."\n"; - } - } - } + // Les contraintes indesirables ont un nom qui commence par 0_ ou se termine par ibfk_999 + $listtables=array( + MAIN_DB_PREFIX.'adherent_options', + MAIN_DB_PREFIX.'bank_class', + MAIN_DB_PREFIX.'c_ecotaxe', + MAIN_DB_PREFIX.'c_methode_commande_fournisseur', // table renamed + MAIN_DB_PREFIX.'c_input_method' + ); + + $listtables = $db->DDLListTables($conf->db->name,''); + foreach ($listtables as $val) + { + // Database prefix filter + if (preg_match('/^'.MAIN_DB_PREFIX.'/', $val)) + { + //print "x".$val."
"; + $sql = "SHOW CREATE TABLE ".$val; + $resql = $db->query($sql); + if ($resql) + { + $values=$db->fetch_array($resql); + $i=0; + $createsql=$values[1]; + while (preg_match('/CONSTRAINT `(0_[0-9a-zA-Z]+|[_0-9a-zA-Z]+_ibfk_[0-9]+)`/i',$createsql,$reg) && $i < 100) + { + $sqldrop="ALTER TABLE ".$val." DROP FOREIGN KEY ".$reg[1]; + $resqldrop = $db->query($sqldrop); + if ($resqldrop) + { + print ''.$sqldrop.";\n"; + } + $createsql=preg_replace('/CONSTRAINT `'.$reg[1].'`/i','XXX',$createsql); + $i++; + } + $db->free($resql); + } + else + { + if ($db->lasterrno() != 'DB_ERROR_NOSUCHTABLE') + { + print ''.$sql.' : '.$db->lasterror()."\n"; + } + } + } + } } }