-
Notifications
You must be signed in to change notification settings - Fork 1
/
QueryBuilder.php
338 lines (312 loc) · 9.33 KB
/
QueryBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace App\Core\Framework\Classes;
use App\Core\Framework\Enumerables\QueryTypes;
use App\Core\Framework\Structures\DatabaseResult;
use App\Core\Server\Database\Database;
use BadFunctionCallException;
use LogicException;
/**
* Class QueryBuilder
*
* Represents a query builder for constructing SQL queries.
*/
class QueryBuilder
{
private $queryParts = [];
private $params = [];
private $queryType;
private $ignoreFrom = false;
public const LIKE_ENDS_WITH = "%@"; // Starts with
public const LIKE_STARTS_WITH = "@%"; // Ends with
public const LIKE_CONTAINS = "%@%"; // Contains
/**
* QueryBuilder constructor.
*
* Initializes the query parts and parameters.
*/
public function __construct()
{
$this->setDefaults();
}
/**
* Sets the default values for the query parts and parameters.
*/
public function setDefaults()
{
$this->queryParts = [
'select' => '',
'from' => '',
'join' => [],
'where' => [],
'group' => '',
'having' => '',
'order' => '',
'limit' => '',
'offset' => '',
'insert' => '',
'update' => '',
'delete' => '',
];
$this->params = [];
$this->queryType = null;
$this->ignoreFrom = false;
}
/**
* Sets the query type to SELECT and constructs the SELECT query.
*
* @param string $table The name of the table to select from.
* @param array $columns An array of column names to select.
* @return $this The QueryBuilder instance.
*/
public function select(string $table, array $columns)
{
$this->queryType = QueryTypes::SELECT;
$this->queryParts['select'] = 'SELECT ' . implode(', ', $columns) . ' FROM ' . $table;
return $this;
}
/**
* Sets the query type to INSERT and constructs the INSERT query.
*
* @param string $table The name of the table to insert into.
* @param array $data An associative array of column names and values to insert.
* @return $this The QueryBuilder instance.
*/
public function insert(string $table, array $data)
{
$this->queryType = QueryTypes::INSERT;
$columns = implode(', ', array_keys($data));
$placeholders = implode(', ', array_fill(0, count($data), '?'));
$this->queryParts['insert'] = "INSERT INTO $table ($columns) VALUES ($placeholders)";
$this->params = array_values($data);
return $this;
}
/**
* Sets the query type to UPDATE and constructs the UPDATE query.
*
* @param string $table The name of the table to update.
* @param array $data An associative array of column names and values to update.
* @return $this The QueryBuilder instance.
*/
public function update(string $table, array $data)
{
$this->queryType = QueryTypes::UPDATE;
$set = [];
foreach ($data as $column => $value) {
$set[] = "$column = ?";
$this->params[] = $value;
}
$set = implode(', ', $set);
$this->queryParts['update'] = "UPDATE $table SET $set";
return $this;
}
/**
* Sets the query type to DELETE and constructs the DELETE query.
*
* @param string $table The name of the table to delete from.
* @return $this The QueryBuilder instance.
*/
public function delete(string $table, bool $ignoreFrom = false)
{
$this->queryType = QueryTypes::DELETE;
$this->queryParts['delete'] = "DELETE FROM $table";
$this->ignoreFrom = $ignoreFrom;
return $this;
}
/**
* Adds a JOIN clause to the query.
*
* @param string $table The name of the table to join.
* @param string $condition The join condition.
* @param string $type The type of join (INNER, LEFT, RIGHT).
* @return $this The QueryBuilder instance.
*/
public function join(string $table, $condition, $type = 'INNER')
{
$this->queryParts['join'][] = " $type JOIN $table ON $condition";
return $this;
}
/**
* Adds a WHERE clause to the query.
*
* @param string $column The column to filter by.
* @param mixed $value The value to filter by.
* @param string $condition The condition to use (AND, OR).
* @param string $operator The operator to use (=, <, >, etc.).
* @return $this The QueryBuilder instance.
*/
public function where(string $column, $value, $condition = 'AND', $operator = '=')
{
$this->queryParts['where'][] = ($this->queryParts['where'] ? " $condition " : " WHERE ") . "$column $operator ?";
$this->params[] = $value;
return $this;
}
/**
* Adds a WHERE clause to the query using the LIKE operator.
*
* @param string $column The column to filter by.
* @param mixed $value The value to filter by.
* @param string $operator The condition to use (AND, OR).
* @param string $likeType The type of LIKE operation to use.
* @return $this The QueryBuilder instance.
*/
public function whereLike(string $column, $value, $operator = 'AND', $likeType = self::LIKE_CONTAINS)
{
return $this->where($column, str_replace("@", $value, $likeType), $operator, 'LIKE');
}
/**
* Adds a WHERE clause to the query using the NOT operator.
*
* @param string $column The column to filter by.
* @param mixed $value The value to filter by.
* @return $this The QueryBuilder instance.
*/
public function not(string $column, $value)
{
return $this->where($column, $value, 'AND NOT');
}
/**
* Adds a WHERE clause to the query using the OR operator.
*
* @param string $column The column to filter by.
* @param mixed $value The value to filter by.
* @param string $operator The operator to use (=, <, >, etc.).
* @return $this The QueryBuilder instance.
*/
public function orWhere(string $column, $value, $operator = '=')
{
return $this->where($column, $value, 'OR', $operator);
}
/**
* Adds a GROUP BY clause to the query.
*
* @param array $columns The columns to group by.
* @return $this The QueryBuilder instance.
*/
public function groupBy(array $columns)
{
$this->queryParts['group'] = 'GROUP BY ' . implode(', ', $columns);
return $this;
}
/**
* Adds a HAVING clause to the query.
*
* @param string $condition The condition for the HAVING clause.
* @return $this The QueryBuilder instance.
*/
public function having(string $condition)
{
$this->queryParts['having'] = 'HAVING ' . $condition;
return $this;
}
/**
* Adds an ORDER BY clause to the query.
*
* @param array $columns An array of column names to order by.
* @param string $direction The direction to order by (ASC, DESC).
* @return $this The QueryBuilder instance.
*/
public function orderBy(array $columns, $direction = 'ASC')
{
$column = implode(', ', $columns);
$this->queryParts['order'] = " ORDER BY $column $direction";
return $this;
}
/**
* Adds a LIMIT clause to the query.
*
* @param int $limit The number of rows to limit the query to.
* @return $this The QueryBuilder instance.
*/
public function limit(int $limit)
{
$this->queryParts['limit'] = " LIMIT $limit";
return $this;
}
/**
* Adds an OFFSET clause to the query.
*
* @param int $offset The number of rows to offset the query by.
* @return $this The QueryBuilder instance.
*/
public function offset(int $offset)
{
$this->queryParts['offset'] = " OFFSET $offset";
return $this;
}
/**
* Executes the query, resets the query parts and parameters to their default values and returns the result.
* This method **must** be called after constructing the query to perform the query.
*
* @return DatabaseResult The result of the query.
*/
public function execute(): DatabaseResult
{
$database = Database::DB();
$result = null;
switch ($this->queryType) {
case QueryTypes::SELECT:
$result = $database->select($this->getQuery(), $this->getParams());
break;
case QueryTypes::INSERT:
$result = $database->insert($this->getQuery(), $this->getParams());
break;
case QueryTypes::UPDATE:
$result = $database->update($this->getQuery(), $this->getParams());
break;
case QueryTypes::DELETE:
if (!$this->ignoreFrom && count($this->queryParts['where']) == 0) {
throw new LogicException("DELETE query without WHERE clause is not allowed unless explicitly ignored.");
}
$result = $database->delete($this->getQuery(), $this->getParams());
break;
default:
throw new BadFunctionCallException("Unsupported query type");
break;
}
$this->setDefaults();
return $result;
}
/**
* Gets the query string.
*
* @return string The query string.
*/
public function getQuery(): string
{
$query = '';
switch ($this->queryType) {
case QueryTypes::SELECT:
$query .= $this->queryParts['select'] . $this->addToQuery($this->queryParts['from']) . $this->addToQuery($this->queryParts['join']) . $this->addToQuery($this->queryParts['where']) . $this->addToQuery($this->queryParts['group']) . $this->addToQuery($this->queryParts['having']) . $this->queryParts['order'] . $this->queryParts['limit'] . $this->queryParts['offset'];
break;
case QueryTypes::INSERT:
$query .= $this->queryParts['insert'];
break;
case QueryTypes::UPDATE:
$query .= $this->queryParts['update'] . ' ' . implode(' ', $this->queryParts['where']);
break;
case QueryTypes::DELETE:
$query .= $this->queryParts['delete'] . ' ' . implode(' ', $this->queryParts['where']);
break;
}
return $query;
}
/**
* Gets the query parameters.
*
* @return array The query parameters.
*/
public function getParams()
{
return $this->params;
}
function addToQuery($value): string
{
if (is_string($value) && $value !== '') {
return ' ' . $value;
} elseif (is_array($value) && count($value) > 0) {
return ' ' . implode(' ', $value);
} else {
return '';
}
}
}