|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
|
6
|
|
|
|
|
7
|
|
|
use Carbon\CarbonPeriod; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use Illuminate\Contracts\Database\Query\ConditionExpression; |
|
10
|
|
|
use Illuminate\Contracts\Database\Query\Expression; |
|
11
|
|
|
use Illuminate\Support\Arr; |
|
12
|
|
|
|
|
13
|
|
|
trait BuildsGroups |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Add a "group by" clause to the query. |
|
17
|
|
|
* |
|
18
|
|
|
* @param array|\Illuminate\Contracts\Database\Query\Expression|string ...$groups |
|
19
|
|
|
* @return $this |
|
20
|
|
|
*/ |
|
21
|
|
|
public function groupBy(...$groups) |
|
22
|
|
|
{ |
|
23
|
|
|
foreach ($groups as $group) { |
|
24
|
|
|
$this->groups = array_merge( |
|
|
|
|
|
|
25
|
|
|
(array)$this->groups, |
|
26
|
|
|
Arr::wrap($group) |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function having($column, $operator = null, $value = null, $boolean = 'and') |
|
34
|
|
|
{ |
|
35
|
|
|
$type = 'Basic'; |
|
36
|
|
|
|
|
37
|
|
|
if ($column instanceof ConditionExpression) { |
|
38
|
|
|
$type = 'Expression'; |
|
39
|
|
|
|
|
40
|
|
|
$this->havings[] = compact('type', 'column', 'boolean'); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// Here we will make some assumptions about the operator. If only 2 values are |
|
46
|
|
|
// passed to the method, we will assume that the operator is an equals sign |
|
47
|
|
|
// and keep going. Otherwise, we'll require the operator to be passed in. |
|
48
|
|
|
[$value, $operator] = $this->prepareValueAndOperator( |
|
|
|
|
|
|
49
|
|
|
$value, |
|
50
|
|
|
$operator, |
|
51
|
|
|
func_num_args() === 2 |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
if ($column instanceof Closure && is_null($operator)) { |
|
55
|
|
|
return $this->havingNested($column, $boolean); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// If the given operator is not found in the list of valid operators we will |
|
59
|
|
|
// assume that the developer is just short-cutting the '=' operators and |
|
60
|
|
|
// we will set the operators to '=' and set the values appropriately. |
|
61
|
|
|
if ($this->invalidOperator($operator)) { |
|
|
|
|
|
|
62
|
|
|
[$value, $operator] = [$operator, '=']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($this->isBitwiseOperator($operator)) { |
|
|
|
|
|
|
66
|
|
|
$type = 'Bitwise'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!$value instanceof Expression) { |
|
70
|
|
|
$this->addBinding($this->flattenValue($value), 'having'); |
|
|
|
|
|
|
71
|
|
|
$value = '@' . array_key_last($this->getBindings()); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean'); |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Add a raw having clause to the query. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $sql |
|
83
|
|
|
* @param array $bindings |
|
84
|
|
|
* @param string $boolean |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
|
|
public function havingRaw($sql, array $bindings = [], $boolean = 'and') |
|
88
|
|
|
{ |
|
89
|
|
|
$type = 'Raw'; |
|
90
|
|
|
|
|
91
|
|
|
$this->havings[] = compact('type', 'sql', 'boolean'); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
if (!empty($bindings)) { |
|
94
|
|
|
$this->addBinding($bindings, 'having'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Create a new query instance for nested where condition. |
|
102
|
|
|
* |
|
103
|
|
|
* @return \Illuminate\Database\Query\Builder |
|
104
|
|
|
*/ |
|
105
|
|
|
public function forNestedWhere($aliases = []) |
|
106
|
|
|
{ |
|
107
|
|
|
$query = $this->newQuery(); |
|
|
|
|
|
|
108
|
|
|
foreach($aliases as $alias) { |
|
109
|
|
|
$query->groups[] = $alias; |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
return $query->from($this->from); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Add a nested having statement to the query. |
|
117
|
|
|
* |
|
118
|
|
|
* @param \Closure $callback |
|
119
|
|
|
* @param string $boolean |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
|
|
public function havingNested(Closure $callback, $boolean = 'and') |
|
123
|
|
|
{ |
|
124
|
|
|
$callback($query = $this->forNestedWhere($this->groups)); |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
return $this->addNestedHavingQuery($query, $boolean); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Add another query builder as a nested having to the query builder. |
|
133
|
|
|
* |
|
134
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
|
135
|
|
|
* @param string $boolean |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
|
|
public function addNestedHavingQuery($query, $boolean = 'and') |
|
139
|
|
|
{ |
|
140
|
|
|
if (count($query->havings)) { |
|
141
|
|
|
$type = 'Nested'; |
|
142
|
|
|
|
|
143
|
|
|
$this->havings[] = compact('type', 'query', 'boolean'); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
$this->mergeBindings($query); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Add a "having between " clause to the query. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $column |
|
156
|
|
|
* @param iterable $values |
|
157
|
|
|
* @param string $boolean |
|
158
|
|
|
* @param bool $not |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
|
|
public function havingBetween($column, iterable $values, $boolean = 'and', $not = false) |
|
162
|
|
|
{ |
|
163
|
|
|
$type = 'between'; |
|
164
|
|
|
|
|
165
|
|
|
if ($values instanceof CarbonPeriod) { |
|
166
|
|
|
$values = [$values->start, $values->end]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$bindings = array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2); |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
$this->addBinding($bindings[0], 'having'); |
|
172
|
|
|
$values[0] = '@' . array_key_last($this->getBindings()); |
|
173
|
|
|
|
|
174
|
|
|
$this->addBinding($bindings[1], 'having'); |
|
175
|
|
|
$values[1] = '@' . array_key_last($this->getBindings()); |
|
176
|
|
|
|
|
177
|
|
|
$this->havings[] = compact('type', 'column', 'values', 'boolean', 'not'); |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|