Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| GroupBy | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
| groupBy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| groupByAsc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| groupByDesc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addGroupBy | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| renderGroupBy | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Database Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Database\Manipulation\Traits; |
| 11 | |
| 12 | use Closure; |
| 13 | |
| 14 | /** |
| 15 | * Trait GroupBy. |
| 16 | * |
| 17 | * @see https://mariadb.com/kb/en/group-by/ |
| 18 | * |
| 19 | * @package database |
| 20 | * |
| 21 | * @since 3.4 |
| 22 | */ |
| 23 | trait GroupBy |
| 24 | { |
| 25 | /** |
| 26 | * Appends columns to the GROUP BY clause. |
| 27 | * |
| 28 | * @param Closure|string $column The column name or a subquery |
| 29 | * @param Closure|string ...$columns Extra column names and/or subqueries |
| 30 | * |
| 31 | * @return static |
| 32 | */ |
| 33 | public function groupBy(Closure | string $column, Closure | string ...$columns) : static |
| 34 | { |
| 35 | return $this->addGroupBy($column, $columns, null); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Appends columns with the ASC direction to the GROUP BY clause. |
| 40 | * |
| 41 | * @param Closure|string $column The column name or a subquery |
| 42 | * @param Closure|string ...$columns Extra column names and/or subqueries |
| 43 | * |
| 44 | * @return static |
| 45 | */ |
| 46 | public function groupByAsc(Closure | string $column, Closure | string ...$columns) : static |
| 47 | { |
| 48 | return $this->addGroupBy($column, $columns, 'ASC'); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Appends columns with the DESC direction to the GROUP BY clause. |
| 53 | * |
| 54 | * @param Closure|string $column The column name or a subquery |
| 55 | * @param Closure|string ...$columns Extra column names and/or subqueries |
| 56 | * |
| 57 | * @return static |
| 58 | */ |
| 59 | public function groupByDesc(Closure | string $column, Closure | string ...$columns) : static |
| 60 | { |
| 61 | return $this->addGroupBy($column, $columns, 'DESC'); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Adds a GROUP BY expression. |
| 66 | * |
| 67 | * @param Closure|string $column The column name or a subquery |
| 68 | * @param array<Closure|string> $columns Extra column names and/or subqueries |
| 69 | * @param string|null $direction `ASC`, `DESC` or null for none |
| 70 | * |
| 71 | * @return static |
| 72 | */ |
| 73 | private function addGroupBy(Closure | string $column, array $columns, ?string $direction) : static |
| 74 | { |
| 75 | foreach ([$column, ...$columns] as $column) { |
| 76 | $this->sql['group_by'][] = [ |
| 77 | 'column' => $column, |
| 78 | 'direction' => $direction, |
| 79 | ]; |
| 80 | } |
| 81 | return $this; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Renders the GROUP BY clause. |
| 86 | * |
| 87 | * @return string|null The GROUP BY clause or null if it was not set |
| 88 | */ |
| 89 | protected function renderGroupBy() : ?string |
| 90 | { |
| 91 | if ( ! isset($this->sql['group_by'])) { |
| 92 | return null; |
| 93 | } |
| 94 | $expressions = []; |
| 95 | foreach ($this->sql['group_by'] as $part) { |
| 96 | $expression = $this->renderIdentifier($part['column']); |
| 97 | if ($part['direction']) { |
| 98 | $expression .= " {$part['direction']}"; |
| 99 | } |
| 100 | $expressions[] = $expression; |
| 101 | } |
| 102 | $expressions = \implode(', ', $expressions); |
| 103 | return " GROUP BY {$expressions}"; |
| 104 | } |
| 105 | } |