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