-
Notifications
You must be signed in to change notification settings - Fork 823
/
Copy pathSQLSelect.php
725 lines (649 loc) · 21.6 KB
/
SQLSelect.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<?php
namespace SilverStripe\ORM\Queries;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\DB;
use InvalidArgumentException;
/**
* Object representing a SQL SELECT query.
* The various parts of the SQL query can be manipulated individually.
*/
class SQLSelect extends SQLConditionalExpression
{
/**
* An array of SELECT fields, keyed by an optional alias.
*
* @var array
*/
protected $select = [];
/**
* An array of GROUP BY clauses.
*
* @var array
*/
protected $groupby = [];
/**
* An array of having clauses.
* Each item in this array will be in the form of a single-length array
* in the format ['predicate' => [$parameters]]
*
* @var array
*/
protected $having = [];
/**
* If this is true DISTINCT will be added to the SQL.
*
* @var bool
*/
protected $distinct = false;
/**
* An array of ORDER BY clauses, functions. Stores as an associative
* array of column / function to direction.
*
* May be used on SELECT or single table DELETE queries in some adapters
*
* @var array
*/
protected $orderby = [];
/**
* An array containing limit and offset keys for LIMIT clause.
*
* May be used on SELECT or single table DELETE queries in some adapters
*
* @var array
*/
protected $limit = [];
/**
* Construct a new SQLSelect.
*
* @param array|string $select An array of SELECT fields.
* @param array|string $from An array of FROM clauses. The first one should be just the table name.
* Each should be ANSI quoted.
* @param array $where An array of WHERE clauses.
* @param array $orderby An array ORDER BY clause.
* @param array $groupby An array of GROUP BY clauses.
* @param array $having An array of HAVING clauses.
* @param array|string $limit A LIMIT clause or array with limit and offset keys
* @return static
*/
public static function create(
$select = "*",
$from = [],
$where = [],
$orderby = [],
$groupby = [],
$having = [],
$limit = []
) {
return Injector::inst()->createWithArgs(__CLASS__, func_get_args());
}
/**
* Construct a new SQLSelect.
*
* @param array|string $select An array of SELECT fields.
* @param array|string $from An array of FROM clauses. The first one should be just the table name.
* Each should be ANSI quoted.
* @param array $where An array of WHERE clauses.
* @param array $orderby An array ORDER BY clause.
* @param array $groupby An array of GROUP BY clauses.
* @param array $having An array of HAVING clauses.
* @param array|string $limit A LIMIT clause or array with limit and offset keys
*/
public function __construct(
$select = "*",
$from = [],
$where = [],
$orderby = [],
$groupby = [],
$having = [],
$limit = []
) {
parent::__construct($from, $where);
$this->setSelect($select);
$this->setOrderBy($orderby);
$this->setGroupBy($groupby);
$this->setHaving($having);
$this->setLimit($limit);
}
/**
* Set the list of columns to be selected by the query.
*
* <code>
* // pass fields to select as single parameter array
* $query->setSelect(['"Col1"', '"Col2"'])->setFrom('"MyTable"');
*
* // pass fields to select as multiple parameters
* $query->setSelect('"Col1"', '"Col2"')->setFrom('"MyTable"');
*
* // Set a list of selected fields as aliases
* $query->setSelect(['Name' => '"Col1"', 'Details' => '"Col2"'])->setFrom('"MyTable"');
* </code>
*
* @param string|array $fields Field names should be ANSI SQL quoted. Array keys should be unquoted.
* @return $this Self reference
*/
public function setSelect($fields)
{
$this->select = [];
if (func_num_args() > 1) {
$fields = func_get_args();
} elseif (!is_array($fields)) {
$fields = [$fields];
}
return $this->addSelect($fields);
}
/**
* Add to the list of columns to be selected by the query.
*
* @see setSelect for example usage
*
* @param string|array $fields Field names should be ANSI SQL quoted. Array keys should be unquoted.
* @return $this Self reference
*/
public function addSelect($fields)
{
if (func_num_args() > 1) {
$fields = func_get_args();
} elseif (!is_array($fields)) {
$fields = [$fields];
}
foreach ($fields as $idx => $field) {
$this->selectField($field, is_numeric($idx) ? null : $idx);
}
return $this;
}
/**
* Select an additional field.
*
* @param string $field The field to select (ansi quoted SQL identifier or statement)
* @param string|null $alias The alias of that field (unquoted SQL identifier).
* Defaults to the unquoted column name of the $field parameter.
* @return $this Self reference
*/
public function selectField($field, $alias = null)
{
if (!$alias) {
if (preg_match('/"([^"]+)"$/', $field ?? '', $matches)) {
$alias = $matches[1];
} else {
$alias = $field;
}
}
$this->select[$alias] = $field;
return $this;
}
/**
* Return the SQL expression for the given field alias.
* Returns null if the given alias doesn't exist.
* See {@link selectField()} for details on alias generation.
*
* @param string $field
* @return string
*/
public function expressionForField($field)
{
return isset($this->select[$field]) ? $this->select[$field] : null;
}
/**
* Set distinct property.
*
* @param bool $value
* @return $this Self reference
*/
public function setDistinct($value)
{
$this->distinct = $value;
return $this;
}
/**
* Get the distinct property.
*
* @return bool
*/
public function getDistinct()
{
return $this->distinct;
}
/**
* Get the limit property.
* @return array
*/
public function getLimit()
{
return $this->limit;
}
/**
* Pass LIMIT clause either as SQL snippet or in array format.
* Internally, limit will always be stored as a map containing the keys 'start' and 'limit'
*
* @param int|string|array|null $limit If passed as a string or array, assumes SQL escaped data.
* Only applies for positive values.
* @param int $offset
* @throws InvalidArgumentException
* @return $this Self reference
*/
public function setLimit($limit, $offset = 0)
{
if ((is_numeric($limit) && $limit < 0) || (is_numeric($offset) && $offset < 0)) {
throw new InvalidArgumentException("SQLSelect::setLimit() only takes positive values");
}
if ($limit === 0) {
Deprecation::notice(
'4.3',
"setLimit(0) is deprecated in SS4. To clear limit, call setLimit(null). " .
"In SS5 a limit of 0 will instead return no records."
);
}
if (is_numeric($limit) && ($limit || $offset)) {
$this->limit = [
'start' => (int)$offset,
'limit' => (int)$limit,
];
} elseif ($limit && is_string($limit)) {
if (strpos($limit ?? '', ',') !== false) {
list($start, $innerLimit) = explode(',', $limit ?? '', 2);
} else {
list($innerLimit, $start) = explode(' OFFSET ', strtoupper($limit ?? ''), 2);
}
$this->limit = [
'start' => (int)$start,
'limit' => (int)$innerLimit,
];
} elseif ($limit === null && $offset) {
$this->limit = [
'start' => (int)$offset,
'limit' => $limit
];
} else {
$this->limit = $limit;
}
return $this;
}
/**
* Set ORDER BY clause either as SQL snippet or in array format.
*
* @example $sql->setOrderBy("Column");
* @example $sql->setOrderBy("Column DESC");
* @example $sql->setOrderBy("Column DESC, ColumnTwo ASC");
* @example $sql->setOrderBy("Column", "DESC");
* @example $sql->setOrderBy(["Column" => "ASC", "ColumnTwo" => "DESC"]);
*
* @param string|array $clauses Clauses to add (escaped SQL statement)
* @param string $direction Sort direction, ASC or DESC
*
* @return $this Self reference
*/
public function setOrderBy($clauses = null, $direction = null)
{
$this->orderby = [];
return $this->addOrderBy($clauses, $direction);
}
/**
* Add ORDER BY clause either as SQL snippet or in array format.
*
* @example $sql->addOrderBy("Column");
* @example $sql->addOrderBy("Column DESC");
* @example $sql->addOrderBy("Column DESC, ColumnTwo ASC");
* @example $sql->addOrderBy("Column", "DESC");
* @example $sql->addOrderBy(["Column" => "ASC", "ColumnTwo" => "DESC"]);
*
* @param string|array $clauses Clauses to add (escaped SQL statements)
* @param string $direction Sort direction, ASC or DESC
* @return $this Self reference
*/
public function addOrderBy($clauses = null, $direction = null)
{
if (empty($clauses)) {
return $this;
}
if (is_string($clauses)) {
if (strpos($clauses ?? '', "(") !== false) {
$sort = preg_split("/,(?![^()]*+\\))/", $clauses ?? '');
} else {
$sort = explode(",", $clauses ?? '');
}
$clauses = [];
foreach ($sort as $clause) {
list($column, $direction) = $this->getDirectionFromString($clause, $direction);
$clauses[$column] = $direction;
}
}
if (is_array($clauses)) {
foreach ($clauses as $key => $value) {
if (!is_numeric($key)) {
$column = trim($key ?? '');
$columnDir = strtoupper(trim($value ?? ''));
} else {
list($column, $columnDir) = $this->getDirectionFromString($value);
}
$this->orderby[$column] = $columnDir;
}
} else {
user_error('SQLSelect::orderby() incorrect format for $orderby', E_USER_WARNING);
}
// If sort contains a public function call, let's move the sort clause into a
// separate selected field.
//
// Some versions of MySQL choke if you have a group public function referenced
// directly in the ORDER BY
if ($this->orderby) {
$i = 0;
$orderby = [];
foreach ($this->orderby as $clause => $dir) {
// public function calls and multi-word columns like "CASE WHEN ..."
if (strpos($clause ?? '', '(') !== false || strpos($clause ?? '', " ") !== false) {
// Move the clause to the select fragment, substituting a placeholder column in the sort fragment.
$clause = trim($clause ?? '');
do {
$column = "_SortColumn{$i}";
++$i;
} while (array_key_exists('"' . $column . '"', $this->orderby ?? []));
$this->selectField($clause, $column);
$clause = '"' . $column . '"';
}
$orderby[$clause] = $dir;
}
$this->orderby = $orderby;
}
return $this;
}
/**
* Extract the direction part of a single-column order by clause.
*
* @param string $value
* @param string $defaultDirection
* @return array A two element array: [$column, $direction]
*/
private function getDirectionFromString($value, $defaultDirection = null)
{
if (preg_match('/^(.*)(asc|desc)$/i', $value ?? '', $matches)) {
$column = trim($matches[1] ?? '');
$direction = strtoupper($matches[2] ?? '');
} else {
$column = $value;
$direction = $defaultDirection ? $defaultDirection : "ASC";
}
return [$column, $direction];
}
/**
* Returns the current order by as array if not already. To handle legacy
* statements which are stored as strings. Without clauses and directions,
* convert the orderby clause to something readable.
*
* @return array
*/
public function getOrderBy()
{
$orderby = $this->orderby;
if (!$orderby) {
$orderby = [];
}
if (!is_array($orderby)) {
// spilt by any commas not within brackets
$orderby = preg_split('/,(?![^()]*+\\))/', $orderby ?? '');
}
foreach ($orderby as $k => $v) {
if (strpos($v ?? '', ' ') !== false) {
unset($orderby[$k]);
$rule = explode(' ', trim($v ?? ''));
$clause = $rule[0];
$dir = (isset($rule[1])) ? $rule[1] : 'ASC';
$orderby[$clause] = $dir;
}
}
return $orderby;
}
/**
* Reverses the order by clause by replacing ASC or DESC references in the
* current order by with it's corollary.
*
* @return $this Self reference
*/
public function reverseOrderBy()
{
$order = $this->getOrderBy();
$this->orderby = [];
foreach ($order as $clause => $dir) {
$dir = (strtoupper($dir ?? '') == 'DESC') ? 'ASC' : 'DESC';
$this->addOrderBy($clause, $dir);
}
return $this;
}
/**
* Set a GROUP BY clause.
*
* @param string|array $groupby Escaped SQL statement
* @return $this Self reference
*/
public function setGroupBy($groupby)
{
$this->groupby = [];
return $this->addGroupBy($groupby);
}
/**
* Add a GROUP BY clause.
*
* @param string|array $groupby Escaped SQL statement
* @return $this Self reference
*/
public function addGroupBy($groupby)
{
if (is_array($groupby)) {
$this->groupby = array_merge($this->groupby, $groupby);
} elseif (!empty($groupby)) {
$this->groupby[] = $groupby;
}
return $this;
}
/**
* Set a HAVING clause.
*
* @see SQLSelect::addWhere() for syntax examples
*
* @param mixed ...$having Predicate(s) to set, as escaped SQL statements or parameterised queries
* @return $this Self reference
*/
public function setHaving($having)
{
$having = func_num_args() > 1 ? func_get_args() : $having;
$this->having = [];
return $this->addHaving($having);
}
/**
* Add a HAVING clause
*
* @see SQLSelect::addWhere() for syntax examples
*
* @param mixed ...$having Predicate(s) to set, as escaped SQL statements or parameterised queries
* @return $this Self reference
*/
public function addHaving($having)
{
$having = $this->normalisePredicates(func_get_args());
// If the function is called with an array of items
$this->having = array_merge($this->having, $having);
return $this;
}
/**
* Return a list of HAVING clauses used internally.
* @return array
*/
public function getHaving()
{
return $this->having;
}
/**
* Return a list of HAVING clauses used internally.
*
* @param array $parameters Out variable for parameters required for this query
* @return array
*/
public function getHavingParameterised(&$parameters)
{
$this->splitQueryParameters($this->having, $conditions, $parameters);
return $conditions;
}
/**
* Return a list of GROUP BY clauses used internally.
*
* @return array
*/
public function getGroupBy()
{
return $this->groupby;
}
/**
* Return an itemised select list as a map, where keys are the aliases, and values are the column sources.
* Aliases will always be provided (if the alias is implicit, the alias value will be inferred), and won't be
* quoted.
* E.g., 'Title' => '"SiteTree"."Title"'.
*
* @return array
*/
public function getSelect()
{
return $this->select;
}
/// VARIOUS TRANSFORMATIONS BELOW
/**
* Return the number of rows in this query if the limit were removed. Useful in paged data sets.
*
* @param string $column
* @return int
*/
public function unlimitedRowCount($column = null)
{
// we can't clear the select if we're relying on its output by a HAVING clause
if (count($this->having ?? [])) {
$records = $this->execute();
return $records->numRecords();
}
$clone = clone $this;
$clone->limit = null;
$clone->orderby = null;
// Choose a default column
if ($column == null) {
if ($this->groupby) {
// @todo Test case required here
$countQuery = new SQLSelect();
$countQuery->setSelect("count(*)");
$countQuery->setFrom(['(' . $clone->sql($innerParameters) . ') all_distinct']);
$sql = $countQuery->sql($parameters); // $parameters should be empty
$result = DB::prepared_query($sql, $innerParameters);
return (int)$result->value();
} else {
$clone->setSelect(["count(*)"]);
}
} else {
$clone->setSelect(["count($column)"]);
}
$clone->setGroupBy([]);
return (int)$clone->execute()->value();
}
/**
* Returns true if this query can be sorted by the given field.
*
* @param string $fieldName
* @return bool
*/
public function canSortBy($fieldName)
{
$fieldName = preg_replace('/(\s+?)(A|DE)SC$/', '', $fieldName ?? '');
return isset($this->select[$fieldName]);
}
/**
* Return the number of rows in this query, respecting limit and offset.
*
* @param string $column Quoted, escaped column name
* @return int
*/
public function count($column = null)
{
// we can't clear the select if we're relying on its output by a HAVING clause
if (!empty($this->having)) {
$records = $this->execute();
return $records->numRecords();
} elseif ($column == null) {
// Choose a default column
if ($this->groupby) {
$column = 'DISTINCT ' . implode(", ", $this->groupby);
} else {
$column = '*';
}
}
$clone = clone $this;
$clone->select = ['Count' => "count($column)"];
$clone->limit = null;
$clone->orderby = null;
$clone->groupby = null;
$count = (int)$clone->execute()->value();
// If there's a limit set, then that limit is going to heavily affect the count
if ($this->limit) {
if ($this->limit['limit'] !== null && $count >= ($this->limit['start'] + $this->limit['limit'])) {
return $this->limit['limit'];
} else {
return max(0, $count - $this->limit['start']);
}
// Otherwise, the count is going to be the output of the SQL query
} else {
return $count;
}
}
/**
* Return a new SQLSelect that calls the given aggregate functions on this data.
*
* @param string $column An aggregate expression, such as 'MAX("Balance")', or a set of them
* (as an escaped SQL statement)
* @param string $alias An optional alias for the aggregate column.
* @return SQLSelect A clone of this object with the given aggregate function
*/
public function aggregate($column, $alias = null)
{
$clone = clone $this;
// don't set an ORDER BY clause if no limit has been set. It doesn't make
// sense to add an ORDER BY if there is no limit, and it will break
// queries to databases like MSSQL if you do so. Note that the reason
// this came up is because DataQuery::initialiseQuery() introduces
// a default sort.
if ($this->limit) {
$clone->setLimit($this->limit);
$clone->setOrderBy($this->orderby);
} else {
$clone->setOrderBy([]);
}
$clone->setGroupBy($this->groupby);
if ($alias) {
$clone->setSelect([]);
$clone->selectField($column, $alias);
} else {
$clone->setSelect($column);
}
return $clone;
}
/**
* Returns a query that returns only the first row of this query
*
* @return SQLSelect A clone of this object with the first row only
*/
public function firstRow()
{
$query = clone $this;
$offset = $this->limit ? $this->limit['start'] : 0;
$query->setLimit(1, $offset);
return $query;
}
/**
* Returns a query that returns only the last row of this query
*
* @return SQLSelect A clone of this object with the last row only
*/
public function lastRow()
{
$query = clone $this;
$offset = $this->limit ? $this->limit['start'] : 0;
// Limit index to start in case of empty results
$index = max($this->count() + $offset - 1, 0);
$query->setLimit(1, $index);
return $query;
}
}