Change to support more databases

This commit is contained in:
Laurent Destailleur 2009-10-22 17:15:09 +00:00
parent 154da1aee0
commit 839e0f8ba0
5 changed files with 649 additions and 647 deletions

View File

@ -255,7 +255,7 @@ if (! $error && $db->connected)
{ {
// We keep only utf8 and iso // We keep only utf8 and iso
$linedisabled=false; $linedisabled=false;
if (! eregi('(utf8|latin1)',$characterSet['charset'])) $linedisabled=true; if (($_POST["db_type"] == 'mysql' ||$_POST["db_type"] == 'mysqli') && ! eregi('(utf8|latin1)',$characterSet['charset'])) $linedisabled=true;
if ($defaultCharacterSet == $characterSet['charset'] ) if ($defaultCharacterSet == $characterSet['charset'] )
{ {
@ -298,7 +298,7 @@ if (! $error && $db->connected)
{ {
// We keep only utf8 and iso // We keep only utf8 and iso
$linedisabled=false; $linedisabled=false;
if (! eregi('(utf8_general|latin1_swedish)',$collation['collation'])) $linedisabled=true; if (($_POST["db_type"] == 'mysql' ||$_POST["db_type"] == 'mysqli') && ! eregi('(utf8_general|latin1_swedish)',$collation['collation'])) $linedisabled=true;
if ($defaultCollationConnection == $collation['collation']) if ($defaultCollationConnection == $collation['collation'])
{ {

View File

@ -321,7 +321,7 @@ if ($_POST["action"] == "set")
if ($db->connected) if ($db->connected)
{ {
if ($db->DDLCreateDb($dolibarr_main_db_name, $dolibarr_main_db_character_set, $dolibarr_main_db_collation)) if ($db->DDLCreateDb($dolibarr_main_db_name, $dolibarr_main_db_character_set, $dolibarr_main_db_collation, $dolibarr_main_db_user))
{ {
print '<tr><td>'; print '<tr><td>';
print $langs->trans("DatabaseCreation").' : '; print $langs->trans("DatabaseCreation").' : ';
@ -378,7 +378,7 @@ if ($_POST["action"] == "set")
/* /*
* On test maintenant l'acc<EFBFBD>s par le user admin dolibarr * On test maintenant l'acces par le user admin dolibarr
*/ */
if (! $error) if (! $error)
{ {

View File

@ -749,11 +749,12 @@ class DoliDb
* \param database Database name to create * \param database Database name to create
* \param charset Charset used to store data * \param charset Charset used to store data
* \param collation Charset used to sort data * \param collation Charset used to sort data
* \param owner Username of database owner
* \return resource resource defined if OK, null if KO * \return resource resource defined if OK, null if KO
* \remarks Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated * \remarks Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated
* We force to create database with charset this->forcecharset and collate this->forcecollate * We force to create database with charset this->forcecharset and collate this->forcecollate
*/ */
function DDLCreateDb($database,$charset='',$collation='') function DDLCreateDb($database,$charset='',$collation='',$owner='')
{ {
if (empty($charset)) $charset=$this->forcecharset; if (empty($charset)) $charset=$this->forcecharset;
if (empty($collation)) $collation=$this->collation; if (empty($collation)) $collation=$this->collation;
@ -963,12 +964,12 @@ class DoliDb
/** /**
* \brief Create a user * \brief Create a user and privileges to connect to database (even if database does not exists yet)
* \param dolibarr_main_db_host Ip serveur * \param dolibarr_main_db_host Ip server
* \param dolibarr_main_db_user Nom user a creer * \param dolibarr_main_db_user Username to create
* \param dolibarr_main_db_pass Mot de passe user a creer * \param dolibarr_main_db_pass Password
* \param dolibarr_main_db_name Database name where user must be granted * \param dolibarr_main_db_name Database name where user must be granted
* \return int <0 si KO, >=0 si OK * \return int <0 if KO, >=0 if OK
*/ */
function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name) function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name)
{ {

View File

@ -761,11 +761,12 @@ class DoliDb
* \param database Database name to create * \param database Database name to create
* \param charset Charset used to store data * \param charset Charset used to store data
* \param collation Charset used to sort data * \param collation Charset used to sort data
* \param owner Username of database owner
* \return resource resource defined if OK, null if KO * \return resource resource defined if OK, null if KO
* \remarks Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated * \remarks Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated
* We force to create database with charset this->forcecharset and collate this->forcecollate * We force to create database with charset this->forcecharset and collate this->forcecollate
*/ */
function DDLCreateDb($database,$charset='',$collation='') function DDLCreateDb($database,$charset='',$collation='',$owner='')
{ {
if (empty($charset)) $charset=$this->forcecharset; if (empty($charset)) $charset=$this->forcecharset;
if (empty($collation)) $collation=$this->collation; if (empty($collation)) $collation=$this->collation;
@ -976,12 +977,12 @@ class DoliDb
/** /**
* \brief Create a user * \brief Create a user and privileges to connect to database (even if database does not exists yet)
* \param dolibarr_main_db_host Ip serveur * \param dolibarr_main_db_host Ip server
* \param dolibarr_main_db_user Nom user a creer * \param dolibarr_main_db_user Username to create
* \param dolibarr_main_db_pass Mot de passe user a creer * \param dolibarr_main_db_pass Password
* \param dolibarr_main_db_name Database name where user must be granted * \param dolibarr_main_db_name Database name where user must be granted
* \return int <0 si KO, >=0 si OK * \return int <0 if KO, >=0 if OK
*/ */
function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name) function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name)
{ {

View File

@ -100,8 +100,8 @@ class DoliDb
} }
// Essai connexion serveur // Essai connexion serveur
//print "$host, $user, $pass, $name, $port";
$this->db = $this->connect($host, $user, $pass, $name, $port); $this->db = $this->connect($host, $user, $pass, $name, $port);
if ($this->db) if ($this->db)
{ {
$this->connected = 1; $this->connected = 1;
@ -831,15 +831,16 @@ class DoliDb
* \param database Database name to create * \param database Database name to create
* \param charset Charset used to store data * \param charset Charset used to store data
* \param collation Charset used to sort data * \param collation Charset used to sort data
* \param owner Username of database owner
* \return resource resource defined if OK, null if KO * \return resource resource defined if OK, null if KO
* \remarks Ne pas utiliser les fonctions xxx_create_db (xxx=mysql, ...) car elles sont deprecated * \remarks Ne pas utiliser les fonctions xxx_create_db (xxx=mysql, ...) car elles sont deprecated
*/ */
function DDLCreateDb($database,$charset='',$collation='') function DDLCreateDb($database,$charset='',$collation='',$owner='')
{ {
if (empty($charset)) $charset=$this->forcecharset; if (empty($charset)) $charset=$this->forcecharset;
if (empty($collation)) $collation=$this->collation; if (empty($collation)) $collation=$this->collation;
$ret=$this->query('CREATE DATABASE '.$database.' OWNER '.$this->db_user.' ENCODING \''.$charset.'\' ;'); $ret=$this->query('CREATE DATABASE '.$database.' OWNER '.$owner.' ENCODING \''.$charset.'\'');
return $ret; return $ret;
} }
@ -850,7 +851,7 @@ class DoliDb
*/ */
function DDLListTables($database) function DDLListTables($database)
{ {
$this->results = pg_query($this->db, "SHOW TABLES;"); $this->results = pg_query($this->db, "SHOW TABLES");
return $this->results; return $this->results;
} }
@ -958,7 +959,8 @@ class DoliDb
* \brief Return charset used to store data in database * \brief Return charset used to store data in database
* \return string Charset * \return string Charset
*/ */
function getDefaultCharacterSetDatabase(){ function getDefaultCharacterSetDatabase()
{
$resql=$this->query('SHOW SERVER_ENCODING'); $resql=$this->query('SHOW SERVER_ENCODING');
$liste=$this->fetch_array($resql); $liste=$this->fetch_array($resql);
return $liste['server_encoding']; return $liste['server_encoding'];
@ -968,16 +970,17 @@ class DoliDb
* \brief Return list of available charset that can be used to store data in database * \brief Return list of available charset that can be used to store data in database
* \return array List of Charset * \return array List of Charset
*/ */
function getListOfCharacterSet(){ function getListOfCharacterSet()
$resql=$this->query('SHOW CHARSET'); {
$resql=$this->query('SHOW SERVER_ENCODING');
$liste = array(); $liste = array();
if ($resql) if ($resql)
{ {
$i = 0; $i = 0;
while ($obj = $this->fetch_object($resql) ) while ($obj = $this->fetch_object($resql) )
{ {
$liste[$i]['charset'] = $obj->Charset; $liste[$i]['charset'] = $obj->server_encoding;
$liste[$i]['description'] = $obj->Description; $liste[$i]['description'] = 'Default database charset';
$i++; $i++;
} }
$this->free($resql); $this->free($resql);
@ -992,30 +995,27 @@ class DoliDb
* \brief Return collation used in database * \brief Return collation used in database
* \return string Collation value * \return string Collation value
*/ */
function getDefaultCollationDatabase(){ function getDefaultCollationDatabase()
$resql=$this->query('SHOW VARIABLES LIKE \'collation_database\'');
if (!$resql)
{ {
// version Mysql < 4.1.1 $resql=$this->query('SHOW SERVER_ENCODING');
return $this->forcecollate;
}
$liste=$this->fetch_array($resql); $liste=$this->fetch_array($resql);
return $liste['Value']; return $liste['lc_collate'];
} }
/** /**
* \brief Return list of available collation that can be used for database * \brief Return list of available collation that can be used for database
* \return array Liste of Collation * \return array Liste of Collation
*/ */
function getListOfCollation(){ function getListOfCollation()
$resql=$this->query('SHOW COLLATION'); {
$resql=$this->query('SHOW LC_COLLATE');
$liste = array(); $liste = array();
if ($resql) if ($resql)
{ {
$i = 0; $i = 0;
while ($obj = $this->fetch_object($resql) ) while ($obj = $this->fetch_object($resql) )
{ {
$liste[$i]['collation'] = $obj->Collation; $liste[$i]['collation'] = $obj->lc_collate;
$i++; $i++;
} }
$this->free($resql); $this->free($resql);