|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class DataBase: |
|
5
|
|
|
* This class performs the basic CRUD operations which compose of |
|
6
|
|
|
* various methods such as create, read, update, and delete. |
|
7
|
|
|
* This class class query the database to achieve its function. |
|
8
|
|
|
* |
|
9
|
|
|
* @author: Raimi Ademola <[email protected]> |
|
10
|
|
|
* @copyright: 2016 Andela |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Demo; |
|
13
|
|
|
|
|
14
|
|
|
use PDO; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class DataBaseQuery |
|
18
|
|
|
{ |
|
19
|
|
|
protected $tableName; |
|
20
|
|
|
protected $splitTableField; |
|
21
|
|
|
protected $formatTableValues; |
|
22
|
|
|
protected $dbConnection; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* This is a constructor; a default method that will be called automatically during class |
|
26
|
|
|
* instantiation. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($dbConn = null) |
|
29
|
|
|
{ |
|
30
|
|
|
if (is_null($dbConn)) { |
|
31
|
|
|
$this->dbConnection = new DataBaseConnection(); |
|
32
|
|
|
} else { |
|
33
|
|
|
$this->dbConnection = $dbConn; |
|
34
|
72 |
|
} |
|
35
|
|
|
} |
|
36
|
72 |
|
|
|
37
|
51 |
|
/** |
|
38
|
34 |
|
* This method create or insert new users to the table. |
|
39
|
72 |
|
* |
|
40
|
|
|
* @param $associativeArray |
|
41
|
72 |
|
* @param $tableName |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public function create($associativeArrayToCreate, $tableName) |
|
46
|
|
|
{ |
|
47
|
|
|
$tableFields = []; |
|
48
|
|
|
$tableValues = []; |
|
49
|
|
|
|
|
50
|
|
|
foreach ($associativeArrayToCreate as $key => $val) { |
|
51
|
6 |
|
$tableFields[] = $key; |
|
52
|
|
|
$tableValues[] = $val; |
|
53
|
6 |
|
} |
|
54
|
6 |
|
|
|
55
|
|
|
$unexpectedArray = array_diff($tableFields, $this->getColumnNames($tableName)); |
|
56
|
6 |
|
|
|
57
|
6 |
|
if (count($unexpectedArray) < 1) { |
|
58
|
6 |
|
$sql = 'INSERT INTO '.$tableName; |
|
59
|
4 |
|
$sql .= '('.$this->splitTableField($tableFields).') '; |
|
60
|
|
|
$sql .= 'VALUES ('.$this->formatTableValues($tableValues).')'; |
|
61
|
6 |
|
|
|
62
|
|
|
$bool = $this->dbConnection->exec($sql); |
|
63
|
6 |
|
|
|
64
|
3 |
|
return $bool; |
|
65
|
3 |
|
} |
|
66
|
3 |
|
|
|
67
|
|
|
throw new FieldUndefinedException('Oops, '.$this->splitTableField($unexpectedArray).' is not defined as a field'); |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
/** |
|
71
|
|
|
* This method read the data in the table name of the id being passed to it. |
|
72
|
|
|
* |
|
73
|
3 |
|
* @param $id |
|
74
|
|
|
* @param $tableName |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function read($id, $tableName, $dbConn = null) |
|
79
|
|
|
{ |
|
80
|
|
|
if (is_null($dbConn)) { |
|
81
|
|
|
$dbConn = new DataBaseConnection(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
9 |
|
$sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
|
85
|
|
|
$statement = $dbConn->prepare($sql); |
|
86
|
9 |
|
$statement->execute(); |
|
87
|
|
|
|
|
88
|
|
|
$results = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
89
|
|
|
|
|
90
|
9 |
|
if (count($results) < 1) { |
|
91
|
9 |
|
throw new IdNotFoundException('Oops, the id '.$id.' is not in the table, try another id'); |
|
92
|
9 |
|
} |
|
93
|
|
|
|
|
94
|
9 |
|
return $results; |
|
95
|
|
|
} |
|
96
|
9 |
|
|
|
97
|
3 |
|
/** |
|
98
|
|
|
* This method delete the table name of the id being passed to it. |
|
99
|
|
|
* |
|
100
|
6 |
|
* @param $update Params |
|
101
|
|
|
* @param $associativeArrayToUpdate |
|
102
|
|
|
* @param $tableName |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
public function update($updateParams, $associativeArrayToUpdate, $tableName) |
|
107
|
|
|
{ |
|
108
|
|
|
$updateSql = "UPDATE `$tableName` SET "; |
|
109
|
|
|
|
|
110
|
|
|
unset($associativeArrayToUpdate['id']); |
|
111
|
|
|
|
|
112
|
6 |
|
foreach ($associativeArrayToUpdate as $key => $val) { |
|
113
|
|
|
$tableFields[] = $key; |
|
|
|
|
|
|
114
|
6 |
|
} |
|
115
|
|
|
|
|
116
|
6 |
|
$unexpectedArray = array_diff($tableFields, $this->getColumnNames($tableName)); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
6 |
|
if (count($unexpectedArray) < 1) { |
|
119
|
6 |
|
$updateSql .= $this->updateArraySql($associativeArrayToUpdate); |
|
120
|
4 |
|
|
|
121
|
|
|
foreach ($updateParams as $field => $value) { |
|
122
|
6 |
|
$updateSql .= " WHERE $field = $value"; |
|
123
|
|
|
} |
|
124
|
6 |
|
|
|
125
|
3 |
|
$statement = $this->dbConnection->exec($updateSql); |
|
126
|
|
|
|
|
127
|
3 |
|
return $statement; |
|
128
|
3 |
|
} |
|
129
|
2 |
|
|
|
130
|
|
|
throw new FieldUndefinedException('Oops, '.$this->splitTableField($unexpectedArray).' is not defined as a field'); |
|
131
|
3 |
|
} |
|
132
|
|
|
|
|
133
|
3 |
|
/** |
|
134
|
|
|
* This method delete the table name of the id passed to it. |
|
135
|
|
|
* |
|
136
|
3 |
|
* @param $id |
|
137
|
|
|
* @param $tableName |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public static function delete($id, $tableName, $dbConn = null) |
|
142
|
|
|
{ |
|
143
|
|
|
if (is_null($dbConn)) { |
|
144
|
|
|
$dbConn = new DataBaseConnection(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
9 |
|
$sql = 'SELECT * FROM '.$tableName.' WHERE id = '.$id; |
|
148
|
|
|
$statement = $dbConn->prepare($sql); |
|
149
|
9 |
|
$statement->execute(); |
|
150
|
|
|
$results = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
151
|
|
|
|
|
152
|
|
|
if (count($results) < 1) { |
|
153
|
9 |
|
throw new IdNotFoundException('Oops, the id '.$id.' is not in the database, try another id'); |
|
154
|
9 |
|
} |
|
155
|
9 |
|
|
|
156
|
9 |
|
$sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
157
|
|
|
$statement = $dbConn->exec($sql); |
|
|
|
|
|
|
158
|
9 |
|
|
|
159
|
3 |
|
return true; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
6 |
|
/** |
|
163
|
6 |
|
* This method returns a string form an array by making us of the implode function. |
|
164
|
|
|
* |
|
165
|
6 |
|
* @param $tableField |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function splitTableField($tableField) |
|
170
|
|
|
{ |
|
171
|
|
|
$splitTableField = implode(', ', $tableField); |
|
172
|
|
|
|
|
173
|
|
|
return $splitTableField; |
|
174
|
|
|
} |
|
175
|
12 |
|
|
|
176
|
|
|
/** |
|
177
|
12 |
|
* This method returns a string formed fron an array, It format the array. |
|
178
|
|
|
* |
|
179
|
12 |
|
* @param $tableValues |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
public function formatTableValues($tableValues) |
|
184
|
|
|
{ |
|
185
|
|
|
$formattedValues = []; |
|
186
|
|
|
|
|
187
|
|
|
foreach ($tableValues as $key => $value) { |
|
188
|
|
|
$formattedValues[] = "'".$value."'"; |
|
189
|
3 |
|
} |
|
190
|
|
|
|
|
191
|
3 |
|
$ValueSql = implode(',', $formattedValues); |
|
192
|
|
|
|
|
193
|
3 |
|
return $ValueSql; |
|
194
|
3 |
|
} |
|
195
|
2 |
|
|
|
196
|
|
|
/** |
|
197
|
3 |
|
* This method returns a string formed from an array. |
|
198
|
|
|
* |
|
199
|
3 |
|
* @param $array |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
public function updateArraySql($array) |
|
204
|
|
|
{ |
|
205
|
|
|
$updatedValues = []; |
|
206
|
|
|
|
|
207
|
|
|
foreach ($array as $key => $val) { |
|
208
|
|
|
$updatedValues[] = "`$key` = '$val'"; |
|
209
|
6 |
|
} |
|
210
|
|
|
|
|
211
|
6 |
|
$valueSql = implode(',', $updatedValues); |
|
212
|
|
|
|
|
213
|
6 |
|
return $valueSql; |
|
214
|
6 |
|
} |
|
215
|
4 |
|
|
|
216
|
|
|
/** |
|
217
|
6 |
|
* This method returns column fields of a particular table. |
|
218
|
|
|
* |
|
219
|
6 |
|
* @param $table |
|
220
|
|
|
* |
|
221
|
|
|
* @return array |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getColumnNames($table) |
|
224
|
|
|
{ |
|
225
|
|
|
$tableFields = []; |
|
226
|
|
|
|
|
227
|
|
|
$sql = 'SHOW COLUMNS FROM '.$table; |
|
228
|
|
|
$stmt = $this->dbConnection->prepare($sql); |
|
229
|
15 |
|
$stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
230
|
|
|
$stmt->execute(); |
|
231
|
15 |
|
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
232
|
|
|
|
|
233
|
15 |
|
foreach ($results as $result) { |
|
234
|
15 |
|
array_push($tableFields, $result['Field']); |
|
235
|
15 |
|
} |
|
236
|
15 |
|
|
|
237
|
15 |
|
return $tableFields; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.