1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Willry\QueryBuilder; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class Base |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var bool |
10
|
|
|
*/ |
11
|
|
|
protected $silentErrors; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \Exception|null |
15
|
|
|
*/ |
16
|
|
|
protected $fail; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string $entity database table |
20
|
|
|
*/ |
21
|
|
|
protected $entity; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $columns = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $where; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $order; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $limit; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $offset; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $joins; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $query; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $groupBy; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $having; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var \PDO |
70
|
|
|
*/ |
71
|
|
|
protected $db; |
72
|
|
|
|
73
|
|
|
protected $fields = []; |
74
|
|
|
|
75
|
|
|
public $bindings = [ |
76
|
|
|
'select' => [], |
77
|
|
|
'from' => [], |
78
|
|
|
'join' => [], |
79
|
|
|
'create' => [], |
80
|
|
|
'update' => [], |
81
|
|
|
'where' => [], |
82
|
|
|
'groupBy' => [], |
83
|
|
|
'having' => [], |
84
|
|
|
'order' => [], |
85
|
|
|
'union' => [], |
86
|
|
|
'unionOrder' => [], |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
public $type = "select"; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var array|null |
93
|
|
|
*/ |
94
|
|
|
protected $connectionConfig; |
95
|
|
|
|
96
|
|
|
public const TYPE_SELECT = 'select'; |
97
|
|
|
|
98
|
|
|
public const TYPE_CREATE = 'create'; |
99
|
|
|
|
100
|
|
|
public const TYPE_UPDATE = 'update'; |
101
|
|
|
|
102
|
|
|
public const TYPE_DELETE = 'delete'; |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
public function __construct(string $connectionName = 'default', bool $regenerateConnection = false) |
106
|
|
|
{ |
107
|
|
|
$this->db = Connect::getInstance($connectionName, $regenerateConnection); |
108
|
|
|
$this->connectionConfig = Connect::getConfig($connectionName); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function from(string $entity) |
112
|
|
|
{ |
113
|
|
|
$this->entity = $entity; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
public function fromSubQuery($callback, string $alias = 'sub') |
119
|
|
|
{ |
120
|
|
|
$query = new Query(); |
121
|
|
|
$callback($query); |
122
|
|
|
|
123
|
|
|
$this->entity = "(" . $query->toSQL() . ") as $alias"; |
124
|
|
|
$this->setBindings($query->flatBindings(), 'from'); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function setSilentErrors(bool $silentErrors = false) |
130
|
|
|
{ |
131
|
|
|
$this->silentErrors = $silentErrors; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $where |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function where(string $where, array $params = []): static |
140
|
|
|
{ |
141
|
|
|
$this->setBindings($params, 'where'); |
142
|
|
|
|
143
|
|
|
if (!empty($this->where)) { |
144
|
|
|
$this->where .= " AND {$where}"; |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->where = "WHERE $where"; |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $where |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function orWhere(string $where, array $params = []): static |
157
|
|
|
{ |
158
|
|
|
$this->setBindings($params, 'where'); |
159
|
|
|
if (!empty($this->where)) { |
160
|
|
|
$this->where .= " OR {$where}"; |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->where = "WHERE $where"; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function whereIn(string $column, array $data): static |
169
|
|
|
{ |
170
|
|
|
$inString = implode(',', array_fill(0, count($data), '?')); |
171
|
|
|
$this->setBindings($data, 'where'); |
172
|
|
|
|
173
|
|
|
if (!empty($this->where)) { |
174
|
|
|
$this->where .= " AND {$column} IN ({$inString})"; |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->where = "WHERE {$column} IN ({$inString})"; |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function orWhereIn(string $column, array $data): static |
183
|
|
|
{ |
184
|
|
|
|
185
|
|
|
$inString = implode(',', array_fill(0, count($data), '?')); |
186
|
|
|
$this->setBindings($data, 'where'); |
187
|
|
|
|
188
|
|
|
if (!empty($this->where)) { |
189
|
|
|
$this->where .= " OR {$column} IN ($inString)"; |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$this->where = "WHERE {$column} IN ($inString)"; |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param int $limit |
200
|
|
|
*/ |
201
|
|
|
public function limit(int $limit): static |
202
|
|
|
{ |
203
|
|
|
$this->limit = "LIMIT $limit"; |
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param int $offset |
209
|
|
|
* @return DB |
210
|
|
|
*/ |
211
|
|
|
public function offset(int $offset): static |
212
|
|
|
{ |
213
|
|
|
$this->offset = "OFFSET $offset"; |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
public function getBindings() |
219
|
|
|
{ |
220
|
|
|
return $this->bindings; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param array $params |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
|
|
public function setBindings(array $params = [], string $type = 'where'): static |
228
|
|
|
{ |
229
|
|
|
$this->bindings[$type] = array_merge($this->bindings[$type], $params); |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param string $join |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
|
|
public function leftJoin(string $join, array $params = []): static |
239
|
|
|
{ |
240
|
|
|
$this->setBindings($params, 'join'); |
241
|
|
|
|
242
|
|
|
$this->joins .= PHP_EOL; |
243
|
|
|
|
244
|
|
|
$this->joins .= "LEFT JOIN $join"; |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param string $join |
251
|
|
|
* @return $this |
252
|
|
|
*/ |
253
|
|
|
public function rightJoin(string $join, array $params = []): static |
254
|
|
|
{ |
255
|
|
|
$this->setBindings($params, 'join'); |
256
|
|
|
|
257
|
|
|
$this->joins .= PHP_EOL; |
258
|
|
|
|
259
|
|
|
$this->joins .= "RIGHT JOIN $join"; |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $join |
266
|
|
|
* @return $this |
267
|
|
|
*/ |
268
|
|
|
public function join(string $join, array $params = []): static |
269
|
|
|
{ |
270
|
|
|
$this->setBindings($params, 'join'); |
271
|
|
|
|
272
|
|
|
$this->joins .= PHP_EOL; |
273
|
|
|
|
274
|
|
|
$this->joins .= "INNER JOIN $join"; |
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function joinSub(Query $subquery, string $alias, $condition, array $params = []) |
279
|
|
|
{ |
280
|
|
|
$this->setBindings($subquery->flatBindings(), 'join'); |
281
|
|
|
$this->setBindings($params, 'join'); |
282
|
|
|
|
283
|
|
|
$this->joins .= PHP_EOL; |
284
|
|
|
|
285
|
|
|
$this->joins .= "INNER JOIN ({$subquery->toSQL()}) AS $alias ON $condition"; |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function leftJoinSub(Query $subquery, string $alias, $condition, array $params = []) |
291
|
|
|
{ |
292
|
|
|
$this->setBindings($subquery->flatBindings(), 'join'); |
293
|
|
|
$this->setBindings($params, 'join'); |
294
|
|
|
|
295
|
|
|
$this->joins .= PHP_EOL; |
296
|
|
|
|
297
|
|
|
$this->joins .= "LEFT JOIN ({$subquery->toSQL()}) AS $alias ON $condition"; |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function rightJoinSub(Query $subquery, string $alias, $condition, array $params = []) |
303
|
|
|
{ |
304
|
|
|
$this->setBindings($subquery->flatBindings(), 'join'); |
305
|
|
|
$this->setBindings($params, 'join'); |
306
|
|
|
|
307
|
|
|
$this->joins .= PHP_EOL; |
308
|
|
|
|
309
|
|
|
$this->joins .= "RIGHT JOIN ({$subquery->toSQL()}) AS $alias ON $condition"; |
310
|
|
|
|
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
|
316
|
|
|
|
317
|
|
|
|
318
|
|
|
public function beginTransaction(): bool |
319
|
|
|
{ |
320
|
|
|
return $this->db->beginTransaction(); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
public function commit(): bool |
325
|
|
|
{ |
326
|
|
|
return $this->db->commit(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
public function rollback(): bool |
331
|
|
|
{ |
332
|
|
|
return $this->db->rollBack(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param $exception |
337
|
|
|
* @throws Exception |
338
|
|
|
*/ |
339
|
|
|
protected function handleError(\Exception $exception) |
340
|
|
|
{ |
341
|
|
|
if (!$this->silentErrors) { |
342
|
|
|
throw $exception; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$this->fail = $exception; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
|
349
|
|
|
protected function mountQuery(): void |
350
|
|
|
{ |
351
|
|
|
|
352
|
|
|
$columns = !empty($this->columns) ? $this->columns : ['*']; |
353
|
|
|
$columns = implode(',', $columns); |
354
|
|
|
$this->query = "SELECT $columns FROM $this->entity $this->joins $this->where $this->groupBy $this->having $this->order $this->limit $this->offset"; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
|
358
|
|
|
|
359
|
|
|
|
360
|
|
|
|
361
|
|
|
public function dump(): array |
362
|
|
|
{ |
363
|
|
|
$this->mountQuery(); |
364
|
|
|
|
365
|
|
|
return [ |
366
|
|
|
"query" => $this->query, |
367
|
|
|
"raw_params" => $this->bindings, |
368
|
|
|
]; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function toSQL() |
372
|
|
|
{ |
373
|
|
|
$this->mountQuery(); |
374
|
|
|
return $this->query; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
|
378
|
|
|
public function flatBindings() |
379
|
|
|
{ |
380
|
|
|
$params = []; |
381
|
|
|
foreach ($this->bindings as $key => $binds) { |
382
|
|
|
$params = array_merge($params, $this->bindings[$key]); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
return $params; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function mergeBindFromAnotherQuery(Query $query) |
389
|
|
|
{ |
390
|
|
|
$array1 = $this->getBindings(); |
391
|
|
|
$array2 = $query->getBindings(); |
392
|
|
|
$mergedArray = [ |
393
|
|
|
'select' => array_merge($array1['select'], $array2['select']), |
394
|
|
|
'from' => array_merge($array1['from'], $array2['from']), |
395
|
|
|
'join' => array_merge($array1['join'], $array2['join']), |
396
|
|
|
'update' => array_merge($array1['update'], $array2['update']), |
397
|
|
|
'where' => array_merge($array1['where'], $array2['where']), |
398
|
|
|
'groupBy' => array_merge($array1['groupBy'], $array2['groupBy']), |
399
|
|
|
'having' => array_merge($array1['having'], $array2['having']), |
400
|
|
|
'order' => array_merge($array1['order'], $array2['order']), |
401
|
|
|
'union' => array_merge($array1['union'], $array2['union']), |
402
|
|
|
'unionOrder' => array_merge($array1['unionOrder'], $array2['unionOrder']), |
403
|
|
|
]; |
404
|
|
|
|
405
|
|
|
$this->bindings = $mergedArray; |
406
|
|
|
} |
407
|
|
|
} |