From 5e03191c3d6678e138fc7b5edd9cbffe6598490a Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 27 May 2017 09:41:52 +0200 Subject: [PATCH] Updated DB library --- RestControllers/searchController.php | 10 +++++++--- classes/DBLibrary.php | 14 ++++++++++++-- classes/components/searchUser.php | 23 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 classes/components/searchUser.php diff --git a/RestControllers/searchController.php b/RestControllers/searchController.php index 77fe573..22a3d37 100644 --- a/RestControllers/searchController.php +++ b/RestControllers/searchController.php @@ -5,7 +5,7 @@ * @author Pierre HUBERT */ -class userController +class searchController { /** * Peform a research on the database @@ -18,13 +18,17 @@ class userController Rest_fatal_error(400, "Please specify search terms"); //Check for search limit - $seachLimit = (isset($_POST['searchLimit']) ? $_POST['searchLimit']*1 : 5); + $searchLimit = (isset($_POST['searchLimit']) ? $_POST['searchLimit']*1 : 5); //Check the limit - if($seachLimit < 1 || $seachLimit > 25) + if($searchLimit < 1 || $searchLimit > 25) Rest_fatal_error(401, "Invalid search limit !"); //Perform research on the database and return results + if(!$results = CS::get()->components->searchUser->search($_POST['query'], $searchLimit)) + Rest_fatal_error(500, "An error occured while trying to perform a research in user list !"); + //Return results + return $results; } } \ No newline at end of file diff --git a/classes/DBLibrary.php b/classes/DBLibrary.php index 832c6b9..ac86f6d 100755 --- a/classes/DBLibrary.php +++ b/classes/DBLibrary.php @@ -317,9 +317,10 @@ class DBLibrary { * @param String $tableName The name of the table * @param String $conditions The conditions * @param Array $datasCond The values of condition + * @param Array $fieldsList Optionnal, specify the fields to select during the request. * @return Array The result */ - public function select($tableName, $conditions = "", array $datasCond = array()){ + public function select($tableName, $conditions = "", array $datasCond = array(), array $fieldsList = array()){ //We try to perform the task try{ //We check if any database is opened @@ -327,8 +328,17 @@ class DBLibrary { throw new Exception("There isn't any opened DataBase !"); } + //Process fields to select + if(count($fieldsList) == 0) + $fields = "*"; + else { + $fields = ""; + foreach($fieldsList as $processField) + $fields .= $processField.", "; + } + //Generating SQL - $sql = "SELECT * FROM ".$tableName." ".$conditions; + $sql = "SELECT ".$fields." FROM ".$tableName." ".$conditions; $selectOBJ = $this->db->prepare($sql); $selectOBJ->execute($datasCond); diff --git a/classes/components/searchUser.php b/classes/components/searchUser.php new file mode 100644 index 0000000..02fa18c --- /dev/null +++ b/classes/components/searchUser.php @@ -0,0 +1,23 @@ +