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