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