dblayer : add support to hooks (pre|post)(insert|update|delete)
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2042 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
67
dblayer.php
67
dblayer.php
@@ -551,6 +551,7 @@ class dblayer extends PDO
|
|||||||
array_walk ($binds, function(&$value, $key) {
|
array_walk ($binds, function(&$value, $key) {
|
||||||
$value = md5 ($value);
|
$value = md5 ($value);
|
||||||
});
|
});
|
||||||
|
$datasOK = $this->pre-insert ($datasOK);
|
||||||
$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 ($datasOK)).
|
||||||
@@ -586,7 +587,9 @@ class dblayer extends PDO
|
|||||||
echo "dblayer execute exception : ".$e->getMessage()."\n";
|
echo "dblayer execute exception : ".$e->getMessage()."\n";
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
return $this->lastInsertId();
|
$lastID = $this->lastInsertId();
|
||||||
|
$lastID = $this->hookpostinsert ($datasOK, $lastID);
|
||||||
|
return $lastID;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read the table content based on a select filter, ordered by order
|
/** Read the table content based on a select filter, ordered by order
|
||||||
@@ -785,6 +788,7 @@ class dblayer extends PDO
|
|||||||
$datasOK[$field] = $datas[$field];
|
$datasOK[$field] = $datas[$field];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$datasOK = $this->hookpreupdate ($updatekey, $datasOK);
|
||||||
$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 ($datasOK as $key=>$val)
|
||||||
@@ -840,7 +844,10 @@ class dblayer extends PDO
|
|||||||
}
|
}
|
||||||
|
|
||||||
$st->execute ();
|
$st->execute ();
|
||||||
return $st->rowCount ();
|
$nbLinesUpdated = $st->rowCount ();
|
||||||
|
$nbLinesUpdated = $this->hookpostupdate ($updatekey, $datasOK,
|
||||||
|
$nbLinesUpdated);
|
||||||
|
return $nbLinesUpdated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete a tuple identified by its primary key
|
/** Delete a tuple identified by its primary key
|
||||||
@@ -851,6 +858,7 @@ class dblayer extends PDO
|
|||||||
if ($this->debug) echo "== Entering delete\n";
|
if ($this->debug) echo "== Entering delete\n";
|
||||||
if ($this->sep === "")
|
if ($this->sep === "")
|
||||||
throw new Exception (dgettext("domframework", "Database not connected"));
|
throw new Exception (dgettext("domframework", "Database not connected"));
|
||||||
|
$deletekey = $this->hookpredelete ($deletekey);
|
||||||
$req = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep ";
|
$req = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep ";
|
||||||
$req .= "WHERE $this->primary = :primary";
|
$req .= "WHERE $this->primary = :primary";
|
||||||
$st = $this->prepare ($req);
|
$st = $this->prepare ($req);
|
||||||
@@ -859,7 +867,9 @@ class dblayer extends PDO
|
|||||||
var_export ($deletekey, TRUE)."\n";
|
var_export ($deletekey, TRUE)."\n";
|
||||||
$st->bindValue (":primary", $deletekey);
|
$st->bindValue (":primary", $deletekey);
|
||||||
$st->execute ();
|
$st->execute ();
|
||||||
return $st->rowCount();
|
$nbLinesDeleted = $st->rowCount();
|
||||||
|
$nbLinesDeleted = $this->hookpostdelete ($deletekey, $nbLinesDeleted);
|
||||||
|
return $nbLinesDeleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Translation of fields */
|
/** Translation of fields */
|
||||||
@@ -1225,6 +1235,57 @@ class dblayer extends PDO
|
|||||||
$res[] = $d;
|
$res[] = $d;
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Hook preinsert
|
||||||
|
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 */
|
||||||
|
public function hookpreinsert ($data)
|
||||||
|
{
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Hook postinsert
|
||||||
|
This hook is run after successfuly insert a new data in the database
|
||||||
|
@return the modified lastID */
|
||||||
|
public function hookpostinsert ($data, $lastID)
|
||||||
|
{
|
||||||
|
return $lastID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Hook preupdate
|
||||||
|
This hook is run before updating a data in the database, after the
|
||||||
|
verification
|
||||||
|
@return the modified datas */
|
||||||
|
public function hookpreupdate ($updatekey, $data)
|
||||||
|
{
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Hook postupdate
|
||||||
|
This hook is run after successfuly update a data in the database
|
||||||
|
@return the modified $nbLinesUpdated */
|
||||||
|
public function hookpostupdate ($updatekey, $data, $nbLinesUpdated)
|
||||||
|
{
|
||||||
|
return $nbLinesUpdated;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Hook predelete
|
||||||
|
This hook is run before deleting a data in the database
|
||||||
|
@return the modified $deletekey */
|
||||||
|
public function hookpredelete ($deletekey)
|
||||||
|
{
|
||||||
|
return $deletekey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Hook postdelete
|
||||||
|
This hook is run after successfuly deleting a data in the database
|
||||||
|
@return $nbLinesUpdated */
|
||||||
|
public function hookpostdelete ($deletekey, $nbLinesDeleted)
|
||||||
|
{
|
||||||
|
return $nbLinesDeleted;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** POC :
|
/** POC :
|
||||||
|
|||||||
Reference in New Issue
Block a user