From 05533ffe7742d9c2b01b8d5d223e5c6ebb201d53 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 25 Oct 2009 18:53:09 +0000 Subject: [PATCH] Qual: More portable SQL --- htdocs/install/etape2.php | 31 ++++++---- .../mysql/data/llx_accountingaccount.sql | 2 +- htdocs/install/pgsql/functions/functions.sql | 19 +----- htdocs/lib/databases/pgsql.lib.php | 60 +++++++++---------- 4 files changed, 50 insertions(+), 62 deletions(-) diff --git a/htdocs/install/etape2.php b/htdocs/install/etape2.php index 6259a2d0501..f3a9d9d7b57 100644 --- a/htdocs/install/etape2.php +++ b/htdocs/install/etape2.php @@ -349,13 +349,16 @@ if ($_POST["action"] == "set") // Creation donnees $file = "functions.sql"; - if (file_exists($dir.$file)) { - $fp = fopen($dir.$file,"r"); - if ($fp) + if (file_exists($dir.$file)) + { + $fp = fopen($dir.$file,"r"); + dolibarr_install_syslog("Open function file ".$dir.$file." handle=".$fp,LOG_DEBUG); + if ($fp) { + $buffer=''; while (!feof ($fp)) { - $buffer = fgets($fp, 4096); + $buf = fgets($fp, 4096); if (substr($buf, 0, 2) <> '--') { $buffer .= $buf; @@ -365,11 +368,15 @@ if ($_POST["action"] == "set") } // Si plusieurs requetes, on boucle sur chaque - $listesql=explode('§',preg_replace(";';",";'§",$buffer)); // TODO vérifier expression - foreach ($listesql as $buffer) { - if (trim($buffer)) { - - if ($db->query(trim($buffer))) + $buffer=preg_replace('/;\';/',";'§",$buffer); + $listesql=explode('§',$buffer); + foreach ($listesql as $buffer) + { + $buffer=trim($buffer); + if ($buffer) + { + dolibarr_install_syslog("Request: ".$buffer,LOG_DEBUG); + if ($db->query($buffer)) { $ok = 1; } @@ -382,7 +389,7 @@ if ($_POST["action"] == "set") else { $ok = 0; - print $langs->trans("ErrorSQL")." : ".$db->errno()." - '$buffer' - ".$db->error()."
"; + print $langs->trans("ErrorSQL")." : ".$db->errno()." - '$buffer' - ".$db->lastqueryerror()."
"; } } } @@ -413,9 +420,9 @@ if ($_POST["action"] == "set") // We always choose in mysql directory (Conversion is done by driver to translate SQL syntax) $dir = "mysql/data/"; - // Creation donnees + // Insert data $handle=opendir($dir); - dolibarr_install_syslog("Ouverture repertoire data ".$dir." handle=".$handle,LOG_DEBUG); + dolibarr_install_syslog("Open directory data ".$dir." handle=".$handle,LOG_DEBUG); while (($file = readdir($handle))!==false) { if (preg_match('/\.sql$/i',$file) && preg_match('/^llx_/i',$file)) diff --git a/htdocs/install/mysql/data/llx_accountingaccount.sql b/htdocs/install/mysql/data/llx_accountingaccount.sql index d560fdd244b..56ceea3d807 100644 --- a/htdocs/install/mysql/data/llx_accountingaccount.sql +++ b/htdocs/install/mysql/data/llx_accountingaccount.sql @@ -35,7 +35,7 @@ delete from llx_accountingaccount; delete from llx_accountingsystem; -insert into llx_accountingsystem (pcg_version, fk_pays, label, datec, fk_author, active) VALUES ('PCG99-ABREGE', 1, 'Plan de compte standard français abrégé', curdate(), null, 0); +insert into llx_accountingsystem (pcg_version, fk_pays, label, datec, fk_author, active) VALUES ('PCG99-ABREGE', 1, 'The simple accountancy french plan', CURRENT_DATE, null, 0); insert into llx_accountingaccount (rowid, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, label) VALUES ( 1,'PCG99-ABREGE','CAPIT', 'CAPITAL', '101', '1', 'Capital'); insert into llx_accountingaccount (rowid, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, label) VALUES ( 2,'PCG99-ABREGE','CAPIT', 'XXXXXX', '105', '1', 'Ecarts de réévaluation'); diff --git a/htdocs/install/pgsql/functions/functions.sql b/htdocs/install/pgsql/functions/functions.sql index 780c306c565..e4f641f5403 100644 --- a/htdocs/install/pgsql/functions/functions.sql +++ b/htdocs/install/pgsql/functions/functions.sql @@ -16,23 +16,10 @@ -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -- $Id$ --- -- ============================================================================ -CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(TIMESTAMP WITHOUT TIME ZONE) -RETURNS BIGINT -LANGUAGE SQL -IMMUTABLE STRICT -AS 'SELECT EXTRACT(EPOCH FROM $1)::bigint;'; +CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(TIMESTAMP WITHOUT TIME ZONE) RETURNS BIGINT LANGUAGE SQL IMMUTABLE STRICT AS 'SELECT EXTRACT(EPOCH FROM $1)::bigint;'; -CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(TIMESTAMP WITH TIME ZONE) -RETURNS BIGINT -LANGUAGE SQL -IMMUTABLE STRICT -AS 'SELECT EXTRACT(EPOCH FROM $1)::bigint;'; +CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(TIMESTAMP WITH TIME ZONE) RETURNS BIGINT LANGUAGE SQL IMMUTABLE STRICT AS 'SELECT EXTRACT(EPOCH FROM $1)::bigint;'; -CREATE OR REPLACE FUNCTION FROM_UNIXTIME(BIGINT, VARCHAR) -RETURNS TIMESTAMP WITH TIME ZONE -LANGUAGE SQL -IMMUTABLE STRICT -AS 'SELECT TIMESTAMP WITH TIME ZONE \'epoch\' + $1 * interval \'1 second\' ;'; \ No newline at end of file +CREATE OR REPLACE FUNCTION FROM_UNIXTIME(BIGINT, VARCHAR) RETURNS TIMESTAMP WITH TIME ZONE LANGUAGE SQL IMMUTABLE STRICT AS 'SELECT TIMESTAMP WITH TIME ZONE \'epoch\' + $1 * interval \'1 second\' ;'; diff --git a/htdocs/lib/databases/pgsql.lib.php b/htdocs/lib/databases/pgsql.lib.php index bbbde427996..0037bbc4210 100644 --- a/htdocs/lib/databases/pgsql.lib.php +++ b/htdocs/lib/databases/pgsql.lib.php @@ -1,7 +1,7 @@ * Copyright (C) 2002-2005 Rodolphe Quiedeville - * Copyright (C) 2004-2008 Laurent Destailleur + * Copyright (C) 2004-2009 Laurent Destailleur * Copyright (C) 2004 Sebastien Di Cintio * Copyright (C) 2004 Benoit Mortier * Copyright (C) 2005-2009 Regis Houssin @@ -22,9 +22,9 @@ */ /** - \file htdocs/lib/databases/pgsql.lib.php - \brief Fichier de la classe permettant de gerer une base pgsql - \version $Id$ + * \file htdocs/lib/databases/pgsql.lib.php + * \brief Fichier de la classe permettant de gerer une base pgsql + * \version $Id$ */ // For compatibility during upgrade if (! defined('DOL_DOCUMENT_ROOT')) define('DOL_DOCUMENT_ROOT', '../..'); @@ -32,8 +32,8 @@ if (! defined('ADODB_DATE_VERSION')) include_once(DOL_DOCUMENT_ROOT."/includes/a /** - \class DoliDb - \brief Classe permettant de gerer la database de dolibarr + * \class DoliDb + * \brief Classe permettant de gerer la database de dolibarr */ class DoliDb { @@ -201,17 +201,14 @@ class DoliDb $line=preg_replace('/datetime not null/i','datetime',$line); $line=preg_replace('/datetime/i','timestamp',$line); - # nuke size of timestamp - $line=preg_replace('/timestamp\([^)]*\)/i','timestamp',$line); - # double -> numeric - $line=preg_replace('/^double/i','numeric',$line); - $line=preg_replace('/(\s*)double/i','\\1numeric',$line); + $line=preg_replace('/^double/i','numeric',$line); + $line=preg_replace('/(\s*)double/i','\\1numeric',$line); # float -> numeric - $line=preg_replace('/^float/i','numeric',$line); - $line=preg_replace('/(\s*)float/i','\\1numeric',$line); + $line=preg_replace('/^float/i','numeric',$line); + $line=preg_replace('/(\s*)float/i','\\1numeric',$line); - # unique index(field1,field2) + # unique index(field1,field2) if (preg_match('/unique index\s*\((\w+\s*,\s*\w+)\)/i',$line)) { $line=preg_replace('/unique index\s*\((\w+\s*,\s*\w+)\)/i','UNIQUE\(\\1\)',$line); @@ -422,9 +419,9 @@ class DoliDb /** - * \brief Effectue une requete et renvoi le resultset de reponse de la base - * \param query Contenu de la query - * \return resource Resultset de la reponse + * \brief Convert request to PostgreSQL syntax, execute it and return the resultset + * \param query query string + * \return resource Resultset of answer */ function query($query) { @@ -432,7 +429,7 @@ class DoliDb // Convert MySQL syntax to PostgresSQL syntax $query=$this->convertSQLFromMysql($query); - + //print "FF\n".$query."
\n"; $ret = @pg_query($this->db, $query); if (! preg_match("/^COMMIT/i",$query) && ! preg_match("/^ROLLBACK/i",$query)) { @@ -695,11 +692,10 @@ class DoliDb 1046 => 'DB_ERROR_NODBSELECTED', 1048 => 'DB_ERROR_CONSTRAINT', '42P07' => 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS', - 1051 => 'DB_ERROR_NOSUCHTABLE', - 1054 => 'DB_ERROR_NOSUCHFIELD', + '42703' => 'DB_ERROR_NOSUCHFIELD', 1060 => 'DB_ERROR_COLUMN_ALREADY_EXISTS', '42710' => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS', - 1062 => 'DB_ERROR_RECORD_ALREADY_EXISTS', + '23505' => 'DB_ERROR_RECORD_ALREADY_EXISTS', '42704' => 'DB_ERROR_SYNTAX', '42601' => 'DB_ERROR_SYNTAX', '42P16' => 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS', @@ -707,8 +703,8 @@ class DoliDb 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', + '42P01' => 'DB_ERROR_NOSUCHTABLE', + '23503' => 'DB_ERROR_NO_PARENT', 1217 => 'DB_ERROR_CHILD_EXISTS', 1451 => 'DB_ERROR_CHILD_EXISTS' ); @@ -726,14 +722,14 @@ class DoliDb $errno=$errorcode?$errorcode:$errorlabel; return ($errno?'DB_ERROR_'.$errno:'0'); } -// '/(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$/' => 'DB_ERROR_NOSUCHTABLE', -// '/table [\"\'].*[\"\'] does not exist/' => 'DB_ERROR_NOSUCHTABLE', -// '/Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*/' => 'DB_ERROR_RECORD_ALREADY_EXISTS', -// '/divide by zero$/' => 'DB_ERROR_DIVZERO', -// '/pg_atoi: error in .*: can\'t parse /' => 'DB_ERROR_INVALID_NUMBER', -// '/ttribute [\"\'].*[\"\'] not found$|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']/' => 'DB_ERROR_NOSUCHFIELD', -// '/parser: parse error at or near \"/' => 'DB_ERROR_SYNTAX', -// '/referential integrity violation/' => 'DB_ERROR_CONSTRAINT' + // '/(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$/' => 'DB_ERROR_NOSUCHTABLE', + // '/table [\"\'].*[\"\'] does not exist/' => 'DB_ERROR_NOSUCHTABLE', + // '/Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*/' => 'DB_ERROR_RECORD_ALREADY_EXISTS', + // '/divide by zero$/' => 'DB_ERROR_DIVZERO', + // '/pg_atoi: error in .*: can\'t parse /' => 'DB_ERROR_INVALID_NUMBER', + // '/ttribute [\"\'].*[\"\'] not found$|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']/' => 'DB_ERROR_NOSUCHFIELD', + // '/parser: parse error at or near \"/' => 'DB_ERROR_SYNTAX', + // '/referential integrity violation/' => 'DB_ERROR_CONSTRAINT' } /** @@ -961,7 +957,6 @@ class DoliDb } $this->free($resql); } else { - // version Mysql < 4.1.1 return null; } return $liste; @@ -996,7 +991,6 @@ class DoliDb } $this->free($resql); } else { - // version Mysql < 4.1.1 return null; } return $liste;