dblayer : datas -> data
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2362 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
140
dblayer.php
140
dblayer.php
@@ -65,8 +65,8 @@ class dblayer
|
||||
@param string $val The value of the field to test */
|
||||
public function verifyOne ($field, $val) {}
|
||||
/** The verify global stack
|
||||
@param array $datas The associative array of contents */
|
||||
public function verifyAll ($datas) {}
|
||||
@param array $data The associative array of contents */
|
||||
public function verifyAll ($data) {}
|
||||
|
||||
// TODO !!
|
||||
/** Create Table creation from $this->fields with engine abstraction
|
||||
@@ -82,7 +82,7 @@ class dblayer
|
||||
// TODO !!
|
||||
/** Allow to modify tables if the definition is changed
|
||||
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 */
|
||||
// Based on an idea of http://tonylandis.com/php/php5-pdo-singleton-class/
|
||||
@@ -252,12 +252,12 @@ class dblayer
|
||||
return $res;
|
||||
}
|
||||
|
||||
/** Verify if the provided datas can be inserted/updated in the database.
|
||||
@param $datas An array containing the datas to verify with keys
|
||||
/** Verify if the provided data can be inserted/updated in the database.
|
||||
@param $data An array containing the data to verify with keys
|
||||
@param $updatekey the key to update
|
||||
@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";
|
||||
$errors = array ();
|
||||
@@ -269,15 +269,15 @@ class dblayer
|
||||
// Don't check if there is an update : the database is already filled
|
||||
// For autoincrement, in INSERT mode, force the value to null
|
||||
if (in_array ("autoincrement", $params))
|
||||
$datas[$key] = null;
|
||||
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
|
||||
$data[$key] = null;
|
||||
if (in_array ("not null", $params) && !array_key_exists ($key, $data))
|
||||
{
|
||||
$errors[$key] = array ("error", sprintf (dgettext("domframework",
|
||||
"Mandatory field '%s' not provided"),
|
||||
$key));
|
||||
continue;
|
||||
}
|
||||
if (in_array ("not null", $params) && $datas[$key] === "")
|
||||
if (in_array ("not null", $params) && $data[$key] === "")
|
||||
{
|
||||
$errors[$key] = array ("error", sprintf (dgettext("domframework",
|
||||
"Mandatory field '%s' is empty"),
|
||||
@@ -286,12 +286,12 @@ class dblayer
|
||||
}
|
||||
}
|
||||
|
||||
// Do not verify the non provided datas (if they are not mandatory)
|
||||
if (!array_key_exists ($key, $datas))
|
||||
// Do not verify the non provided data (if they are not mandatory)
|
||||
if (!array_key_exists ($key, $data))
|
||||
continue;
|
||||
|
||||
// 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))
|
||||
{
|
||||
$errors[$key] = array ($verify[0], $verify[1]);
|
||||
@@ -300,22 +300,22 @@ class dblayer
|
||||
}
|
||||
|
||||
// Check for type inconsistencies if the value is provided
|
||||
if (is_null ($datas[$key]))
|
||||
if (is_null ($data[$key]))
|
||||
{
|
||||
// Skipped the removed autoincrement keys
|
||||
continue;
|
||||
}
|
||||
elseif (! is_string ($datas[$key]) && ! is_integer ($datas[$key]))
|
||||
elseif (! is_string ($data[$key]) && ! is_integer ($data[$key]))
|
||||
{
|
||||
$errors[$key] = array ("error", sprintf (
|
||||
dgettext("domframework",
|
||||
"Errors in consistency : '%s' is not an integer or a string [is %s]"),
|
||||
$key, gettype ($datas[$key])));
|
||||
$key, gettype ($data[$key])));
|
||||
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 (
|
||||
dgettext("domframework",
|
||||
@@ -324,7 +324,7 @@ class dblayer
|
||||
continue;
|
||||
}
|
||||
}
|
||||
elseif ($datas[$key] !== "" && $params[0] === "varchar")
|
||||
elseif ($data[$key] !== "" && $params[0] === "varchar")
|
||||
{
|
||||
if (! isset ($params[1]))
|
||||
{
|
||||
@@ -334,7 +334,7 @@ class dblayer
|
||||
$key));
|
||||
continue;
|
||||
}
|
||||
if (strlen ($datas[$key]) > $params[1])
|
||||
if (strlen ($data[$key]) > $params[1])
|
||||
{
|
||||
$errors[$key] = array ("error", sprintf (
|
||||
dgettext("domframework",
|
||||
@@ -343,11 +343,11 @@ class dblayer
|
||||
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
|
||||
$d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]);
|
||||
if (!$d || $d->format("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") !== $data[$key])
|
||||
{
|
||||
$errors[$key] = array ("error", sprintf (
|
||||
dgettext("domframework",
|
||||
@@ -356,11 +356,11 @@ class dblayer
|
||||
continue;
|
||||
}
|
||||
}
|
||||
elseif ($datas[$key] !== "" && $params[0] === "date")
|
||||
elseif ($data[$key] !== "" && $params[0] === "date")
|
||||
{
|
||||
// The date format must be in ANSI SQL : YYYY-MM-DD
|
||||
$d = DateTime::createFromFormat("Y-m-d", $datas[$key]);
|
||||
if (!$d || $d->format("Y-m-d") !== $datas[$key])
|
||||
$d = DateTime::createFromFormat("Y-m-d", $data[$key]);
|
||||
if (!$d || $d->format("Y-m-d") !== $data[$key])
|
||||
{
|
||||
$errors[$key] = array ("error", sprintf (
|
||||
dgettext("domframework",
|
||||
@@ -369,7 +369,7 @@ class dblayer
|
||||
continue;
|
||||
}
|
||||
}
|
||||
elseif ($datas[$key] !== "")
|
||||
elseif ($data[$key] !== "")
|
||||
{
|
||||
$errors[$key] = array ("error", sprintf (dgettext("domframework",
|
||||
"Unknown field type for '%s'"), $key));
|
||||
@@ -386,15 +386,15 @@ class dblayer
|
||||
|
||||
if ($this->debug) echo " verify inconsistency\n";
|
||||
// Check for inconsistency
|
||||
$verify = $this->verifyAll ($datas);
|
||||
$verify = $this->verifyAll ($data);
|
||||
if (count ($verify))
|
||||
return $verify;
|
||||
|
||||
$datasOK = array ();
|
||||
$dataOK = array ();
|
||||
foreach ($this->fields as $field=>$desc)
|
||||
{
|
||||
if (isset ($datas[$field]))
|
||||
$datasOK[$field] = $datas[$field];
|
||||
if (isset ($data[$field]))
|
||||
$dataOK[$field] = $data[$field];
|
||||
}
|
||||
|
||||
if ($updatekey !== false)
|
||||
@@ -409,13 +409,13 @@ class dblayer
|
||||
$before = reset ($before);
|
||||
// 2. Map the proposal entries into the before state
|
||||
$after = $before;
|
||||
foreach ($datasOK as $field=>$val)
|
||||
foreach ($dataOK as $field=>$val)
|
||||
$after[$field] = $val;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($this->debug) echo " verify NO updatekey\n";
|
||||
$after = $datasOK;
|
||||
$after = $dataOK;
|
||||
|
||||
}
|
||||
// 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
|
||||
// not mandatory and are not checked for existancy.
|
||||
if ($this->debug) echo " verify foreign $foreign\n";
|
||||
if (! isset ($datas[$foreign]))
|
||||
if (! isset ($data[$foreign]))
|
||||
{
|
||||
$errors[] = array ("error", sprintf (dgettext("domframework",
|
||||
"The foreign column '%s' is not provided"),
|
||||
$foreign));
|
||||
return $errors;
|
||||
}
|
||||
if (! isset ($datas[$foreign][0]))
|
||||
if (! isset ($data[$foreign][0]))
|
||||
{
|
||||
$errors[] = array ("error", sprintf (dgettext("domframework",
|
||||
"The field type for column '%s' is not provided"),
|
||||
@@ -515,7 +515,7 @@ class dblayer
|
||||
"WHERE $this->sep$column$this->sep=:".md5 ($column);
|
||||
if ($this->debug) echo "DEBUG : $req\n";
|
||||
$st = self::$instance[$this->dsn]->prepare ($req);
|
||||
$val = $datas[$foreign];
|
||||
$val = $data[$foreign];
|
||||
$key = $column;
|
||||
if ($this->debug) echo "DEBUG BIND : ".$this->fields[$foreign][0]."\n";
|
||||
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
|
||||
@param array $datas Datas to be recorded (column=>value)
|
||||
@param array $data Datas to be recorded (column=>value)
|
||||
@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
|
||||
@param array $datas Datas to be recorded (column=>value)*/
|
||||
public function insert ($datas)
|
||||
/** Insert a new line of data in the table. Datas must be an indexed array
|
||||
@param array $data Datas to be recorded (column=>value)*/
|
||||
public function insert ($data)
|
||||
{
|
||||
if ($this->debug) echo "== Entering insert\n";
|
||||
if ($this->sep === "")
|
||||
@@ -572,19 +572,19 @@ class dblayer
|
||||
if (! is_array ($this->unique))
|
||||
throw new Exception (dgettext("domframework",
|
||||
"The unique configuration is not an array"), 500);
|
||||
if (!is_array ($datas))
|
||||
if (!is_array ($data))
|
||||
throw new Exception (dgettext("domframework",
|
||||
"The datas provided to create are not array"),
|
||||
"The data provided to create are not array"),
|
||||
405);
|
||||
foreach ($this->fields as $key=>$params)
|
||||
{
|
||||
if (in_array ("autoincrement", $params))
|
||||
$datas[$key] = null;
|
||||
$data[$key] = null;
|
||||
}
|
||||
if (!in_array ($this->primary, $this->unique))
|
||||
$this->unique[] = $this->primary;
|
||||
$datasOK = array ();
|
||||
$errors = $this->verify ($datas);
|
||||
$dataOK = array ();
|
||||
$errors = $this->verify ($data);
|
||||
if (count ($errors) !== 0)
|
||||
{
|
||||
$errors = reset ($errors);
|
||||
@@ -592,23 +592,23 @@ class dblayer
|
||||
}
|
||||
foreach ($this->fields as $field=>$desc)
|
||||
{
|
||||
if (isset ($datas[$field]))
|
||||
$datasOK[$field] = $datas[$field];
|
||||
if (isset ($data[$field]))
|
||||
$dataOK[$field] = $data[$field];
|
||||
}
|
||||
$binds = array_keys ($datasOK);
|
||||
$binds = array_keys ($dataOK);
|
||||
array_walk ($binds, function(&$value, $key) {
|
||||
$value = md5 ($value);
|
||||
});
|
||||
$datasOK = $this->hookpreinsert ($datasOK);
|
||||
$dataOK = $this->hookpreinsert ($dataOK);
|
||||
$req = "INSERT INTO $this->sep$this->tableprefix$this->table$this->sep ";
|
||||
$req .= "($this->sep".
|
||||
implode ("$this->sep,$this->sep", array_keys ($datasOK)).
|
||||
implode ("$this->sep,$this->sep", array_keys ($dataOK)).
|
||||
"$this->sep)";
|
||||
$req .= " VALUES ";
|
||||
$req .= "(:".implode (",:", $binds).")";
|
||||
if ($this->debug) echo "DEBUG : $req\n";
|
||||
$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).")->".
|
||||
var_export ($val, TRUE)."\n";
|
||||
@@ -636,7 +636,7 @@ class dblayer
|
||||
exit;
|
||||
}
|
||||
$lastID = self::$instance[$this->dsn]->lastInsertId();
|
||||
$lastID = $this->hookpostinsert ($datasOK, $lastID);
|
||||
$lastID = $this->hookpostinsert ($dataOK, $lastID);
|
||||
return $lastID;
|
||||
}
|
||||
|
||||
@@ -807,13 +807,13 @@ class dblayer
|
||||
return $res;
|
||||
}
|
||||
|
||||
/** Update the key tuple with the provided datas
|
||||
/** Update the key tuple with the provided data
|
||||
Return the number of rows modified
|
||||
@param string|integer $updatekey The key applied on primary key to be
|
||||
updated
|
||||
@param array $datas The values to be updated
|
||||
@param array $data The values to be updated
|
||||
@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->sep === "")
|
||||
@@ -823,11 +823,11 @@ class dblayer
|
||||
throw new Exception (dgettext("domframework", "No Field defined"), 500);
|
||||
if ($this->primary === null)
|
||||
throw new Exception (dgettext("domframework", "No Primary defined"), 500);
|
||||
if (count ($datas) === 0)
|
||||
if (count ($data) === 0)
|
||||
throw new Exception (dgettext("domframework",
|
||||
"No data to update provided"), 500);
|
||||
$datasOK = array ();
|
||||
$errors = $this->verify ($datas, $updatekey);
|
||||
$dataOK = array ();
|
||||
$errors = $this->verify ($data, $updatekey);
|
||||
if (count ($errors) !== 0)
|
||||
{
|
||||
if (is_array ($errors))
|
||||
@@ -841,14 +841,14 @@ class dblayer
|
||||
}
|
||||
foreach ($this->fields as $field=>$desc)
|
||||
{
|
||||
if (isset ($datas[$field]))
|
||||
$datasOK[$field] = $datas[$field];
|
||||
if (isset ($data[$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 ";
|
||||
$i = 0;
|
||||
foreach ($datasOK as $key=>$val)
|
||||
foreach ($dataOK as $key=>$val)
|
||||
{
|
||||
if ($i>0) $req .= ",";
|
||||
$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
|
||||
// primary key
|
||||
$fields = $this->fields;
|
||||
$datasOK["PRIMARY".$this->primary] = $updatekey;
|
||||
$dataOK["PRIMARY".$this->primary] = $updatekey;
|
||||
$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).")->".
|
||||
var_export ($val, TRUE)." ";
|
||||
@@ -902,7 +902,7 @@ class dblayer
|
||||
|
||||
$st->execute ();
|
||||
$nbLinesUpdated = $st->rowCount ();
|
||||
$nbLinesUpdated = $this->hookpostupdate ($updatekey, $datasOK,
|
||||
$nbLinesUpdated = $this->hookpostupdate ($updatekey, $dataOK,
|
||||
$nbLinesUpdated);
|
||||
return $nbLinesUpdated;
|
||||
}
|
||||
@@ -1284,7 +1284,7 @@ class dblayer
|
||||
}
|
||||
|
||||
/** 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)
|
||||
{
|
||||
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
|
||||
verification
|
||||
@param array the data to insert in the database
|
||||
@return the modified datas */
|
||||
@return the modified data */
|
||||
public function hookpreinsert ($data)
|
||||
{
|
||||
return $data;
|
||||
@@ -1377,7 +1377,7 @@ class dblayer
|
||||
/** Hook preupdate
|
||||
This hook is run before updating a data in the database, after the
|
||||
verification
|
||||
@return the modified datas */
|
||||
@return the modified data */
|
||||
public function hookpreupdate ($updatekey, $data)
|
||||
{
|
||||
return $data;
|
||||
@@ -1504,7 +1504,7 @@ class zone extends dbLayer
|
||||
}
|
||||
|
||||
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",
|
||||
"opendate"=>date("Y-m-d H:i:s")));
|
||||
//print_r ($zone->read ());
|
||||
|
||||
Reference in New Issue
Block a user