[bug #270] PostgreSQL backend try to connect throught TCP socket for

local database
This commit is contained in:
Regis Houssin 2012-01-06 09:31:02 +01:00
parent c598fb34c0
commit 33049e9f19

View File

@ -1,10 +1,11 @@
<?php
/* Copyright (C) 2001 Fabien Seisen <seisen@linuxfr.org>
* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
* Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
* Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
/* Copyright (C) 2001 Fabien Seisen <seisen@linuxfr.org>
* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
* Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
* Copyright (C) 2005-2012 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2012 Yann Droneaud <yann@droneaud.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -351,22 +352,42 @@ class DoliDBPgsql
*/
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;
}