|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\FluentAQL; |
|
6
|
|
|
|
|
7
|
|
|
use LaravelFreelancerNL\FluentAQL\AQL\HasFunctions; |
|
8
|
|
|
use LaravelFreelancerNL\FluentAQL\AQL\HasGraphClauses; |
|
9
|
|
|
use LaravelFreelancerNL\FluentAQL\AQL\HasOperatorExpressions; |
|
10
|
|
|
use LaravelFreelancerNL\FluentAQL\AQL\HasQueryClauses; |
|
11
|
|
|
use LaravelFreelancerNL\FluentAQL\AQL\HasStatementClauses; |
|
12
|
|
|
use LaravelFreelancerNL\FluentAQL\AQL\HasSupportCommands; |
|
13
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\Clause; |
|
14
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException; |
|
15
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\BindExpression; |
|
16
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\Expression; |
|
17
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface; |
|
18
|
|
|
use LaravelFreelancerNL\FluentAQL\Traits\CompilesPredicates; |
|
19
|
|
|
use LaravelFreelancerNL\FluentAQL\Traits\NormalizesExpressions; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class QueryBuilder |
|
23
|
|
|
* Fluent ArangoDB AQL Query Builder. |
|
24
|
|
|
* Creates and compiles AQL queries. Returns all data necessary to run the query, |
|
25
|
|
|
* including bindings and a list of used read/write collections. |
|
26
|
|
|
*/ |
|
27
|
|
|
class QueryBuilder |
|
28
|
|
|
{ |
|
29
|
|
|
use NormalizesExpressions; |
|
30
|
|
|
use CompilesPredicates; |
|
|
|
|
|
|
31
|
|
|
use HasQueryClauses; |
|
32
|
|
|
use HasStatementClauses; |
|
33
|
|
|
use HasGraphClauses; |
|
34
|
|
|
use HasFunctions; |
|
35
|
|
|
use HasOperatorExpressions; |
|
36
|
|
|
use HasSupportCommands; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The database query grammar instance. |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
|
|
public Grammar $grammar; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The AQL query. |
|
46
|
|
|
*/ |
|
47
|
|
|
public ?string $query = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Bindings for $query. |
|
51
|
|
|
* |
|
52
|
|
|
* @var array<mixed> $binds |
|
53
|
|
|
*/ |
|
54
|
|
|
public array $binds = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* List of read/write/exclusive collections required for transactions. |
|
58
|
|
|
* |
|
59
|
|
|
* @var array<mixed> $collections |
|
60
|
|
|
*/ |
|
61
|
|
|
public $collections = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* List of Clauses to be compiled into a query. |
|
65
|
|
|
* |
|
66
|
|
|
* @var array<mixed> $commands |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $commands = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Registry of variable names used in this query. |
|
72
|
|
|
* |
|
73
|
|
|
* @var array<mixed> $variables |
|
74
|
|
|
*/ |
|
75
|
|
|
protected array $variables = []; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* ID of the query |
|
79
|
|
|
* Used as prefix for automatically generated bindings. |
|
80
|
|
|
* |
|
81
|
|
|
* @var int |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $queryId = 1; |
|
84
|
|
|
|
|
85
|
28 |
|
public function __construct() |
|
86
|
|
|
{ |
|
87
|
28 |
|
$this->grammar = new Grammar(); |
|
88
|
|
|
|
|
89
|
28 |
|
$this->queryId = spl_object_id($this); |
|
90
|
28 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Add an AQL command (raw AQL and Clauses. |
|
94
|
|
|
* |
|
95
|
|
|
* @param Clause|Expression|QueryBuilder $command |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
19 |
|
public function addCommand($command) |
|
100
|
|
|
{ |
|
101
|
19 |
|
$this->commands[] = $command; |
|
102
|
19 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the clause list. |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function getCommands() |
|
110
|
|
|
{ |
|
111
|
3 |
|
return $this->commands; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the last, or a specific, command. |
|
116
|
|
|
* |
|
117
|
|
|
* @param int|null $index |
|
118
|
|
|
* |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
4 |
|
public function getCommand(int $index = null) |
|
122
|
|
|
{ |
|
123
|
4 |
|
if ($index === null) { |
|
124
|
1 |
|
return end($this->commands); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
3 |
|
return $this->commands[$index]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Remove the last, or the specified, Command. |
|
132
|
|
|
* |
|
133
|
|
|
* @param int|null $index |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
3 |
|
public function removeCommand(int $index = null): bool |
|
137
|
|
|
{ |
|
138
|
3 |
|
if ($index === null) { |
|
139
|
1 |
|
return (bool)array_pop($this->commands); |
|
140
|
|
|
} |
|
141
|
2 |
|
if (isset($this->commands[$index])) { |
|
142
|
1 |
|
unset($this->commands[$index]); |
|
143
|
|
|
|
|
144
|
1 |
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param mixed $collections |
|
152
|
|
|
* @param string $mode |
|
153
|
|
|
* |
|
154
|
|
|
* @return QueryBuilder |
|
155
|
|
|
*/ |
|
156
|
4 |
|
public function registerCollections($collections, $mode = 'write'): self |
|
157
|
|
|
{ |
|
158
|
4 |
|
if (!is_array($collections)) { |
|
159
|
2 |
|
$collections = [$collections]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
4 |
|
$this->collections[$mode] = array_unique(array_merge($collections)); |
|
163
|
|
|
|
|
164
|
4 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Register variables on declaration for later data normalization. |
|
169
|
|
|
* @param string|array<mixed>|object $variableName |
|
170
|
|
|
*/ |
|
171
|
11 |
|
public function registerVariable( |
|
172
|
|
|
string|array|object $variableName |
|
173
|
|
|
): self { |
|
174
|
11 |
|
if ($variableName instanceof ExpressionInterface) { |
|
|
|
|
|
|
175
|
1 |
|
$variableName = $variableName->compile($this); |
|
176
|
|
|
} |
|
177
|
11 |
|
if (is_string($variableName)) { |
|
|
|
|
|
|
178
|
11 |
|
$variableName = [$variableName => $variableName]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
11 |
|
if (is_array($variableName)) { |
|
|
|
|
|
|
182
|
11 |
|
$this->variables = array_unique(array_merge($this->variables, $variableName)); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
11 |
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Bind data to a variable. |
|
190
|
|
|
* |
|
191
|
|
|
* @param object|array<mixed>|string|int|float|bool|null $data |
|
192
|
|
|
* @throws BindException |
|
193
|
|
|
*/ |
|
194
|
5 |
|
public function bind( |
|
195
|
|
|
object|array|string|int|float|bool|null $data, |
|
|
|
|
|
|
196
|
|
|
string $to = null |
|
197
|
|
|
): BindExpression { |
|
198
|
5 |
|
$this->validateBindVariable($to); |
|
199
|
|
|
|
|
200
|
4 |
|
$to = $this->generateBindVariable($to); |
|
201
|
|
|
|
|
202
|
4 |
|
$this->binds[$to] = $data; |
|
203
|
|
|
|
|
204
|
4 |
|
$to = $this->grammar->formatBind($to, false); |
|
205
|
|
|
|
|
206
|
4 |
|
return new BindExpression($to); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param array<array-key, array<array-key, mixed>|object|scalar|null> $array |
|
|
|
|
|
|
211
|
|
|
* @throws BindException |
|
212
|
|
|
*/ |
|
213
|
6 |
|
protected function bindArrayValues(array $array): void |
|
214
|
|
|
{ |
|
215
|
6 |
|
foreach ($array as $key => $value) { |
|
216
|
2 |
|
$to = null; |
|
217
|
2 |
|
if (is_string($key)) { |
|
218
|
2 |
|
$to = $key; |
|
219
|
|
|
} |
|
220
|
2 |
|
$this->bind($value, $to); |
|
221
|
|
|
} |
|
222
|
6 |
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Bind a collection name to a variable. |
|
226
|
|
|
* |
|
227
|
|
|
* @throws BindException |
|
228
|
|
|
*/ |
|
229
|
1 |
|
public function bindCollection( |
|
230
|
|
|
mixed $data, |
|
231
|
|
|
string $to = null |
|
232
|
|
|
): BindExpression { |
|
233
|
1 |
|
$this->validateBindVariable($to); |
|
234
|
|
|
|
|
235
|
1 |
|
$to = $this->generateBindVariable($to); |
|
236
|
|
|
|
|
237
|
1 |
|
$this->binds[$to] = $data; |
|
238
|
|
|
|
|
239
|
1 |
|
$to = $this->grammar->formatBind($to, true); |
|
240
|
|
|
|
|
241
|
1 |
|
return new BindExpression($to); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
6 |
|
protected function validateBindVariable(?string $to): void |
|
245
|
|
|
{ |
|
246
|
6 |
|
if (isset($to) && !$this->grammar->isBindParameter($to)) { |
|
247
|
1 |
|
throw new BindException('Invalid bind parameter.'); |
|
248
|
|
|
} |
|
249
|
5 |
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param null|string $to |
|
253
|
|
|
* |
|
254
|
|
|
* @return string |
|
255
|
|
|
*/ |
|
256
|
5 |
|
protected function generateBindVariable(?string $to): string |
|
257
|
|
|
{ |
|
258
|
5 |
|
if ($to == null) { |
|
|
|
|
|
|
259
|
3 |
|
$to = $this->queryId . '_' . (count($this->binds) + 1); |
|
260
|
|
|
} |
|
261
|
5 |
|
return $to; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Compile the query with its bindings and collection list. |
|
267
|
|
|
*/ |
|
268
|
14 |
|
public function compile(): self |
|
269
|
|
|
{ |
|
270
|
14 |
|
$this->query = ''; |
|
271
|
|
|
/** @var Expression|Clause @command */ |
|
272
|
14 |
|
foreach ($this->commands as $command) { |
|
273
|
13 |
|
$this->query .= ' ' . $command->compile($this); |
|
274
|
|
|
} |
|
275
|
13 |
|
$this->query = trim($this->query); |
|
276
|
|
|
|
|
277
|
13 |
|
return $this; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @return QueryBuilder $this |
|
282
|
|
|
*/ |
|
283
|
14 |
|
public function get() |
|
284
|
|
|
{ |
|
285
|
14 |
|
$this->compile(); |
|
286
|
|
|
|
|
287
|
13 |
|
return $this; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @return int $this |
|
292
|
|
|
*/ |
|
293
|
4 |
|
public function getQueryId(): int |
|
294
|
|
|
{ |
|
295
|
4 |
|
return $this->queryId; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @return string |
|
300
|
|
|
*/ |
|
301
|
6 |
|
public function toAql() |
|
302
|
|
|
{ |
|
303
|
6 |
|
return $this->get()->query ?: ""; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
5 |
|
public function __toString() |
|
307
|
|
|
{ |
|
308
|
5 |
|
return $this->toAql(); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @return array<mixed> |
|
313
|
|
|
*/ |
|
314
|
7 |
|
public function getVariables(): array |
|
315
|
|
|
{ |
|
316
|
7 |
|
return $this->variables; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @param array<mixed> $arguments |
|
321
|
|
|
* @return array<mixed> |
|
322
|
|
|
*/ |
|
323
|
1 |
|
public function unsetNullValues(array $arguments): array |
|
324
|
|
|
{ |
|
325
|
1 |
|
return array_filter( |
|
326
|
1 |
|
$arguments, |
|
327
|
1 |
|
function ($value) { |
|
328
|
1 |
|
return !is_null($value); |
|
329
|
1 |
|
} |
|
330
|
|
|
); |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|