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
This commit is contained in:
2014-07-30 11:49:36 +00:00
parent 725356dda0
commit 7afa8bfc54

View File

@@ -64,7 +64,7 @@ class dblayer extends PDO
public function databasename () public function databasename ()
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception ("Database not connected"); throw new Exception (_("Database not connected"));
$vals = explode (";", substr (strstr ($this->dsn, ":"), 1)); $vals = explode (";", substr (strstr ($this->dsn, ":"), 1));
$dsnExplode = array (); $dsnExplode = array ();
foreach ($vals as $val) foreach ($vals as $val)
@@ -81,7 +81,7 @@ class dblayer extends PDO
public function listTables () public function listTables ()
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception ("Database not connected"); throw new Exception (_("Database not connected"));
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
{ {
case "sqlite": case "sqlite":
@@ -204,7 +204,8 @@ class dblayer extends PDO
if (in_array ("autoincrement", $params)) if (in_array ("autoincrement", $params))
$datas[$key] = null; $datas[$key] = null;
if (in_array ("not null", $params) && !array_key_exists ($key, $datas)) 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)) if (!array_key_exists ($key, $datas))
continue; continue;
// Verify the fields, if $verify is defined, before doing insertion // Verify the fields, if $verify is defined, before doing insertion
@@ -219,8 +220,8 @@ class dblayer extends PDO
// Check for inconsistency // Check for inconsistency
$verify = $this->verifyAll ($datas); $verify = $this->verifyAll ($datas);
if (count ($verify)) if (count ($verify))
throw new Exception ("Errors in consistency : ".print_r ($verify, TRUE), throw new Exception (_("Errors in consistency : ").
405); print_r ($verify, TRUE), 405);
// Check if the unique constrain is valid before doing the insertion // Check if the unique constrain is valid before doing the insertion
foreach ($this->unique as $columns) foreach ($this->unique as $columns)
@@ -336,7 +337,7 @@ class dblayer extends PDO
foreach ($display as $f) foreach ($display as $f)
{ {
if (!in_array ($f, array_keys ($this->fields))) 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 else
@@ -559,7 +560,7 @@ class dblayer extends PDO
public function delete ($deletekey) public function delete ($deletekey)
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception ("Database not connected"); throw new Exception (_("Database not connected"));
$req = "DELETE FROM `$this->tableprefix$this->table` "; $req = "DELETE FROM `$this->tableprefix$this->table` ";
$req .= "WHERE $this->primary = :primary"; $req .= "WHERE $this->primary = :primary";
$st = $this->db->prepare ($req); $st = $this->db->prepare ($req);
@@ -599,7 +600,7 @@ class dblayer extends PDO
public function createTable () public function createTable ()
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception ("Database not connected"); throw new Exception (_("Database not connected"));
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
{ {
case "sqlite": case "sqlite":
@@ -613,7 +614,10 @@ class dblayer extends PDO
$sql .= "`$field` "; $sql .= "`$field` ";
// Type of field : in $params[0] // Type of field : in $params[0]
if (!isset ($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]) switch ($params[0])
{ {
case "integer": case "integer":
@@ -630,8 +634,14 @@ class dblayer extends PDO
$sql .= "DATETIME"; $sql .= "DATETIME";
$params = array_slice ($params, 1); $params = array_slice ($params, 1);
break; break;
case "date":
$sql .= "DATE";
$params = array_slice ($params, 1);
break;
default: 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 // Primary key
if ($this->primary === $field) if ($this->primary === $field)
@@ -711,8 +721,14 @@ class dblayer extends PDO
$sql .= "DATETIME"; $sql .= "DATETIME";
$params = array_slice ($params, 1); $params = array_slice ($params, 1);
break; break;
case "date":
$sql .= "DATE";
$params = array_slice ($params, 1);
break;
default: default:
throw new Exception (_("Unknown type provided for field")); throw new Exception (sprintf (
_("Unknown type provided for field '%s'"),
$field), 500);
} }
// Primary key // Primary key
if ($this->primary === $field) if ($this->primary === $field)
@@ -728,8 +744,9 @@ class dblayer extends PDO
case "not null": $sql .= " NOT NULL"; break; case "not null": $sql .= " NOT NULL"; break;
case "autoincrement": $sql .= " AUTO_INCREMENT";break; case "autoincrement": $sql .= " AUTO_INCREMENT";break;
default: default:
throw new Exception (_("Unknown additionnal parameter for field"), throw new Exception (sprintf (
500); _("Unknown additionnal parameter for field '%s'"),
$field), 500);
} }
} }
$i ++; $i ++;
@@ -777,7 +794,9 @@ class dblayer extends PDO
{ {
// Type of field : in $params[0] // Type of field : in $params[0]
if (!isset ($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]) switch ($params[0])
{ {
case "integer": case "integer":
@@ -786,7 +805,9 @@ class dblayer extends PDO
break; break;
case "varchar": case "varchar":
if (!isset ($params[1])) 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].")"; $sql .= "VARCHAR(".$params[1].")";
$params = array_slice ($params, 2); $params = array_slice ($params, 2);
break; break;
@@ -794,8 +815,14 @@ class dblayer extends PDO
$sql .= "timestamp with time zone"; $sql .= "timestamp with time zone";
$params = array_slice ($params, 1); $params = array_slice ($params, 1);
break; break;
case "date":
$sql .= "DATE";
$params = array_slice ($params, 1);
break;
default: default:
throw new Exception (_("Unknown type provided for field")); throw new Exception (sprintf (
_("Unknown type provided for field '%s'"),
$field), 500);
} }
// Primary key // Primary key
if ($this->primary === $field) if ($this->primary === $field)
@@ -810,8 +837,9 @@ class dblayer extends PDO
{ {
case "not null": $sql .= " NOT NULL"; break; case "not null": $sql .= " NOT NULL"; break;
default: default:
throw new Exception (_("Unknown additionnal parameter for field"), throw new Exception (sprintf (
500); _("Unknown additionnal parameter for field '%s'"),
$field), 500);
} }
} }
} }
@@ -845,7 +873,7 @@ class dblayer extends PDO
$sql .=")"; $sql .=")";
break; break;
default: default:
throw new Exception (_("PDO Engine not supported in dbLayer", 500)); throw new Exception (_("PDO Engine not supported in dbLayer"), 500);
} }
if ($this->debug) if ($this->debug)