1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Laztopaz\potato-ORM |
5
|
|
|
* @author Temitope Olotin <[email protected]> |
6
|
|
|
* @license <https://opensource.org/license/MIT> MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Laztopaz\potatoORM; |
10
|
|
|
|
11
|
|
|
use PDO; |
12
|
|
|
use Laztopaz\potatoORM\DatabaseHelper; |
13
|
|
|
use Laztopaz\potatoORM\TableFieldUndefinedException; |
14
|
|
|
use Laztopaz\potatoORM\EmptyArrayException; |
15
|
|
|
|
16
|
|
|
class DatabaseHandler { |
17
|
|
|
|
18
|
|
|
private $tableFields; |
|
|
|
|
19
|
|
|
private $dbHelperInstance; |
|
|
|
|
20
|
|
|
private $dbConnection; |
21
|
|
|
private $model; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This is a constructor; a default method that will be called automatically during class instantiation |
25
|
|
|
*/ |
26
|
|
|
public function __construct($modelClassName, $dbConn = Null) |
27
|
|
|
{ |
28
|
|
|
if (is_null($dbConn)) { |
29
|
|
|
$this->dbConnection = new DatabaseConnection(); |
30
|
|
|
} else { |
31
|
|
|
$this->dbConnection = $dbConn; |
32
|
|
|
} |
33
|
|
|
$this->model = $modelClassName; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* This method create a record and store it in a table row |
38
|
|
|
* @params associative array, string tablename |
39
|
|
|
* @return boolean true or false |
40
|
|
|
*/ |
41
|
|
|
public function create($associative1DArray, $tableName, $dbConn = Null) |
42
|
|
|
{ |
43
|
|
|
$unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection),$associative1DArray); |
44
|
|
|
|
45
|
|
|
if (count($unexpectedFields) > 0) { |
46
|
|
|
throw TableFieldUndefinedException::fieldsNotDefinedException($unexpectedFields,"needs to be created as table field"); |
47
|
|
|
} |
48
|
|
|
// if ($this->findAndWhere(['alias' => $associative1DArray['alias']], $this->model, $this->dbConnection)) { |
|
|
|
|
49
|
|
|
// throw NoRecordInsertionException::checkNoRecordAddedException("Insertion Error: Record already exist"); |
50
|
|
|
// } |
51
|
|
|
unset($this->getColumnNames($this->model, $this->dbConnection)[0]); |
52
|
|
|
|
53
|
|
|
if (is_null($dbConn)) { |
54
|
|
|
$dbConn = $this->dbConnection; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->insertRecord($dbConn, $tableName, $associative1DArray); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
private function insertRecord($dbConn, $tableName, $associative1DArray) { |
62
|
|
|
|
63
|
|
|
$insertQuery = 'INSERT INTO '.$tableName; |
64
|
|
|
$TableValues = implode(',',array_keys($associative1DArray)); |
65
|
|
|
foreach ($associative1DArray as $field => $value) { |
66
|
|
|
|
67
|
|
|
$FormValues[] = "'".trim(addslashes($value))."'"; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
$splittedTableValues = implode(',', $FormValues); |
|
|
|
|
70
|
|
|
$insertQuery.= ' ('.$TableValues.')'; |
71
|
|
|
$insertQuery.= ' VALUES ('.$splittedTableValues.')'; |
72
|
|
|
$executeQuery = $dbConn->exec($insertQuery); |
73
|
|
|
|
74
|
|
|
return $executeQuery ? : false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/* |
78
|
|
|
* This method updates any table by supplying 3 parameter |
79
|
|
|
* @params: $updateParams, $tableName, $associative1DArray |
80
|
|
|
* @return boolean true or false |
81
|
|
|
*/ |
82
|
|
|
public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = Null) |
83
|
|
|
{ |
84
|
|
|
$sql = ""; |
85
|
|
|
if (is_null($dbConn)) { |
86
|
|
|
$dbConn = $this->dbConnection; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$updateSql = "UPDATE `$tableName` SET "; |
90
|
|
|
unset($associative1DArray['id']); |
91
|
|
|
$unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection),$associative1DArray); |
92
|
|
|
|
93
|
|
|
if (count($unexpectedFields) > 0) { |
94
|
|
|
throw TableFieldUndefinedException::fieldsNotDefinedException($unexpectedFields, "needs to be created as table field"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach($associative1DArray as $field => $value) { |
98
|
|
|
$sql .= "`$field` = '$value'".","; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$updateSql .= $this->prepareUpdateQuery($sql); |
102
|
|
|
|
103
|
|
|
foreach ($updateParams as $key => $val) { |
104
|
|
|
$updateSql .= " WHERE $key = $val"; |
105
|
|
|
} |
106
|
|
|
$stmt = $dbConn->prepare($updateSql); |
107
|
|
|
$boolResponse = $stmt->execute(); |
108
|
|
|
|
109
|
|
|
return $boolResponse ? : false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* This method retrieves record from a table |
114
|
|
|
* @params int id, string tableName |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public static function read($id, $tableName, $dbConn = Null) |
118
|
|
|
{ |
119
|
|
|
$tableData = []; |
120
|
|
|
|
121
|
|
|
if (is_null($dbConn)) { |
122
|
|
|
$dbConn = new DatabaseConnection(); |
123
|
|
|
} |
124
|
|
|
$sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
$stmt = $dbConn->prepare($sql); |
128
|
|
|
$stmt->bindValue(':table', $tableName); |
129
|
|
|
$stmt->bindValue(':id', $id); |
130
|
|
|
$stmt->execute(); |
131
|
|
|
} catch (PDOException $e) { |
|
|
|
|
132
|
|
|
return $e->getMessage(); |
133
|
|
|
} |
134
|
|
|
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
135
|
|
|
|
136
|
|
|
foreach($results as $result) { |
137
|
|
|
array_push($tableData, $result); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $tableData; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* This method deletes a record from a table row |
145
|
|
|
* @params int id, string tableName |
146
|
|
|
* @return boolean true or false |
147
|
|
|
*/ |
148
|
|
|
public static function delete($id, $tableName, $dbConn = Null) |
149
|
|
|
{ |
150
|
|
|
if (is_null($dbConn)) { |
151
|
|
|
$dbConn = new DatabaseConnection(); |
152
|
|
|
} |
153
|
|
|
$sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
154
|
|
|
$boolResponse = $dbConn->exec($sql); |
155
|
|
|
|
156
|
|
|
return $boolResponse ? : false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* This method checks if the magic setters array is the same as the table columns |
161
|
|
|
* @param array $tableColumn |
162
|
|
|
* @param array $userSetterArray |
163
|
|
|
* @return array $unexpectedFields |
164
|
|
|
*/ |
165
|
|
|
public static function checkIfMagicSetterContainsIsSameAsClassModel(array $tableColumn, array $userSetterArray) |
166
|
|
|
{ |
167
|
|
|
$unexpectedFields = []; |
168
|
|
|
|
169
|
|
|
foreach ($userSetterArray as $key => $val) { |
170
|
|
|
if (!in_array($key,$tableColumn)) { |
171
|
|
|
|
172
|
|
|
$unexpectedFields[] = $key; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
return $unexpectedFields; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* This method returns sql query |
180
|
|
|
* @param $sql |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function prepareUpdateQuery($sql) |
184
|
|
|
{ |
185
|
|
|
$splittedQuery = explode(",",$sql); |
186
|
|
|
array_pop($splittedQuery); |
187
|
|
|
$mergeData = implode(",",$splittedQuery); |
188
|
|
|
|
189
|
|
|
return $mergeData; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param array $params |
194
|
|
|
* @param $tableName |
195
|
|
|
* @param $dbConn |
196
|
|
|
* @return bool |
197
|
|
|
* @throws EmptyArrayException |
198
|
|
|
*/ |
199
|
|
|
public function findAndWhere($params, $tableName, $dbConn) |
200
|
|
|
{ |
201
|
|
|
if (is_null($dbConn)) { |
202
|
|
|
$dbConn = $this->dbConnection; |
203
|
|
|
} |
204
|
|
|
if (is_array($params) && !empty($params)) { |
205
|
|
|
$sql = "SELECT * FROM ".$tableName; |
206
|
|
|
foreach ($params as $key => $val) { |
207
|
|
|
$sql .= " WHERE `$key` = '$val'"; |
208
|
|
|
} |
209
|
|
|
$statement = $dbConn->prepare($sql); |
210
|
|
|
$statement->execute(); |
211
|
|
|
$returnedRowNumbers = $statement->rowCount(); |
212
|
|
|
return $returnedRowNumbers ? true : false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array"); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* This method returns column fields of a particular table |
220
|
|
|
* @param $table |
221
|
|
|
* @param $conn |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
public function getColumnNames($table, $dbConn = Null) { |
225
|
|
|
|
226
|
|
|
$tableFields = []; |
227
|
|
|
|
228
|
|
|
if (is_null($dbConn)) { |
229
|
|
|
$dbConn = $this->dbConnection; |
230
|
|
|
} |
231
|
|
|
$sql = "SHOW COLUMNS FROM ".$table; |
232
|
|
|
|
233
|
|
|
$stmt = $dbConn->prepare($sql); |
234
|
|
|
$stmt->bindValue(':table', $table, PDO::PARAM_STR); |
235
|
|
|
$stmt->execute(); |
236
|
|
|
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
237
|
|
|
|
238
|
|
|
foreach($results as $result) { |
239
|
|
|
array_push($tableFields, $result['Field']); |
240
|
|
|
} |
241
|
|
|
return $tableFields; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.