CRUD layer for AMS
--HG-- branch : Gsoc14-ryzomAppImprovements
This commit is contained in:
parent
63e30cb2a9
commit
23c0a8bc24
1 changed files with 110 additions and 2 deletions
|
@ -81,5 +81,113 @@ class DBLayer{
|
|||
$this->PDO->commit();
|
||||
return $lastId;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* Select function using prepared statement
|
||||
* @param string $tb_name Table Name to Select
|
||||
* @param array $data Associative array
|
||||
* @param string $where where to select
|
||||
* @return array Array containing fetched data
|
||||
*/
|
||||
public function select($query, $data)
|
||||
{
|
||||
try{
|
||||
$sth = $this->PDO->prepare($query);
|
||||
$this->PDO->beginTransaction();
|
||||
$sth->execute(array($data));
|
||||
$this->PDO->commit();
|
||||
}catch(Exception $e)
|
||||
{
|
||||
$this->PDO->rollBack();
|
||||
throw new Exception("error selection");
|
||||
return false;
|
||||
}
|
||||
return $sth;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Update function with prepared statement
|
||||
* @param string $tb_name name of the table
|
||||
* @param array $data associative array with values
|
||||
* @param string $where where part
|
||||
* @throws Exception error in updating
|
||||
*/
|
||||
public function update($tb_name, $data, $where)
|
||||
{
|
||||
$field_option_values=null;
|
||||
foreach ($data as $key => $value)
|
||||
{
|
||||
$field_option_values.=",$key".'=:'.$value;
|
||||
}
|
||||
$field_option_values = ltrim($field_option_values,',');
|
||||
try {
|
||||
$sth = $this->PDO->prepare("UPDATE $tb_name SET $field_option_values WHERE $where ");
|
||||
|
||||
foreach ($data as $key => $value)
|
||||
{
|
||||
$sth->bindValue(":$key", $value);
|
||||
}
|
||||
$this->PDO->beginTransaction();
|
||||
$sth->execute();
|
||||
$this->PDO->commit();
|
||||
}catch (Exception $e)
|
||||
{
|
||||
$this->PDO->rollBack();
|
||||
throw new Exception('error in updating');
|
||||
}
|
||||
}
|
||||
/**
|
||||
*
|
||||
* insert function using prepared statements
|
||||
* @param string $tb_name Name of the table to insert in
|
||||
* @param array $data Associative array of data to insert
|
||||
*/
|
||||
|
||||
public function insert($tb_name, $data)
|
||||
{
|
||||
$field_values =':'. implode(',:', array_keys($data));
|
||||
$field_options = implode(',', array_keys($data));
|
||||
try{
|
||||
$sth = $this->PDO->prepare("INSERT INTO $tb_name ($field_options) VALUE ($field_values)");
|
||||
foreach ($data as $key => $value )
|
||||
{
|
||||
|
||||
$sth->bindValue(":$key", $value);
|
||||
}
|
||||
$this->PDO->beginTransaction();
|
||||
//execution
|
||||
$sth->execute();
|
||||
$this->PDO->commit();
|
||||
|
||||
}catch (Exception $e)
|
||||
{
|
||||
//for rolling back the changes during transaction
|
||||
$this->PDO->rollBack();
|
||||
throw new Exception("error in inseting");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Delete database entery using prepared statement
|
||||
* @param string $tb_name
|
||||
* @param string $where
|
||||
* @throws error in deleting
|
||||
*/
|
||||
public function delete($tb_name, $where)
|
||||
{
|
||||
try {
|
||||
$sth = $this->prepare("DELETE FROM $tb_name WHERE $where");
|
||||
$this->PDO->beginTransaction();
|
||||
$sth->execute();
|
||||
$this->PDO->commit();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$this->rollBack();
|
||||
throw new Exception("error in deleting");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue