From c0d4b0ef244f1df214121eeb7679b9416aa41ee1 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 21 May 2014 14:23:09 +0000 Subject: [PATCH] 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 --- dblayer.php | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/dblayer.php b/dblayer.php index bff4426..f5df20b 100644 --- a/dblayer.php +++ b/dblayer.php @@ -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,29 +345,41 @@ 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(); + return $st->rowCount (); } /** Delete a tuple identified by its primary key