Update the dbLayer to update more easily the tables

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1317 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-05-21 14:23:09 +00:00
parent bb18deadab
commit c0d4b0ef24

View File

@@ -39,6 +39,9 @@ class dblayer extends PDO
protected $unique = null;
/** The db connection */
protected $db = null;
/** The verify stack */
protected function verifyOne ($field, $val) {}
protected function verifyAll ($datas) {}
/** Debug of the SQL */
public $debug = FALSE;
/** Return all the tables available in the database */
@@ -96,11 +99,21 @@ class dblayer extends PDO
$datas[$key] = null;
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
throw new Exception ("Mandatory field '$key' not provided", 405);
// Verify the fields, if $verify is defined, before doing insertion
$verify = $this->verifyOne ($key, $datas[$key]);
if (is_array ($verify) && count ($verify))
throw new Exception ($verify[0]." ".$verify[1]." in ".$key);
// TODO : Check for type inconsistancies before create $datasOK
if (array_key_exists ($key, $datas))
$datasOK[$key] = $datas[$key];
}
// Check for inconsistancy
$verify = $this->verifyAll ($datas);
if (count ($verify))
throw new Exception ("Errors in consistancy : ".print_r ($verify, TRUE),
405);
// Check if the unique constrain is valid before doing the insertion
foreach ($this->unique as $columns)
{
@@ -256,7 +269,15 @@ class dblayer extends PDO
// Check for missing parameters
foreach ($this->fields as $key=>$params)
{
// TODO : Check for type inconsistancies before create $datasOK
// Check for type inconsistancies before create $datasOK
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);
// Verify the fields, if $verify is defined, before doing insertion
$verify = $this->verifyOne ($key, $datas[$key]);
if (is_array ($verify) && count ($verify))
throw new Exception ($verify[0]." ".$verify[1]." in ".$key);
if (array_key_exists ($key, $datas))
$datasOK[$key] = $datas[$key];
}
@@ -314,6 +335,7 @@ class dblayer extends PDO
}
// TODO : Check if the foreign keys constrains are valid before doing the
$datasOK[$this->primary] = $updatekey;
$req = "UPDATE `".$this->table."` SET ";
$i = 0;
foreach ($datasOK as $key=>$val)
@@ -323,26 +345,38 @@ class dblayer extends PDO
$i++;
}
$req .= " WHERE $this->primary=:primary";
$req .= " WHERE $this->primary=:$this->primary";
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
if ($this->debug) echo "DEBUG BIND : primary->".
var_export ($updatekey, TRUE)."\n";
$st->bindValue (":primary", $updatekey);
foreach ($datasOK as $key=>$val)
{
if ($this->debug) echo "DEBUG BIND : $key->".var_export ($val, TRUE)."\n";
if ($this->debug) echo "DEBUG BIND : $key->".var_export ($val, TRUE)." ";
if ($val === null)
{
if ($this->debug) echo "(null)\n";
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
}
elseif ($this->fields[$key][0] === "integer")
{
if ($this->debug) echo "(integer)\n";
$st->bindValue (":$key", $val, PDO::PARAM_INT);
}
elseif ($this->fields[$key][0] === "varchar")
{
if ($this->debug) echo "(varchar)\n";
$st->bindValue (":$key", $val, PDO::PARAM_STR);
}
elseif ($this->fields[$key][0] === "datetime")
{
if ($this->debug) echo "(datetime)\n";
$st->bindValue (":$key", $val, PDO::PARAM_STR);
}
else
{
if ($this->debug) echo "(UNKNOWN)\n";
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
}
}
$st->execute ();
return $st->rowCount ();