1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
6
|
|
|
|
7
|
|
|
use Carbon\CarbonPeriod; |
8
|
|
|
use Closure; |
9
|
|
|
use Illuminate\Contracts\Database\Query\ConditionExpression; |
10
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
11
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; |
12
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
13
|
|
|
use Illuminate\Database\Query\Expression; |
14
|
|
|
use Illuminate\Support\Arr; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder; |
17
|
|
|
use LogicException; |
18
|
|
|
|
19
|
|
|
trait BuildsWheres |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Add a "where fulltext" clause to the query. |
23
|
|
|
* |
24
|
|
|
* @param string|string[] $columns |
25
|
|
|
* @param string $value |
26
|
|
|
* @param string $boolean |
27
|
|
|
* @return $this |
28
|
|
|
* |
29
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
30
|
|
|
*/ |
31
|
|
|
public function whereFullText($columns, $value, array $options = [], $boolean = 'and') |
32
|
|
|
{ |
33
|
|
|
// NOTE: can be done by listing all fulltext calls and using it on the collection name from $builder->from |
34
|
|
|
// distinctly merging results from multiple calls on the same collection. |
35
|
|
|
// For a call on a joined collection it might need to be moved to the JoinClause |
36
|
|
|
throw new LogicException('This database driver does not support the whereFullText method.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Prepare the value and operator for a where clause. |
41
|
|
|
* |
42
|
|
|
* @param string $value |
43
|
|
|
* @param string $operator |
44
|
|
|
* @param bool $useDefault |
45
|
|
|
* @return array |
46
|
|
|
* |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
* |
49
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
50
|
|
|
*/ |
51
|
|
|
public function prepareValueAndOperator($value, $operator, $useDefault = false) |
52
|
|
|
{ |
53
|
|
|
if ($useDefault) { |
54
|
|
|
return [$operator, '==']; |
55
|
|
|
} elseif ($this->invalidOperatorAndValue($operator, $value)) { |
|
|
|
|
56
|
|
|
throw new InvalidArgumentException('Illegal operator and value combination.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return [$value, $operator]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add an array of where clauses to the query. |
64
|
|
|
* |
65
|
|
|
* @param array $column |
66
|
|
|
* @param string $boolean |
67
|
|
|
* @param string $method |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
protected function addArrayOfWheres($column, $boolean, $method = 'where') |
71
|
|
|
{ |
72
|
|
|
$column = associativeFlatten($column); |
73
|
|
|
|
74
|
|
|
return $this->whereNested(function ($query) use ($column, $method, $boolean) { |
|
|
|
|
75
|
|
|
foreach ($column as $key => $value) { |
76
|
|
|
$query->{$method}($key, '==', $value, $boolean); |
77
|
|
|
} |
78
|
|
|
}, $boolean); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $operator |
83
|
|
|
* @param mixed $value |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function validateOperator(mixed $operator, mixed $value): array |
87
|
|
|
{ |
88
|
|
|
// If the given operator is not found in the list of valid operators we will |
89
|
|
|
// assume that the developer is just short-cutting the '==' operators and |
90
|
|
|
// we will set the operators to '==' and set the values appropriately. |
91
|
|
|
if ($this->invalidOperator($operator)) { |
|
|
|
|
92
|
|
|
[$value, $operator] = [$operator, '==']; |
93
|
|
|
} |
94
|
|
|
return array($value, $operator); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param mixed $operator |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getType(mixed $operator): string |
102
|
|
|
{ |
103
|
|
|
$type = 'Basic'; |
104
|
|
|
|
105
|
|
|
if ($this->isBitwiseOperator($operator)) { |
|
|
|
|
106
|
|
|
$type = 'Bitwise'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $type; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Add a date based (year, month, day, time) statement to the query. |
114
|
|
|
* |
115
|
|
|
* @param string $type |
116
|
|
|
* @param string $column |
117
|
|
|
* @param string $operator |
118
|
|
|
* @param mixed $value |
119
|
|
|
* @param string $boolean |
120
|
|
|
* @return IlluminateQueryBuilder |
121
|
|
|
*/ |
122
|
|
|
protected function addDateBasedWhere($type, $column, $operator, $value, $boolean = 'and') |
123
|
|
|
{ |
124
|
|
|
$value = $this->bindValue($value); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$this->wheres[] = compact('column', 'type', 'boolean', 'operator', 'value'); |
|
|
|
|
127
|
|
|
|
128
|
|
|
return $this; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add another query builder as a nested where to the query builder. |
134
|
|
|
* |
135
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
136
|
|
|
* @param string $boolean |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function addNestedWhereQuery($query, $boolean = 'and') |
140
|
|
|
{ |
141
|
|
|
if (count($query->wheres)) { |
142
|
|
|
$type = 'Nested'; |
143
|
|
|
|
144
|
|
|
$this->wheres[] = compact('type', 'query', 'boolean'); |
|
|
|
|
145
|
|
|
$this->importBindings($query); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Add an exists clause to the query. |
153
|
|
|
* |
154
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
155
|
|
|
* @param string $boolean |
156
|
|
|
* @param bool $not |
157
|
|
|
* @return $this |
158
|
|
|
* |
159
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
160
|
|
|
*/ |
161
|
|
|
public function addWhereExistsQuery(IlluminateQueryBuilder $query, $boolean = 'and', $not = false) |
162
|
|
|
{ |
163
|
|
|
$type = $not ? 'NotExists' : 'Exists'; |
164
|
|
|
|
165
|
|
|
[$subquery] = $this->parseSub($query); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$this->wheres[] = compact('type', 'subquery', 'boolean'); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Merge an array of where clauses and bindings. |
174
|
|
|
* |
175
|
|
|
* @param array $wheres |
176
|
|
|
* @param array $bindings |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function mergeWheres($wheres, $bindings) |
180
|
|
|
{ |
181
|
|
|
$this->wheres = array_merge($this->wheres, (array) $wheres); |
|
|
|
|
182
|
|
|
|
183
|
|
|
$this->bindings['where'] = array_merge($this->bindings['where'], (array) $bindings); |
|
|
|
|
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Add a basic where clause to the query. |
190
|
|
|
* |
191
|
|
|
* @param \Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|Expression|string|array $column |
192
|
|
|
* @param mixed $operator |
193
|
|
|
* @param mixed $value |
194
|
|
|
* @param string $boolean |
195
|
|
|
* @return IlluminateQueryBuilder |
196
|
|
|
* |
197
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
198
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
199
|
|
|
*/ |
200
|
|
|
public function where($column, $operator = null, $value = null, $boolean = 'and') |
201
|
|
|
{ |
202
|
|
|
if ($column instanceof ConditionExpression) { |
203
|
|
|
$type = 'Expression'; |
204
|
|
|
|
205
|
|
|
$this->wheres[] = compact('type', 'column', 'boolean'); |
|
|
|
|
206
|
|
|
|
207
|
|
|
return $this; |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// If the column is an array, we will assume it is an array of key-value pairs |
211
|
|
|
// and can add them each as a where clause. We will maintain the boolean we |
212
|
|
|
// received when the method was called and pass it into the nested where. |
213
|
|
|
if (is_array($column) && !array_is_list($column)) { |
214
|
|
|
return $this->addArrayOfWheres($column, $boolean); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// Here we will make some assumptions about the operator. If only 2 values are |
218
|
|
|
// passed to the method, we will assume that the operator is an equals sign |
219
|
|
|
// and keep going. Otherwise, we'll require the operator to be passed in. |
220
|
|
|
[$value, $operator] = $this->prepareValueAndOperator( |
221
|
|
|
$value, |
222
|
|
|
$operator, |
223
|
|
|
func_num_args() === 2 |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
// If the column is actually a Closure instance, we will assume the developer |
227
|
|
|
// wants to begin a nested where statement which is wrapped in parentheses. |
228
|
|
|
// We will add that Closure to the query and return back out immediately. |
229
|
|
|
if ($column instanceof Closure && is_null($operator)) { |
230
|
|
|
return $this->whereNested($column, $boolean); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// If the column is a Closure instance and there is an operator value, we will |
234
|
|
|
// assume the developer wants to run a subquery and then compare the result |
235
|
|
|
// of that subquery with the given value that was provided to the method. |
236
|
|
|
if ($this->isQueryable($column) && !is_null($operator)) { |
|
|
|
|
237
|
|
|
[$subquery] = $this->createSub($column, true); |
|
|
|
|
238
|
|
|
|
239
|
|
|
return $this->where(new Expression($subquery), $operator, $value, $boolean); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
list($value, $operator) = $this->validateOperator($operator, $value); |
243
|
|
|
|
244
|
|
|
// If the value is a Closure, it means the developer is performing an entire |
245
|
|
|
// sub-select within the query and we will need to compile the sub-select |
246
|
|
|
// within the where clause to get the appropriate query record results. |
247
|
|
|
if ($this->isQueryable($value)) { |
248
|
|
|
return $this->whereSub($column, $operator, $value, $boolean); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// If the value is "null", we will just assume the developer wants to add a |
252
|
|
|
// where null clause to the query. So, we will allow a short-cut here to |
253
|
|
|
// that method for convenience so the developer doesn't have to check. |
254
|
|
|
if (is_null($value)) { |
255
|
|
|
return $this->whereNull($column, $boolean, $operator !== '=='); |
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$type = $this->getType($operator); |
259
|
|
|
|
260
|
|
|
$value = $this->bindValue($value); |
261
|
|
|
|
262
|
|
|
// Now that we are working with just a simple query we can put the elements |
263
|
|
|
// in our array and add the query binding to our array of bindings that |
264
|
|
|
// will be bound to each SQL statements when it is finally executed. |
265
|
|
|
$this->wheres[] = compact( |
266
|
|
|
'type', |
267
|
|
|
'column', |
268
|
|
|
'operator', |
269
|
|
|
'value', |
270
|
|
|
'boolean' |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
return $this; |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Add a where between statement to the query. |
278
|
|
|
* |
279
|
|
|
* @param string|Expression $column |
280
|
|
|
* @param string $boolean |
281
|
|
|
* @param bool $not |
282
|
|
|
* @return IlluminateQueryBuilder |
283
|
|
|
* |
284
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
285
|
|
|
*/ |
286
|
|
|
public function whereBetween($column, iterable $values, $boolean = 'and', $not = false) |
287
|
|
|
{ |
288
|
|
|
$type = 'between'; |
289
|
|
|
|
290
|
|
|
if ($values instanceof CarbonPeriod) { |
291
|
|
|
$values = $values->toArray(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$values = array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2); |
|
|
|
|
295
|
|
|
$values[0] = $this->bindValue($values[0]); |
296
|
|
|
$values[1] = $this->bindValue($values[1]); |
297
|
|
|
|
298
|
|
|
$this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not'); |
|
|
|
|
299
|
|
|
|
300
|
|
|
return $this; |
|
|
|
|
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Add a "where" clause comparing two columns to the query. |
305
|
|
|
* |
306
|
|
|
* @param string|array $first |
307
|
|
|
* @param string|null $operator |
308
|
|
|
* @param string|null $second |
309
|
|
|
* @param string|null $boolean |
310
|
|
|
* @return $this |
311
|
|
|
*/ |
312
|
|
|
public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') |
313
|
|
|
{ |
314
|
|
|
// If the column is an array, we will assume it is an array of key-value pairs |
315
|
|
|
// and can add them each as a where clause. We will maintain the boolean we |
316
|
|
|
// received when the method was called and pass it into the nested where. |
317
|
|
|
if (is_array($first) && !array_is_list($first)) { |
318
|
|
|
return $this->addArrayOfWheres($first, $boolean, 'whereColumn'); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// If the given operator is not found in the list of valid operators we will |
322
|
|
|
// assume that the developer is just short-cutting the '=' operators and |
323
|
|
|
// we will set the operators to '=' and set the values appropriately. |
324
|
|
|
if ($this->invalidOperator($operator)) { |
325
|
|
|
[$second, $operator] = [$operator, '=']; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
// Finally, we will add this where clause into this array of clauses that we |
329
|
|
|
// are building for the query. All of them will be compiled via a grammar |
330
|
|
|
// once the query is about to be executed and run against the database. |
331
|
|
|
$type = 'Column'; |
332
|
|
|
|
333
|
|
|
$this->wheres[] = compact( |
|
|
|
|
334
|
|
|
'type', |
335
|
|
|
'first', |
336
|
|
|
'operator', |
337
|
|
|
'second', |
338
|
|
|
'boolean' |
339
|
|
|
); |
340
|
|
|
|
341
|
|
|
return $this; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Add a "where in" clause to the query. |
347
|
|
|
* |
348
|
|
|
* @param string $column |
349
|
|
|
* @param mixed $values |
350
|
|
|
* @param string $boolean |
351
|
|
|
* @param bool $not |
352
|
|
|
* @return IlluminateQueryBuilder |
353
|
|
|
* |
354
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
355
|
|
|
*/ |
356
|
|
|
public function whereIn($column, $values, $boolean = 'and', $not = false) |
357
|
|
|
{ |
358
|
|
|
$type = $not ? 'NotIn' : 'In'; |
359
|
|
|
|
360
|
|
|
// If the value is a query builder instance we will assume the developer wants to |
361
|
|
|
// look for any values that exist within this given query. So, we will add the |
362
|
|
|
// query accordingly so that this query is properly executed when it is run. |
363
|
|
|
if ($this->isQueryable($values)) { |
364
|
|
|
[$query, $bindings] = $this->createSub($values); |
365
|
|
|
|
366
|
|
|
$values = [new Expression($query)]; |
367
|
|
|
|
368
|
|
|
$this->addBinding($bindings, 'where'); |
|
|
|
|
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
// Next, if the value is Arrayable we need to cast it to its raw array form so we |
372
|
|
|
// have the underlying array value instead of an Arrayable object which is not |
373
|
|
|
// able to be added as a binding, etc. We will then add to the wheres array. |
374
|
|
|
if ($values instanceof Arrayable) { |
375
|
|
|
$values = $values->toArray(); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
$values = $this->bindValue($this->cleanBindings($values)); |
379
|
|
|
|
380
|
|
|
$this->wheres[] = compact('type', 'column', 'values', 'boolean'); |
|
|
|
|
381
|
|
|
|
382
|
|
|
return $this; |
|
|
|
|
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Add a "where JSON contains" clause to the query. |
387
|
|
|
* |
388
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
389
|
|
|
* |
390
|
|
|
* @param string $column |
391
|
|
|
* @param mixed $value |
392
|
|
|
* @param string $boolean |
393
|
|
|
* @param bool $not |
394
|
|
|
* @return IlluminateQueryBuilder |
395
|
|
|
*/ |
396
|
|
|
public function whereJsonContains($column, $value, $boolean = 'and', $not = false) |
397
|
|
|
{ |
398
|
|
|
$type = 'JsonContains'; |
399
|
|
|
|
400
|
|
|
$value = $this->bindValue($value); |
401
|
|
|
|
402
|
|
|
$this->wheres[] = compact('type', 'column', 'value', 'boolean', 'not'); |
|
|
|
|
403
|
|
|
|
404
|
|
|
return $this; |
|
|
|
|
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Add a "where JSON length" clause to the query. |
409
|
|
|
* |
410
|
|
|
* @param string $column |
411
|
|
|
* @param mixed $operator |
412
|
|
|
* @param mixed $value |
413
|
|
|
* @param string $boolean |
414
|
|
|
* @return IlluminateQueryBuilder |
415
|
|
|
*/ |
416
|
|
|
public function whereJsonLength($column, $operator, $value = null, $boolean = 'and') |
417
|
|
|
{ |
418
|
|
|
$type = 'JsonLength'; |
419
|
|
|
|
420
|
|
|
[$value, $operator] = $this->prepareValueAndOperator( |
421
|
|
|
$value, |
422
|
|
|
$operator, |
423
|
|
|
func_num_args() === 2 |
424
|
|
|
); |
425
|
|
|
|
426
|
|
|
$value = $this->bindValue((int) $this->flattenValue($value)); |
|
|
|
|
427
|
|
|
|
428
|
|
|
$this->wheres[] = compact('type', 'column', 'operator', 'value', 'boolean'); |
|
|
|
|
429
|
|
|
|
430
|
|
|
return $this; |
|
|
|
|
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Add a full sub-select to the query. |
435
|
|
|
* |
436
|
|
|
* @param \Illuminate\Contracts\Database\Query\Expression|string $column |
437
|
|
|
* @param string $operator |
438
|
|
|
* @param \Closure|\Illuminate\Database\Query\Builder|IlluminateEloquentBuilder $callback |
439
|
|
|
* @param string $boolean |
440
|
|
|
* @return $this |
441
|
|
|
*/ |
442
|
|
|
protected function whereSub($column, $operator, $callback, $boolean) |
443
|
|
|
{ |
444
|
|
|
assert($this instanceof Builder); |
445
|
|
|
|
446
|
|
|
$type = 'Sub'; |
447
|
|
|
|
448
|
|
|
// Once we have the query instance we can simply execute it so it can add all |
449
|
|
|
// of the sub-select's conditions to itself, and then we can cache it off |
450
|
|
|
// in the array of where clauses for the "main" parent query instance. |
451
|
|
|
call_user_func($callback, $query = $this->forSubQuery()); |
|
|
|
|
452
|
|
|
|
453
|
|
|
assert($query instanceof Builder); |
454
|
|
|
|
455
|
|
|
$query->returnSingleValue = true; |
456
|
|
|
|
457
|
|
|
[$subquery] = $this->parseSub($query); |
458
|
|
|
|
459
|
|
|
$this->wheres[] = compact( |
460
|
|
|
'type', |
461
|
|
|
'column', |
462
|
|
|
'operator', |
463
|
|
|
'subquery', |
464
|
|
|
'boolean' |
465
|
|
|
); |
466
|
|
|
|
467
|
|
|
return $this; |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|