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