1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 6/3/14 |
5
|
|
|
* Time: 12:07 AM. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Sql\QueryBuilder\Manipulation; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory; |
14
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\Table; |
15
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\Where; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Select. |
19
|
|
|
*/ |
20
|
|
|
class Select extends AbstractBaseQuery |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Table |
24
|
|
|
*/ |
25
|
|
|
protected $table; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $groupBy = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $camelCaseTableName = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Where |
39
|
|
|
*/ |
40
|
|
|
protected $having; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $havingOperator = 'AND'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $isDistinct = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Where |
54
|
|
|
*/ |
55
|
|
|
protected $where; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var JoinQuery |
59
|
|
|
*/ |
60
|
|
|
protected $joinQuery; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var ColumnQuery |
64
|
|
|
*/ |
65
|
|
|
protected $columnQuery; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $table |
69
|
|
|
* @param array $columns |
70
|
|
|
*/ |
71
|
|
|
public function __construct($table = null, array $columns = null) |
72
|
|
|
{ |
73
|
|
|
if (isset($table)) { |
74
|
|
|
$this->setTable($table); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->joinQuery = new JoinQuery($this); |
78
|
|
|
$this->columnQuery = new ColumnQuery($this, $this->joinQuery, $columns); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* This __clone method will create an exact clone but without the object references due to the fact these |
83
|
|
|
* are lost in the process of serialization and un-serialization. |
84
|
|
|
* |
85
|
|
|
* @return Select |
86
|
|
|
*/ |
87
|
|
|
public function __clone() |
88
|
|
|
{ |
89
|
|
|
return \unserialize(\serialize($this)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function partName() |
96
|
|
|
{ |
97
|
|
|
return 'SELECT'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $table |
102
|
|
|
* @param string $selfColumn |
103
|
|
|
* @param string $refColumn |
104
|
|
|
* @param string[] $columns |
105
|
|
|
* |
106
|
|
|
* @return Select |
107
|
|
|
*/ |
108
|
|
|
public function leftJoin($table, $selfColumn = null, $refColumn = null, $columns = []) |
109
|
|
|
{ |
110
|
|
|
return $this->joinQuery->leftJoin($table, $selfColumn, $refColumn, $columns); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $table |
115
|
|
|
* @param string $selfColumn |
116
|
|
|
* @param string $refColumn |
117
|
|
|
* @param string[] $columns |
118
|
|
|
* @param string $joinType |
119
|
|
|
* |
120
|
|
|
* @return Select |
121
|
|
|
*/ |
122
|
|
|
public function join( |
123
|
|
|
$table, |
124
|
|
|
$selfColumn = null, |
125
|
|
|
$refColumn = null, |
126
|
|
|
$columns = [], |
127
|
|
|
$joinType = null |
128
|
|
|
) { |
129
|
|
|
return $this->joinQuery->join($table, $selfColumn, $refColumn, $columns, $joinType); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* WHERE constrains used for the ON clause of a (LEFT/RIGHT/INNER/CROSS) JOIN. |
134
|
|
|
* |
135
|
|
|
* @return Where |
136
|
|
|
*/ |
137
|
|
|
public function joinCondition() |
138
|
|
|
{ |
139
|
|
|
return $this->joinQuery->joinCondition(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param Select $select |
144
|
|
|
* @param string $selfColumn |
145
|
|
|
* @param string $refColumn |
146
|
|
|
* |
147
|
|
|
* @return Select |
148
|
|
|
*/ |
149
|
|
|
public function addJoin(Select $select, $selfColumn, $refColumn) |
150
|
|
|
{ |
151
|
|
|
return $this->joinQuery->addJoin($select, $selfColumn, $refColumn); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Transforms Select in a joint. |
156
|
|
|
* |
157
|
|
|
* @param bool $isJoin |
158
|
|
|
* |
159
|
|
|
* @return JoinQuery |
160
|
|
|
*/ |
161
|
|
|
public function isJoin($isJoin = true) |
162
|
|
|
{ |
163
|
|
|
return $this->joinQuery->setJoin($isJoin); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $table |
168
|
|
|
* @param string $selfColumn |
169
|
|
|
* @param string $refColumn |
170
|
|
|
* @param string[] $columns |
171
|
|
|
* |
172
|
|
|
* @internal param null $selectClass |
173
|
|
|
* |
174
|
|
|
* @return Select |
175
|
|
|
*/ |
176
|
|
|
public function rightJoin($table, $selfColumn = null, $refColumn = null, $columns = []) |
177
|
|
|
{ |
178
|
|
|
return $this->joinQuery->rightJoin($table, $selfColumn, $refColumn, $columns); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $table |
183
|
|
|
* @param string $selfColumn |
184
|
|
|
* @param string $refColumn |
185
|
|
|
* @param string[] $columns |
186
|
|
|
* |
187
|
|
|
* @return Select |
188
|
|
|
*/ |
189
|
|
|
public function crossJoin($table, $selfColumn = null, $refColumn = null, $columns = []) |
190
|
|
|
{ |
191
|
|
|
return $this->joinQuery->crossJoin($table, $selfColumn, $refColumn, $columns); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $table |
196
|
|
|
* @param string $selfColumn |
197
|
|
|
* @param string $refColumn |
198
|
|
|
* @param string[] $columns |
199
|
|
|
* |
200
|
|
|
* @return Select |
201
|
|
|
*/ |
202
|
|
|
public function innerJoin($table, $selfColumn = null, $refColumn = null, $columns = []) |
203
|
|
|
{ |
204
|
|
|
return $this->joinQuery->innerJoin($table, $selfColumn, $refColumn, $columns); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Alias to joinCondition. |
209
|
|
|
* |
210
|
|
|
* @return Where |
211
|
|
|
*/ |
212
|
|
|
public function on() |
213
|
|
|
{ |
214
|
|
|
return $this->joinQuery->joinCondition(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
public function isJoinSelect() |
221
|
|
|
{ |
222
|
|
|
return $this->joinQuery->isJoin(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
public function getAllColumns() |
229
|
|
|
{ |
230
|
|
|
return $this->columnQuery->getAllColumns(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Column |
235
|
|
|
* |
236
|
|
|
* @throws QueryException |
237
|
|
|
*/ |
238
|
|
|
public function getColumns() |
239
|
|
|
{ |
240
|
|
|
return $this->columnQuery->getColumns(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Sets the column names used to write the SELECT statement. |
245
|
|
|
* If key is set, key is the column's alias. Value is always the column names. |
246
|
|
|
* |
247
|
|
|
* @param string[] $columns |
248
|
|
|
* |
249
|
|
|
* @return ColumnQuery |
250
|
|
|
*/ |
251
|
|
|
public function setColumns(array $columns) |
252
|
|
|
{ |
253
|
|
|
return $this->columnQuery->setColumns($columns); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Allows setting a Select query as a column value. |
258
|
|
|
* |
259
|
|
|
* @param array $column |
260
|
|
|
* |
261
|
|
|
* @return ColumnQuery |
262
|
|
|
*/ |
263
|
|
|
public function setSelectAsColumn(array $column) |
264
|
|
|
{ |
265
|
|
|
return $this->columnQuery->setSelectAsColumn($column); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
|
|
public function getColumnSelects() |
272
|
|
|
{ |
273
|
|
|
return $this->columnQuery->getColumnSelects(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Allows setting a value to the select statement. |
278
|
|
|
* |
279
|
|
|
* @param string $value |
280
|
|
|
* @param string $alias |
281
|
|
|
* |
282
|
|
|
* @return ColumnQuery |
283
|
|
|
*/ |
284
|
|
|
public function setValueAsColumn($value, $alias) |
285
|
|
|
{ |
286
|
|
|
return $this->columnQuery->setValueAsColumn($value, $alias); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return array |
291
|
|
|
*/ |
292
|
|
|
public function getColumnValues() |
293
|
|
|
{ |
294
|
|
|
return $this->columnQuery->getColumnValues(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Allows calculation on columns using predefined SQL functions. |
299
|
|
|
* |
300
|
|
|
* @param string $funcName |
301
|
|
|
* @param string[] $arguments |
302
|
|
|
* @param string $alias |
303
|
|
|
* |
304
|
|
|
* @return ColumnQuery |
305
|
|
|
*/ |
306
|
|
|
public function setFunctionAsColumn($funcName, array $arguments, $alias) |
307
|
|
|
{ |
308
|
|
|
return $this->columnQuery->setFunctionAsColumn($funcName, $arguments, $alias); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return array |
313
|
|
|
*/ |
314
|
|
|
public function getColumnFuncs() |
315
|
|
|
{ |
316
|
|
|
return $this->columnQuery->getColumnFuncs(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Returns all the Where conditions to the BuilderInterface class in order to write the SQL WHERE statement. |
321
|
|
|
* |
322
|
|
|
* @return array |
323
|
|
|
*/ |
324
|
|
|
public function getAllWheres() |
325
|
|
|
{ |
326
|
|
|
return $this->getAllOperation($this->where, 'getAllWheres'); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param null|Where $data |
331
|
|
|
* @param string $operation |
332
|
|
|
* |
333
|
|
|
* @return array |
334
|
|
|
*/ |
335
|
|
|
protected function getAllOperation($data, $operation) |
336
|
|
|
{ |
337
|
|
|
$collection = []; |
338
|
|
|
|
339
|
|
|
if (!is_null($data)) { |
340
|
|
|
$collection[] = $data; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
foreach ($this->joinQuery->getJoins() as $join) { |
344
|
|
|
$collection = \array_merge($collection, $join->$operation()); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $collection; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @return array |
352
|
|
|
*/ |
353
|
|
|
public function getAllHavings() |
354
|
|
|
{ |
355
|
|
|
return $this->getAllOperation($this->having, 'getAllHavings'); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @param string $columnName |
360
|
|
|
* @param string $alias |
361
|
|
|
* |
362
|
|
|
* @return ColumnQuery |
363
|
|
|
*/ |
364
|
|
|
public function count($columnName = '*', $alias = '') |
365
|
|
|
{ |
366
|
|
|
return $this->columnQuery->count($columnName, $alias); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* @return bool |
371
|
|
|
*/ |
372
|
|
|
public function isCount() |
373
|
|
|
{ |
374
|
|
|
return $this->columnQuery->isCount(); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param int $start |
379
|
|
|
* @param $count |
380
|
|
|
* |
381
|
|
|
* @return $this |
382
|
|
|
*/ |
383
|
|
|
public function limit($start, $count = 0) |
384
|
|
|
{ |
385
|
|
|
$this->limitStart = $start; |
386
|
|
|
$this->limitCount = $count; |
387
|
|
|
|
388
|
|
|
return $this; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @return array |
393
|
|
|
*/ |
394
|
|
|
public function getAllJoins() |
395
|
|
|
{ |
396
|
|
|
return $this->joinQuery->getAllJoins(); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @return array |
401
|
|
|
*/ |
402
|
|
|
public function getGroupBy() |
403
|
|
|
{ |
404
|
|
|
return SyntaxFactory::createColumns($this->groupBy, $this->getTable()); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @param string[] $columns |
409
|
|
|
* |
410
|
|
|
* @return $this |
411
|
|
|
*/ |
412
|
|
|
public function groupBy(array $columns) |
413
|
|
|
{ |
414
|
|
|
$this->groupBy = $columns; |
415
|
|
|
|
416
|
|
|
return $this; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* @return Where |
421
|
|
|
*/ |
422
|
|
|
public function getJoinCondition() |
423
|
|
|
{ |
424
|
|
|
return $this->joinQuery->getJoinCondition(); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @return string |
429
|
|
|
*/ |
430
|
|
|
public function getJoinType() |
431
|
|
|
{ |
432
|
|
|
return $this->joinQuery->getJoinType(); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @param string|null $joinType |
437
|
|
|
* |
438
|
|
|
* @return $this |
439
|
|
|
*/ |
440
|
|
|
public function setJoinType($joinType) |
441
|
|
|
{ |
442
|
|
|
$this->joinQuery->setJoinType($joinType); |
443
|
|
|
|
444
|
|
|
return $this; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @param $havingOperator |
449
|
|
|
* |
450
|
|
|
* @throws QueryException |
451
|
|
|
* |
452
|
|
|
* @return Where |
453
|
|
|
*/ |
454
|
|
|
public function having($havingOperator = 'AND') |
455
|
|
|
{ |
456
|
|
|
if (!isset($this->having)) { |
457
|
|
|
$this->having = QueryFactory::createWhere($this); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
if (!in_array($havingOperator, array(Where::CONJUNCTION_AND, Where::CONJUNCTION_OR))) { |
461
|
|
|
throw new QueryException( |
462
|
|
|
"Invalid conjunction specified, must be one of AND or OR, but '".$havingOperator."' was found." |
463
|
|
|
); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
$this->havingOperator = $havingOperator; |
467
|
|
|
|
468
|
|
|
return $this->having; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* @return string |
473
|
|
|
*/ |
474
|
|
|
public function getHavingOperator() |
475
|
|
|
{ |
476
|
|
|
return $this->havingOperator; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* @return $this |
481
|
|
|
*/ |
482
|
|
|
public function distinct() |
483
|
|
|
{ |
484
|
|
|
$this->isDistinct = true; |
485
|
|
|
|
486
|
|
|
return $this; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* @return bool |
491
|
|
|
*/ |
492
|
|
|
public function isDistinct() |
493
|
|
|
{ |
494
|
|
|
return $this->isDistinct; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* @return array |
499
|
|
|
*/ |
500
|
|
|
public function getAllOrderBy() |
501
|
|
|
{ |
502
|
|
|
$order = $this->orderBy; |
503
|
|
|
|
504
|
|
|
foreach ($this->joinQuery->getJoins() as $join) { |
505
|
|
|
$order = \array_merge($order, $join->getAllOrderBy()); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
return $order; |
509
|
|
|
} |
510
|
|
|
} |
511
|
|
|
|