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

@ -125,7 +125,7 @@ if (! $error)
$databasefortest=$_POST["db_name"]; $databasefortest=$_POST["db_name"];
if (! empty($_POST["db_create_database"])) if (! empty($_POST["db_create_database"]))
{ {
if ($_POST["db_type"] == 'mysql' ||$_POST["db_type"] == 'mysqli') if ($_POST["db_type"] == 'mysql' || $_POST["db_type"] == 'mysqli')
{ {
$databasefortest='mysql'; $databasefortest='mysql';
} }
@ -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

@ -25,7 +25,7 @@
\file htdocs/lib/databases/pgsql.lib.php \file htdocs/lib/databases/pgsql.lib.php
\brief Fichier de la classe permettant de gerer une base pgsql \brief Fichier de la classe permettant de gerer une base pgsql
\version $Id$ \version $Id$
*/ */
// For compatibility during upgrade // For compatibility during upgrade
if (! defined('DOL_DOCUMENT_ROOT')) define('DOL_DOCUMENT_ROOT', '../..'); if (! defined('DOL_DOCUMENT_ROOT')) define('DOL_DOCUMENT_ROOT', '../..');
if (! defined('ADODB_DATE_VERSION')) include_once(DOL_DOCUMENT_ROOT."/includes/adodbtime/adodb-time.inc.php"); if (! defined('ADODB_DATE_VERSION')) include_once(DOL_DOCUMENT_ROOT."/includes/adodbtime/adodb-time.inc.php");
@ -34,7 +34,7 @@ if (! defined('ADODB_DATE_VERSION')) include_once(DOL_DOCUMENT_ROOT."/includes/a
/** /**
\class DoliDb \class DoliDb
\brief Classe permettant de gerer la database de dolibarr \brief Classe permettant de gerer la database de dolibarr
*/ */
class DoliDb class DoliDb
{ {
var $db; // Handler de base var $db; // Handler de base
@ -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;
@ -168,13 +168,13 @@ class DoliDb
} }
# int, auto_increment -> serial # int, auto_increment -> serial
// } elsif (/^[\s\t]*(\w*)\s*.*int.*auto_increment/i) { // } elsif (/^[\s\t]*(\w*)\s*.*int.*auto_increment/i) {
// $seq = qq~${table}_${1}_seq~; // $seq = qq~${table}_${1}_seq~;
// s/[\s\t]*([a-zA-Z_0-9]*)\s*.*int.*auto_increment[^,]*/ $1 SERIAL PRIMARY KEY/ig; // s/[\s\t]*([a-zA-Z_0-9]*)\s*.*int.*auto_increment[^,]*/ $1 SERIAL PRIMARY KEY/ig;
// $create_sql.=$_; // $create_sql.=$_;
# int type conversion # int type conversion
/* } elsif (/(\w*)int\(\d+\)/i) { /* } elsif (/(\w*)int\(\d+\)/i) {
$size=$1; $size=$1;
$size =~ tr [A-Z] [a-z]; $size =~ tr [A-Z] [a-z];
if ($size eq "tiny" || $size eq "small") { if ($size eq "tiny" || $size eq "small") {
@ -186,7 +186,7 @@ class DoliDb
} }
s/\w*int\(\d+\)/$out/g; s/\w*int\(\d+\)/$out/g;
} }
*/ */
$line=str_replace('tinyint','smallint',$line); $line=str_replace('tinyint','smallint',$line);
# nuke unsigned # nuke unsigned
@ -206,12 +206,12 @@ class DoliDb
# char -> varchar # char -> varchar
# PostgreSQL would otherwise pad with spaces as opposed # PostgreSQL would otherwise pad with spaces as opposed
# to MySQL! Your user interface may depend on this! # to MySQL! Your user interface may depend on this!
// s/(\s+)char/${1}varchar/gi; // s/(\s+)char/${1}varchar/gi;
# nuke date representation (not supported in PostgreSQL) # nuke date representation (not supported in PostgreSQL)
// s/datetime default '[^']+'/datetime/i; // s/datetime default '[^']+'/datetime/i;
// s/date default '[^']+'/datetime/i; // s/date default '[^']+'/datetime/i;
// s/time default '[^']+'/datetime/i; // s/time default '[^']+'/datetime/i;
# change not null datetime field to null valid ones # change not null datetime field to null valid ones
# (to support remapping of "zero time" to null # (to support remapping of "zero time" to null
@ -219,46 +219,46 @@ class DoliDb
$line=preg_replace('/datetime/i','timestamp'); $line=preg_replace('/datetime/i','timestamp');
# nuke size of timestamp # nuke size of timestamp
// s/timestamp\([^)]*\)/timestamp/i; // s/timestamp\([^)]*\)/timestamp/i;
# double -> real # double -> real
// s/^double/real/i; // s/^double/real/i;
// s/(\s*)double/${1}real/i; // s/(\s*)double/${1}real/i;
# unique key(field1,field2) # unique key(field1,field2)
/* if (/unique key\s*\((\w+\s*,\s*\w+)\)/i) { /* if (/unique key\s*\((\w+\s*,\s*\w+)\)/i) {
s/unique key\s*\((\w+\s*,\s*\w+)\)/UNIQUE\($1\)/i; s/unique key\s*\((\w+\s*,\s*\w+)\)/UNIQUE\($1\)/i;
$create_sql.=$_; $create_sql.=$_;
next; next;
} }
*/ */
# unique index(field1,field2) # unique index(field1,field2)
/* if (/unique index\s*\((\w+\s*,\s*\w+)\)/i) { /* if (/unique index\s*\((\w+\s*,\s*\w+)\)/i) {
s/unique index\s*\((\w+\s*,\s*\w+)\)/UNIQUE\($1\)/i; s/unique index\s*\((\w+\s*,\s*\w+)\)/UNIQUE\($1\)/i;
$create_sql.=$_; $create_sql.=$_;
next; next;
} }
*/ */
# unique key [name] (field) # unique key [name] (field)
/* if (/unique key\s*(\w*)\s*\((\w+)\)/i) { /* if (/unique key\s*(\w*)\s*\((\w+)\)/i) {
s/unique key\s*(\w*)\s*\((\w+)\)/UNIQUE\($2\)/i; s/unique key\s*(\w*)\s*\((\w+)\)/UNIQUE\($2\)/i;
my $idxname=($1?"$1":"idx_${table}_$2"); my $idxname=($1?"$1":"idx_${table}_$2");
$create_sql.=$_; $create_sql.=$_;
$create_index .= "CREATE INDEX $idxname ON $table ($2);\n"; $create_index .= "CREATE INDEX $idxname ON $table ($2);\n";
next; next;
} }
*/ */
# unique index [name] (field) # unique index [name] (field)
/* if (/unique index\s*(\w*)\s*\((\w+)\)/i) { /* if (/unique index\s*(\w*)\s*\((\w+)\)/i) {
s/unique index\s*(\w*)\s*\((\w+)\)/UNIQUE\($2\)/i; s/unique index\s*(\w*)\s*\((\w+)\)/UNIQUE\($2\)/i;
my $idxname=($1?"$1":"idx_${table}_$2"); my $idxname=($1?"$1":"idx_${table}_$2");
$create_sql.=$_; $create_sql.=$_;
$create_index .= "CREATE INDEX $idxname ON $table ($2);\n"; $create_index .= "CREATE INDEX $idxname ON $table ($2);\n";
next; next;
} }
*/ */
# unique (field) et unique (field1, field2 ...) # unique (field) et unique (field1, field2 ...)
/* if (/unique\s*\(([\w,\s]+)\)/i) { /* if (/unique\s*\(([\w,\s]+)\)/i) {
s/unique\s*\(([\w,\s]+)\)/UNIQUE\($1\)/i; s/unique\s*\(([\w,\s]+)\)/UNIQUE\($1\)/i;
my $fieldlist="$1"; my $fieldlist="$1";
my $idxname="idx_${table}_${fieldlist}"; my $idxname="idx_${table}_${fieldlist}";
@ -267,52 +267,52 @@ class DoliDb
$create_index .= "CREATE INDEX $idxname ON $table ($fieldlist);\n"; $create_index .= "CREATE INDEX $idxname ON $table ($fieldlist);\n";
next; next;
} }
*/ */
# index(field) # index(field)
/* if (/index\s*(\w*)\s*\((\w+)\)/i) { /* if (/index\s*(\w*)\s*\((\w+)\)/i) {
my $idxname=($1?"$1":"idx_${table}_$2"); my $idxname=($1?"$1":"idx_${table}_$2");
$create_index .= "CREATE INDEX $idxname ON $table ($2);\n"; $create_index .= "CREATE INDEX $idxname ON $table ($2);\n";
next; next;
} }
*/ */
# primary key # primary key
/* if (/\bkey\b/i && !/^\s+primary key\s+/i) { /* if (/\bkey\b/i && !/^\s+primary key\s+/i) {
s/KEY(\s+)[^(]*(\s+)/$1 UNIQUE $2/i; # hack off name of the non-primary key s/KEY(\s+)[^(]*(\s+)/$1 UNIQUE $2/i; # hack off name of the non-primary key
} }
*/ */
# key(xxx) # key(xxx)
/* if (/key\s*\((\w+)\)/i) { /* if (/key\s*\((\w+)\)/i) {
my $idxname="idx_${table}_$1"; my $idxname="idx_${table}_$1";
$create_index .= "CREATE INDEX $idxname ON $table ($1);\n"; $create_index .= "CREATE INDEX $idxname ON $table ($1);\n";
next; next;
} }
*/ */
# Quote column names # Quote column names
/* s/(^\s*)([^\s\-\(]+)(\s*)/$1"$2"$3/gi if (!/\bkey\b/i); /* s/(^\s*)([^\s\-\(]+)(\s*)/$1"$2"$3/gi if (!/\bkey\b/i);
*/ */
# Remap colums with names of existing system attribute # Remap colums with names of existing system attribute
/* if (/"oid"/i) { /* if (/"oid"/i) {
s/"oid"/"_oid"/g; s/"oid"/"_oid"/g;
print STDERR "WARNING: table $table uses column \"oid\" which is renamed to \"_oid\"\nYou should fix application manually! Press return to continue."; print STDERR "WARNING: table $table uses column \"oid\" which is renamed to \"_oid\"\nYou should fix application manually! Press return to continue.";
my $wait=<STDIN>; my $wait=<STDIN>;
} }
s/oid/_oid/i if (/key/i && /oid/i); # fix oid in key s/oid/_oid/i if (/key/i && /oid/i); # fix oid in key
$create_sql.=$_; $create_sql.=$_;
*/ */
} # END of if ($create_sql ne "") i.e. were inside create table statement so processed datatypes } # END of if ($create_sql ne "") i.e. were inside create table statement so processed datatypes
else { # not inside create table else { # not inside create table
#---- fix data in inserted data: (from MS world) #---- fix data in inserted data: (from MS world)
# FIX: disabled for now # FIX: disabled for now
/* if (00 && /insert into/i) { /* if (00 && /insert into/i) {
s!\x96!-!g; # -- s!\x96!-!g; # --
s!\x93!"!g; # `` s!\x93!"!g; # ``
s!\x94!"!g; # '' s!\x94!"!g; # ''
s!\x85!... !g; # \ldots s!\x85!... !g; # \ldots
s!\x92!`!g; s!\x92!`!g;
} }
*/ */
# fix dates '0000-00-00 00:00:00' (should be null) # fix dates '0000-00-00 00:00:00' (should be null)
/* s/'0000-00-00 00:00:00'/null/gi; /* s/'0000-00-00 00:00:00'/null/gi;
s/'0000-00-00'/null/gi; s/'0000-00-00'/null/gi;
s/'00:00:00'/null/gi; s/'00:00:00'/null/gi;
s/([12]\d\d\d)([01]\d)([0-3]\d)([0-2]\d)([0-6]\d)([0-6]\d)/'$1-$2-$3 $4:$5:$6'/; s/([12]\d\d\d)([01]\d)([0-3]\d)([0-2]\d)([0-6]\d)([0-6]\d)/'$1-$2-$3 $4:$5:$6'/;
@ -324,7 +324,7 @@ class DoliDb
} else { } else {
print OUT $_; print OUT $_;
} }
*/ */
} # end of if inside create_table } # end of if inside create_table
@ -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);