|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Traits\Macroable; |
|
7
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Concerns\CompilesAggregates; |
|
8
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Concerns\CompilesJoins; |
|
9
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Concerns\CompilesWhereClauses; |
|
10
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Concerns\HasAliases; |
|
11
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException as BindException; |
|
12
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression; |
|
13
|
|
|
use LaravelFreelancerNL\FluentAQL\Grammar as FluentAqlGrammar; |
|
14
|
|
|
|
|
15
|
|
|
/* |
|
16
|
|
|
* Provides AQL syntax functions |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
class Grammar extends FluentAqlGrammar |
|
20
|
|
|
{ |
|
21
|
|
|
use CompilesAggregates; |
|
22
|
|
|
use CompilesJoins; |
|
|
|
|
|
|
23
|
|
|
use CompilesWhereClauses; |
|
|
|
|
|
|
24
|
|
|
use HasAliases; |
|
|
|
|
|
|
25
|
|
|
use Macroable; |
|
26
|
|
|
|
|
27
|
|
|
public $name; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The grammar table prefix. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $tablePrefix = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The grammar table prefix. |
|
38
|
|
|
* |
|
39
|
|
|
* @var null|int |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $offset = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The components that make up a select clause. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $selectComponents = [ |
|
49
|
|
|
'from', |
|
50
|
|
|
'joins', |
|
51
|
|
|
'wheres', |
|
52
|
|
|
'groups', |
|
53
|
|
|
'aggregate', |
|
54
|
|
|
'havings', |
|
55
|
|
|
'orders', |
|
56
|
|
|
'offset', |
|
57
|
|
|
'limit', |
|
58
|
|
|
'columns', |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
protected $operatorTranslations = [ |
|
62
|
|
|
'=' => '==', |
|
63
|
|
|
'<>' => '!=', |
|
64
|
|
|
'<=>' => '==', |
|
65
|
|
|
'rlike' => '=~', |
|
66
|
|
|
'not rlike' => '!~', |
|
67
|
|
|
'regexp' => '=~', |
|
68
|
|
|
'not regexp' => '!~', |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
protected $whereTypeOperators = [ |
|
72
|
|
|
'In' => 'IN', |
|
73
|
|
|
'NotIn' => 'NOT IN', |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get the format for database stored dates. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
57 |
|
public function getDateFormat() |
|
82
|
|
|
{ |
|
83
|
57 |
|
return 'Y-m-d\TH:i:s.v\Z'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the grammar specific operators. |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function getOperators() |
|
92
|
|
|
{ |
|
93
|
2 |
|
return $this->comparisonOperators; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
80 |
|
protected function prefixTable($table) |
|
97
|
|
|
{ |
|
98
|
80 |
|
return $this->tablePrefix . $table; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Compile an insert statement into AQL. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Builder $builder |
|
105
|
|
|
* @param array $values |
|
106
|
|
|
* |
|
107
|
|
|
* @throws BindException |
|
108
|
|
|
* |
|
109
|
|
|
* @return Builder |
|
110
|
|
|
*/ |
|
111
|
57 |
|
public function compileInsert(Builder $builder, array $values) |
|
112
|
|
|
{ |
|
113
|
57 |
|
if (Arr::isAssoc($values)) { |
|
114
|
8 |
|
$values = [$values]; |
|
115
|
|
|
} |
|
116
|
57 |
|
$table = $this->prefixTable($builder->from); |
|
117
|
|
|
|
|
118
|
57 |
|
if (empty($values)) { |
|
119
|
|
|
$builder->aqb = $builder->aqb->insert('{}', $table)->get(); |
|
120
|
|
|
|
|
121
|
|
|
return $builder; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
57 |
|
$builder->aqb = $builder->aqb->let('values', $values) |
|
125
|
57 |
|
->for('value', 'values') |
|
126
|
57 |
|
->insert('value', $table) |
|
127
|
57 |
|
->return('NEW._key') |
|
128
|
57 |
|
->get(); |
|
129
|
|
|
|
|
130
|
57 |
|
return $builder; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Compile an insert and get ID statement into SQL. |
|
135
|
|
|
* |
|
136
|
|
|
* @param Builder $builder |
|
137
|
|
|
* @param array $values |
|
138
|
|
|
* |
|
139
|
|
|
* @throws BindException |
|
140
|
|
|
* |
|
141
|
|
|
* @return Builder |
|
142
|
|
|
*/ |
|
143
|
8 |
|
public function compileInsertGetId(Builder $builder, $values) |
|
144
|
|
|
{ |
|
145
|
8 |
|
return $this->compileInsert($builder, $values); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Compile a select query into AQL. |
|
150
|
|
|
* |
|
151
|
|
|
* @param Builder $builder |
|
152
|
|
|
* |
|
153
|
|
|
* @return Builder |
|
154
|
|
|
*/ |
|
155
|
76 |
|
public function compileSelect(Builder $builder) |
|
156
|
|
|
{ |
|
157
|
|
|
// if ($builder->unions && $builder->aggregate) { |
|
158
|
|
|
// return $this->compileUnionAggregate($builder); |
|
159
|
|
|
// } |
|
160
|
|
|
|
|
161
|
|
|
// To compile the query, we'll spin through each component of the query and |
|
162
|
|
|
// see if that component exists. If it does we'll just call the compiler |
|
163
|
|
|
// function for the component which is responsible for making the SQL. |
|
164
|
|
|
|
|
165
|
76 |
|
$builder = $this->compileComponents($builder); |
|
166
|
|
|
|
|
167
|
|
|
// if ($builder->unions) { |
|
168
|
|
|
// $sql = $this->wrapUnion($sql).' '.$this->compileUnions($builder); |
|
169
|
|
|
// } |
|
170
|
|
|
|
|
171
|
76 |
|
$builder->aqb = $builder->aqb->get(); |
|
172
|
|
|
|
|
173
|
76 |
|
return $builder; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Compile the components necessary for a select clause. |
|
178
|
|
|
* |
|
179
|
|
|
* @param Builder $builder |
|
180
|
|
|
* |
|
181
|
|
|
* @return Builder |
|
182
|
|
|
*/ |
|
183
|
76 |
|
protected function compileComponents(Builder $builder) |
|
184
|
|
|
{ |
|
185
|
76 |
|
foreach ($this->selectComponents as $component) { |
|
186
|
|
|
// To compile the query, we'll spin through each component of the query and |
|
187
|
|
|
// see if that component exists. If it does we'll just call the compiler |
|
188
|
|
|
// function for the component which is responsible for making the SQL. |
|
189
|
|
|
|
|
190
|
76 |
|
if (isset($builder->$component) && !is_null($builder->$component)) { |
|
191
|
76 |
|
$method = 'compile' . ucfirst($component); |
|
192
|
|
|
|
|
193
|
76 |
|
$builder = $this->$method($builder, $builder->$component); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
76 |
|
return $builder; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Compile the "from" portion of the query -> FOR in AQL. |
|
202
|
|
|
* |
|
203
|
|
|
* @param Builder $builder |
|
204
|
|
|
* @param string $table |
|
205
|
|
|
* |
|
206
|
|
|
* @return Builder |
|
207
|
|
|
*/ |
|
208
|
76 |
|
protected function compileFrom(Builder $builder, $table) |
|
209
|
|
|
{ |
|
210
|
76 |
|
$table = $this->prefixTable($table); |
|
211
|
76 |
|
$alias = $this->registerTableAlias($table); |
|
212
|
|
|
|
|
213
|
76 |
|
$builder->aqb = $builder->aqb->for($alias, $table); |
|
214
|
|
|
|
|
215
|
76 |
|
return $builder; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Compile the "order by" portions of the query. |
|
222
|
|
|
* |
|
223
|
|
|
* @param Builder $builder |
|
224
|
|
|
* @param array $orders |
|
225
|
|
|
* |
|
226
|
|
|
* @return Builder |
|
227
|
|
|
*/ |
|
228
|
2 |
|
protected function compileOrders(Builder $builder, $orders) |
|
229
|
|
|
{ |
|
230
|
2 |
|
if (!empty($orders)) { |
|
231
|
2 |
|
$orders = $this->compileOrdersToFlatArray($builder, $orders); |
|
232
|
2 |
|
$builder->aqb = $builder->aqb->sort(...$orders); |
|
233
|
|
|
|
|
234
|
2 |
|
return $builder; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return $builder; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Compile the query orders to an array. |
|
242
|
|
|
* |
|
243
|
|
|
* @param Builder $builder |
|
244
|
|
|
* @param array $orders |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
2 |
|
protected function compileOrdersToFlatArray(Builder $builder, $orders) |
|
249
|
|
|
{ |
|
250
|
2 |
|
$flatOrders = []; |
|
251
|
|
|
|
|
252
|
2 |
|
foreach ($orders as $order) { |
|
253
|
2 |
|
if (!isset($order['type']) || $order['type'] != 'Raw') { |
|
254
|
1 |
|
$order['column'] = $this->normalizeColumn($builder, $order['column']); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
2 |
|
$flatOrders[] = $order['column']; |
|
258
|
|
|
|
|
259
|
2 |
|
if (isset($order['direction'])) { |
|
260
|
1 |
|
$flatOrders[] = $order['direction']; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
2 |
|
return $flatOrders; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Compile the "offset" portions of the query. |
|
269
|
|
|
* We are handling this first by saving the offset which will be used by the FluentAQL's limit function. |
|
270
|
|
|
* |
|
271
|
|
|
* @param Builder $builder |
|
272
|
|
|
* @param int $offset |
|
273
|
|
|
* |
|
274
|
|
|
* @return Builder |
|
275
|
|
|
*/ |
|
276
|
2 |
|
protected function compileOffset(Builder $builder, $offset) |
|
277
|
|
|
{ |
|
278
|
2 |
|
$this->offset = (int) $offset; |
|
279
|
|
|
|
|
280
|
2 |
|
return $builder; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Compile the "limit" portions of the query. |
|
285
|
|
|
* |
|
286
|
|
|
* @param Builder $builder |
|
287
|
|
|
* @param int $limit |
|
288
|
|
|
* |
|
289
|
|
|
* @return Builder |
|
290
|
|
|
*/ |
|
291
|
38 |
|
protected function compileLimit(Builder $builder, $limit) |
|
292
|
|
|
{ |
|
293
|
38 |
|
if ($this->offset !== null) { |
|
294
|
2 |
|
$builder->aqb = $builder->aqb->limit((int) $this->offset, (int) $limit); |
|
295
|
|
|
|
|
296
|
2 |
|
return $builder; |
|
297
|
|
|
} |
|
298
|
36 |
|
$builder->aqb = $builder->aqb->limit((int) $limit); |
|
299
|
|
|
|
|
300
|
36 |
|
return $builder; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Compile the "RETURN" portion of the query. |
|
305
|
|
|
* |
|
306
|
|
|
* @param Builder $builder |
|
307
|
|
|
* @param array $columns |
|
308
|
|
|
* |
|
309
|
|
|
* @return Builder |
|
310
|
|
|
*/ |
|
311
|
75 |
|
protected function compileColumns(Builder $builder, array $columns): Builder |
|
312
|
|
|
{ |
|
313
|
75 |
|
$returnDocs = []; |
|
314
|
75 |
|
$returnAttributes = []; |
|
315
|
75 |
|
$values = []; |
|
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
// Prepare columns |
|
318
|
75 |
|
foreach ($columns as $column) { |
|
319
|
|
|
// Extract rows |
|
320
|
75 |
|
if (substr($column, strlen($column) - 2) === '.*') { |
|
321
|
11 |
|
$table = substr($column, 0, strlen($column) - 2); |
|
322
|
11 |
|
$returnDocs[] = $this->getTableAlias($table); |
|
323
|
|
|
|
|
324
|
11 |
|
continue; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
75 |
|
if ($column != null && $column != '*') { |
|
328
|
17 |
|
[$column, $alias] = $this->extractAlias($column); |
|
329
|
|
|
|
|
330
|
17 |
|
$returnAttributes[$alias] = $this->normalizeColumn($builder, $column); |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
75 |
|
$values = $this->determineReturnValues($builder, $returnAttributes, $returnDocs); |
|
334
|
75 |
|
$builder->aqb = $builder->aqb->return($values, (bool) $builder->distinct); |
|
335
|
|
|
|
|
336
|
75 |
|
return $builder; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
75 |
|
protected function determineReturnValues($builder, $returnAttributes = [], $returnDocs = []) |
|
340
|
|
|
{ |
|
341
|
75 |
|
$values = []; |
|
342
|
|
|
|
|
343
|
75 |
|
if (! empty($returnAttributes)) { |
|
344
|
17 |
|
$values = $returnAttributes; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
75 |
|
if (! empty($returnAttributes) && ! empty($returnDocs)) { |
|
348
|
11 |
|
$returnDocs[] = $returnAttributes; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
75 |
|
if (! empty($returnDocs)) { |
|
352
|
11 |
|
$values = $builder->aqb->merge(...$returnDocs); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
75 |
|
if ($builder->aggregate !== null) { |
|
356
|
7 |
|
$values = ['aggregate' => 'aggregateResult']; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
75 |
|
if (empty($values)) { |
|
360
|
69 |
|
$values = $this->getTableAlias($builder->from); |
|
361
|
69 |
|
if (is_array($builder->joins) && !empty($builder->joins)) { |
|
362
|
3 |
|
$values = $this->mergeJoinResults($builder, $values); |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
75 |
|
return $values; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
|
|
370
|
3 |
|
protected function mergeJoinResults($builder, $baseTable) |
|
371
|
|
|
{ |
|
372
|
3 |
|
$tablesToJoin = []; |
|
373
|
3 |
|
foreach ($builder->joins as $join) { |
|
374
|
3 |
|
$tablesToJoin[] = $this->getTableAlias($join->table); |
|
375
|
|
|
} |
|
376
|
3 |
|
$tablesToJoin = array_reverse($tablesToJoin); |
|
377
|
3 |
|
$tablesToJoin[] = $baseTable; |
|
378
|
|
|
|
|
379
|
3 |
|
return $builder->aqb->merge(...$tablesToJoin); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Compile an update statement into SQL. |
|
384
|
|
|
* |
|
385
|
|
|
* @param Builder $builder |
|
386
|
|
|
* @param array $values |
|
387
|
|
|
* |
|
388
|
|
|
* @return Builder |
|
389
|
|
|
*/ |
|
390
|
13 |
|
public function compileUpdate(Builder $builder, array $values) |
|
391
|
|
|
{ |
|
392
|
|
|
|
|
393
|
13 |
|
$table = $this->prefixTable($builder->from); |
|
394
|
13 |
|
$tableAlias = $this->generateTableAlias($table); |
|
395
|
|
|
|
|
396
|
13 |
|
$builder->aqb = $builder->aqb->for($tableAlias, $table); |
|
397
|
|
|
|
|
398
|
|
|
//Fixme: joins? |
|
399
|
13 |
|
$builder = $this->compileWheres($builder); |
|
400
|
|
|
|
|
401
|
13 |
|
$builder->aqb = $builder->aqb->update($tableAlias, $values, $table)->get(); |
|
402
|
|
|
|
|
403
|
13 |
|
return $builder; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Compile a delete statement into SQL. |
|
408
|
|
|
* |
|
409
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseParameterName) |
|
410
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseVariableName) |
|
411
|
|
|
* |
|
412
|
|
|
* @param Builder $builder |
|
413
|
|
|
* @param null $id |
|
|
|
|
|
|
414
|
|
|
* |
|
415
|
|
|
* @return Builder |
|
416
|
|
|
*/ |
|
417
|
6 |
|
public function compileDelete(Builder $builder, $id = null) |
|
418
|
|
|
{ |
|
419
|
6 |
|
$table = $this->prefixTable($builder->from); |
|
420
|
6 |
|
$tableAlias = $this->generateTableAlias($table); |
|
421
|
|
|
|
|
422
|
|
|
|
|
423
|
6 |
|
if (!is_null($id)) { |
|
|
|
|
|
|
424
|
1 |
|
$builder->aqb = $builder->aqb->remove((string) $id, $table)->get(); |
|
425
|
|
|
|
|
426
|
1 |
|
return $builder; |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
6 |
|
$builder->aqb = $builder->aqb->for($tableAlias, $table); |
|
430
|
|
|
|
|
431
|
|
|
//Fixme: joins? |
|
432
|
6 |
|
$builder = $this->compileWheres($builder); |
|
433
|
|
|
|
|
434
|
6 |
|
$builder->aqb = $builder->aqb->remove($tableAlias, $table)->get(); |
|
435
|
|
|
|
|
436
|
6 |
|
return $builder; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* Compile the random statement into SQL. |
|
441
|
|
|
* |
|
442
|
|
|
* @param Builder $builder |
|
443
|
|
|
* |
|
444
|
|
|
* @return FunctionExpression; |
|
445
|
|
|
*/ |
|
446
|
1 |
|
public function compileRandom(Builder $builder) |
|
447
|
|
|
{ |
|
448
|
1 |
|
return $builder->aqb->rand(); |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
|