@@ -18,132 +18,132 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | class DatabaseHandler { |
| 20 | 20 | |
| 21 | - private $tableFields; |
|
| 22 | - private $dbHelperInstance; |
|
| 23 | - private $dbConnection; |
|
| 24 | - private $model; |
|
| 21 | + private $tableFields; |
|
| 22 | + private $dbHelperInstance; |
|
| 23 | + private $dbConnection; |
|
| 24 | + private $model; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * This is a constructor; a default method that will be called automatically during class instantiation |
|
| 28 | - */ |
|
| 29 | - public function __construct($modelClassName, $dbConn = Null) |
|
| 30 | - { |
|
| 26 | + /** |
|
| 27 | + * This is a constructor; a default method that will be called automatically during class instantiation |
|
| 28 | + */ |
|
| 29 | + public function __construct($modelClassName, $dbConn = Null) |
|
| 30 | + { |
|
| 31 | 31 | |
| 32 | - if (is_null($dbConn)) { |
|
| 33 | - $this->dbConnection = new DatabaseConnection(); |
|
| 32 | + if (is_null($dbConn)) { |
|
| 33 | + $this->dbConnection = new DatabaseConnection(); |
|
| 34 | 34 | |
| 35 | - } else { |
|
| 36 | - $this->dbConnection = $dbConn; |
|
| 35 | + } else { |
|
| 36 | + $this->dbConnection = $dbConn; |
|
| 37 | 37 | |
| 38 | - } |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - $this->model = $modelClassName; |
|
| 41 | - } |
|
| 40 | + $this->model = $modelClassName; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * This method create a record and store it in a table row |
|
| 45 | - * @params associative array, string tablename |
|
| 46 | - * @return boolean true or false |
|
| 47 | - */ |
|
| 48 | - public function create($associative1DArray, $tableName, $dbConn = Null) |
|
| 49 | - { |
|
| 50 | - $tableFields = $this->getColumnNames($this->model, $this->dbConnection); |
|
| 51 | - |
|
| 52 | - $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields, $associative1DArray); |
|
| 43 | + /** |
|
| 44 | + * This method create a record and store it in a table row |
|
| 45 | + * @params associative array, string tablename |
|
| 46 | + * @return boolean true or false |
|
| 47 | + */ |
|
| 48 | + public function create($associative1DArray, $tableName, $dbConn = Null) |
|
| 49 | + { |
|
| 50 | + $tableFields = $this->getColumnNames($this->model, $this->dbConnection); |
|
| 51 | + |
|
| 52 | + $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields, $associative1DArray); |
|
| 53 | 53 | |
| 54 | - if (count($unexpectedFields) > 0) { |
|
| 55 | - throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields,"needs to be created as a table field"); |
|
| 56 | - } |
|
| 54 | + if (count($unexpectedFields) > 0) { |
|
| 55 | + throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields,"needs to be created as a table field"); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - unset($associative1DArray[0]); |
|
| 58 | + unset($associative1DArray[0]); |
|
| 59 | 59 | |
| 60 | - if (is_null($dbConn)) { |
|
| 61 | - $dbConn = $this->dbConnection; |
|
| 60 | + if (is_null($dbConn)) { |
|
| 61 | + $dbConn = $this->dbConnection; |
|
| 62 | 62 | |
| 63 | - } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
| 65 | + return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
| 66 | 66 | |
| 67 | - } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * This method runs the insertion query |
|
| 71 | - * @param $dbConn |
|
| 72 | - * @param $tableName |
|
| 73 | - * @param $associative1DArray |
|
| 74 | - * @return boolean true |
|
| 75 | - */ |
|
| 76 | - private function insertRecord($dbConn, $tableName, $associative1DArray) |
|
| 77 | - { |
|
| 78 | - $insertQuery = 'INSERT INTO '.$tableName; |
|
| 69 | + /** |
|
| 70 | + * This method runs the insertion query |
|
| 71 | + * @param $dbConn |
|
| 72 | + * @param $tableName |
|
| 73 | + * @param $associative1DArray |
|
| 74 | + * @return boolean true |
|
| 75 | + */ |
|
| 76 | + private function insertRecord($dbConn, $tableName, $associative1DArray) |
|
| 77 | + { |
|
| 78 | + $insertQuery = 'INSERT INTO '.$tableName; |
|
| 79 | 79 | |
| 80 | - $TableValues = implode(',',array_keys($associative1DArray)); |
|
| 80 | + $TableValues = implode(',',array_keys($associative1DArray)); |
|
| 81 | 81 | |
| 82 | - foreach ($associative1DArray as $field => $value) { |
|
| 83 | - $FormValues[] = "'".trim(addslashes($value))."'"; |
|
| 84 | - } |
|
| 82 | + foreach ($associative1DArray as $field => $value) { |
|
| 83 | + $FormValues[] = "'".trim(addslashes($value))."'"; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - $splittedTableValues = implode(',', $FormValues); |
|
| 86 | + $splittedTableValues = implode(',', $FormValues); |
|
| 87 | 87 | |
| 88 | - $insertQuery.= ' ('.$TableValues.')'; |
|
| 89 | - $insertQuery.= ' VALUES ('.$splittedTableValues.')'; |
|
| 88 | + $insertQuery.= ' ('.$TableValues.')'; |
|
| 89 | + $insertQuery.= ' VALUES ('.$splittedTableValues.')'; |
|
| 90 | 90 | |
| 91 | - $executeQuery = $dbConn->exec($insertQuery); |
|
| 91 | + $executeQuery = $dbConn->exec($insertQuery); |
|
| 92 | 92 | |
| 93 | - if ($executeQuery) { |
|
| 94 | - return true; |
|
| 95 | - } |
|
| 93 | + if ($executeQuery) { |
|
| 94 | + return true; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - return false; |
|
| 97 | + return false; |
|
| 98 | 98 | |
| 99 | - throw NoRecordInsertionException::checkNoRecordAddedException("Record not inserted successfully"); |
|
| 100 | - } |
|
| 99 | + throw NoRecordInsertionException::checkNoRecordAddedException("Record not inserted successfully"); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * This method updates any table by supplying 3 parameter |
|
| 104 | - * @params: $updateParams, $tableName, $associative1DArray |
|
| 105 | - * @return boolean true or false |
|
| 106 | - */ |
|
| 107 | - public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null) |
|
| 108 | - { |
|
| 109 | - $sql = ""; |
|
| 102 | + /** |
|
| 103 | + * This method updates any table by supplying 3 parameter |
|
| 104 | + * @params: $updateParams, $tableName, $associative1DArray |
|
| 105 | + * @return boolean true or false |
|
| 106 | + */ |
|
| 107 | + public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null) |
|
| 108 | + { |
|
| 109 | + $sql = ""; |
|
| 110 | 110 | |
| 111 | - if (is_null($dbConn)) { |
|
| 112 | - $dbConn = $this->dbConnection; |
|
| 113 | - } |
|
| 111 | + if (is_null($dbConn)) { |
|
| 112 | + $dbConn = $this->dbConnection; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - $updateSql = "UPDATE `$tableName` SET "; |
|
| 115 | + $updateSql = "UPDATE `$tableName` SET "; |
|
| 116 | 116 | |
| 117 | - unset($associative1DArray['id']); |
|
| 117 | + unset($associative1DArray['id']); |
|
| 118 | 118 | |
| 119 | - $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection), $associative1DArray); |
|
| 119 | + $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection), $associative1DArray); |
|
| 120 | 120 | |
| 121 | - if (count($unexpectedFields) > 0) { |
|
| 122 | - throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as a table field"); |
|
| 123 | - } |
|
| 121 | + if (count($unexpectedFields) > 0) { |
|
| 122 | + throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as a table field"); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - foreach ($associative1DArray as $field => $value) { |
|
| 126 | - $sql .= "`$field` = '$value'".","; |
|
| 127 | - } |
|
| 125 | + foreach ($associative1DArray as $field => $value) { |
|
| 126 | + $sql .= "`$field` = '$value'".","; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - $updateSql .= $this->prepareUpdateQuery($sql); |
|
| 129 | + $updateSql .= $this->prepareUpdateQuery($sql); |
|
| 130 | 130 | |
| 131 | - foreach ($updateParams as $key => $val) { |
|
| 132 | - $updateSql .= " WHERE $key = $val"; |
|
| 133 | - } |
|
| 131 | + foreach ($updateParams as $key => $val) { |
|
| 132 | + $updateSql .= " WHERE $key = $val"; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - $stmt = $dbConn->prepare($updateSql); |
|
| 135 | + $stmt = $dbConn->prepare($updateSql); |
|
| 136 | 136 | |
| 137 | - $boolResponse = $stmt->execute(); |
|
| 137 | + $boolResponse = $stmt->execute(); |
|
| 138 | 138 | |
| 139 | - if ($boolResponse) { |
|
| 140 | - return true; |
|
| 141 | - } |
|
| 139 | + if ($boolResponse) { |
|
| 140 | + return true; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - return false; |
|
| 143 | + return false; |
|
| 144 | 144 | |
| 145 | - throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
| 146 | - } |
|
| 145 | + throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * This method retrieves record from a table |
@@ -152,32 +152,32 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | public static function read($id, $tableName, $dbConn = null) |
| 154 | 154 | { |
| 155 | - $tableData = []; |
|
| 155 | + $tableData = []; |
|
| 156 | 156 | |
| 157 | - if (is_null($dbConn)) { |
|
| 158 | - $dbConn = new DatabaseConnection(); |
|
| 159 | - } |
|
| 157 | + if (is_null($dbConn)) { |
|
| 158 | + $dbConn = new DatabaseConnection(); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
|
| 161 | + $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
|
| 162 | 162 | |
| 163 | - try { |
|
| 164 | - $stmt = $dbConn->prepare($sql); |
|
| 165 | - $stmt->bindValue(':table', $tableName); |
|
| 166 | - $stmt->bindValue(':id', $id); |
|
| 167 | - $stmt->execute(); |
|
| 163 | + try { |
|
| 164 | + $stmt = $dbConn->prepare($sql); |
|
| 165 | + $stmt->bindValue(':table', $tableName); |
|
| 166 | + $stmt->bindValue(':id', $id); |
|
| 167 | + $stmt->execute(); |
|
| 168 | 168 | |
| 169 | - } catch (PDOException $e) { |
|
| 170 | - return $e->getMessage(); |
|
| 169 | + } catch (PDOException $e) { |
|
| 170 | + return $e->getMessage(); |
|
| 171 | 171 | |
| 172 | - } |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
| 174 | + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
| 175 | 175 | |
| 176 | - foreach ($results as $result) { |
|
| 177 | - array_push($tableData, $result); |
|
| 178 | - } |
|
| 176 | + foreach ($results as $result) { |
|
| 177 | + array_push($tableData, $result); |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - return $tableData; |
|
| 180 | + return $tableData; |
|
| 181 | 181 | |
| 182 | 182 | } |
| 183 | 183 | |
@@ -188,20 +188,20 @@ discard block |
||
| 188 | 188 | */ |
| 189 | 189 | public static function delete($id, $tableName, $dbConn = null) |
| 190 | 190 | { |
| 191 | - if (is_null($dbConn)) { |
|
| 192 | - $dbConn = new DatabaseConnection(); |
|
| 193 | - } |
|
| 191 | + if (is_null($dbConn)) { |
|
| 192 | + $dbConn = new DatabaseConnection(); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
| 195 | + $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
| 196 | 196 | |
| 197 | - $boolResponse = $dbConn->exec($sql); |
|
| 197 | + $boolResponse = $dbConn->exec($sql); |
|
| 198 | 198 | |
| 199 | - if ($boolResponse) { |
|
| 200 | - return true; |
|
| 199 | + if ($boolResponse) { |
|
| 200 | + return true; |
|
| 201 | 201 | |
| 202 | - } |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - throw NoRecordDeletionException::checkNoRecordDeleteException("Record not deleted successfully"); |
|
| 204 | + throw NoRecordDeletionException::checkNoRecordDeleteException("Record not deleted successfully"); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -212,15 +212,15 @@ discard block |
||
| 212 | 212 | */ |
| 213 | 213 | public static function checkIfMagicSetterContainsIsSameAsClassModel(array $tableColumn, array $userSetterArray) |
| 214 | 214 | { |
| 215 | - $unexpectedFields = []; |
|
| 215 | + $unexpectedFields = []; |
|
| 216 | 216 | |
| 217 | - foreach ($userSetterArray as $key => $val) { |
|
| 218 | - if (!in_array($key,$tableColumn)) { |
|
| 219 | - $unexpectedFields[] = $key; |
|
| 220 | - } |
|
| 221 | - } |
|
| 217 | + foreach ($userSetterArray as $key => $val) { |
|
| 218 | + if (!in_array($key,$tableColumn)) { |
|
| 219 | + $unexpectedFields[] = $key; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - return $unexpectedFields; |
|
| 223 | + return $unexpectedFields; |
|
| 224 | 224 | |
| 225 | 225 | } |
| 226 | 226 | |
@@ -231,13 +231,13 @@ discard block |
||
| 231 | 231 | */ |
| 232 | 232 | public function prepareUpdateQuery($sql) |
| 233 | 233 | { |
| 234 | - $splittedQuery = explode(",",$sql); |
|
| 234 | + $splittedQuery = explode(",",$sql); |
|
| 235 | 235 | |
| 236 | - array_pop($splittedQuery); |
|
| 236 | + array_pop($splittedQuery); |
|
| 237 | 237 | |
| 238 | - $mergeData = implode(",",$splittedQuery); |
|
| 238 | + $mergeData = implode(",",$splittedQuery); |
|
| 239 | 239 | |
| 240 | - return $mergeData; |
|
| 240 | + return $mergeData; |
|
| 241 | 241 | |
| 242 | 242 | } |
| 243 | 243 | |
@@ -250,26 +250,26 @@ discard block |
||
| 250 | 250 | */ |
| 251 | 251 | public function findAndWhere($params, $tableName, $dbConn = null) |
| 252 | 252 | { |
| 253 | - if (is_null($dbConn)) { |
|
| 254 | - $dbConn = $this->dbConnection; |
|
| 255 | - } |
|
| 253 | + if (is_null($dbConn)) { |
|
| 254 | + $dbConn = $this->dbConnection; |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - if (is_array($params) && !empty($params)) { |
|
| 258 | - $sql = "SELECT * FROM ".$tableName; |
|
| 257 | + if (is_array($params) && !empty($params)) { |
|
| 258 | + $sql = "SELECT * FROM ".$tableName; |
|
| 259 | 259 | |
| 260 | - foreach ($params as $key => $val) { |
|
| 261 | - $sql .= " WHERE `$key` = '$val'"; |
|
| 262 | - } |
|
| 260 | + foreach ($params as $key => $val) { |
|
| 261 | + $sql .= " WHERE `$key` = '$val'"; |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - $statement = $dbConn->prepare($sql); |
|
| 265 | - $statement->execute(); |
|
| 264 | + $statement = $dbConn->prepare($sql); |
|
| 265 | + $statement->execute(); |
|
| 266 | 266 | |
| 267 | - $returnedRowNumbers = $statement->rowCount(); |
|
| 267 | + $returnedRowNumbers = $statement->rowCount(); |
|
| 268 | 268 | |
| 269 | - return $returnedRowNumbers ? true : false; |
|
| 270 | - } |
|
| 269 | + return $returnedRowNumbers ? true : false; |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array"); |
|
| 272 | + throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array"); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /** |
@@ -280,25 +280,25 @@ discard block |
||
| 280 | 280 | */ |
| 281 | 281 | public function getColumnNames($table, $dbConn = null) |
| 282 | 282 | { |
| 283 | - $tableFields = []; |
|
| 283 | + $tableFields = []; |
|
| 284 | 284 | |
| 285 | - if (is_null($dbConn)) { |
|
| 286 | - $dbConn = $this->dbConnection; |
|
| 287 | - } |
|
| 285 | + if (is_null($dbConn)) { |
|
| 286 | + $dbConn = $this->dbConnection; |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - $sql = "SHOW COLUMNS FROM ".$table; |
|
| 289 | + $sql = "SHOW COLUMNS FROM ".$table; |
|
| 290 | 290 | |
| 291 | - $stmt = $dbConn->prepare($sql); |
|
| 292 | - $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
| 293 | - $stmt->execute(); |
|
| 291 | + $stmt = $dbConn->prepare($sql); |
|
| 292 | + $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
| 293 | + $stmt->execute(); |
|
| 294 | 294 | |
| 295 | - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
| 295 | + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
| 296 | 296 | |
| 297 | - foreach ($results as $result) { |
|
| 298 | - array_push($tableFields, $result['Field']); |
|
| 299 | - } |
|
| 297 | + foreach ($results as $result) { |
|
| 298 | + array_push($tableFields, $result['Field']); |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - return $tableFields; |
|
| 301 | + return $tableFields; |
|
| 302 | 302 | |
| 303 | 303 | } |
| 304 | 304 | |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields, $associative1DArray); |
| 53 | 53 | |
| 54 | 54 | if (count($unexpectedFields) > 0) { |
| 55 | - throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields,"needs to be created as a table field"); |
|
| 55 | + throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as a table field"); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | unset($associative1DArray[0]); |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | { |
| 78 | 78 | $insertQuery = 'INSERT INTO '.$tableName; |
| 79 | 79 | |
| 80 | - $TableValues = implode(',',array_keys($associative1DArray)); |
|
| 80 | + $TableValues = implode(',', array_keys($associative1DArray)); |
|
| 81 | 81 | |
| 82 | 82 | foreach ($associative1DArray as $field => $value) { |
| 83 | 83 | $FormValues[] = "'".trim(addslashes($value))."'"; |
@@ -85,8 +85,8 @@ discard block |
||
| 85 | 85 | |
| 86 | 86 | $splittedTableValues = implode(',', $FormValues); |
| 87 | 87 | |
| 88 | - $insertQuery.= ' ('.$TableValues.')'; |
|
| 89 | - $insertQuery.= ' VALUES ('.$splittedTableValues.')'; |
|
| 88 | + $insertQuery .= ' ('.$TableValues.')'; |
|
| 89 | + $insertQuery .= ' VALUES ('.$splittedTableValues.')'; |
|
| 90 | 90 | |
| 91 | 91 | $executeQuery = $dbConn->exec($insertQuery); |
| 92 | 92 | |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | $unexpectedFields = []; |
| 216 | 216 | |
| 217 | 217 | foreach ($userSetterArray as $key => $val) { |
| 218 | - if (!in_array($key,$tableColumn)) { |
|
| 218 | + if (!in_array($key, $tableColumn)) { |
|
| 219 | 219 | $unexpectedFields[] = $key; |
| 220 | 220 | } |
| 221 | 221 | } |
@@ -231,11 +231,11 @@ discard block |
||
| 231 | 231 | */ |
| 232 | 232 | public function prepareUpdateQuery($sql) |
| 233 | 233 | { |
| 234 | - $splittedQuery = explode(",",$sql); |
|
| 234 | + $splittedQuery = explode(",", $sql); |
|
| 235 | 235 | |
| 236 | 236 | array_pop($splittedQuery); |
| 237 | 237 | |
| 238 | - $mergeData = implode(",",$splittedQuery); |
|
| 238 | + $mergeData = implode(",", $splittedQuery); |
|
| 239 | 239 | |
| 240 | 240 | return $mergeData; |
| 241 | 241 | |