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:
48
dblayer.php
48
dblayer.php
@@ -39,6 +39,9 @@ class dblayer extends PDO
|
|||||||
protected $unique = null;
|
protected $unique = null;
|
||||||
/** The db connection */
|
/** The db connection */
|
||||||
protected $db = null;
|
protected $db = null;
|
||||||
|
/** The verify stack */
|
||||||
|
protected function verifyOne ($field, $val) {}
|
||||||
|
protected function verifyAll ($datas) {}
|
||||||
/** Debug of the SQL */
|
/** Debug of the SQL */
|
||||||
public $debug = FALSE;
|
public $debug = FALSE;
|
||||||
/** Return all the tables available in the database */
|
/** Return all the tables available in the database */
|
||||||
@@ -96,11 +99,21 @@ class dblayer extends PDO
|
|||||||
$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 ("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
|
// TODO : Check for type inconsistancies before create $datasOK
|
||||||
if (array_key_exists ($key, $datas))
|
if (array_key_exists ($key, $datas))
|
||||||
$datasOK[$key] = $datas[$key];
|
$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
|
// Check if the unique constrain is valid before doing the insertion
|
||||||
foreach ($this->unique as $columns)
|
foreach ($this->unique as $columns)
|
||||||
{
|
{
|
||||||
@@ -256,7 +269,15 @@ class dblayer extends PDO
|
|||||||
// Check for missing parameters
|
// Check for missing parameters
|
||||||
foreach ($this->fields as $key=>$params)
|
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))
|
if (array_key_exists ($key, $datas))
|
||||||
$datasOK[$key] = $datas[$key];
|
$datasOK[$key] = $datas[$key];
|
||||||
}
|
}
|
||||||
@@ -314,6 +335,7 @@ class dblayer extends PDO
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO : Check if the foreign keys constrains are valid before doing the
|
// TODO : Check if the foreign keys constrains are valid before doing the
|
||||||
|
$datasOK[$this->primary] = $updatekey;
|
||||||
$req = "UPDATE `".$this->table."` SET ";
|
$req = "UPDATE `".$this->table."` SET ";
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($datasOK as $key=>$val)
|
foreach ($datasOK as $key=>$val)
|
||||||
@@ -323,29 +345,41 @@ class dblayer extends PDO
|
|||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$req .= " WHERE $this->primary=:primary";
|
$req .= " WHERE $this->primary=:$this->primary";
|
||||||
if ($this->debug) echo "DEBUG : $req\n";
|
if ($this->debug) echo "DEBUG : $req\n";
|
||||||
$st = $this->db->prepare ($req);
|
$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)
|
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 ($val === null)
|
||||||
|
{
|
||||||
|
if ($this->debug) echo "(null)\n";
|
||||||
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
|
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
|
||||||
|
}
|
||||||
elseif ($this->fields[$key][0] === "integer")
|
elseif ($this->fields[$key][0] === "integer")
|
||||||
|
{
|
||||||
|
if ($this->debug) echo "(integer)\n";
|
||||||
$st->bindValue (":$key", $val, PDO::PARAM_INT);
|
$st->bindValue (":$key", $val, PDO::PARAM_INT);
|
||||||
|
}
|
||||||
elseif ($this->fields[$key][0] === "varchar")
|
elseif ($this->fields[$key][0] === "varchar")
|
||||||
|
{
|
||||||
|
if ($this->debug) echo "(varchar)\n";
|
||||||
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
||||||
|
}
|
||||||
elseif ($this->fields[$key][0] === "datetime")
|
elseif ($this->fields[$key][0] === "datetime")
|
||||||
|
{
|
||||||
|
if ($this->debug) echo "(datetime)\n";
|
||||||
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
if ($this->debug) echo "(UNKNOWN)\n";
|
||||||
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
|
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$st->execute ();
|
$st->execute ();
|
||||||
return $st->rowCount();
|
return $st->rowCount ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete a tuple identified by its primary key
|
/** Delete a tuple identified by its primary key
|
||||||
|
|||||||
Reference in New Issue
Block a user