From 7afa8bfc540afe2bdd4b108b00725b77e9105317 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 30 Jul 2014 11:49:36 +0000 Subject: [PATCH] dblayer : Add better error messages (gettext, Exception code, field name) dblayer : Add support to date type git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1601 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 66 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/dblayer.php b/dblayer.php index a133fc7..afa7051 100644 --- a/dblayer.php +++ b/dblayer.php @@ -64,7 +64,7 @@ class dblayer extends PDO public function databasename () { if ($this->db === null) - throw new Exception ("Database not connected"); + throw new Exception (_("Database not connected")); $vals = explode (";", substr (strstr ($this->dsn, ":"), 1)); $dsnExplode = array (); foreach ($vals as $val) @@ -81,7 +81,7 @@ class dblayer extends PDO public function listTables () { if ($this->db === null) - throw new Exception ("Database not connected"); + throw new Exception (_("Database not connected")); switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) { case "sqlite": @@ -204,7 +204,8 @@ class dblayer extends PDO if (in_array ("autoincrement", $params)) $datas[$key] = null; if (in_array ("not null", $params) && !array_key_exists ($key, $datas)) - throw new Exception ("Mandatory field '$key' not provided", 405); + throw new Exception (sprintf (_("Mandatory field '%s' not provided"), + $key), 405); if (!array_key_exists ($key, $datas)) continue; // Verify the fields, if $verify is defined, before doing insertion @@ -219,8 +220,8 @@ class dblayer extends PDO // Check for inconsistency $verify = $this->verifyAll ($datas); if (count ($verify)) - throw new Exception ("Errors in consistency : ".print_r ($verify, TRUE), - 405); + throw new Exception (_("Errors in consistency : "). + print_r ($verify, TRUE), 405); // Check if the unique constrain is valid before doing the insertion foreach ($this->unique as $columns) @@ -336,7 +337,7 @@ class dblayer extends PDO foreach ($display as $f) { if (!in_array ($f, array_keys ($this->fields))) - throw new Exception (sprintf (_("Field %s not allowed"), $f), 506); + throw new Exception (sprintf (_("Field '%s' not allowed"), $f), 506); } } else @@ -559,7 +560,7 @@ class dblayer extends PDO public function delete ($deletekey) { if ($this->db === null) - throw new Exception ("Database not connected"); + throw new Exception (_("Database not connected")); $req = "DELETE FROM `$this->tableprefix$this->table` "; $req .= "WHERE $this->primary = :primary"; $st = $this->db->prepare ($req); @@ -599,7 +600,7 @@ class dblayer extends PDO public function createTable () { if ($this->db === null) - throw new Exception ("Database not connected"); + throw new Exception (_("Database not connected")); switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) { case "sqlite": @@ -613,7 +614,10 @@ class dblayer extends PDO $sql .= "`$field` "; // Type of field : in $params[0] if (!isset ($params[0])) - throw new Exception (_("No database type defined for field")); + throw new Exception (sprintf ( + _("No database type defined for field '%s'"), + $field), 500); + switch ($params[0]) { case "integer": @@ -630,8 +634,14 @@ class dblayer extends PDO $sql .= "DATETIME"; $params = array_slice ($params, 1); break; + case "date": + $sql .= "DATE"; + $params = array_slice ($params, 1); + break; default: - throw new Exception (_("Unknown type provided for field")); + throw new Exception (sprintf ( + _("Unknown type '%s' provided for field '%s'"), + $params[0], $field), 500); } // Primary key if ($this->primary === $field) @@ -711,8 +721,14 @@ class dblayer extends PDO $sql .= "DATETIME"; $params = array_slice ($params, 1); break; + case "date": + $sql .= "DATE"; + $params = array_slice ($params, 1); + break; default: - throw new Exception (_("Unknown type provided for field")); + throw new Exception (sprintf ( + _("Unknown type provided for field '%s'"), + $field), 500); } // Primary key if ($this->primary === $field) @@ -728,8 +744,9 @@ class dblayer extends PDO case "not null": $sql .= " NOT NULL"; break; case "autoincrement": $sql .= " AUTO_INCREMENT";break; default: - throw new Exception (_("Unknown additionnal parameter for field"), - 500); + throw new Exception (sprintf ( + _("Unknown additionnal parameter for field '%s'"), + $field), 500); } } $i ++; @@ -777,7 +794,9 @@ class dblayer extends PDO { // Type of field : in $params[0] if (!isset ($params[0])) - throw new Exception (_("No database type defined for field")); + throw new Exception (sprintf ( + _("No database type defined for field '%s'"), + $field), 500); switch ($params[0]) { case "integer": @@ -786,7 +805,9 @@ class dblayer extends PDO break; case "varchar": if (!isset ($params[1])) - throw new Exception (_("No Size provided for varchar field")); + throw new Exception (sprintf ( + _("No Size provided for varchar field '%s'"), + $field), 500); $sql .= "VARCHAR(".$params[1].")"; $params = array_slice ($params, 2); break; @@ -794,8 +815,14 @@ class dblayer extends PDO $sql .= "timestamp with time zone"; $params = array_slice ($params, 1); break; + case "date": + $sql .= "DATE"; + $params = array_slice ($params, 1); + break; default: - throw new Exception (_("Unknown type provided for field")); + throw new Exception (sprintf ( + _("Unknown type provided for field '%s'"), + $field), 500); } // Primary key if ($this->primary === $field) @@ -810,8 +837,9 @@ class dblayer extends PDO { case "not null": $sql .= " NOT NULL"; break; default: - throw new Exception (_("Unknown additionnal parameter for field"), - 500); + throw new Exception (sprintf ( + _("Unknown additionnal parameter for field '%s'"), + $field), 500); } } } @@ -845,7 +873,7 @@ class dblayer extends PDO $sql .=")"; break; default: - throw new Exception (_("PDO Engine not supported in dbLayer", 500)); + throw new Exception (_("PDO Engine not supported in dbLayer"), 500); } if ($this->debug)