From 6eeda3819b81f6653af618e6b77db66cde7736fb Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 15 Feb 2005 22:13:41 +0000 Subject: [PATCH] =?UTF-8?q?Qual:=20Les=20codes=20erreurs=20des=20requetes?= =?UTF-8?q?=20est=20remplac=E9=20par=20l'=E9quivalent=20g=E9n=E9rique,=20c?= =?UTF-8?q?e=20qui=20permet=20compatibilit=E9=20des=20gestions=20d'erreurs?= =?UTF-8?q?=20entre=20mysql=20et=20pgsql.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/admin/dict.php | 2 +- htdocs/facturefourn.class.php | 2 +- htdocs/lib/mysql.lib.php | 70 +++++++++++++++++++++-------------- htdocs/lib/pgsql.lib.php | 2 +- htdocs/product.class.php | 2 +- htdocs/societe.class.php | 4 +- 6 files changed, 48 insertions(+), 34 deletions(-) diff --git a/htdocs/admin/dict.php b/htdocs/admin/dict.php index 34538b3cc23..ea4736c81ea 100644 --- a/htdocs/admin/dict.php +++ b/htdocs/admin/dict.php @@ -181,7 +181,7 @@ if ($_POST["actionadd"]) { $result = $db->query($sql); if (!$result) { - if ($db->errno() == $db->ERROR_DUPLICATE) { + if ($db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS) { $msg="Une entrée pour cette clé existe déjà
"; } else { diff --git a/htdocs/facturefourn.class.php b/htdocs/facturefourn.class.php index 3bd76ce0a54..3c73fa4c811 100644 --- a/htdocs/facturefourn.class.php +++ b/htdocs/facturefourn.class.php @@ -135,7 +135,7 @@ class FactureFourn } else { - if ($this->db->errno() == $this->db->ERROR_DUPLICATE) { + if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS) { print "Erreur : Une facture possédant cet id existe déjà"; } else { diff --git a/htdocs/lib/mysql.lib.php b/htdocs/lib/mysql.lib.php index 16b09dd2905..5f9af00ec12 100644 --- a/htdocs/lib/mysql.lib.php +++ b/htdocs/lib/mysql.lib.php @@ -23,17 +23,18 @@ */ /** - \file htdocs/lib/mysql.lib.php - \brief Fichier de la classe permettant de gérer une base mysql - \author Fabien Seisen - \author Rodolphe Quiedeville. - \author Laurent Destailleur. - \version $Revision$ + \file htdocs/lib/mysql.lib.php + \brief Fichier de la classe permettant de gérer une base mysql + \author Fabien Seisen + \author Rodolphe Quiedeville. + \author Laurent Destailleur. + \version $Revision$ */ -/** \class DoliDb - \brief Classe permettant de gérér la database de dolibarr +/** + \class DoliDb + \brief Classe permettant de gérér la database de dolibarr */ class DoliDb @@ -47,10 +48,24 @@ class DoliDb var $ok; - // Constantes pour code erreurs - var $ERROR_DUPLICATE=1062; - var $ERROR_TABLEEXISTS=1050; - + // Constantes pour conversion code erreur MySql en code erreur générique + var $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, + 1046 => DB_ERROR_NODBSELECTED, + 1050 => DB_ERROR_TABLE_ALREADY_EXISTS, + 1051 => DB_ERROR_NOSUCHTABLE, + 1054 => DB_ERROR_NOSUCHFIELD, + 1062 => DB_ERROR_RECORD_ALREADY_EXISTS, + 1064 => DB_ERROR_SYNTAX, + 1100 => DB_ERROR_NOT_LOCKED, + 1136 => DB_ERROR_VALUE_COUNT_ON_ROW, + 1146 => DB_ERROR_NOSUCHTABLE, + 1048 => DB_ERROR_CONSTRAINT, + ); /** \brief Ouverture d'une connection vers le serveur et éventuellement une database. @@ -447,8 +462,21 @@ class DoliDb } /** - \brief Renvoie le texte de l'erreur mysql de l'operation precedente. - \return error_text + \brief Renvoie le code erreur generique de l'operation precedente. + \return error_num (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...) + */ + + function errno() + { + if (isset($this->errorcode_map[mysql_errno($this->db)])) { + return $this->errorcode_map[mysql_errno($this->db)]; + } + return DB_ERROR; + } + + /** + \brief Renvoie le texte de l'erreur mysql de l'operation precedente. + \return error_text */ function error() @@ -456,20 +484,6 @@ class DoliDb return mysql_error($this->db); } - /** - \brief Renvoie la valeur numerique de l'erreur de l'operation precedente. - pour etre exploiter par l'appelant et détecter les erreurs du genre: - echec car doublons, table deja existante... - \return error_num - */ - - function errno() - { - // $ERROR_DUPLICATE=1062; - // $ERROR_TABLEEXISTS=1050; - return mysql_errno($this->db); - } - /** \brief Obtient l'id genéré par le dernier INSERT. \return id diff --git a/htdocs/lib/pgsql.lib.php b/htdocs/lib/pgsql.lib.php index 00589cc88dc..16a3d0b5814 100644 --- a/htdocs/lib/pgsql.lib.php +++ b/htdocs/lib/pgsql.lib.php @@ -465,7 +465,7 @@ class DoliDb $error_regexps = array( '/(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_ALREADY_EXISTS, + '/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, diff --git a/htdocs/product.class.php b/htdocs/product.class.php index edc8c768797..dc38444f829 100644 --- a/htdocs/product.class.php +++ b/htdocs/product.class.php @@ -188,7 +188,7 @@ class Product } else { - if ($this->db->errno() == $this->db->ERROR_DUPLICATE) { + if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS) { $this->mesg_error=$langs->trans("Error")." : ".$langs->trans("ErrorProductAlreadyExists",$this->ref); return -1; } diff --git a/htdocs/societe.class.php b/htdocs/societe.class.php index 289e5e56887..c3c0bd1e3b7 100644 --- a/htdocs/societe.class.php +++ b/htdocs/societe.class.php @@ -139,7 +139,7 @@ class Societe { } else { - if ($this->db->errno() == $this->db->ERROR_DUPLICATE) + if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS) { $this->error=$langs->trans("ErrorCompanyNameAlreadyExists",$this->nom); } @@ -348,7 +348,7 @@ class Societe { } else { - if ($this->db->errno() == $this->db->ERROR_DUPLICATE) + if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS) { // Doublon $this->error = $langs->trans("ErrorPrefixAlreadyExists",$this->prefix_comm);