1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This class handles building sql query statement and check |
4
|
|
|
* that the table exist in the database. |
5
|
|
|
* |
6
|
|
|
* @package Ibonly\PotatoORM\DatabaseQuery |
7
|
|
|
* @author Ibraheem ADENIYI <[email protected]> |
8
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Ibonly\PotatoORM; |
12
|
|
|
|
13
|
|
|
use PDOException; |
14
|
|
|
use Ibonly\PotatoORM\DatabaseQueryInterface; |
15
|
|
|
use Ibonly\PotatoORM\ColumnNotExistExeption; |
16
|
|
|
use Ibonly\PotatoORM\InvalidConnectionException; |
17
|
|
|
use Ibonly\PotatoORM\TableDoesNotExistException; |
18
|
|
|
|
19
|
|
|
class DatabaseQuery implements DatabaseQueryInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* connect Setup database connection |
23
|
|
|
*/ |
24
|
|
|
protected static function connect() |
25
|
|
|
{ |
26
|
|
|
return new DBConfig(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* sanitize(argument) Removes unwanted characters |
31
|
|
|
* |
32
|
|
|
* @param $value |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
protected static function sanitize($value) |
37
|
|
|
{ |
38
|
|
|
$value = trim($value); |
39
|
|
|
$value = htmlentities($value); |
40
|
|
|
return $value; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* checkConnection |
45
|
|
|
* |
46
|
|
|
* @param $con |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected static function checkConnection($con) |
51
|
|
|
{ |
52
|
|
|
if( is_null($con) ) |
53
|
|
|
{ |
54
|
|
|
$con = self::connect(); |
55
|
|
|
} |
56
|
|
|
return $con; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* checkTableExist Check if table already in the database |
61
|
|
|
* |
62
|
|
|
* @param $tablename |
63
|
|
|
* @param $con |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function checkTableExist($table, $con=NULL) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$connection = $this->checkConnection($con); |
70
|
|
|
$query = $connection->query("SELECT 1 FROM {$table} LIMIT 1"); |
|
|
|
|
71
|
|
|
if( $query !== false ) |
72
|
|
|
{ |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* checkTableName Return the table name |
79
|
|
|
* |
80
|
|
|
* @param $tablename |
81
|
|
|
* @param $con |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public static function checkTableName($tableName, $con=NULL) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$connection = self::checkConnection($con); |
88
|
|
|
// if( ! is_null($connection) ) |
|
|
|
|
89
|
|
|
// { |
90
|
|
|
$query = $connection->query("SELECT 1 FROM {$tableName} LIMIT 1"); |
|
|
|
|
91
|
|
|
if( $query !== false ) |
92
|
|
|
{ |
93
|
|
|
return $tableName; |
94
|
|
|
} |
95
|
|
|
throw new TableDoesNotExistException(); |
96
|
|
|
// } |
97
|
|
|
// throw new InvalidConnectionException(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* checkColumn Check if column exist in table |
102
|
|
|
* |
103
|
|
|
* @param $tableName |
104
|
|
|
* @param $columnName |
105
|
|
|
* @param $con |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected static function checkColumn($tableName, $columnName, $con=NULL) |
110
|
|
|
{ |
111
|
|
|
$connection = self::checkConnection($con); |
112
|
|
|
|
113
|
|
|
$result = $connection->prepare("SELECT {$columnName} FROM {$tableName}"); |
|
|
|
|
114
|
|
|
$result->execute(); |
115
|
|
|
if ( ! $result->columnCount() ) |
116
|
|
|
{ |
117
|
|
|
throw new ColumnNotExistExeption(); |
118
|
|
|
} |
119
|
|
|
return $columnName; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* buildColumn Build the column name |
124
|
|
|
* |
125
|
|
|
* @param $data |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public static function buildColumn($data) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$counter = 0; |
132
|
|
|
$insertQuery = ""; |
133
|
|
|
$arraySize = sizeof($data); |
134
|
|
|
|
135
|
|
|
foreach ( $data as $key => $value ) |
136
|
|
|
{ |
137
|
|
|
$counter++; |
138
|
|
|
$insertQuery .= self::sanitize($key); |
139
|
|
|
if( $arraySize > $counter ) |
140
|
|
|
$insertQuery .= ", "; |
141
|
|
|
} |
142
|
|
|
return $insertQuery; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* buildValues Build the column values |
147
|
|
|
* |
148
|
|
|
* @param $data |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public static function buildValues($data) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$counter = 0; |
155
|
|
|
$insertQuery = ""; |
156
|
|
|
$arraySize = sizeof($data); |
157
|
|
|
|
158
|
|
|
foreach ( $data as $key => $value ) |
159
|
|
|
{ |
160
|
|
|
$counter++; |
161
|
|
|
$insertQuery .= "'".self::sanitize($value) ."'"; |
162
|
|
|
if( $arraySize > $counter ) |
163
|
|
|
$insertQuery .= ", "; |
164
|
|
|
} |
165
|
|
|
return $insertQuery; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* buildClause Build the clause value |
170
|
|
|
* |
171
|
|
|
* @param $data |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
protected static function buildClause($tableName, $data) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$counter = 0; |
178
|
|
|
$updateQuery = ""; |
179
|
|
|
$arraySize = sizeof($data); |
180
|
|
|
|
181
|
|
|
foreach ( $data as $key => $value ) |
182
|
|
|
{ |
183
|
|
|
$counter++; |
184
|
|
|
$columnName = self::checkColumn($tableName, self::sanitize($key)); |
185
|
|
|
$updateQuery .= $columnName ." = '".self::sanitize($value)."'"; |
186
|
|
|
if ( $arraySize > $counter ) |
187
|
|
|
{ |
188
|
|
|
$updateQuery .= ", "; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
return $updateQuery; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* selectAllQuery |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public static function selectAllQuery($tableName) |
200
|
|
|
{ |
201
|
|
|
return "SELECT * FROM {$tableName}"; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* whereAndClause |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
View Code Duplication |
public static function whereAndClause($tableName, $data, $condition) |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$where = ""; |
212
|
|
|
$counter = 0; |
213
|
|
|
$arraySize = sizeof($data); |
214
|
|
|
|
215
|
|
|
foreach ( $data as $key => $value ) |
216
|
|
|
{ |
217
|
|
|
$counter++; |
218
|
|
|
$columnName = self::checkColumn($tableName, self::sanitize($key)); |
219
|
|
|
$where .= $columnName ." = '".self::sanitize($value)."'"; |
220
|
|
|
if ( $arraySize > $counter ) |
221
|
|
|
{ |
222
|
|
|
$where .= " " . $condition . " "; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $where; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* selectQuery |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public static function selectQuery($tableName, $data, $condition, $connection) |
235
|
|
|
{ |
236
|
|
|
$query = ""; |
|
|
|
|
237
|
|
|
try |
238
|
|
|
{ |
239
|
|
|
$arraySize = sizeof($data); |
240
|
|
|
if( $arraySize > 1 && $condition == NULL) |
241
|
|
|
{ |
242
|
|
|
$query = "Please Supply the condition"; |
243
|
|
|
} |
244
|
|
|
else |
245
|
|
|
{ |
246
|
|
|
$columnName = self::whereAndClause($tableName, $data, $condition); |
247
|
|
|
$query = "SELECT * FROM $tableName WHERE $columnName"; |
248
|
|
|
} |
249
|
|
|
} catch ( PDOException $e ) { |
250
|
|
|
$query = $e->getMessage(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $query; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* insertQuery |
258
|
|
|
* |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
public function insertQuery($tableName) |
262
|
|
|
{ |
263
|
|
|
$data = ( array )$this; |
264
|
|
|
array_shift($data); |
265
|
|
|
|
266
|
|
|
$columnNames = self::buildColumn($data); |
267
|
|
|
$values = self::buildValues($data); |
268
|
|
|
$insertQuery = "INSERT INTO $tableName ({$columnNames}) VALUES ({$values})"; |
269
|
|
|
|
270
|
|
|
return $insertQuery; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* updateQuery |
275
|
|
|
* |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
|
|
public function updateQuery($tableName) |
279
|
|
|
{ |
280
|
|
|
$data = ( array ) $this; |
281
|
|
|
$data = array_slice ($data, 2); |
282
|
|
|
|
283
|
|
|
$values = self::buildClause($tableName, $data); |
284
|
|
|
$updateQuery = "UPDATE $tableName SET {$values} WHERE id = ". self::sanitize($this->id); |
|
|
|
|
285
|
|
|
|
286
|
|
|
return $updateQuery; |
287
|
|
|
} |
288
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.