|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; |
|
9
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
|
10
|
|
|
use Illuminate\Database\Query\Expression; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Grammar; |
|
13
|
|
|
|
|
14
|
|
|
trait BuildsSelects |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Set the table which the query is targeting. |
|
18
|
|
|
* |
|
19
|
|
|
* @param \Closure|IlluminateQueryBuilder|string $table |
|
20
|
|
|
* @param string|null $as |
|
21
|
|
|
* @return IlluminateQueryBuilder |
|
22
|
|
|
*/ |
|
23
|
|
|
public function from($table, $as = null) |
|
24
|
|
|
{ |
|
25
|
|
|
if ($this->isQueryable($table) && $as !== null) { |
|
|
|
|
|
|
26
|
|
|
return $this->fromSub($table, $as); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
assert(is_string($table)); |
|
30
|
|
|
|
|
31
|
|
|
$this->registerTableAlias($table, $as); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$this->from = $table; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Set the table which the query is targeting. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array<mixed> $options |
|
42
|
|
|
* |
|
43
|
|
|
* @return IlluminateQueryBuilder |
|
44
|
|
|
*/ |
|
45
|
|
|
public function fromOptions($options) |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
$boundOptions = []; |
|
49
|
|
|
foreach ($options as $key => $option) { |
|
50
|
|
|
$boundOptions[$key] = $this->bindValue($option, 'fromOptions'); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->fromOptions = $boundOptions; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set the columns to be selected. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array<mixed>|mixed $columns |
|
62
|
|
|
* @return IlluminateQueryBuilder |
|
63
|
|
|
* @throws Exception |
|
64
|
|
|
*/ |
|
65
|
|
|
public function select($columns = ['*']): IlluminateQueryBuilder |
|
66
|
|
|
{ |
|
67
|
|
|
$this->columns = []; |
|
|
|
|
|
|
68
|
|
|
$this->bindings['select'] = []; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$columns = is_array($columns) ? $columns : func_get_args(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($columns as $as => $column) { |
|
73
|
|
|
if (is_string($as) && $this->isQueryable($column)) { |
|
74
|
|
|
$this->selectSub($column, $as); |
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->addColumns([$as => $column]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add a subselect expression to the query. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|string $query |
|
88
|
|
|
* @param string $as |
|
89
|
|
|
* @return $this |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \InvalidArgumentException |
|
92
|
|
|
* @throws Exception |
|
93
|
|
|
*/ |
|
94
|
|
|
public function selectSub($query, $as) |
|
95
|
|
|
{ |
|
96
|
|
|
[$query] = $this->createSub($query, true); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$this->set($as, new Expression($query), 'postIterationVariables'); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$this->addColumns([$as]); |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Add a new select column to the query. |
|
107
|
|
|
* |
|
108
|
|
|
* @param array|mixed $column |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function addSelect($column) |
|
112
|
|
|
{ |
|
113
|
|
|
$columns = is_array($column) ? $column : func_get_args(); |
|
114
|
|
|
|
|
115
|
|
|
$this->addColumns($columns); |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array<mixed> $columns |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function addColumns(array $columns): void |
|
124
|
|
|
{ |
|
125
|
|
|
$table = (string) $this->grammar->getValue($this->from); |
|
126
|
|
|
|
|
127
|
|
|
foreach ($columns as $as => $column) { |
|
128
|
|
|
if (is_string($as) && $this->isQueryable($column)) { |
|
129
|
|
|
if (empty($this->columns)) { |
|
130
|
|
|
$this->select($table . '.*'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->selectSub($column, $as); |
|
134
|
|
|
|
|
135
|
|
|
continue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if (is_string($as)) { |
|
139
|
|
|
$this->columns[$as] = $column; |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
continue; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->columns[] = $column; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Add an "order by" clause to the query. |
|
150
|
|
|
* |
|
151
|
|
|
* @param \Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|Expression|string $column |
|
152
|
|
|
* @param string $direction |
|
153
|
|
|
* @return $this |
|
154
|
|
|
* |
|
155
|
|
|
* @throws \InvalidArgumentException |
|
156
|
|
|
*/ |
|
157
|
|
|
public function orderBy($column, $direction = 'asc') |
|
158
|
|
|
{ |
|
159
|
|
|
if ($this->isQueryable($column)) { |
|
160
|
|
|
assert(!$column instanceof Expression); |
|
161
|
|
|
[$query, $bindings] = $this->createSub($column); |
|
162
|
|
|
|
|
163
|
|
|
$column = new Expression('(' . $query . ')'); |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
$this->addBinding($bindings, $this->unions ? 'unionOrder' : 'order'); |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$direction = strtoupper($direction); |
|
169
|
|
|
|
|
170
|
|
|
if (!in_array($direction, ['ASC', 'DESC'], true)) { |
|
171
|
|
|
throw new InvalidArgumentException('Order direction must be "asc" or "desc".'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$this->{$this->unions ? 'unionOrders' : 'orders'}[] = [ |
|
175
|
|
|
'column' => $column, |
|
176
|
|
|
'direction' => $direction, |
|
177
|
|
|
]; |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param string $aql |
|
184
|
|
|
* @param array<mixed> $bindings |
|
185
|
|
|
* @return $this |
|
186
|
|
|
*/ |
|
187
|
|
|
public function orderByRaw($aql, $bindings = []) |
|
188
|
|
|
{ |
|
189
|
|
|
$type = 'Raw'; |
|
190
|
|
|
|
|
191
|
|
|
$sql = new Expression($aql); |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
$this->{$this->unions ? 'unionOrders' : 'orders'}[] = compact('type', 'sql'); |
|
194
|
|
|
|
|
195
|
|
|
if (!isset($this->bindings[$this->unions ? 'unionOrders' : 'orders'])) { |
|
196
|
|
|
$this->bindings[$this->unions ? 'unionOrders' : 'orders'] = $bindings; |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$this->bindings[$this->unions ? 'unionOrders' : 'orders'] = array_merge( |
|
202
|
|
|
$this->bindings[$this->unions ? 'unionOrders' : 'orders'], |
|
203
|
|
|
$bindings, |
|
204
|
|
|
); |
|
205
|
|
|
|
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Put the query's results in random order. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $seed |
|
213
|
|
|
* @return $this |
|
214
|
|
|
*/ |
|
215
|
|
|
public function inRandomOrder($seed = '') |
|
216
|
|
|
{ |
|
217
|
|
|
assert($this->grammar instanceof Grammar); |
|
218
|
|
|
|
|
219
|
|
|
// ArangoDB's random function doesn't accept a seed. |
|
220
|
|
|
unset($seed); |
|
221
|
|
|
|
|
222
|
|
|
return $this->orderByRaw($this->grammar->compileRandom()); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Add a union statement to the query. |
|
227
|
|
|
* |
|
228
|
|
|
* @param \Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder $query |
|
229
|
|
|
* @param bool $all |
|
230
|
|
|
* @return $this |
|
231
|
|
|
* |
|
232
|
|
|
* @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
|
233
|
|
|
*/ |
|
234
|
|
|
public function union($query, $all = false) |
|
235
|
|
|
{ |
|
236
|
|
|
if ($query instanceof \Closure) { |
|
237
|
|
|
$query($query = $this->newQuery()); |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
if ($query instanceof IlluminateEloquentBuilder) { |
|
241
|
|
|
$query = $query->getQuery(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$this->importBindings($query); |
|
|
|
|
|
|
245
|
|
|
$this->unions[] = compact('query', 'all'); |
|
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
return $this; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|