1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\FluentAQL\AQL; |
4
|
|
|
|
5
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\AggregateClause; |
6
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\CollectClause; |
7
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\FilterClause; |
8
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\ForClause; |
9
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\IntoClause; |
10
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\KeepClause; |
11
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\LimitClause; |
12
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\OptionsClause; |
13
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\RawClause; |
14
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\ReturnClause; |
15
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\SearchClause; |
16
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\SortClause; |
17
|
|
|
use LaravelFreelancerNL\FluentAQL\Clauses\WithCountClause; |
18
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface; |
19
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait hasQueryClauses |
23
|
|
|
* API calls to add clause commands to the builder. |
24
|
|
|
*/ |
25
|
|
|
trait HasQueryClauses |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
abstract public function addCommand($command); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a for clause. |
32
|
|
|
* |
33
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-for.html |
34
|
|
|
* |
35
|
|
|
* @param string|array|ExpressionInterface $variableName |
36
|
|
|
* @param mixed $in |
37
|
|
|
* |
38
|
|
|
* @return QueryBuilder |
39
|
|
|
*/ |
40
|
16 |
|
public function for($variableName, $in = null): self |
41
|
|
|
{ |
42
|
16 |
|
if (!is_array($variableName)) { |
43
|
16 |
|
$variableName = [$variableName]; |
44
|
|
|
} |
45
|
|
|
|
46
|
16 |
|
$this->addCommand(new ForClause($variableName, $in)); |
47
|
|
|
|
48
|
16 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Filter results from a for clause. |
53
|
|
|
* |
54
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-filter.html |
55
|
|
|
* |
56
|
|
|
* @param mixed $leftOperand |
57
|
|
|
* @param string|array|null $comparisonOperator |
58
|
|
|
* @param mixed $rightOperand |
59
|
|
|
* @param string $logicalOperator |
60
|
|
|
* |
61
|
|
|
* @return QueryBuilder |
62
|
|
|
*/ |
63
|
8 |
|
public function filter( |
64
|
|
|
$leftOperand, |
65
|
|
|
$comparisonOperator = null, |
66
|
|
|
$rightOperand = null, |
67
|
|
|
$logicalOperator = null |
68
|
|
|
): self { |
69
|
8 |
|
$predicates = $leftOperand; |
70
|
8 |
|
if (is_string($comparisonOperator)) { |
71
|
6 |
|
$predicates = [[$leftOperand, $comparisonOperator, $rightOperand, $logicalOperator]]; |
72
|
|
|
} |
73
|
|
|
|
74
|
8 |
|
$this->addCommand(new FilterClause($predicates)); |
75
|
|
|
|
76
|
8 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Search a view. |
81
|
|
|
* |
82
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-search.html |
83
|
|
|
* |
84
|
|
|
* @param $leftOperand |
85
|
|
|
* @param string $comparisonOperator |
86
|
|
|
* @param null $rightOperand |
|
|
|
|
87
|
|
|
* @param string $logicalOperator |
88
|
|
|
* |
89
|
|
|
* @return QueryBuilder |
90
|
|
|
*/ |
91
|
1 |
|
public function search( |
92
|
|
|
$leftOperand, |
93
|
|
|
$comparisonOperator = null, |
94
|
|
|
$rightOperand = null, |
95
|
|
|
$logicalOperator = null |
96
|
|
|
): self { |
97
|
1 |
|
$predicates = $leftOperand; |
98
|
1 |
|
if (is_string($comparisonOperator)) { |
99
|
1 |
|
$predicates = [[$leftOperand, $comparisonOperator, $rightOperand, $logicalOperator]]; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$this->addCommand(new SearchClause($predicates)); |
103
|
|
|
|
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Collect clause. |
109
|
|
|
* |
110
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-collect.html |
111
|
|
|
* |
112
|
|
|
* @param string|null $variableName |
113
|
|
|
* @param null $expression |
|
|
|
|
114
|
|
|
* |
115
|
|
|
* @return QueryBuilder |
116
|
|
|
*/ |
117
|
1 |
|
public function collect($variableName = null, $expression = null): self |
118
|
|
|
{ |
119
|
|
|
|
120
|
1 |
|
$this->addCommand(new CollectClause($variableName, $expression)); |
121
|
|
|
|
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Group clause: |
127
|
|
|
* Creates the INTO clause of a collect clause. |
128
|
|
|
* |
129
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-collect.html#grouping-syntaxes |
130
|
|
|
* |
131
|
|
|
* @param $groupsVariable |
132
|
|
|
* @param null $projectionExpression |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return QueryBuilder |
135
|
|
|
*/ |
136
|
1 |
|
public function into($groupsVariable, $projectionExpression = null): self |
137
|
|
|
{ |
138
|
1 |
|
$this->addCommand(new IntoClause($groupsVariable, $projectionExpression)); |
139
|
|
|
|
140
|
1 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Keep clause |
145
|
|
|
* Limits the attributes of the data that is grouped. |
146
|
|
|
* |
147
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-collect.html#discarding-obsolete-variables |
148
|
|
|
* |
149
|
|
|
* @param $keepVariable |
150
|
|
|
* |
151
|
|
|
* @return QueryBuilder |
152
|
|
|
*/ |
153
|
1 |
|
public function keep($keepVariable): self |
154
|
|
|
{ |
155
|
1 |
|
$this->addCommand(new KeepClause($keepVariable)); |
156
|
|
|
|
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* withCount clause |
162
|
|
|
* Count the collected and grouped data. |
163
|
|
|
* withCount can only be used after a into clause. |
164
|
|
|
* |
165
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-collect.html#group-length-calculation |
166
|
|
|
* |
167
|
|
|
* @param $countVariableName |
168
|
|
|
* |
169
|
|
|
* @return QueryBuilder |
170
|
|
|
*/ |
171
|
1 |
|
public function withCount($countVariableName): self |
172
|
|
|
{ |
173
|
1 |
|
$this->addCommand(new WithCountClause($countVariableName)); |
174
|
|
|
|
175
|
1 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Aggregate clause |
180
|
|
|
* Creates the INTO clause of a collect clause. |
181
|
|
|
* |
182
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-collect.html#aggregation |
183
|
|
|
* |
184
|
|
|
* @param $variableName |
185
|
|
|
* @param $aggregateExpression |
186
|
|
|
* |
187
|
|
|
* @return QueryBuilder |
188
|
|
|
*/ |
189
|
1 |
|
public function aggregate($variableName, $aggregateExpression): self |
190
|
|
|
{ |
191
|
1 |
|
$this->addCommand(new AggregateClause($variableName, $aggregateExpression)); |
192
|
|
|
|
193
|
1 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Sort documents to return. |
198
|
|
|
* |
199
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-sort.html |
200
|
|
|
* |
201
|
|
|
* @param mixed ...$references |
202
|
|
|
* @return QueryBuilder |
203
|
|
|
*/ |
204
|
1 |
|
public function sort(...$references): self |
205
|
|
|
{ |
206
|
1 |
|
$this->addCommand(new SortClause($references)); |
207
|
|
|
|
208
|
1 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Limit results. |
213
|
|
|
* |
214
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-limit.html |
215
|
|
|
* |
216
|
|
|
* @param int $offsetOrCount |
217
|
|
|
* @param int $count |
218
|
|
|
* |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
1 |
|
public function limit(int $offsetOrCount, int $count = null): self |
222
|
|
|
{ |
223
|
1 |
|
$this->addCommand(new LimitClause($offsetOrCount, $count)); |
224
|
|
|
|
225
|
1 |
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Return data. |
230
|
|
|
* |
231
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/operations-return.html |
232
|
|
|
* |
233
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
234
|
|
|
* |
235
|
|
|
* @param $expression |
236
|
|
|
* @param bool $distinct |
237
|
|
|
* |
238
|
|
|
* @return QueryBuilder |
239
|
|
|
*/ |
240
|
7 |
|
public function return($expression, $distinct = false): self |
241
|
|
|
{ |
242
|
7 |
|
$this->addCommand(new ReturnClause($expression, $distinct)); |
243
|
|
|
|
244
|
7 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
1 |
|
public function options($options): self |
248
|
|
|
{ |
249
|
1 |
|
$this->addCommand(new OptionsClause($options)); |
250
|
|
|
|
251
|
1 |
|
return $this; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|