1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BaoPham\DynamoDb; |
4
|
|
|
|
5
|
|
|
use BaoPham\DynamoDb\Concerns\HasParsers; |
6
|
|
|
use BaoPham\DynamoDb\ConditionAnalyzer\Analyzer; |
7
|
|
|
use BaoPham\DynamoDb\Facades\DynamoDb; |
8
|
|
|
use BaoPham\DynamoDb\H; |
9
|
|
|
use Closure; |
10
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
11
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
12
|
|
|
use Illuminate\Database\Eloquent\Scope; |
13
|
|
|
|
14
|
|
|
class DynamoDbQueryBuilder |
15
|
|
|
{ |
16
|
|
|
use HasParsers; |
17
|
|
|
|
18
|
|
|
const MAX_LIMIT = -1; |
19
|
|
|
const DEFAULT_TO_ITERATOR = true; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The maximum number of records to return. |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
public $limit; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $wheres = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DynamoDbModel |
35
|
|
|
*/ |
36
|
|
|
protected $model; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Aws\DynamoDb\DynamoDbClient |
40
|
|
|
*/ |
41
|
|
|
protected $client; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Closure |
45
|
|
|
*/ |
46
|
|
|
protected $decorator; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Applied global scopes. |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $scopes = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Removed global scopes. |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $removedScopes = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* When not using the iterator, you can store the lastEvaluatedKey to |
64
|
|
|
* paginate through the results. The getAll method will take this into account |
65
|
|
|
* when used with $use_iterator = false. |
66
|
|
|
* |
67
|
|
|
* @var mixed |
68
|
|
|
*/ |
69
|
|
|
protected $lastEvaluatedKey; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Specified index name for the query. |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected $index; |
77
|
|
|
|
78
|
131 |
|
public function __construct(DynamoDbModel $model) |
79
|
|
|
{ |
80
|
131 |
|
$this->model = $model; |
81
|
131 |
|
$this->client = $model->getClient(); |
82
|
131 |
|
$this->setupExpressions(); |
83
|
131 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Alias to set the "limit" value of the query. |
87
|
|
|
* |
88
|
|
|
* @param int $value |
89
|
|
|
* @return DynamoDbQueryBuilder |
90
|
|
|
*/ |
91
|
6 |
|
public function take($value) |
92
|
|
|
{ |
93
|
6 |
|
return $this->limit($value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the "limit" value of the query. |
98
|
|
|
* |
99
|
|
|
* @param int $value |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
14 |
|
public function limit($value) |
103
|
|
|
{ |
104
|
14 |
|
$this->limit = $value; |
105
|
|
|
|
106
|
14 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Alias to set the "offset" value of the query. |
111
|
|
|
* |
112
|
|
|
* @param int $value |
113
|
|
|
* @throws NotSupportedException |
114
|
|
|
*/ |
115
|
|
|
public function skip($value) |
116
|
|
|
{ |
117
|
|
|
return $this->offset($value); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the "offset" value of the query. |
122
|
|
|
* |
123
|
|
|
* @param int $value |
124
|
|
|
* @throws NotSupportedException |
125
|
|
|
*/ |
126
|
|
|
public function offset($value) |
127
|
|
|
{ |
128
|
|
|
throw new NotSupportedException('Skip/Offset is not supported. Consider using after() instead'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Determine the starting point (exclusively) of the query. |
133
|
|
|
* Unfortunately, offset of how many records to skip does not make sense for DynamoDb. |
134
|
|
|
* Instead, provide the last result of the previous query as the starting point for the next query. |
135
|
|
|
* |
136
|
|
|
* @param DynamoDbModel|null $after |
137
|
|
|
* Examples: |
138
|
|
|
* |
139
|
|
|
* For query such as |
140
|
|
|
* $query = $model->where('count', 10)->limit(2); |
141
|
|
|
* $last = $query->all()->last(); |
142
|
|
|
* Take the last item of this query result as the next "offset": |
143
|
|
|
* $nextPage = $query->after($last)->limit(2)->all(); |
144
|
|
|
* |
145
|
|
|
* Alternatively, pass in nothing to reset the starting point. |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
4 |
|
public function after(DynamoDbModel $after = null) |
150
|
|
|
{ |
151
|
4 |
|
if (empty($after)) { |
152
|
4 |
|
$this->lastEvaluatedKey = null; |
153
|
|
|
|
154
|
4 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
$afterKey = $after->getKeys(); |
158
|
|
|
|
159
|
4 |
|
$analyzer = $this->getConditionAnalyzer(); |
160
|
|
|
|
161
|
4 |
|
if ($index = $analyzer->index()) { |
162
|
1 |
|
foreach ($index->columns() as $column) { |
163
|
1 |
|
$afterKey[$column] = $after->getAttribute($column); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
4 |
|
$this->lastEvaluatedKey = DynamoDb::marshalItem($afterKey); |
168
|
|
|
|
169
|
4 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Similar to after(), but instead of using the model instance, the model's keys are used. |
174
|
|
|
* Use $collection->lastKey() or $model->getKeys() to retrieve the value. |
175
|
|
|
* |
176
|
|
|
* @param Array $key |
177
|
|
|
* Examples: |
178
|
|
|
* |
179
|
|
|
* For query such as |
180
|
|
|
* $query = $model->where('count', 10)->limit(2); |
181
|
|
|
* $items = $query->all(); |
182
|
|
|
* Take the last item of this query result as the next "offset": |
183
|
|
|
* $nextPage = $query->afterKey($items->lastKey())->limit(2)->all(); |
184
|
|
|
* |
185
|
|
|
* Alternatively, pass in nothing to reset the starting point. |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
4 |
|
public function afterKey($key = null) |
190
|
|
|
{ |
191
|
4 |
|
$this->lastEvaluatedKey = empty($key) ? null : DynamoDb::marshalItem($key); |
192
|
4 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set the index name manually |
197
|
|
|
* |
198
|
|
|
* @param string $index The index name |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
1 |
|
public function withIndex($index) |
202
|
|
|
{ |
203
|
1 |
|
$this->index = $index; |
204
|
1 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
76 |
|
public function where($column, $operator = null, $value = null, $boolean = 'and') |
208
|
|
|
{ |
209
|
|
|
// If the column is an array, we will assume it is an array of key-value pairs |
210
|
|
|
// and can add them each as a where clause. We will maintain the boolean we |
211
|
|
|
// received when the method was called and pass it into the nested where. |
212
|
76 |
|
if (is_array($column)) { |
213
|
2 |
|
foreach ($column as $key => $value) { |
214
|
2 |
|
$this->where($key, '=', $value, $boolean); |
215
|
|
|
} |
216
|
|
|
|
217
|
2 |
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// Here we will make some assumptions about the operator. If only 2 values are |
221
|
|
|
// passed to the method, we will assume that the operator is an equals sign |
222
|
|
|
// and keep going. Otherwise, we'll require the operator to be passed in. |
223
|
76 |
|
if (func_num_args() == 2) { |
224
|
45 |
|
list($value, $operator) = [$operator, '=']; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// If the columns is actually a Closure instance, we will assume the developer |
228
|
|
|
// wants to begin a nested where statement which is wrapped in parenthesis. |
229
|
|
|
// We'll add that Closure to the query then return back out immediately. |
230
|
76 |
|
if ($column instanceof Closure) { |
231
|
2 |
|
return $this->whereNested($column, $boolean); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// If the given operator is not found in the list of valid operators we will |
235
|
|
|
// assume that the developer is just short-cutting the '=' operators and |
236
|
|
|
// we will set the operators to '=' and set the values appropriately. |
237
|
76 |
|
if (!ComparisonOperator::isValidOperator($operator)) { |
238
|
4 |
|
list($value, $operator) = [$operator, '=']; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// If the value is a Closure, it means the developer is performing an entire |
242
|
|
|
// sub-select within the query and we will need to compile the sub-select |
243
|
|
|
// within the where clause to get the appropriate query record results. |
244
|
76 |
|
if ($value instanceof Closure) { |
245
|
|
|
throw new NotSupportedException('Closure in where clause is not supported'); |
246
|
|
|
} |
247
|
|
|
|
248
|
76 |
|
$this->wheres[] = [ |
249
|
76 |
|
'column' => $column, |
250
|
76 |
|
'type' => ComparisonOperator::getDynamoDbOperator($operator), |
251
|
76 |
|
'value' => $value, |
252
|
76 |
|
'boolean' => $boolean, |
253
|
|
|
]; |
254
|
|
|
|
255
|
76 |
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Add a nested where statement to the query. |
260
|
|
|
* |
261
|
|
|
* @param \Closure $callback |
262
|
|
|
* @param string $boolean |
263
|
|
|
* @return $this |
264
|
|
|
*/ |
265
|
2 |
|
public function whereNested(Closure $callback, $boolean = 'and') |
266
|
|
|
{ |
267
|
2 |
|
call_user_func($callback, $query = $this->forNestedWhere()); |
268
|
|
|
|
269
|
2 |
|
return $this->addNestedWhereQuery($query, $boolean); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Create a new query instance for nested where condition. |
274
|
|
|
* |
275
|
|
|
* @return $this |
276
|
|
|
*/ |
277
|
2 |
|
public function forNestedWhere() |
278
|
|
|
{ |
279
|
2 |
|
return $this->newQuery(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Add another query builder as a nested where to the query builder. |
284
|
|
|
* |
285
|
|
|
* @param DynamoDbQueryBuilder $query |
286
|
|
|
* @param string $boolean |
287
|
|
|
* @return $this |
288
|
|
|
*/ |
289
|
2 |
|
public function addNestedWhereQuery($query, $boolean = 'and') |
290
|
|
|
{ |
291
|
2 |
|
if (count($query->wheres)) { |
292
|
2 |
|
$type = 'Nested'; |
293
|
2 |
|
$column = null; |
294
|
2 |
|
$value = $query->wheres; |
295
|
2 |
|
$this->wheres[] = compact('column', 'type', 'value', 'boolean'); |
296
|
|
|
} |
297
|
|
|
|
298
|
2 |
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Add an "or where" clause to the query. |
303
|
|
|
* |
304
|
|
|
* @param string $column |
305
|
|
|
* @param string $operator |
306
|
|
|
* @param mixed $value |
307
|
|
|
* @return $this |
308
|
|
|
*/ |
309
|
22 |
|
public function orWhere($column, $operator = null, $value = null) |
310
|
|
|
{ |
311
|
22 |
|
return $this->where($column, $operator, $value, 'or'); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Add a "where in" clause to the query. |
316
|
|
|
* |
317
|
|
|
* @param string $column |
318
|
|
|
* @param mixed $values |
319
|
|
|
* @param string $boolean |
320
|
|
|
* @param bool $not |
321
|
|
|
* @return $this |
322
|
|
|
* @throws NotSupportedException |
323
|
|
|
*/ |
324
|
2 |
|
public function whereIn($column, $values, $boolean = 'and', $not = false) |
325
|
|
|
{ |
326
|
2 |
|
if ($not) { |
327
|
|
|
throw new NotSupportedException('"not in" is not a valid DynamoDB comparison operator'); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// If the value is a query builder instance, not supported |
331
|
2 |
|
if ($values instanceof static) { |
332
|
|
|
throw new NotSupportedException('Value is a query builder instance'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// If the value of the where in clause is actually a Closure, not supported |
336
|
2 |
|
if ($values instanceof Closure) { |
337
|
|
|
throw new NotSupportedException('Value is a Closure'); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
// Next, if the value is Arrayable we need to cast it to its raw array form |
341
|
2 |
|
if ($values instanceof Arrayable) { |
342
|
|
|
$values = $values->toArray(); |
343
|
|
|
} |
344
|
|
|
|
345
|
2 |
|
return $this->where($column, ComparisonOperator::IN, $values, $boolean); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Add an "or where in" clause to the query. |
350
|
|
|
* |
351
|
|
|
* @param string $column |
352
|
|
|
* @param mixed $values |
353
|
|
|
* @return $this |
354
|
|
|
*/ |
355
|
2 |
|
public function orWhereIn($column, $values) |
356
|
|
|
{ |
357
|
2 |
|
return $this->whereIn($column, $values, 'or'); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Add a "where null" clause to the query. |
362
|
|
|
* |
363
|
|
|
* @param string $column |
364
|
|
|
* @param string $boolean |
365
|
|
|
* @param bool $not |
366
|
|
|
* @return $this |
367
|
|
|
*/ |
368
|
4 |
|
public function whereNull($column, $boolean = 'and', $not = false) |
369
|
|
|
{ |
370
|
4 |
|
$type = $not ? ComparisonOperator::NOT_NULL : ComparisonOperator::NULL; |
371
|
|
|
|
372
|
4 |
|
$this->wheres[] = compact('column', 'type', 'boolean'); |
373
|
|
|
|
374
|
4 |
|
return $this; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Add an "or where null" clause to the query. |
379
|
|
|
* |
380
|
|
|
* @param string $column |
381
|
|
|
* @return $this |
382
|
|
|
*/ |
383
|
2 |
|
public function orWhereNull($column) |
384
|
|
|
{ |
385
|
2 |
|
return $this->whereNull($column, 'or'); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Add an "or where not null" clause to the query. |
390
|
|
|
* |
391
|
|
|
* @param string $column |
392
|
|
|
* @return $this |
393
|
|
|
*/ |
394
|
2 |
|
public function orWhereNotNull($column) |
395
|
|
|
{ |
396
|
2 |
|
return $this->whereNotNull($column, 'or'); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Add a "where not null" clause to the query. |
401
|
|
|
* |
402
|
|
|
* @param string $column |
403
|
|
|
* @param string $boolean |
404
|
|
|
* @return $this |
405
|
|
|
*/ |
406
|
2 |
|
public function whereNotNull($column, $boolean = 'and') |
407
|
|
|
{ |
408
|
2 |
|
return $this->whereNull($column, $boolean, true); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Get a new instance of the query builder. |
413
|
|
|
* |
414
|
|
|
* @return DynamoDbQueryBuilder |
415
|
|
|
*/ |
416
|
2 |
|
public function newQuery() |
417
|
|
|
{ |
418
|
2 |
|
return new static($this->getModel()); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Implements the Query Chunk method |
423
|
|
|
* |
424
|
|
|
* @param int $chunkSize |
425
|
|
|
* @param callable $callback |
426
|
|
|
*/ |
427
|
9 |
|
public function chunk($chunkSize, callable $callback) |
428
|
|
|
{ |
429
|
9 |
|
while (true) { |
430
|
9 |
|
$results = $this->getAll([], $chunkSize, false); |
431
|
|
|
|
432
|
9 |
|
if ($results->isNotEmpty()) { |
433
|
9 |
|
if (call_user_func($callback, $results) === false) { |
434
|
2 |
|
return false; |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
7 |
|
if (empty($this->lastEvaluatedKey)) { |
439
|
7 |
|
break; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
443
|
7 |
|
return true; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @param $id |
448
|
|
|
* @param array $columns |
449
|
|
|
* @return DynamoDbModel|\Illuminate\Database\Eloquent\Collection|null |
450
|
|
|
*/ |
451
|
40 |
|
public function find($id, array $columns = []) |
452
|
|
|
{ |
453
|
40 |
|
if ($this->isMultipleIds($id)) { |
454
|
4 |
|
return $this->findMany($id, $columns); |
455
|
|
|
} |
456
|
|
|
|
457
|
36 |
|
$this->resetExpressions(); |
458
|
|
|
|
459
|
36 |
|
$this->model->setId($id); |
460
|
|
|
|
461
|
36 |
|
$query = DynamoDb::table($this->model->getTable()) |
462
|
36 |
|
->setKey(DynamoDb::marshalItem($this->model->getKeys())) |
463
|
36 |
|
->setConsistentRead(true); |
464
|
|
|
|
465
|
36 |
|
if (!empty($columns)) { |
466
|
|
|
$query |
467
|
3 |
|
->setProjectionExpression($this->projectionExpression->parse($columns)) |
468
|
3 |
|
->setExpressionAttributeNames($this->expressionAttributeNames->all()); |
469
|
|
|
} |
470
|
|
|
|
471
|
36 |
|
$item = $query->prepare($this->client)->getItem(); |
472
|
|
|
|
473
|
36 |
|
$item = array_get($item->toArray(), 'Item'); |
474
|
|
|
|
475
|
36 |
|
if (empty($item)) { |
476
|
4 |
|
return null; |
477
|
|
|
} |
478
|
|
|
|
479
|
32 |
|
$item = DynamoDb::unmarshalItem($item); |
480
|
|
|
|
481
|
32 |
|
$model = $this->model->newInstance([], true); |
482
|
|
|
|
483
|
32 |
|
$model->setRawAttributes($item, true); |
484
|
|
|
|
485
|
32 |
|
return $model; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @param $ids |
490
|
|
|
* @param array $columns |
491
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
492
|
|
|
*/ |
493
|
4 |
|
public function findMany($ids, array $columns = []) |
494
|
|
|
{ |
495
|
4 |
|
$collection = $this->model->newCollection(); |
496
|
|
|
|
497
|
4 |
|
if (empty($ids)) { |
498
|
|
|
return $collection; |
499
|
|
|
} |
500
|
|
|
|
501
|
4 |
|
$this->resetExpressions(); |
502
|
|
|
|
503
|
4 |
|
$table = $this->model->getTable(); |
504
|
|
|
|
505
|
|
|
$keys = collect($ids)->map(function ($id) { |
506
|
4 |
|
if (! is_array($id)) { |
507
|
2 |
|
$id = [$this->model->getKeyName() => $id]; |
508
|
|
|
} |
509
|
|
|
|
510
|
4 |
|
return DynamoDb::marshalItem($id); |
511
|
4 |
|
}); |
512
|
|
|
|
513
|
4 |
|
$subQuery = DynamoDb::newQuery() |
514
|
4 |
|
->setKeys($keys->toArray()) |
515
|
4 |
|
->setProjectionExpression($this->projectionExpression->parse($columns)) |
516
|
4 |
|
->setExpressionAttributeNames($this->expressionAttributeNames->all()) |
517
|
4 |
|
->prepare($this->client) |
518
|
4 |
|
->query; |
519
|
|
|
|
520
|
4 |
|
$results = DynamoDb::newQuery() |
521
|
4 |
|
->setRequestItems([$table => $subQuery]) |
522
|
4 |
|
->prepare($this->client) |
523
|
4 |
|
->batchGetItem(); |
524
|
|
|
|
525
|
4 |
|
foreach ($results['Responses'][$table] as $item) { |
526
|
4 |
|
$item = DynamoDb::unmarshalItem($item); |
527
|
4 |
|
$model = $this->model->newInstance([], true); |
528
|
4 |
|
$model->setRawAttributes($item, true); |
529
|
4 |
|
$collection->add($model); |
530
|
|
|
} |
531
|
|
|
|
532
|
4 |
|
return $collection; |
533
|
|
|
} |
534
|
|
|
|
535
|
5 |
|
public function findOrFail($id, $columns = []) |
536
|
|
|
{ |
537
|
5 |
|
$result = $this->find($id, $columns); |
538
|
|
|
|
539
|
5 |
|
if ($this->isMultipleIds($id)) { |
540
|
1 |
|
if (count($result) == count(array_unique($id))) { |
|
|
|
|
541
|
1 |
|
return $result; |
542
|
|
|
} |
543
|
4 |
|
} elseif (! is_null($result)) { |
544
|
2 |
|
return $result; |
545
|
|
|
} |
546
|
|
|
|
547
|
2 |
|
throw (new ModelNotFoundException)->setModel( |
548
|
2 |
|
get_class($this->model), |
549
|
2 |
|
$id |
550
|
|
|
); |
551
|
|
|
} |
552
|
|
|
|
553
|
16 |
|
public function first($columns = []) |
554
|
|
|
{ |
555
|
16 |
|
$items = $this->getAll($columns, 1); |
556
|
|
|
|
557
|
16 |
|
return $items->first(); |
558
|
|
|
} |
559
|
|
|
|
560
|
6 |
|
public function firstOrFail($columns = []) |
561
|
|
|
{ |
562
|
6 |
|
if (! is_null($model = $this->first($columns))) { |
563
|
4 |
|
return $model; |
564
|
|
|
} |
565
|
|
|
|
566
|
2 |
|
throw (new ModelNotFoundException)->setModel(get_class($this->model)); |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* Remove attributes from an existing item |
571
|
|
|
* |
572
|
|
|
* @param array ...$attributes |
573
|
|
|
* @return bool |
574
|
|
|
* @throws InvalidQuery |
575
|
|
|
*/ |
576
|
6 |
|
public function removeAttribute(...$attributes) |
577
|
|
|
{ |
578
|
6 |
|
$keySet = !empty(array_filter($this->model->getKeys())); |
579
|
|
|
|
580
|
6 |
|
if (!$keySet) { |
581
|
4 |
|
$analyzer = $this->getConditionAnalyzer(); |
582
|
|
|
|
583
|
4 |
|
if (!$analyzer->isExactSearch()) { |
584
|
|
|
throw new InvalidQuery('Need to provide the key in your query'); |
585
|
|
|
} |
586
|
|
|
|
587
|
4 |
|
$id = $analyzer->identifierConditionValues(); |
588
|
4 |
|
$this->model->setId($id); |
589
|
|
|
} |
590
|
|
|
|
591
|
6 |
|
$key = DynamoDb::marshalItem($this->model->getKeys()); |
592
|
|
|
|
593
|
6 |
|
$this->resetExpressions(); |
594
|
|
|
|
595
|
|
|
/** @var \Aws\Result $result */ |
596
|
6 |
|
$result = DynamoDb::table($this->model->getTable()) |
597
|
6 |
|
->setKey($key) |
598
|
6 |
|
->setUpdateExpression($this->updateExpression->remove($attributes)) |
599
|
6 |
|
->setExpressionAttributeNames($this->expressionAttributeNames->all()) |
600
|
6 |
|
->setReturnValues('ALL_NEW') |
601
|
6 |
|
->prepare($this->client) |
602
|
6 |
|
->updateItem(); |
603
|
|
|
|
604
|
6 |
|
$success = array_get($result, '@metadata.statusCode') === 200; |
605
|
|
|
|
606
|
6 |
|
if ($success) { |
607
|
6 |
|
$this->model->setRawAttributes(DynamoDb::unmarshalItem($result->get('Attributes'))); |
608
|
6 |
|
$this->model->syncOriginal(); |
609
|
|
|
} |
610
|
|
|
|
611
|
6 |
|
return $success; |
612
|
|
|
} |
613
|
|
|
|
614
|
2 |
|
public function delete() |
615
|
|
|
{ |
616
|
2 |
|
$result = DynamoDb::table($this->model->getTable()) |
617
|
2 |
|
->setKey(DynamoDb::marshalItem($this->model->getKeys())) |
618
|
2 |
|
->prepare($this->client) |
619
|
2 |
|
->deleteItem(); |
620
|
|
|
|
621
|
2 |
|
return array_get($result->toArray(), '@metadata.statusCode') === 200; |
622
|
|
|
} |
623
|
|
|
|
624
|
2 |
|
public function deleteAsync() |
625
|
|
|
{ |
626
|
2 |
|
$promise = DynamoDb::table($this->model->getTable()) |
627
|
2 |
|
->setKey(DynamoDb::marshalItem($this->model->getKeys())) |
628
|
2 |
|
->prepare($this->client) |
629
|
2 |
|
->deleteItemAsync(); |
630
|
|
|
|
631
|
2 |
|
return $promise; |
632
|
|
|
} |
633
|
|
|
|
634
|
8 |
|
public function save() |
635
|
|
|
{ |
636
|
8 |
|
$result = DynamoDb::table($this->model->getTable()) |
637
|
8 |
|
->setItem(DynamoDb::marshalItem($this->model->getAttributes())) |
638
|
8 |
|
->prepare($this->client) |
639
|
8 |
|
->putItem(); |
640
|
|
|
|
641
|
8 |
|
return array_get($result, '@metadata.statusCode') === 200; |
642
|
|
|
} |
643
|
|
|
|
644
|
6 |
|
public function saveAsync() |
645
|
|
|
{ |
646
|
6 |
|
$promise = DynamoDb::table($this->model->getTable()) |
647
|
6 |
|
->setItem(DynamoDb::marshalItem($this->model->getAttributes())) |
648
|
6 |
|
->prepare($this->client) |
649
|
6 |
|
->putItemAsync(); |
650
|
|
|
|
651
|
6 |
|
return $promise; |
652
|
|
|
} |
653
|
|
|
|
654
|
55 |
|
public function get($columns = []) |
655
|
|
|
{ |
656
|
55 |
|
return $this->all($columns); |
657
|
|
|
} |
658
|
|
|
|
659
|
67 |
|
public function all($columns = []) |
660
|
|
|
{ |
661
|
67 |
|
$limit = isset($this->limit) ? $this->limit : static::MAX_LIMIT; |
662
|
67 |
|
return $this->getAll($columns, $limit, !isset($this->limit)); |
663
|
|
|
} |
664
|
|
|
|
665
|
4 |
|
public function count() |
666
|
|
|
{ |
667
|
4 |
|
$limit = isset($this->limit) ? $this->limit : static::MAX_LIMIT; |
668
|
4 |
|
$raw = $this->toDynamoDbQuery(['count(*)'], $limit); |
669
|
|
|
|
670
|
4 |
|
if ($raw->op === 'Scan') { |
671
|
4 |
|
$res = $this->client->scan($raw->query); |
672
|
|
|
} else { |
673
|
|
|
$res = $this->client->query($raw->query); |
674
|
|
|
} |
675
|
|
|
|
676
|
4 |
|
return $res['Count']; |
677
|
|
|
} |
678
|
|
|
|
679
|
4 |
|
public function decorate(Closure $closure) |
680
|
|
|
{ |
681
|
4 |
|
$this->decorator = $closure; |
682
|
4 |
|
return $this; |
683
|
|
|
} |
684
|
|
|
|
685
|
88 |
|
protected function getAll( |
686
|
|
|
$columns = [], |
687
|
|
|
$limit = DynamoDbQueryBuilder::MAX_LIMIT, |
688
|
|
|
$useIterator = DynamoDbQueryBuilder::DEFAULT_TO_ITERATOR |
689
|
|
|
) { |
690
|
88 |
|
$analyzer = $this->getConditionAnalyzer(); |
691
|
|
|
|
692
|
88 |
|
if ($analyzer->isExactSearch()) { |
693
|
7 |
|
$item = $this->find($analyzer->identifierConditionValues(), $columns); |
694
|
|
|
|
695
|
7 |
|
return $this->getModel()->newCollection([$item]); |
696
|
|
|
} |
697
|
|
|
|
698
|
82 |
|
$raw = $this->toDynamoDbQuery($columns, $limit); |
699
|
|
|
|
700
|
82 |
|
if ($useIterator) { |
701
|
65 |
|
$iterator = $this->client->getIterator($raw->op, $raw->query); |
702
|
|
|
|
703
|
65 |
|
if (isset($raw->query['Limit'])) { |
704
|
12 |
|
$iterator = new \LimitIterator($iterator, 0, $raw->query['Limit']); |
705
|
|
|
} |
706
|
|
|
} else { |
707
|
21 |
|
if ($raw->op === 'Scan') { |
708
|
18 |
|
$res = $this->client->scan($raw->query); |
709
|
|
|
} else { |
710
|
3 |
|
$res = $this->client->query($raw->query); |
711
|
|
|
} |
712
|
|
|
|
713
|
21 |
|
$this->lastEvaluatedKey = array_get($res, 'LastEvaluatedKey'); |
714
|
21 |
|
$iterator = $res['Items']; |
715
|
|
|
} |
716
|
|
|
|
717
|
82 |
|
$results = []; |
718
|
|
|
|
719
|
82 |
|
foreach ($iterator as $item) { |
720
|
82 |
|
$item = DynamoDb::unmarshalItem($item); |
721
|
82 |
|
$model = $this->model->newInstance([], true); |
722
|
82 |
|
$model->setRawAttributes($item, true); |
723
|
82 |
|
$results[] = $model; |
724
|
|
|
} |
725
|
|
|
|
726
|
82 |
|
return $this->getModel()->newCollection($results, $analyzer->index()); |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* Return the raw DynamoDb query |
731
|
|
|
* |
732
|
|
|
* @param array $columns |
733
|
|
|
* @param int $limit |
734
|
|
|
* @return RawDynamoDbQuery |
735
|
|
|
*/ |
736
|
92 |
|
public function toDynamoDbQuery( |
737
|
|
|
$columns = [], |
738
|
|
|
$limit = DynamoDbQueryBuilder::MAX_LIMIT |
739
|
|
|
) { |
740
|
92 |
|
$this->applyScopes(); |
741
|
|
|
|
742
|
92 |
|
$this->resetExpressions(); |
743
|
|
|
|
744
|
92 |
|
$op = 'Scan'; |
745
|
92 |
|
$queryBuilder = DynamoDb::table($this->model->getTable()); |
746
|
|
|
|
747
|
92 |
|
if (! empty($this->wheres)) { |
748
|
70 |
|
$analyzer = $this->getConditionAnalyzer(); |
749
|
|
|
|
750
|
70 |
|
if ($keyConditions = $analyzer->keyConditions()) { |
751
|
16 |
|
$op = 'Query'; |
752
|
16 |
|
$queryBuilder->setKeyConditionExpression($this->keyConditionExpression->parse($keyConditions)); |
753
|
|
|
} |
754
|
|
|
|
755
|
70 |
|
if ($filterConditions = $analyzer->filterConditions()) { |
756
|
61 |
|
$queryBuilder->setFilterExpression($this->filterExpression->parse($filterConditions)); |
757
|
|
|
} |
758
|
|
|
|
759
|
70 |
|
if ($index = $analyzer->index()) { |
760
|
8 |
|
$queryBuilder->setIndexName($index->name); |
761
|
|
|
} |
762
|
|
|
} |
763
|
|
|
|
764
|
92 |
|
if ($this->index) { |
765
|
|
|
// If user specifies the index manually, respect that |
766
|
1 |
|
$queryBuilder->setIndexName($this->index); |
767
|
|
|
} |
768
|
|
|
|
769
|
92 |
|
if ($limit !== static::MAX_LIMIT) { |
770
|
33 |
|
$queryBuilder->setLimit($limit); |
771
|
|
|
} |
772
|
|
|
|
773
|
92 |
|
if (!empty($columns)) { |
774
|
|
|
// Either we try to get the count or specific columns |
775
|
8 |
|
if ($columns == ['count(*)']) { |
776
|
6 |
|
$queryBuilder->setSelect('COUNT'); |
777
|
|
|
} else { |
778
|
2 |
|
$queryBuilder->setProjectionExpression($this->projectionExpression->parse($columns)); |
779
|
|
|
} |
780
|
|
|
} |
781
|
|
|
|
782
|
92 |
|
if (!empty($this->lastEvaluatedKey)) { |
783
|
15 |
|
$queryBuilder->setExclusiveStartKey($this->lastEvaluatedKey); |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
$queryBuilder |
787
|
92 |
|
->setExpressionAttributeNames($this->expressionAttributeNames->all()) |
788
|
92 |
|
->setExpressionAttributeValues($this->expressionAttributeValues->all()); |
789
|
|
|
|
790
|
92 |
|
$raw = new RawDynamoDbQuery($op, $queryBuilder->prepare($this->client)->query); |
791
|
|
|
|
792
|
92 |
|
if ($this->decorator) { |
793
|
4 |
|
call_user_func($this->decorator, $raw); |
794
|
|
|
} |
795
|
|
|
|
796
|
92 |
|
return $raw; |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
/** |
800
|
|
|
* @return Analyzer |
801
|
|
|
*/ |
802
|
97 |
|
protected function getConditionAnalyzer() |
803
|
|
|
{ |
804
|
97 |
|
return with(new Analyzer) |
805
|
97 |
|
->on($this->model) |
806
|
97 |
|
->withIndex($this->index) |
807
|
97 |
|
->analyze($this->wheres); |
808
|
|
|
} |
809
|
|
|
|
810
|
40 |
|
protected function isMultipleIds($id) |
811
|
|
|
{ |
812
|
40 |
|
$keys = collect($this->model->getKeyNames()); |
813
|
|
|
|
814
|
|
|
// could be ['id' => 'foo'], ['id1' => 'foo', 'id2' => 'bar'] |
815
|
|
|
$single = $keys->first(function ($name) use ($id) { |
816
|
40 |
|
return !isset($id[$name]); |
817
|
40 |
|
}) === null; |
818
|
|
|
|
819
|
40 |
|
if ($single) { |
820
|
22 |
|
return false; |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
// could be ['foo', 'bar'], [['id1' => 'foo', 'id2' => 'bar'], ...] |
824
|
18 |
|
return $this->model->hasCompositeKey() ? is_array(H::array_first($id)) : is_array($id); |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
/** |
828
|
|
|
* @return DynamoDbModel |
829
|
|
|
*/ |
830
|
88 |
|
public function getModel() |
831
|
|
|
{ |
832
|
88 |
|
return $this->model; |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
/** |
836
|
|
|
* @return \Aws\DynamoDb\DynamoDbClient |
837
|
|
|
*/ |
838
|
|
|
public function getClient() |
839
|
|
|
{ |
840
|
|
|
return $this->client; |
841
|
|
|
} |
842
|
|
|
|
843
|
|
|
/** |
844
|
|
|
* Register a new global scope. |
845
|
|
|
* |
846
|
|
|
* @param string $identifier |
847
|
|
|
* @param \Illuminate\Database\Eloquent\Scope|\Closure $scope |
848
|
|
|
* @return $this |
849
|
|
|
*/ |
850
|
7 |
|
public function withGlobalScope($identifier, $scope) |
851
|
|
|
{ |
852
|
7 |
|
$this->scopes[$identifier] = $scope; |
853
|
|
|
|
854
|
7 |
|
if (method_exists($scope, 'extend')) { |
855
|
|
|
$scope->extend($this); |
856
|
|
|
} |
857
|
|
|
|
858
|
7 |
|
return $this; |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
/** |
862
|
|
|
* Remove a registered global scope. |
863
|
|
|
* |
864
|
|
|
* @param \Illuminate\Database\Eloquent\Scope|string $scope |
865
|
|
|
* @return $this |
866
|
|
|
*/ |
867
|
3 |
|
public function withoutGlobalScope($scope) |
868
|
|
|
{ |
869
|
3 |
|
if (! is_string($scope)) { |
870
|
|
|
$scope = get_class($scope); |
871
|
|
|
} |
872
|
|
|
|
873
|
3 |
|
unset($this->scopes[$scope]); |
874
|
|
|
|
875
|
3 |
|
$this->removedScopes[] = $scope; |
876
|
|
|
|
877
|
3 |
|
return $this; |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
/** |
881
|
|
|
* Remove all or passed registered global scopes. |
882
|
|
|
* |
883
|
|
|
* @param array|null $scopes |
884
|
|
|
* @return $this |
885
|
|
|
*/ |
886
|
5 |
|
public function withoutGlobalScopes(array $scopes = null) |
887
|
|
|
{ |
888
|
5 |
|
if (is_array($scopes)) { |
889
|
|
|
foreach ($scopes as $scope) { |
890
|
|
|
$this->withoutGlobalScope($scope); |
891
|
|
|
} |
892
|
|
|
} else { |
893
|
5 |
|
$this->scopes = []; |
894
|
|
|
} |
895
|
|
|
|
896
|
5 |
|
return $this; |
897
|
|
|
} |
898
|
|
|
|
899
|
|
|
/** |
900
|
|
|
* Get an array of global scopes that were removed from the query. |
901
|
|
|
* |
902
|
|
|
* @return array |
903
|
|
|
*/ |
904
|
|
|
public function removedScopes() |
905
|
|
|
{ |
906
|
|
|
return $this->removedScopes; |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
/** |
910
|
|
|
* Apply the scopes to the Eloquent builder instance and return it. |
911
|
|
|
* |
912
|
|
|
* @return DynamoDbQueryBuilder |
913
|
|
|
*/ |
914
|
92 |
|
public function applyScopes() |
915
|
|
|
{ |
916
|
92 |
|
if (! $this->scopes) { |
917
|
91 |
|
return $this; |
918
|
|
|
} |
919
|
|
|
|
920
|
3 |
|
$builder = $this; |
921
|
|
|
|
922
|
3 |
|
foreach ($builder->scopes as $identifier => $scope) { |
923
|
3 |
|
if (! isset($builder->scopes[$identifier])) { |
924
|
|
|
continue; |
925
|
|
|
} |
926
|
|
|
|
927
|
|
|
$builder->callScope(function (DynamoDbQueryBuilder $builder) use ($scope) { |
928
|
|
|
// If the scope is a Closure we will just go ahead and call the scope with the |
929
|
|
|
// builder instance. The "callScope" method will properly group the clauses |
930
|
|
|
// that are added to this query so "where" clauses maintain proper logic. |
931
|
3 |
|
if ($scope instanceof Closure) { |
932
|
3 |
|
$scope($builder); |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
// If the scope is a scope object, we will call the apply method on this scope |
936
|
|
|
// passing in the builder and the model instance. After we run all of these |
937
|
|
|
// scopes we will return back the builder instance to the outside caller. |
938
|
3 |
|
if ($scope instanceof Scope) { |
939
|
|
|
throw new NotSupportedException('Scope object is not yet supported'); |
940
|
|
|
} |
941
|
3 |
|
}); |
942
|
|
|
|
943
|
3 |
|
$builder->withoutGlobalScope($identifier); |
944
|
|
|
} |
945
|
|
|
|
946
|
3 |
|
return $builder; |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
/** |
950
|
|
|
* Apply the given scope on the current builder instance. |
951
|
|
|
* |
952
|
|
|
* @param callable $scope |
953
|
|
|
* @param array $parameters |
954
|
|
|
* @return mixed |
955
|
|
|
*/ |
956
|
7 |
|
protected function callScope(callable $scope, $parameters = []) |
957
|
|
|
{ |
958
|
7 |
|
array_unshift($parameters, $this); |
959
|
|
|
|
960
|
|
|
// $query = $this->getQuery(); |
961
|
|
|
|
962
|
|
|
// // We will keep track of how many wheres are on the query before running the |
963
|
|
|
// // scope so that we can properly group the added scope constraints in the |
964
|
|
|
// // query as their own isolated nested where statement and avoid issues. |
965
|
|
|
// $originalWhereCount = is_null($query->wheres) |
966
|
|
|
// ? 0 : count($query->wheres); |
967
|
|
|
|
968
|
7 |
|
$result = $scope(...array_values($parameters)) ?: $this; |
969
|
|
|
|
970
|
|
|
// if (count((array) $query->wheres) > $originalWhereCount) { |
971
|
|
|
// $this->addNewWheresWithinGroup($query, $originalWhereCount); |
972
|
|
|
// } |
973
|
|
|
|
974
|
7 |
|
return $result; |
975
|
|
|
} |
976
|
|
|
|
977
|
|
|
/** |
978
|
|
|
* Dynamically handle calls into the query instance. |
979
|
|
|
* |
980
|
|
|
* @param string $method |
981
|
|
|
* @param array $parameters |
982
|
|
|
* @return mixed |
983
|
|
|
*/ |
984
|
7 |
|
public function __call($method, $parameters) |
985
|
|
|
{ |
986
|
7 |
|
if (method_exists($this->model, $scope = 'scope'.ucfirst($method))) { |
987
|
5 |
|
return $this->callScope([$this->model, $scope], $parameters); |
988
|
|
|
} |
989
|
|
|
|
990
|
2 |
|
return $this; |
991
|
|
|
} |
992
|
|
|
} |
993
|
|
|
|