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