dblayer : datas -> data

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2362 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-10-13 16:13:50 +00:00
parent 4c5e06011b
commit bb2dbb6dae

View File

@@ -65,8 +65,8 @@ class dblayer
@param string $val The value of the field to test */ @param string $val The value of the field to test */
public function verifyOne ($field, $val) {} public function verifyOne ($field, $val) {}
/** The verify global stack /** The verify global stack
@param array $datas The associative array of contents */ @param array $data The associative array of contents */
public function verifyAll ($datas) {} public function verifyAll ($data) {}
// TODO !! // TODO !!
/** Create Table creation from $this->fields with engine abstraction /** Create Table creation from $this->fields with engine abstraction
@@ -82,7 +82,7 @@ class dblayer
// TODO !! // TODO !!
/** Allow to modify tables if the definition is changed /** Allow to modify tables if the definition is changed
Attention : SQLite don't supports adding Foreign keys without deleting all Attention : SQLite don't supports adding Foreign keys without deleting all
the table, and re-import the datas (http://www.sqlite.org/omitted.html) */ the table, and re-import the data (http://www.sqlite.org/omitted.html) */
/** Limit to one instance of the connection to the same database */ /** Limit to one instance of the connection to the same database */
// Based on an idea of http://tonylandis.com/php/php5-pdo-singleton-class/ // Based on an idea of http://tonylandis.com/php/php5-pdo-singleton-class/
@@ -252,12 +252,12 @@ class dblayer
return $res; return $res;
} }
/** Verify if the provided datas can be inserted/updated in the database. /** Verify if the provided data can be inserted/updated in the database.
@param $datas An array containing the datas to verify with keys @param $data An array containing the data to verify with keys
@param $updatekey the key to update @param $updatekey the key to update
@return an array with the errors in array($key=>array($priority,$message)) @return an array with the errors in array($key=>array($priority,$message))
*/ */
public function verify ($datas, $updatekey=false) public function verify ($data, $updatekey=false)
{ {
if ($this->debug) echo "== Entering verify\n"; if ($this->debug) echo "== Entering verify\n";
$errors = array (); $errors = array ();
@@ -269,15 +269,15 @@ class dblayer
// Don't check if there is an update : the database is already filled // Don't check if there is an update : the database is already filled
// For autoincrement, in INSERT mode, force the value to null // For autoincrement, in INSERT mode, force the value to null
if (in_array ("autoincrement", $params)) if (in_array ("autoincrement", $params))
$datas[$key] = null; $data[$key] = null;
if (in_array ("not null", $params) && !array_key_exists ($key, $datas)) if (in_array ("not null", $params) && !array_key_exists ($key, $data))
{ {
$errors[$key] = array ("error", sprintf (dgettext("domframework", $errors[$key] = array ("error", sprintf (dgettext("domframework",
"Mandatory field '%s' not provided"), "Mandatory field '%s' not provided"),
$key)); $key));
continue; continue;
} }
if (in_array ("not null", $params) && $datas[$key] === "") if (in_array ("not null", $params) && $data[$key] === "")
{ {
$errors[$key] = array ("error", sprintf (dgettext("domframework", $errors[$key] = array ("error", sprintf (dgettext("domframework",
"Mandatory field '%s' is empty"), "Mandatory field '%s' is empty"),
@@ -286,12 +286,12 @@ class dblayer
} }
} }
// Do not verify the non provided datas (if they are not mandatory) // Do not verify the non provided data (if they are not mandatory)
if (!array_key_exists ($key, $datas)) if (!array_key_exists ($key, $data))
continue; continue;
// Verify the fields, if $verify is defined, before doing insertion // Verify the fields, if $verify is defined, before doing insertion
$verify = $this->verifyOne ($key, $datas[$key]); $verify = $this->verifyOne ($key, $data[$key]);
if (is_array ($verify) && count ($verify)) if (is_array ($verify) && count ($verify))
{ {
$errors[$key] = array ($verify[0], $verify[1]); $errors[$key] = array ($verify[0], $verify[1]);
@@ -300,22 +300,22 @@ class dblayer
} }
// Check for type inconsistencies if the value is provided // Check for type inconsistencies if the value is provided
if (is_null ($datas[$key])) if (is_null ($data[$key]))
{ {
// Skipped the removed autoincrement keys // Skipped the removed autoincrement keys
continue; continue;
} }
elseif (! is_string ($datas[$key]) && ! is_integer ($datas[$key])) elseif (! is_string ($data[$key]) && ! is_integer ($data[$key]))
{ {
$errors[$key] = array ("error", sprintf ( $errors[$key] = array ("error", sprintf (
dgettext("domframework", dgettext("domframework",
"Errors in consistency : '%s' is not an integer or a string [is %s]"), "Errors in consistency : '%s' is not an integer or a string [is %s]"),
$key, gettype ($datas[$key]))); $key, gettype ($data[$key])));
continue; continue;
} }
elseif ($datas[$key] !== "" && $params[0] === "integer") elseif ($data[$key] !== "" && $params[0] === "integer")
{ {
if (strspn ($datas[$key], "0123456789") !== strlen ($datas[$key])) if (strspn ($data[$key], "0123456789") !== strlen ($data[$key]))
{ {
$errors[$key] = array ("error", sprintf ( $errors[$key] = array ("error", sprintf (
dgettext("domframework", dgettext("domframework",
@@ -324,7 +324,7 @@ class dblayer
continue; continue;
} }
} }
elseif ($datas[$key] !== "" && $params[0] === "varchar") elseif ($data[$key] !== "" && $params[0] === "varchar")
{ {
if (! isset ($params[1])) if (! isset ($params[1]))
{ {
@@ -334,7 +334,7 @@ class dblayer
$key)); $key));
continue; continue;
} }
if (strlen ($datas[$key]) > $params[1]) if (strlen ($data[$key]) > $params[1])
{ {
$errors[$key] = array ("error", sprintf ( $errors[$key] = array ("error", sprintf (
dgettext("domframework", dgettext("domframework",
@@ -343,11 +343,11 @@ class dblayer
continue; continue;
} }
} }
elseif ($datas[$key] !== "" && $params[0] === "datetime") elseif ($data[$key] !== "" && $params[0] === "datetime")
{ {
// The date format must be in ANSI SQL : YYYY-MM-DD HH:MM:SS // The date format must be in ANSI SQL : YYYY-MM-DD HH:MM:SS
$d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]); $d = DateTime::createFromFormat("Y-m-d H:i:s", $data[$key]);
if (!$d || $d->format("Y-m-d H:i:s") !== $datas[$key]) if (!$d || $d->format("Y-m-d H:i:s") !== $data[$key])
{ {
$errors[$key] = array ("error", sprintf ( $errors[$key] = array ("error", sprintf (
dgettext("domframework", dgettext("domframework",
@@ -356,11 +356,11 @@ class dblayer
continue; continue;
} }
} }
elseif ($datas[$key] !== "" && $params[0] === "date") elseif ($data[$key] !== "" && $params[0] === "date")
{ {
// The date format must be in ANSI SQL : YYYY-MM-DD // The date format must be in ANSI SQL : YYYY-MM-DD
$d = DateTime::createFromFormat("Y-m-d", $datas[$key]); $d = DateTime::createFromFormat("Y-m-d", $data[$key]);
if (!$d || $d->format("Y-m-d") !== $datas[$key]) if (!$d || $d->format("Y-m-d") !== $data[$key])
{ {
$errors[$key] = array ("error", sprintf ( $errors[$key] = array ("error", sprintf (
dgettext("domframework", dgettext("domframework",
@@ -369,7 +369,7 @@ class dblayer
continue; continue;
} }
} }
elseif ($datas[$key] !== "") elseif ($data[$key] !== "")
{ {
$errors[$key] = array ("error", sprintf (dgettext("domframework", $errors[$key] = array ("error", sprintf (dgettext("domframework",
"Unknown field type for '%s'"), $key)); "Unknown field type for '%s'"), $key));
@@ -386,15 +386,15 @@ class dblayer
if ($this->debug) echo " verify inconsistency\n"; if ($this->debug) echo " verify inconsistency\n";
// Check for inconsistency // Check for inconsistency
$verify = $this->verifyAll ($datas); $verify = $this->verifyAll ($data);
if (count ($verify)) if (count ($verify))
return $verify; return $verify;
$datasOK = array (); $dataOK = array ();
foreach ($this->fields as $field=>$desc) foreach ($this->fields as $field=>$desc)
{ {
if (isset ($datas[$field])) if (isset ($data[$field]))
$datasOK[$field] = $datas[$field]; $dataOK[$field] = $data[$field];
} }
if ($updatekey !== false) if ($updatekey !== false)
@@ -409,13 +409,13 @@ class dblayer
$before = reset ($before); $before = reset ($before);
// 2. Map the proposal entries into the before state // 2. Map the proposal entries into the before state
$after = $before; $after = $before;
foreach ($datasOK as $field=>$val) foreach ($dataOK as $field=>$val)
$after[$field] = $val; $after[$field] = $val;
} }
else else
{ {
if ($this->debug) echo " verify NO updatekey\n"; if ($this->debug) echo " verify NO updatekey\n";
$after = $datasOK; $after = $dataOK;
} }
// Check if the unique constrain is valid before doing the insertion // Check if the unique constrain is valid before doing the insertion
@@ -487,14 +487,14 @@ class dblayer
// Before doing the insert, check the foreign keys. In update, they are // Before doing the insert, check the foreign keys. In update, they are
// not mandatory and are not checked for existancy. // not mandatory and are not checked for existancy.
if ($this->debug) echo " verify foreign $foreign\n"; if ($this->debug) echo " verify foreign $foreign\n";
if (! isset ($datas[$foreign])) if (! isset ($data[$foreign]))
{ {
$errors[] = array ("error", sprintf (dgettext("domframework", $errors[] = array ("error", sprintf (dgettext("domframework",
"The foreign column '%s' is not provided"), "The foreign column '%s' is not provided"),
$foreign)); $foreign));
return $errors; return $errors;
} }
if (! isset ($datas[$foreign][0])) if (! isset ($data[$foreign][0]))
{ {
$errors[] = array ("error", sprintf (dgettext("domframework", $errors[] = array ("error", sprintf (dgettext("domframework",
"The field type for column '%s' is not provided"), "The field type for column '%s' is not provided"),
@@ -515,7 +515,7 @@ class dblayer
"WHERE $this->sep$column$this->sep=:".md5 ($column); "WHERE $this->sep$column$this->sep=:".md5 ($column);
if ($this->debug) echo "DEBUG : $req\n"; if ($this->debug) echo "DEBUG : $req\n";
$st = self::$instance[$this->dsn]->prepare ($req); $st = self::$instance[$this->dsn]->prepare ($req);
$val = $datas[$foreign]; $val = $data[$foreign];
$key = $column; $key = $column;
if ($this->debug) echo "DEBUG BIND : ".$this->fields[$foreign][0]."\n"; if ($this->debug) echo "DEBUG BIND : ".$this->fields[$foreign][0]."\n";
if ($this->debug) echo "DEBUG BIND : $column(".md5 ($column)."->". if ($this->debug) echo "DEBUG BIND : $column(".md5 ($column)."->".
@@ -551,16 +551,16 @@ class dblayer
} }
/** Create a new entry in the table. Datas must be an indexed array /** Create a new entry in the table. Datas must be an indexed array
@param array $datas Datas to be recorded (column=>value) @param array $data Datas to be recorded (column=>value)
@obsolete 0.5 */ @obsolete 0.5 */
public function create ($datas) public function create ($data)
{ {
return $this->insert ($datas); return $this->insert ($data);
} }
/** Insert a new line of datas in the table. Datas must be an indexed array /** Insert a new line of data in the table. Datas must be an indexed array
@param array $datas Datas to be recorded (column=>value)*/ @param array $data Datas to be recorded (column=>value)*/
public function insert ($datas) public function insert ($data)
{ {
if ($this->debug) echo "== Entering insert\n"; if ($this->debug) echo "== Entering insert\n";
if ($this->sep === "") if ($this->sep === "")
@@ -572,19 +572,19 @@ class dblayer
if (! is_array ($this->unique)) if (! is_array ($this->unique))
throw new Exception (dgettext("domframework", throw new Exception (dgettext("domframework",
"The unique configuration is not an array"), 500); "The unique configuration is not an array"), 500);
if (!is_array ($datas)) if (!is_array ($data))
throw new Exception (dgettext("domframework", throw new Exception (dgettext("domframework",
"The datas provided to create are not array"), "The data provided to create are not array"),
405); 405);
foreach ($this->fields as $key=>$params) foreach ($this->fields as $key=>$params)
{ {
if (in_array ("autoincrement", $params)) if (in_array ("autoincrement", $params))
$datas[$key] = null; $data[$key] = null;
} }
if (!in_array ($this->primary, $this->unique)) if (!in_array ($this->primary, $this->unique))
$this->unique[] = $this->primary; $this->unique[] = $this->primary;
$datasOK = array (); $dataOK = array ();
$errors = $this->verify ($datas); $errors = $this->verify ($data);
if (count ($errors) !== 0) if (count ($errors) !== 0)
{ {
$errors = reset ($errors); $errors = reset ($errors);
@@ -592,23 +592,23 @@ class dblayer
} }
foreach ($this->fields as $field=>$desc) foreach ($this->fields as $field=>$desc)
{ {
if (isset ($datas[$field])) if (isset ($data[$field]))
$datasOK[$field] = $datas[$field]; $dataOK[$field] = $data[$field];
} }
$binds = array_keys ($datasOK); $binds = array_keys ($dataOK);
array_walk ($binds, function(&$value, $key) { array_walk ($binds, function(&$value, $key) {
$value = md5 ($value); $value = md5 ($value);
}); });
$datasOK = $this->hookpreinsert ($datasOK); $dataOK = $this->hookpreinsert ($dataOK);
$req = "INSERT INTO $this->sep$this->tableprefix$this->table$this->sep "; $req = "INSERT INTO $this->sep$this->tableprefix$this->table$this->sep ";
$req .= "($this->sep". $req .= "($this->sep".
implode ("$this->sep,$this->sep", array_keys ($datasOK)). implode ("$this->sep,$this->sep", array_keys ($dataOK)).
"$this->sep)"; "$this->sep)";
$req .= " VALUES "; $req .= " VALUES ";
$req .= "(:".implode (",:", $binds).")"; $req .= "(:".implode (",:", $binds).")";
if ($this->debug) echo "DEBUG : $req\n"; if ($this->debug) echo "DEBUG : $req\n";
$st = self::$instance[$this->dsn]->prepare ($req); $st = self::$instance[$this->dsn]->prepare ($req);
foreach ($datasOK as $key=>$val) foreach ($dataOK as $key=>$val)
{ {
if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->". if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->".
var_export ($val, TRUE)."\n"; var_export ($val, TRUE)."\n";
@@ -636,7 +636,7 @@ class dblayer
exit; exit;
} }
$lastID = self::$instance[$this->dsn]->lastInsertId(); $lastID = self::$instance[$this->dsn]->lastInsertId();
$lastID = $this->hookpostinsert ($datasOK, $lastID); $lastID = $this->hookpostinsert ($dataOK, $lastID);
return $lastID; return $lastID;
} }
@@ -807,13 +807,13 @@ class dblayer
return $res; return $res;
} }
/** Update the key tuple with the provided datas /** Update the key tuple with the provided data
Return the number of rows modified Return the number of rows modified
@param string|integer $updatekey The key applied on primary key to be @param string|integer $updatekey The key applied on primary key to be
updated updated
@param array $datas The values to be updated @param array $data The values to be updated
@return the number of lines modified */ @return the number of lines modified */
public function update ($updatekey, $datas) public function update ($updatekey, $data)
{ {
if ($this->debug) echo "== Entering update\n"; if ($this->debug) echo "== Entering update\n";
if ($this->sep === "") if ($this->sep === "")
@@ -823,11 +823,11 @@ class dblayer
throw new Exception (dgettext("domframework", "No Field defined"), 500); throw new Exception (dgettext("domframework", "No Field defined"), 500);
if ($this->primary === null) if ($this->primary === null)
throw new Exception (dgettext("domframework", "No Primary defined"), 500); throw new Exception (dgettext("domframework", "No Primary defined"), 500);
if (count ($datas) === 0) if (count ($data) === 0)
throw new Exception (dgettext("domframework", throw new Exception (dgettext("domframework",
"No data to update provided"), 500); "No data to update provided"), 500);
$datasOK = array (); $dataOK = array ();
$errors = $this->verify ($datas, $updatekey); $errors = $this->verify ($data, $updatekey);
if (count ($errors) !== 0) if (count ($errors) !== 0)
{ {
if (is_array ($errors)) if (is_array ($errors))
@@ -841,14 +841,14 @@ class dblayer
} }
foreach ($this->fields as $field=>$desc) foreach ($this->fields as $field=>$desc)
{ {
if (isset ($datas[$field])) if (isset ($data[$field]))
$datasOK[$field] = $datas[$field]; $dataOK[$field] = $data[$field];
} }
$datasOK = $this->hookpreupdate ($updatekey, $datasOK); $dataOK = $this->hookpreupdate ($updatekey, $dataOK);
$req = "UPDATE $this->sep".$this->tableprefix."$this->table$this->sep SET "; $req = "UPDATE $this->sep".$this->tableprefix."$this->table$this->sep SET ";
$i = 0; $i = 0;
foreach ($datasOK as $key=>$val) foreach ($dataOK as $key=>$val)
{ {
if ($i>0) $req .= ","; if ($i>0) $req .= ",";
$req .= "$this->sep$key$this->sep=:".md5 ($key); $req .= "$this->sep$key$this->sep=:".md5 ($key);
@@ -862,9 +862,9 @@ class dblayer
// Add the primary key to field list temporaly. It will permit to update the // Add the primary key to field list temporaly. It will permit to update the
// primary key // primary key
$fields = $this->fields; $fields = $this->fields;
$datasOK["PRIMARY".$this->primary] = $updatekey; $dataOK["PRIMARY".$this->primary] = $updatekey;
$fields["PRIMARY".$this->primary] = $this->fields[$this->primary]; $fields["PRIMARY".$this->primary] = $this->fields[$this->primary];
foreach ($datasOK as $key=>$val) foreach ($dataOK as $key=>$val)
{ {
if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->". if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->".
var_export ($val, TRUE)." "; var_export ($val, TRUE)." ";
@@ -902,7 +902,7 @@ class dblayer
$st->execute (); $st->execute ();
$nbLinesUpdated = $st->rowCount (); $nbLinesUpdated = $st->rowCount ();
$nbLinesUpdated = $this->hookpostupdate ($updatekey, $datasOK, $nbLinesUpdated = $this->hookpostupdate ($updatekey, $dataOK,
$nbLinesUpdated); $nbLinesUpdated);
return $nbLinesUpdated; return $nbLinesUpdated;
} }
@@ -1284,7 +1284,7 @@ class dblayer
} }
/** This function permit to send a SQL request to the database to do a SELECT /** This function permit to send a SQL request to the database to do a SELECT
Return the an array with the datas */ Return the an array with the data */
public function directRead ($sql) public function directRead ($sql)
{ {
if ($this->debug) echo "== Entering directRead\n"; if ($this->debug) echo "== Entering directRead\n";
@@ -1360,7 +1360,7 @@ class dblayer
This hook is run before inserting a new data in the database, after the This hook is run before inserting a new data in the database, after the
verification verification
@param array the data to insert in the database @param array the data to insert in the database
@return the modified datas */ @return the modified data */
public function hookpreinsert ($data) public function hookpreinsert ($data)
{ {
return $data; return $data;
@@ -1377,7 +1377,7 @@ class dblayer
/** Hook preupdate /** Hook preupdate
This hook is run before updating a data in the database, after the This hook is run before updating a data in the database, after the
verification verification
@return the modified datas */ @return the modified data */
public function hookpreupdate ($updatekey, $data) public function hookpreupdate ($updatekey, $data)
{ {
return $data; return $data;
@@ -1504,7 +1504,7 @@ class zone extends dbLayer
} }
ini_set ("date.timezone", "Europe/Paris"); ini_set ("date.timezone", "Europe/Paris");
$zone = new zone ("sqlite:datas/database.db"); $zone = new zone ("sqlite:data/database.db");
$last = $zone->create (array ("zone"=>"testZone", $last = $zone->create (array ("zone"=>"testZone",
"opendate"=>date("Y-m-d H:i:s"))); "opendate"=>date("Y-m-d H:i:s")));
//print_r ($zone->read ()); //print_r ($zone->read ());