1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phuria\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use Phuria\QueryBuilder\Table\AbstractTable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class QueryBuilder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var TableFactory $tableFactory |
14
|
|
|
*/ |
15
|
|
|
private $tableFactory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var CompilerManager $compilerManager |
19
|
|
|
*/ |
20
|
|
|
private $compilerManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array $selectClauses |
24
|
|
|
*/ |
25
|
|
|
private $selectClauses; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array $whereClauses |
29
|
|
|
*/ |
30
|
|
|
private $whereClauses; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array $orderByClauses |
34
|
|
|
*/ |
35
|
|
|
private $orderByClauses; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array $setClauses |
39
|
|
|
*/ |
40
|
|
|
private $setClauses; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array $groupByClauses |
44
|
|
|
*/ |
45
|
|
|
private $groupByClauses; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array $havingClauses |
49
|
|
|
*/ |
50
|
|
|
private $havingClauses; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var AbstractTable[] $tables |
54
|
|
|
*/ |
55
|
|
|
private $tables = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool $highPriority |
59
|
|
|
*/ |
60
|
|
|
private $highPriority; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var int $maxStatementTime |
64
|
|
|
*/ |
65
|
|
|
private $maxStatementTime; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var bool $straightJoin |
69
|
|
|
*/ |
70
|
|
|
private $straightJoin; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var bool $sqlSmallResult |
74
|
|
|
*/ |
75
|
|
|
private $sqlSmallResult; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var bool $sqlBigResult |
79
|
|
|
*/ |
80
|
|
|
private $sqlBigResult; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var bool $sqlBufferResult |
84
|
|
|
*/ |
85
|
|
|
private $sqlBufferResult; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var bool $sqlNoCache |
89
|
|
|
*/ |
90
|
|
|
private $sqlNoCache; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var bool $sqlCalcFoundRows |
94
|
|
|
*/ |
95
|
|
|
private $sqlCalcFoundRows; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param TableFactory $tableFactory |
99
|
|
|
* @param CompilerManager $compilerManager |
100
|
|
|
*/ |
101
|
|
|
public function __construct(TableFactory $tableFactory = null, CompilerManager $compilerManager = null) |
102
|
|
|
{ |
103
|
|
|
$this->tableFactory = $tableFactory ?: new TableFactory(); |
104
|
|
|
$this->compilerManager = $compilerManager ?: new CompilerManager(); |
105
|
|
|
$this->selectClauses = []; |
106
|
|
|
$this->whereClauses = []; |
107
|
|
|
$this->orderByClauses = []; |
108
|
|
|
$this->groupByClauses = []; |
109
|
|
|
$this->setClauses = []; |
110
|
|
|
$this->havingClauses = []; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return ExprBuilder |
115
|
|
|
*/ |
116
|
|
|
public function expr() |
117
|
|
|
{ |
118
|
|
|
return new ExprBuilder(func_get_args()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function addSelect() |
125
|
|
|
{ |
126
|
|
|
$this->selectClauses[] = ExprBuilder::normalizeExpression(func_get_args()); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function andWhere() |
135
|
|
|
{ |
136
|
|
|
$this->whereClauses[] = ExprBuilder::normalizeExpression(func_get_args()); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function andHaving() |
145
|
|
|
{ |
146
|
|
|
$this->havingClauses[] = ExprBuilder::normalizeExpression(func_get_args()); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $table |
153
|
|
|
* |
154
|
|
|
* @return AbstractTable |
155
|
|
|
*/ |
156
|
|
|
private function addRootTable($table) |
157
|
|
|
{ |
158
|
|
|
$table = $this->tableFactory->createNewTable($table, $this); |
159
|
|
|
$table->setRoot(true); |
160
|
|
|
|
161
|
|
|
$this->tables[] = $table; |
162
|
|
|
|
163
|
|
|
return $table; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param mixed $table |
168
|
|
|
* |
169
|
|
|
* @return AbstractTable |
170
|
|
|
*/ |
171
|
|
|
public function from($table) |
172
|
|
|
{ |
173
|
|
|
return $this->addFrom($table); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param mixed $table |
178
|
|
|
* |
179
|
|
|
* @return AbstractTable |
180
|
|
|
*/ |
181
|
|
|
public function addFrom($table) |
182
|
|
|
{ |
183
|
|
|
return $this->addRootTable($table); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param mixed $table |
188
|
|
|
* |
189
|
|
|
* @return AbstractTable |
190
|
|
|
*/ |
191
|
|
|
public function update($table) |
192
|
|
|
{ |
193
|
|
|
return $this->addRootTable($table); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $joinType |
198
|
|
|
* @param mixed $table |
199
|
|
|
* |
200
|
|
|
* @return AbstractTable |
201
|
|
|
*/ |
202
|
|
|
public function join($joinType, $table) |
203
|
|
|
{ |
204
|
|
|
$table = $this->tableFactory->createNewTable($table, $this); |
205
|
|
|
$table->setJoinType($joinType); |
206
|
|
|
|
207
|
|
|
$this->tables[] = $table; |
208
|
|
|
|
209
|
|
|
return $table; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param mixed $table |
214
|
|
|
* |
215
|
|
|
* @return AbstractTable |
216
|
|
|
*/ |
217
|
|
|
public function crossJoin($table) |
218
|
|
|
{ |
219
|
|
|
return $this->join(AbstractTable::CROSS_JOIN, $table); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param mixed $table |
224
|
|
|
* |
225
|
|
|
* @return AbstractTable |
226
|
|
|
*/ |
227
|
|
|
public function leftJoin($table) |
228
|
|
|
{ |
229
|
|
|
return $this->join(AbstractTable::LEFT_JOIN, $table); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $table |
234
|
|
|
* |
235
|
|
|
* @return AbstractTable |
236
|
|
|
*/ |
237
|
|
|
public function innerJoin($table) |
238
|
|
|
{ |
239
|
|
|
return $this->join(AbstractTable::INNER_JOIN, $table); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return $this |
244
|
|
|
*/ |
245
|
|
|
public function addOrderBy() |
246
|
|
|
{ |
247
|
|
|
$this->orderByClauses[] = ExprBuilder::normalizeExpression(func_get_args()); |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return $this |
254
|
|
|
*/ |
255
|
|
|
public function addSet() |
256
|
|
|
{ |
257
|
|
|
$this->setClauses[] = ExprBuilder::normalizeExpression(func_get_args()); |
258
|
|
|
|
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return $this |
264
|
|
|
*/ |
265
|
|
|
public function addGroupBy() |
266
|
|
|
{ |
267
|
|
|
$this->groupByClauses[] = ExprBuilder::normalizeExpression(func_get_args()); |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return string |
274
|
|
|
*/ |
275
|
|
|
public function buildSQL() |
276
|
|
|
{ |
277
|
|
|
return $this->compilerManager->compile($this); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return Query |
282
|
|
|
*/ |
283
|
|
|
public function buildQuery() |
284
|
|
|
{ |
285
|
|
|
return new Query($this->buildSQL()); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return array |
290
|
|
|
*/ |
291
|
|
|
public function getSelectClauses() |
292
|
|
|
{ |
293
|
|
|
return $this->selectClauses; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return array |
298
|
|
|
*/ |
299
|
|
|
public function getWhereClauses() |
300
|
|
|
{ |
301
|
|
|
return $this->whereClauses; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return array |
306
|
|
|
*/ |
307
|
|
|
public function getOrderByClauses() |
308
|
|
|
{ |
309
|
|
|
return $this->orderByClauses; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return array |
314
|
|
|
*/ |
315
|
|
|
public function getSetClauses() |
316
|
|
|
{ |
317
|
|
|
return $this->setClauses; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @return array |
322
|
|
|
*/ |
323
|
|
|
public function getGroupByClauses() |
324
|
|
|
{ |
325
|
|
|
return $this->groupByClauses; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @return array |
330
|
|
|
*/ |
331
|
|
|
public function getHavingClauses() |
332
|
|
|
{ |
333
|
|
|
return $this->havingClauses; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @return AbstractTable[] |
338
|
|
|
*/ |
339
|
|
|
public function getTables() |
340
|
|
|
{ |
341
|
|
|
return $this->tables; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @return AbstractTable[] |
346
|
|
|
*/ |
347
|
|
|
public function getRootTables() |
348
|
|
|
{ |
349
|
|
|
return array_filter($this->getTables(), function (AbstractTable $table) { |
350
|
|
|
return $table->isRoot(); |
351
|
|
|
}); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return AbstractTable[] |
356
|
|
|
*/ |
357
|
|
|
public function getJoinTables() |
358
|
|
|
{ |
359
|
|
|
return array_filter($this->getTables(), function (AbstractTable $table) { |
360
|
|
|
return $table->isJoin(); |
361
|
|
|
}); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @return boolean |
366
|
|
|
*/ |
367
|
|
|
public function isHighPriority() |
368
|
|
|
{ |
369
|
|
|
return $this->highPriority; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param boolean $highPriority |
374
|
|
|
* |
375
|
|
|
* @return $this |
376
|
|
|
*/ |
377
|
|
|
public function setHighPriority($highPriority) |
378
|
|
|
{ |
379
|
|
|
$this->highPriority = $highPriority; |
380
|
|
|
|
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @return int |
386
|
|
|
*/ |
387
|
|
|
public function getMaxStatementTime() |
388
|
|
|
{ |
389
|
|
|
return $this->maxStatementTime; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* @param integer $maxStatementTime |
394
|
|
|
* |
395
|
|
|
* @return $this |
396
|
|
|
*/ |
397
|
|
|
public function setMaxStatementTime($maxStatementTime) |
398
|
|
|
{ |
399
|
|
|
$this->maxStatementTime = $maxStatementTime; |
400
|
|
|
|
401
|
|
|
return $this; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @return boolean |
406
|
|
|
*/ |
407
|
|
|
public function isStraightJoin() |
408
|
|
|
{ |
409
|
|
|
return $this->straightJoin; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @param boolean $straightJoin |
414
|
|
|
* |
415
|
|
|
* @return $this |
416
|
|
|
*/ |
417
|
|
|
public function setStraightJoin($straightJoin) |
418
|
|
|
{ |
419
|
|
|
$this->straightJoin = $straightJoin; |
420
|
|
|
|
421
|
|
|
return $this; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* @return boolean |
426
|
|
|
*/ |
427
|
|
|
public function isSqlSmallResult() |
428
|
|
|
{ |
429
|
|
|
return $this->sqlSmallResult; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @param boolean $sqlSmallResult |
434
|
|
|
* |
435
|
|
|
* @return $this |
436
|
|
|
*/ |
437
|
|
|
public function setSqlSmallResult($sqlSmallResult) |
438
|
|
|
{ |
439
|
|
|
$this->sqlSmallResult = $sqlSmallResult; |
440
|
|
|
|
441
|
|
|
return $this; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @return boolean |
446
|
|
|
*/ |
447
|
|
|
public function isSqlBigResult() |
448
|
|
|
{ |
449
|
|
|
return $this->sqlBigResult; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @param boolean $sqlBigResult |
454
|
|
|
* |
455
|
|
|
* @return $this |
456
|
|
|
*/ |
457
|
|
|
public function setSqlBigResult($sqlBigResult) |
458
|
|
|
{ |
459
|
|
|
$this->sqlBigResult = $sqlBigResult; |
460
|
|
|
|
461
|
|
|
return $this; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* @return boolean |
466
|
|
|
*/ |
467
|
|
|
public function isSqlBufferResult() |
468
|
|
|
{ |
469
|
|
|
return $this->sqlBufferResult; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* @param boolean $sqlBufferResult |
474
|
|
|
* |
475
|
|
|
* @return $this |
476
|
|
|
*/ |
477
|
|
|
public function setSqlBufferResult($sqlBufferResult) |
478
|
|
|
{ |
479
|
|
|
$this->sqlBufferResult = $sqlBufferResult; |
480
|
|
|
|
481
|
|
|
return $this; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @return boolean |
486
|
|
|
*/ |
487
|
|
|
public function isSqlNoCache() |
488
|
|
|
{ |
489
|
|
|
return $this->sqlNoCache; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* @param boolean $sqlNoCache |
494
|
|
|
* |
495
|
|
|
* @return $this |
496
|
|
|
*/ |
497
|
|
|
public function setSqlNoCache($sqlNoCache) |
498
|
|
|
{ |
499
|
|
|
$this->sqlNoCache = $sqlNoCache; |
500
|
|
|
|
501
|
|
|
return $this; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* @return boolean |
506
|
|
|
*/ |
507
|
|
|
public function isSqlCalcFoundRows() |
508
|
|
|
{ |
509
|
|
|
return $this->sqlCalcFoundRows; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* @param boolean $sqlCalcFoundRows |
514
|
|
|
* |
515
|
|
|
* @return $this |
516
|
|
|
*/ |
517
|
|
|
public function setSqlCalcFoundRows($sqlCalcFoundRows) |
518
|
|
|
{ |
519
|
|
|
$this->sqlCalcFoundRows = $sqlCalcFoundRows; |
520
|
|
|
|
521
|
|
|
return $this; |
522
|
|
|
} |
523
|
|
|
} |