|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
|
6
|
|
|
use Illuminate\Database\Query\Expression; |
|
7
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder; |
|
8
|
|
|
|
|
9
|
|
|
trait CompilesGroups |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Compile the "group by" portions of the query. |
|
13
|
|
|
* |
|
14
|
|
|
* @param IlluminateQueryBuilder $query |
|
15
|
|
|
* @param array $groups |
|
16
|
5 |
|
* @return string |
|
17
|
|
|
* @throws \Exception |
|
18
|
5 |
|
*/ |
|
19
|
5 |
|
protected function compileGroups(IlluminateQueryBuilder $query, $groups): string |
|
20
|
5 |
|
{ |
|
21
|
5 |
|
assert($query instanceof Builder); |
|
22
|
|
|
|
|
23
|
|
|
$aql = "COLLECT "; |
|
24
|
5 |
|
|
|
25
|
|
|
$aqlGroups = []; |
|
26
|
5 |
|
foreach ($groups as $group) { |
|
27
|
|
|
if ($group instanceof Expression) { |
|
28
|
|
|
$groupVariable = $this->extractGroupVariable($group); |
|
29
|
|
|
; |
|
30
|
|
|
$query->registerTableAlias($groupVariable, $groupVariable); |
|
31
|
|
|
|
|
32
|
|
|
$aqlGroups[] = $group->getValue($this); |
|
|
|
|
|
|
33
|
|
|
continue; |
|
34
|
|
|
} |
|
35
|
3 |
|
|
|
36
|
|
|
$aqlGroups[] = $group . " = " . $this->normalizeColumn($query, $group); |
|
|
|
|
|
|
37
|
3 |
|
|
|
38
|
|
|
$query->registerTableAlias($group, $group); |
|
39
|
|
|
$query->groupVariables[] = $group; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$aql .= implode(", ", $aqlGroups); |
|
43
|
|
|
|
|
44
|
|
|
$variablesToKeep = $this->keepColumns($query, $groups); |
|
45
|
|
|
|
|
46
|
|
|
if (!empty($variablesToKeep)) { |
|
47
|
|
|
$query->registerTableAlias('groupsVariable', 'groupsVariable'); |
|
48
|
|
|
$query->groupVariables[] = 'groupsVariable'; |
|
49
|
|
|
|
|
50
|
|
|
$aql .= ' INTO groupsVariable = ' . $this->generateAqlObject($variablesToKeep); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
return $aql; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param IlluminateQueryBuilder $query |
|
57
|
|
|
* @param $groups |
|
58
|
|
|
* @return array |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function keepColumns(IlluminateQueryBuilder $query, $groups) |
|
62
|
|
|
{ |
|
63
|
|
|
$tempGroups = []; |
|
64
|
|
|
foreach($groups as $group) { |
|
65
|
|
|
if ($group instanceof Expression) { |
|
66
|
|
|
$tempGroups[] = $this->extractGroupVariable($group); |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
$tempGroups[] = $group; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$diff = array_diff_assoc($query->columns, $tempGroups); |
|
73
|
|
|
|
|
74
|
|
|
$results = []; |
|
75
|
|
|
foreach ($diff as $key => $value) { |
|
76
|
|
|
if (is_numeric($key)) { |
|
77
|
|
|
$results[$value] = $this->normalizeColumn($query, $value); |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
$results[$key] = $this->normalizeColumn($query, $value); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $results; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function extractGroupVariable(Expression $group) |
|
87
|
|
|
{ |
|
88
|
|
|
return explode(' = ', $group->getValue($this))[0]; |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|