|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; |
|
9
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
|
10
|
|
|
use Illuminate\Database\Query\Expression; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
|
|
13
|
|
|
trait QueriesAranguentRelationships |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param mixed $function |
|
17
|
|
|
* @param IlluminateQueryBuilder $query |
|
18
|
|
|
* @param string $alias |
|
19
|
|
|
*/ |
|
20
|
|
|
public function handleAggregateFunction(IlluminateQueryBuilder $query, mixed $function, string $alias): void |
|
21
|
|
|
{ |
|
22
|
|
|
if ($function === null) { |
|
23
|
|
|
$query->limit(1); |
|
24
|
|
|
|
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
if ($function === 'exists') { |
|
30
|
|
|
[$subquery] = $this->getQuery()->createSub($query); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$expression = new Expression(sprintf('(COUNT(%s)) > 0 ? true : false ', $subquery)); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
$this->getQuery()->set( |
|
35
|
|
|
$alias, |
|
36
|
|
|
$expression, |
|
37
|
|
|
'postIterationVariables', |
|
38
|
|
|
) |
|
39
|
|
|
->addSelect($alias); |
|
40
|
|
|
|
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
[$subquery] = $this->getQuery()->createSub($query); |
|
46
|
|
|
|
|
47
|
|
|
$this->getQuery()->set( |
|
48
|
|
|
$alias, |
|
49
|
|
|
new Expression(strtoupper($function) . '(' . $subquery . ')'), |
|
50
|
|
|
'postIterationVariables', |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$this->addSelect($alias); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array<string> $segments |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @return array<int, string|null> |
|
60
|
|
|
*/ |
|
61
|
|
|
public function extractNameAndAlias(array $segments, string $name): array |
|
62
|
|
|
{ |
|
63
|
|
|
$alias = null; |
|
64
|
|
|
|
|
65
|
|
|
if (count($segments) === 3 && Str::lower($segments[1]) === 'as') { |
|
66
|
|
|
[$name, $alias] = [$segments[0], $segments[2]]; |
|
67
|
|
|
} |
|
68
|
|
|
return [$name, $alias]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add a sub-query count clause to this query. |
|
73
|
|
|
* |
|
74
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
|
75
|
|
|
* @param string $operator |
|
76
|
|
|
* @param int $count |
|
77
|
|
|
* @param string $boolean |
|
78
|
|
|
* @return self |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function addWhereCountQuery(IlluminateQueryBuilder $query, $operator = '>=', $count = 1, $boolean = 'and') |
|
81
|
|
|
{ |
|
82
|
|
|
[$subquery] = $this->getQuery()->createSub($query); |
|
83
|
|
|
|
|
84
|
|
|
return $this->where( |
|
|
|
|
|
|
85
|
|
|
new Expression('LENGTH(' . $subquery . ')'), |
|
|
|
|
|
|
86
|
|
|
$operator, |
|
87
|
|
|
new Expression($count), |
|
|
|
|
|
|
88
|
|
|
$boolean, |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Merge the where constraints from another query to the current query. |
|
94
|
|
|
* |
|
95
|
|
|
* @param IlluminateEloquentBuilder $from |
|
96
|
|
|
* @return IlluminateEloquentBuilder|static |
|
97
|
|
|
*/ |
|
98
|
|
|
public function mergeConstraintsFrom(Builder $from) |
|
99
|
|
|
{ |
|
100
|
|
|
$whereBindings = $this->getQuery()->getBindings(); |
|
101
|
|
|
|
|
102
|
|
|
$wheres = $from->getQuery()->from !== $this->getQuery()->from |
|
103
|
|
|
? $this->requalifyWhereTables( |
|
|
|
|
|
|
104
|
|
|
$from->getQuery()->wheres, |
|
105
|
|
|
(string) $from->getQuery()->grammar->getValue($from->getQuery()->from), |
|
106
|
|
|
$this->getModel()->getTable(), |
|
|
|
|
|
|
107
|
|
|
) : $from->getQuery()->wheres; |
|
108
|
|
|
|
|
109
|
|
|
// Here we have some other query that we want to merge the where constraints from. We will |
|
110
|
|
|
// copy over any where constraints on the query as well as remove any global scopes the |
|
111
|
|
|
// query might have removed. Then we will return ourselves with the finished merging. |
|
112
|
|
|
return $this->withoutGlobalScopes( |
|
|
|
|
|
|
113
|
|
|
$from->removedScopes(), |
|
114
|
|
|
)->mergeWheres( |
|
115
|
|
|
$wheres, |
|
116
|
|
|
$whereBindings, |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Add subselect queries to include an aggregate value for a relationship. |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $relations |
|
124
|
|
|
* @param string $column |
|
125
|
|
|
* @param string $function |
|
126
|
|
|
* @return $this |
|
127
|
|
|
* |
|
128
|
|
|
* @SuppressWarnings("PHPMD.CyclomaticComplexity") |
|
129
|
|
|
*/ |
|
130
|
|
|
public function withAggregate($relations, $column, $function = null) |
|
131
|
|
|
{ |
|
132
|
|
|
if (empty($relations)) { |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$table = (string) $this->query->grammar->getValue($this->query->from); |
|
137
|
|
|
|
|
138
|
|
|
if (empty($this->query->columns)) { |
|
139
|
|
|
$this->query->select([$table . '.*']); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$relations = is_array($relations) ? $relations : [$relations]; |
|
143
|
|
|
|
|
144
|
|
|
foreach ($this->parseWithRelations($relations) as $name => $constraints) { |
|
|
|
|
|
|
145
|
|
|
// First we will determine if the name has been aliased using an "as" clause on the name |
|
146
|
|
|
// and if it has we will extract the actual relationship name and the desired name of |
|
147
|
|
|
// the resulting column. This allows multiple aggregates on the same relationships. |
|
148
|
|
|
$segments = explode(' ', $name); |
|
149
|
|
|
|
|
150
|
|
|
[$name, $alias] = $this->extractNameAndAlias($segments, $name); |
|
151
|
|
|
|
|
152
|
|
|
$relation = $this->getRelationWithoutConstraints((string) $name); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
$expression = $column; |
|
155
|
|
|
|
|
156
|
|
|
if ($function) { |
|
157
|
|
|
$hashedColumn = $this->getRelationHashedColumn($column, $relation); |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
$wrappedColumn = $this->getQuery()->getGrammar()->wrap( |
|
160
|
|
|
$column === '*' ? $column : $relation->getRelated()->qualifyColumn($hashedColumn), |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
$expression = $function === 'exists' ? $wrappedColumn : sprintf('%s(%s)', $function, $wrappedColumn); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Here, we will grab the relationship sub-query and prepare to add it to the main query |
|
167
|
|
|
// as a sub-select. First, we'll get the "has" query and use that to get the relation |
|
168
|
|
|
// sub-query. We'll format this relationship name and append this column if needed. |
|
169
|
|
|
$query = $relation->getRelationExistenceQuery( |
|
170
|
|
|
$relation->getRelated()->newQuery(), |
|
171
|
|
|
$this, |
|
172
|
|
|
[$expression], |
|
173
|
|
|
)->setBindings([], 'select'); |
|
174
|
|
|
|
|
175
|
|
|
$query->callScope($constraints); |
|
176
|
|
|
|
|
177
|
|
|
$query = $query->mergeConstraintsFrom($relation->getQuery())->toBase(); |
|
178
|
|
|
|
|
179
|
|
|
// If the query contains certain elements like orderings / more than one column selected |
|
180
|
|
|
// then we will remove those elements from the query so that it will execute properly |
|
181
|
|
|
// when given to the database. Otherwise, we may receive SQL errors or poor syntax. |
|
182
|
|
|
$query->orders = null; |
|
183
|
|
|
|
|
184
|
|
|
$query->setBindings([], 'order'); |
|
185
|
|
|
|
|
186
|
|
|
if (is_array($query->columns) && count($query->columns) > 1) { |
|
187
|
|
|
$query->columns = [$query->columns[0]]; |
|
188
|
|
|
$query->bindings['select'] = []; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
// Finally, we will make the proper column alias to the query and run this sub-select on |
|
192
|
|
|
// the query builder. Then, we will return the builder instance back to the developer |
|
193
|
|
|
// for further constraint chaining that needs to take place on the query as needed. |
|
194
|
|
|
$alias = Str::snake( |
|
195
|
|
|
(string) preg_replace('/[^[:alnum:][:space:]_]/u', '', "$name $function $column"), |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
$this->handleAggregateFunction($query, $function, $alias); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|