Change to support more databases

This commit is contained in:
Laurent Destailleur 2009-10-22 18:21:06 +00:00
parent 8ce50bce43
commit 3e5595f72f
2 changed files with 159 additions and 109 deletions

View File

@ -380,7 +380,7 @@ class pdf_oursin extends ModelePDFFactures
} }
else else
{ {
$this->error=$outputlangs->trans("ErrorSQL")." sql=".$sql; $this->error=$outputlangs->trans("Error")." sql=".$sql;
dol_syslog($this->db,$this->error, LOG_ERR); dol_syslog($this->db,$this->error, LOG_ERR);
return -1; return -1;
} }

View File

@ -145,7 +145,7 @@ class DoliDb
/** /**
* \brief Convert a SQL request in mysql syntax to database syntax * \brief Convert a SQL request in Mysql syntax to PostgreSQL syntax
* \param line SQL request line to convert * \param line SQL request line to convert
* \return string SQL request line converted * \return string SQL request line converted
*/ */
@ -160,18 +160,17 @@ class DoliDb
{ {
return $line; return $line;
} }
if ($create_sql != "") if ($line != "")
{ # we are inside create table statement so lets process datatypes { # we are inside create table statement so lets process datatypes
if (preg_match('/(ISAM|innodb)/i',$line)) { # end of create table sequence if (preg_match('/(ISAM|innodb)/i',$line)) { # end of create table sequence
$line=preg_replace('/\) *type=(MyISAM|innodb);/i',');'); $line=preg_replace('/\) *type=(MyISAM|innodb);/i',');',$line);
$line=preg_replace('/\) *engine=(MyISAM|innodb);/i',');'); $line=preg_replace('/\) *engine=(MyISAM|innodb);/i',');',$line);
$line=preg_replace('/,$/','',$line);
} }
# int, auto_increment -> serial if (preg_match('/[\s\t]*(\w*)\s*.*int.*auto_increment/i',$line,$reg)) {
// } elsif (/^[\s\t]*(\w*)\s*.*int.*auto_increment/i) { $line=preg_replace('/[\s\t]*([a-zA-Z_0-9]*)\s*.*int.*auto_increment[^,]*/i','\\1 SERIAL PRIMARY KEY',$line);
// $seq = qq~${table}_${1}_seq~; }
// s/[\s\t]*([a-zA-Z_0-9]*)\s*.*int.*auto_increment[^,]*/ $1 SERIAL PRIMARY KEY/ig;
// $create_sql.=$_;
# int type conversion # int type conversion
/* } elsif (/(\w*)int\(\d+\)/i) { /* } elsif (/(\w*)int\(\d+\)/i) {
@ -190,18 +189,14 @@ class DoliDb
$line=str_replace('tinyint','smallint',$line); $line=str_replace('tinyint','smallint',$line);
# nuke unsigned # nuke unsigned
if (preg_replace('/(int\w+|smallint)\s+unsigned/i','smallint',$reg)) $line=preg_replace('/(int\w+|smallint)\s+unsigned/i','\\1',$line);
{
$line=preg_replace('/(int\w+|smallint)\s+unsigned/i',$reg[1]);
}
# blob -> text # blob -> text
$line=preg_replace('/\w*blob/i','text'); $line=preg_replace('/\w*blob/i','text',$line);
# tinytext/mediumtext -> text # tinytext/mediumtext -> text
$line=preg_replace('/tinytext/i','text'); $line=preg_replace('/tinytext/i','text',$line);
$line=preg_replace('/mediumtext/i','text'); $line=preg_replace('/mediumtext/i','text',$line);
# char -> varchar # char -> varchar
# PostgreSQL would otherwise pad with spaces as opposed # PostgreSQL would otherwise pad with spaces as opposed
@ -215,8 +210,8 @@ class DoliDb
# 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
$line=preg_replace('/datetime not null/i','datetime'); $line=preg_replace('/datetime not null/i','datetime',$line);
$line=preg_replace('/datetime/i','timestamp'); $line=preg_replace('/datetime/i','timestamp',$line);
# nuke size of timestamp # nuke size of timestamp
// s/timestamp\([^)]*\)/timestamp/i; // s/timestamp\([^)]*\)/timestamp/i;
@ -365,6 +360,8 @@ class DoliDb
if ($this->db) if ($this->db)
{ {
$this->database_name = $name; $this->database_name = $name;
pg_set_error_verbosity($this->db, PGSQL_ERRORS_VERBOSE); // Set verbosity to max
} }
return $this->db; return $this->db;
} }
@ -492,9 +489,9 @@ class DoliDb
{ {
$query = trim($query); $query = trim($query);
if ($this->forcecharset=="UTF-8"){ // Convert MySQL syntax to PostgresSQL syntax
$buffer=utf8_encode ($buffer); $query=$this->convertSQLFromMysql($query);
}
$ret = pg_query($this->db, $query); $ret = pg_query($this->db, $query);
if (! eregi("^COMMIT",$query) && ! eregi("^ROLLBACK",$query)) if (! eregi("^COMMIT",$query) && ! eregi("^ROLLBACK",$query))
{ {
@ -504,6 +501,7 @@ class DoliDb
$this->lastqueryerror = $query; $this->lastqueryerror = $query;
$this->lasterror = $this->error(); $this->lasterror = $this->error();
$this->lasterrno = $this->errno(); $this->lasterrno = $this->errno();
print '>>'.$this->lasterrno.' - '.$this->lasterror.' - '.$this->laqtqueryerror."<br>\n";
} }
$this->lastquery=$query; $this->lastquery=$query;
$this->results = $ret; $this->results = $ret;
@ -738,6 +736,56 @@ class DoliDb
*/ */
function errno() function errno()
{ {
if (! $this->connected) {
// Si il y a eu echec de connexion, $this->db n'est pas valide.
return 'DB_ERROR_FAILED_TO_CONNECT';
}
else {
// Constants to convert a MySql error code to a generic Dolibarr error code
$errorcode_map = array(
1004 => 'DB_ERROR_CANNOT_CREATE',
1005 => 'DB_ERROR_CANNOT_CREATE',
1006 => 'DB_ERROR_CANNOT_CREATE',
1007 => 'DB_ERROR_ALREADY_EXISTS',
1008 => 'DB_ERROR_CANNOT_DROP',
1025 => 'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
1044 => 'DB_ERROR_ACCESSDENIED',
1046 => 'DB_ERROR_NODBSELECTED',
1048 => 'DB_ERROR_CONSTRAINT',
'42P07' => 'DB_ERROR_TABLE_ALREADY_EXISTS',
1051 => 'DB_ERROR_NOSUCHTABLE',
1054 => 'DB_ERROR_NOSUCHFIELD',
1060 => 'DB_ERROR_COLUMN_ALREADY_EXISTS',
1061 => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
1062 => 'DB_ERROR_RECORD_ALREADY_EXISTS',
1064 => 'DB_ERROR_SYNTAX',
1068 => 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
1075 => 'DB_ERROR_CANT_DROP_PRIMARY_KEY',
1091 => 'DB_ERROR_NOSUCHFIELD',
1100 => 'DB_ERROR_NOT_LOCKED',
1136 => 'DB_ERROR_VALUE_COUNT_ON_ROW',
1146 => 'DB_ERROR_NOSUCHTABLE',
1216 => 'DB_ERROR_NO_PARENT',
1217 => 'DB_ERROR_CHILD_EXISTS',
1451 => 'DB_ERROR_CHILD_EXISTS'
);
$errorlabel=pg_last_error($this->db);
$errorcode='';
if (preg_match('/: *([0-9P]+):/',$errorlabel,$reg))
{
$errorcode=$reg[1];
}
if (isset($errorcode_map[$errorcode]))
{
return $errorcode_map[$errorcode];
}
$errno=$errorlabel;
return ($errno?'DB_ERROR_'.$errno:'0');
}
// TODO Virer cela
if (empty($error_regexps)) if (empty($error_regexps))
{ {
$error_regexps = array( $error_regexps = array(
@ -751,12 +799,14 @@ class DoliDb
'/referential integrity violation/' => 'DB_ERROR_CONSTRAINT' '/referential integrity violation/' => 'DB_ERROR_CONSTRAINT'
); );
} }
print 'WW'.pg_last_error($this->db).'WW';
$valerror=pg_last_error($this->db);
foreach ($error_regexps as $regexp => $code) { foreach ($error_regexps as $regexp => $code) {
if (preg_match($regexp, pg_last_error($this->db))) { if (preg_match($regexp,$valerror)) {
return $code; return $code;
} }
} }
$errno=pg_last_error($this->db); $errno=$valerror;
return ($errno?'DB_ERROR':'0'); return ($errno?'DB_ERROR':'0');
} }