1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder as IlluminateBuilder; |
6
|
|
|
use Illuminate\Database\Query\Builder as IluminateBuilder; |
7
|
|
|
|
8
|
|
|
trait CompilesWhereClauses |
9
|
|
|
{ |
10
|
|
|
protected function getOperatorByWhereType($type) |
11
|
|
|
{ |
12
|
|
|
if (isset($this->whereTypeOperators[$type])) { |
13
|
|
|
return $this->whereTypeOperators[$type]; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
return '=='; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Format the where clause statements into one string. |
21
|
|
|
* |
22
|
148 |
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
23
|
|
|
* @param IlluminateBuilder $query |
24
|
148 |
|
* @param array<mixed> $sql |
25
|
|
|
*/ |
26
|
|
|
protected function concatenateWhereClauses($query, $sql): string |
|
|
|
|
27
|
|
|
{ |
28
|
148 |
|
return 'FILTER ' . $this->removeLeadingBoolean(implode(' ', $sql)); |
|
|
|
|
29
|
|
|
} |
30
|
148 |
|
|
31
|
103 |
|
protected function normalizeOperator($where) |
32
|
|
|
{ |
33
|
|
|
if (isset($where['operator'])) { |
34
|
148 |
|
$where['operator'] = $this->translateOperator($where['operator']); |
35
|
|
|
} |
36
|
|
|
if (! isset($where['operator'])) { |
37
|
|
|
$where['operator'] = $this->getOperatorByWhereType($where['type']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $where; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
148 |
|
* Translate sql operators to their AQL equivalent where possible. |
45
|
|
|
* |
46
|
148 |
|
* @param string $operator |
47
|
103 |
|
* |
48
|
148 |
|
* @return mixed|string |
49
|
|
|
*/ |
50
|
|
|
private function translateOperator(string $operator) |
51
|
|
|
{ |
52
|
|
|
if (isset($this->operatorTranslations[strtolower($operator)])) { |
53
|
|
|
$operator = $this->operatorTranslations[$operator]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $operator; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Compile a basic where clause. |
62
|
|
|
* |
63
|
|
|
* @param array<mixed> $where |
64
|
|
|
*/ |
65
|
|
|
protected function whereBasic(IluminateBuilder $query, $where): string |
|
|
|
|
66
|
90 |
|
{ |
67
|
|
|
$value = $this->parameter($where['value']); |
|
|
|
|
68
|
90 |
|
|
69
|
|
|
$operator = str_replace('?', '??', $where['operator']); |
70
|
|
|
|
71
|
|
|
return $this->wrap($where['column']).' '.$operator.' '.$value; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Compile a "between" where clause. |
76
|
|
|
* |
77
|
|
|
* @param array<mixed> $where |
78
|
90 |
|
*/ |
79
|
|
|
protected function whereBetween(IluminateBuilder $query, $where): string |
80
|
90 |
|
{ |
81
|
|
|
$predicate = []; |
82
|
|
|
|
83
|
94 |
|
[$minOperator, $maxOperator, $boolean] = $this->getBetweenOperators($where['not']); |
84
|
|
|
|
85
|
94 |
|
$min = $this->parameter($query, reset($where['values'])); |
86
|
94 |
|
|
87
|
|
|
$max = $this->parameter($query, end($where['values'])); |
88
|
94 |
|
|
89
|
|
|
$predicate[0][0] = $this->normalizeColumn($query, $where['column']); |
|
|
|
|
90
|
|
|
$predicate[0][1] = $minOperator; |
91
|
|
|
$predicate[0][2] = $min; |
92
|
94 |
|
$predicate[0][3] = $where['boolean']; |
93
|
|
|
|
94
|
|
|
$predicate[1][0] = $this->normalizeColumn($query, $where['column']); |
95
|
|
|
$predicate[1][1] = $maxOperator; |
96
|
|
|
$predicate[1][2] = $max; |
97
|
|
|
$predicate[1][3] = $boolean; |
98
|
|
|
|
99
|
|
|
return $predicate; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
94 |
|
/** |
103
|
|
|
* Generate operators for between and 'not between' |
104
|
94 |
|
* @param bool $notBetween |
105
|
86 |
|
* @return string[] |
106
|
|
|
*/ |
107
|
|
|
protected function getBetweenOperators(bool $notBetween) |
108
|
94 |
|
{ |
109
|
|
|
$minOperator = '>='; |
110
|
|
|
$maxOperator = '<='; |
111
|
|
|
$boolean = 'AND'; |
112
|
|
|
|
113
|
|
|
if ($notBetween) { |
114
|
|
|
$minOperator = '<'; |
115
|
|
|
$maxOperator = '>'; |
116
|
|
|
$boolean = 'OR'; |
117
|
|
|
} |
118
|
|
|
|
119
|
79 |
|
return [$minOperator, $maxOperator, $boolean]; |
120
|
|
|
} |
121
|
79 |
|
|
122
|
|
|
/** |
123
|
79 |
|
* Compile a "between" where clause. |
124
|
|
|
* |
125
|
79 |
|
* @param array<mixed> $where |
126
|
79 |
|
*/ |
127
|
79 |
|
protected function whereBetweenColumns(IluminateBuilder $query, $where): string |
128
|
79 |
|
{ |
129
|
|
|
$predicate = []; |
130
|
79 |
|
|
131
|
|
|
[$minOperator, $maxOperator, $boolean] = $this->getBetweenOperators($where['not']); |
132
|
|
|
|
133
|
|
|
$column = $this->normalizeColumn($query, $where['column']); |
134
|
|
|
|
135
|
|
|
$predicate[0][0] = $column; |
136
|
|
|
$predicate[0][1] = $minOperator; |
137
|
|
|
$predicate[0][2] = $this->normalizeColumn($query, reset($where['values'])); |
138
|
|
|
$predicate[0][3] = $where['boolean']; |
139
|
|
|
|
140
|
3 |
|
$predicate[1][0] = $column; |
141
|
|
|
$predicate[1][1] = $maxOperator; |
142
|
3 |
|
$predicate[1][2] = $this->normalizeColumn($query, end($where['values'])); |
143
|
|
|
$predicate[1][3] = $boolean; |
144
|
3 |
|
|
145
|
|
|
return $predicate; |
|
|
|
|
146
|
3 |
|
} |
147
|
|
|
|
148
|
3 |
|
|
149
|
|
|
/** |
150
|
3 |
|
* Compile a where clause comparing two columns. |
151
|
3 |
|
* |
152
|
3 |
|
* @param array<mixed> $where |
153
|
3 |
|
*/ |
154
|
|
|
protected function whereColumn(IluminateBuilder $query, $where): string |
155
|
3 |
|
{ |
156
|
3 |
|
$predicate = []; |
157
|
3 |
|
|
158
|
3 |
|
$where = $this->normalizeOperator($where); |
159
|
|
|
|
160
|
3 |
|
$predicate[0] = $this->normalizeColumn($query, $where['first']); |
161
|
|
|
$predicate[1] = $where['operator']; |
162
|
|
|
$predicate[2] = $this->normalizeColumn($query, $where['second']); |
163
|
|
|
$predicate[3] = $where['boolean']; |
164
|
|
|
|
165
|
|
|
return $predicate; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
4 |
|
/** |
169
|
|
|
* Compile a "where null" clause. |
170
|
4 |
|
* |
171
|
4 |
|
* @param array<mixed> $where |
172
|
4 |
|
*/ |
173
|
|
|
protected function whereNull(IluminateBuilder $query, $where): string |
174
|
4 |
|
{ |
175
|
1 |
|
$predicate = []; |
176
|
1 |
|
|
177
|
1 |
|
$predicate[0] = $this->normalizeColumn($query, $where['column']); |
178
|
|
|
$predicate[1] = '=='; |
179
|
|
|
$predicate[2] = null; |
180
|
4 |
|
$predicate[3] = $where['boolean']; |
181
|
|
|
|
182
|
|
|
return $predicate; |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Compile a "where not null" clause. |
187
|
|
|
* |
188
|
|
|
* @param array<mixed> $where |
189
|
|
|
*/ |
190
|
1 |
|
protected function whereNotNull(IluminateBuilder $query, $where): string |
191
|
|
|
{ |
192
|
1 |
|
$predicate = []; |
193
|
|
|
|
194
|
1 |
|
$predicate[0] = $this->normalizeColumn($query, $where['column']); |
195
|
|
|
$predicate[1] = '!='; |
196
|
1 |
|
$predicate[2] = null; |
197
|
|
|
$predicate[3] = $where['boolean']; |
198
|
1 |
|
|
199
|
1 |
|
return $predicate; |
|
|
|
|
200
|
1 |
|
} |
201
|
1 |
|
|
202
|
|
|
/** |
203
|
1 |
|
* Compile a "where in" clause. |
204
|
1 |
|
* |
205
|
1 |
|
* @param array<mixed> $where |
206
|
1 |
|
*/ |
207
|
|
|
protected function whereIn(IluminateBuilder $query, $where): string |
208
|
1 |
|
{ |
209
|
|
|
$predicate = []; |
210
|
|
|
|
211
|
|
|
$predicate[0] = $this->normalizeColumn($query, $where['column']); |
212
|
|
|
$predicate[1] = 'IN'; |
213
|
|
|
$predicate[2] = $this->parameter($query, $where['values']); |
214
|
|
|
$predicate[3] = $where['boolean']; |
215
|
|
|
|
216
|
|
|
return $predicate; |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
25 |
|
/** |
220
|
|
|
* Compile a "where in raw" clause. |
221
|
25 |
|
* |
222
|
|
|
* For safety, whereIntegerInRaw ensures this method is only used with integer values. |
223
|
25 |
|
* |
224
|
|
|
* @param array<mixed> $where |
225
|
25 |
|
*/ |
226
|
25 |
|
protected function whereInRaw(IluminateBuilder $query, $where): string |
227
|
25 |
|
{ |
228
|
25 |
|
$predicate = []; |
229
|
|
|
|
230
|
25 |
|
$predicate[0] = $this->normalizeColumn($query, $where['column']); |
231
|
|
|
$predicate[1] = 'IN'; |
232
|
|
|
$predicate[2] = '[' . implode(', ', $where['values']) . ']'; |
233
|
|
|
$predicate[3] = $where['boolean']; |
234
|
|
|
|
235
|
|
|
return $predicate; |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Compile a "where not in" clause. |
240
|
28 |
|
* |
241
|
|
|
* @param array<mixed> $where |
242
|
28 |
|
*/ |
243
|
|
|
protected function whereNotIn(IluminateBuilder $query, $where): string |
244
|
28 |
|
{ |
245
|
28 |
|
$predicate = []; |
246
|
28 |
|
|
247
|
28 |
|
$predicate[0] = $this->normalizeColumn($query, $where['column']); |
248
|
|
|
$predicate[1] = 'NOT IN'; |
249
|
28 |
|
$predicate[2] = $this->parameter($query, $where['values']); |
250
|
|
|
$predicate[3] = $where['boolean']; |
251
|
|
|
|
252
|
|
|
return $predicate; |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Compile a "where not in raw" clause. |
257
|
|
|
* |
258
|
|
|
* For safety, whereIntegerInRaw ensures this method is only used with integer values. |
259
|
16 |
|
* |
260
|
|
|
* @param array<mixed> $where |
261
|
16 |
|
*/ |
262
|
|
|
protected function whereNotInRaw(IluminateBuilder $query, $where): string |
263
|
16 |
|
{ |
264
|
16 |
|
$predicate = []; |
265
|
16 |
|
|
266
|
16 |
|
$predicate[0] = $this->normalizeColumn($query, $where['column']); |
267
|
|
|
$predicate[1] = 'NOT IN'; |
268
|
16 |
|
$predicate[2] = '[' . implode(', ', $where['values']) . ']'; |
269
|
|
|
$predicate[3] = $where['boolean']; |
270
|
|
|
return $predicate; |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Compile a "where JSON contains" clause. |
275
|
|
|
* |
276
|
|
|
* @param array<mixed> $where |
277
|
|
|
*/ |
278
|
13 |
|
protected function whereJsonContains(IluminateBuilder $query, $where): string |
279
|
|
|
{ |
280
|
13 |
|
$predicate = []; |
281
|
|
|
|
282
|
13 |
|
$operator = $where['not'] ? 'NOT IN' : 'IN'; |
283
|
13 |
|
|
284
|
13 |
|
$predicate[0] = $query->aqb->bind($where['value']); |
285
|
13 |
|
$predicate[1] = $operator; |
286
|
|
|
$predicate[2] = $this->normalizeColumn($query, $where['column']); |
287
|
13 |
|
$predicate[3] = $where['boolean']; |
288
|
|
|
|
289
|
|
|
return $predicate; |
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Compile a "where JSON length" clause. |
294
|
|
|
* |
295
|
|
|
* @param array<mixed> $where |
296
|
|
|
*/ |
297
|
|
|
protected function whereJsonLength(IluminateBuilder $query, $where): string |
298
|
|
|
{ |
299
|
1 |
|
$predicate = []; |
300
|
|
|
|
301
|
1 |
|
$where = $this->normalizeOperator($where); |
302
|
|
|
|
303
|
1 |
|
$column = $this->normalizeColumn($query, $where['column']); |
304
|
1 |
|
|
305
|
1 |
|
$predicate[0] = $query->aqb->length($column); |
306
|
1 |
|
$predicate[1] = $where['operator']; |
307
|
|
|
$predicate[2] = $this->parameter($query, $where['value']); |
308
|
1 |
|
$predicate[3] = $where['boolean']; |
309
|
|
|
|
310
|
|
|
return $predicate; |
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Compile a "where date" clause. |
316
|
|
|
* |
317
|
|
|
* @param array<mixed> $where |
318
|
1 |
|
*/ |
319
|
|
|
protected function whereDate(IluminateBuilder $query, $where): string |
320
|
1 |
|
{ |
321
|
|
|
$predicate = []; |
322
|
1 |
|
|
323
|
1 |
|
$where = $this->normalizeOperator($where); |
324
|
1 |
|
|
325
|
1 |
|
$predicate[0] = $query->aqb->dateFormat($this->normalizeColumn($query, $where['column']), "%yyyy-%mm-%dd"); |
326
|
|
|
$predicate[1] = $where['operator']; |
327
|
1 |
|
$predicate[2] = $this->parameter($query, $where['value']); |
328
|
|
|
$predicate[3] = $where['boolean']; |
329
|
|
|
|
330
|
|
|
return $predicate; |
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Compile a "where year" clause. |
335
|
|
|
* |
336
|
|
|
* @param array<mixed> $where |
337
|
|
|
*/ |
338
|
|
|
protected function whereYear(IluminateBuilder $query, $where): string |
339
|
1 |
|
{ |
340
|
|
|
$predicate = []; |
341
|
1 |
|
|
342
|
|
|
$where = $this->normalizeOperator($where); |
343
|
1 |
|
|
344
|
1 |
|
$predicate[0] = $query->aqb->dateYear($this->normalizeColumn($query, $where['column'])); |
345
|
1 |
|
$predicate[1] = $where['operator']; |
346
|
1 |
|
$predicate[2] = $this->parameter($query, $where['value']); |
347
|
1 |
|
$predicate[3] = $where['boolean']; |
348
|
|
|
|
349
|
|
|
return $predicate; |
|
|
|
|
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Compile a "where month" clause. |
354
|
|
|
* |
355
|
|
|
* @param array<mixed> $where |
356
|
|
|
*/ |
357
|
|
|
protected function whereMonth(IluminateBuilder $query, $where): string |
358
|
1 |
|
{ |
359
|
|
|
$predicate = []; |
360
|
1 |
|
|
361
|
|
|
$where = $this->normalizeOperator($where); |
362
|
1 |
|
|
363
|
|
|
$predicate[0] = $query->aqb->dateMonth($this->normalizeColumn($query, $where['column'])); |
364
|
1 |
|
$predicate[1] = $where['operator']; |
365
|
1 |
|
$predicate[2] = $this->parameter($query, $where['value']); |
366
|
1 |
|
$predicate[3] = $where['boolean']; |
367
|
1 |
|
|
368
|
|
|
return $predicate; |
|
|
|
|
369
|
1 |
|
} |
370
|
|
|
|
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Compile a "where day" clause. |
374
|
|
|
* |
375
|
|
|
* @param array<mixed> $where |
376
|
|
|
*/ |
377
|
|
|
protected function whereDay(IluminateBuilder $query, $where): string |
378
|
|
|
{ |
379
|
1 |
|
$predicate = []; |
380
|
|
|
|
381
|
1 |
|
$where = $this->normalizeOperator($where); |
382
|
|
|
|
383
|
1 |
|
$predicate[0] = $query->aqb->dateDay($this->normalizeColumn($query, $where['column'])); |
384
|
|
|
$predicate[1] = $where['operator']; |
385
|
1 |
|
$predicate[2] = $this->parameter($query, $where['value']); |
386
|
|
|
$predicate[3] = $where['boolean']; |
387
|
1 |
|
|
388
|
1 |
|
return $predicate; |
|
|
|
|
389
|
1 |
|
} |
390
|
1 |
|
|
391
|
|
|
/** |
392
|
1 |
|
* Compile a "where time" clause. |
393
|
|
|
* |
394
|
|
|
* @param array<mixed> $where |
395
|
|
|
*/ |
396
|
|
|
protected function whereTime(IluminateBuilder $query, $where): string |
397
|
|
|
{ |
398
|
|
|
$predicate = []; |
399
|
|
|
|
400
|
|
|
$where = $this->normalizeOperator($where); |
401
|
|
|
|
402
|
|
|
$predicate[0] = $query->aqb->dateFormat($this->normalizeColumn($query, $where['column']), "%hh:%ii:%ss"); |
403
|
1 |
|
$predicate[1] = $where['operator']; |
404
|
|
|
$predicate[2] = $this->parameter($query, $where['value']); |
405
|
1 |
|
$predicate[3] = $where['boolean']; |
406
|
|
|
|
407
|
1 |
|
return $predicate; |
|
|
|
|
408
|
|
|
} |
409
|
1 |
|
|
410
|
1 |
|
/** |
411
|
1 |
|
* Compile a nested where clause. |
412
|
1 |
|
* |
413
|
|
|
* @param array<mixed> $where |
414
|
1 |
|
*/ |
415
|
|
|
protected function whereNested(IluminateBuilder $query, $where): string |
416
|
|
|
{ |
417
|
|
|
$predicates = []; |
|
|
|
|
418
|
|
|
$predicates = $this->compileWheresToArray($where['query']); |
|
|
|
|
419
|
|
|
|
420
|
|
|
$query->aqb->binds = array_merge($query->aqb->binds, $where['query']->aqb->binds); |
421
|
|
|
$query->aqb->collections = array_merge_recursive($query->aqb->collections, $where['query']->aqb->collections); |
422
|
|
|
|
423
|
|
|
return $predicates; |
424
|
1 |
|
} |
425
|
|
|
|
426
|
1 |
|
/** |
427
|
|
|
* Compile a where condition with a sub-select. |
428
|
1 |
|
* |
429
|
|
|
* @param array<mixed> $where |
430
|
1 |
|
*/ |
431
|
1 |
|
protected function whereSub(IluminateBuilder $query, $where): string |
432
|
1 |
|
{ |
433
|
1 |
|
$predicate = []; |
434
|
|
|
|
435
|
1 |
|
$where = $this->normalizeOperator($where); |
436
|
|
|
|
437
|
|
|
$predicate[0] = $this->normalizeColumn($query, $where['column']); |
438
|
|
|
$predicate[1] = $where['operator']; |
439
|
|
|
$predicate[2] = $where['query']->aqb; |
440
|
|
|
$predicate[3] = $where['boolean']; |
441
|
|
|
|
442
|
|
|
return $predicate; |
|
|
|
|
443
|
|
|
} |
444
|
|
|
|
445
|
1 |
|
|
446
|
|
|
/** |
447
|
1 |
|
* Compile a where exists clause. |
448
|
|
|
* |
449
|
1 |
|
* @param array<mixed> $where |
450
|
|
|
*/ |
451
|
1 |
|
protected function whereExists(IluminateBuilder $query, $where): string |
|
|
|
|
452
|
1 |
|
{ |
453
|
1 |
|
$predicate = []; |
454
|
1 |
|
|
455
|
|
|
$predicate[0] = $where['query']->aqb; |
456
|
1 |
|
$predicate[1] = $where['operator']; |
457
|
|
|
$predicate[2] = $where['value']; |
458
|
|
|
$predicate[3] = $where['boolean']; |
459
|
|
|
|
460
|
|
|
return $predicate; |
|
|
|
|
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Compile a where exists clause. |
465
|
|
|
* |
466
|
|
|
* @param array<mixed> $where |
467
|
1 |
|
*/ |
468
|
|
|
protected function whereNotExists(IluminateBuilder $query, $where): string |
|
|
|
|
469
|
1 |
|
{ |
470
|
|
|
$predicate = []; |
471
|
1 |
|
|
472
|
|
|
$predicate[0] = $where['query']->aqb; |
473
|
1 |
|
$predicate[1] = $where['operator']; |
474
|
1 |
|
$predicate[2] = $where['value']; |
475
|
1 |
|
$predicate[3] = $where['boolean']; |
476
|
1 |
|
|
477
|
|
|
return $predicate; |
|
|
|
|
478
|
1 |
|
} |
479
|
|
|
} |
480
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.