From a92f32c1c434ee17e2516f5b156eafee840c6c0b Mon Sep 17 00:00:00 2001 From: shubham_meena Date: Tue, 27 May 2014 14:19:37 +0530 Subject: [PATCH] few more changes to the CRUD layer in ams --HG-- branch : Gsoc14-ryzomAppImprovements --- .../ryzom_ams/ams_lib/autoload/dblayer.php | 69 +++++++++++++++---- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php index b095b4dc7..b82e7ea8f 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php @@ -67,34 +67,74 @@ class DBLayer{ return $statement; } - /** + /** * execute a query (an insertion query) that has parameters and return the id of it's insertion * @param $query the mysql query * @param $params the parameters that are being used by the query * @return returns the id of the last inserted element. */ - public function executeReturnId($query,$params){ - $statement = $this->PDO->prepare($query); - $this->PDO->beginTransaction(); - $statement->execute($params); - $lastId =$this->PDO->lastInsertId(); - $this->PDO->commit(); - return $lastId; + public function executeReturnId($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(); + $lastId =$this->PDO->lastInsertId(); + $this->PDO->commit(); + }catch (Exception $e) + { + //for rolling back the changes during transaction + $this->PDO->rollBack(); + throw new Exception("error in inseting"); + } + 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 + * @return statement object */ - public function select($query, $data) - { + public function selectWithParameter($param, $tb_name, $data, $where) + { try{ - $sth = $this->PDO->prepare($query); + $sth = $this->PDO->prepare("SELECT $param FROM $tb_name WHERE $where"); + $this->PDO->beginTransaction(); + $sth->execute($data); + $this->PDO->commit(); + }catch(Exception $e) + { + $this->PDO->rollBack(); + throw new Exception("error selection"); + return false; + } + return $sth; + } + + /** + * + * 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 statement object + */ + public function select($tb_name, $data ,$where) + { + try{ + $sth = $this->PDO->prepare("SELECT * FROM $tb_name WHERE $where"); $this->PDO->beginTransaction(); - $sth->execute(array($data)); + $sth->execute($data); $this->PDO->commit(); }catch(Exception $e) { @@ -152,7 +192,6 @@ class DBLayer{ $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();