1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Guillermoandrae\DynamoDb\Operation; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use Aws\DynamoDb\Exception\DynamoDbException; |
7
|
|
|
use Aws\DynamoDb\Marshaler; |
8
|
|
|
use ErrorException; |
9
|
|
|
use Guillermoandrae\Common\Collection; |
10
|
|
|
use Guillermoandrae\Common\CollectionInterface; |
11
|
|
|
use Guillermoandrae\DynamoDb\Constant\Operators; |
12
|
|
|
use Guillermoandrae\DynamoDb\Contract\AbstractSearchOperation; |
13
|
|
|
use Guillermoandrae\DynamoDb\Factory\ExceptionFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#query |
17
|
|
|
*/ |
18
|
|
|
final class QueryOperation extends AbstractSearchOperation |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string The condition that specifies the key values for items to be retrieved. |
22
|
|
|
*/ |
23
|
|
|
private $keyConditionExpression = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var boolean Whether or not to scan forward. |
27
|
|
|
*/ |
28
|
|
|
private $scanIndexForward = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* QueryRequest constructor. |
32
|
|
|
* |
33
|
|
|
* @param DynamoDbClient $client The DynamoDb client. |
34
|
|
|
* @param Marshaler $marshaler The Marshaler. |
35
|
|
|
* @param string $tableName The table name. |
36
|
|
|
* @param array|null $keyConditions OPTIONAL The key conditions. |
37
|
|
|
* @throws ErrorException |
38
|
|
|
*/ |
39
|
17 |
|
public function __construct( |
40
|
|
|
DynamoDbClient $client, |
41
|
|
|
Marshaler $marshaler, |
42
|
|
|
string $tableName, |
43
|
|
|
?array $keyConditions = [] |
44
|
|
|
) { |
45
|
17 |
|
parent::__construct($client, $marshaler, $tableName); |
46
|
17 |
|
if (!empty($keyConditions)) { |
47
|
1 |
|
$this->setPartitionKeyConditionExpression( |
48
|
1 |
|
$keyConditions['partition']['name'], |
49
|
1 |
|
$keyConditions['partition']['value'] |
50
|
|
|
); |
51
|
1 |
|
if (isset($keyConditions['sort'])) { |
52
|
1 |
|
$this->setSortKeyConditionExpression( |
53
|
1 |
|
$keyConditions['sort']['name'], |
54
|
1 |
|
$keyConditions['sort']['operator'], |
55
|
1 |
|
$keyConditions['sort']['value'] |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
17 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sets condition expression for the partition key. |
63
|
|
|
* |
64
|
|
|
* @param string $keyName The name of the partition key. |
65
|
|
|
* @param mixed $value The desired value. |
66
|
|
|
* @return QueryOperation This object. |
67
|
|
|
* @throws ErrorException Thrown when an unsupported operator is requested. |
68
|
|
|
*/ |
69
|
3 |
|
public function setPartitionKeyConditionExpression(string $keyName, $value): QueryOperation |
70
|
|
|
{ |
71
|
3 |
|
$partitionKeyConditionExpression = $this->parseExpression(Operators::EQ, $keyName); |
72
|
3 |
|
$this->addExpressionAttributeValue($keyName, $value); |
73
|
3 |
|
$this->keyConditionExpression = $partitionKeyConditionExpression; |
74
|
3 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets condition expressions for the sort key. |
79
|
|
|
* |
80
|
|
|
* @param string $keyName The name of the sort key. |
81
|
|
|
* @param string $operator The operator. |
82
|
|
|
* @param mixed $value The desired value. |
83
|
|
|
* @return QueryOperation This object. |
84
|
|
|
* @throws ErrorException Thrown when an unsupported operator is requested. |
85
|
|
|
*/ |
86
|
2 |
|
public function setSortKeyConditionExpression(string $keyName, string $operator, $value): QueryOperation |
87
|
|
|
{ |
88
|
2 |
|
$sortKeyConditionExpression = $this->parseExpression($operator, $keyName); |
89
|
2 |
|
$this->addExpressionAttributeValue($keyName, $value); |
90
|
2 |
|
$this->keyConditionExpression .= ' AND ' . $sortKeyConditionExpression; |
91
|
2 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Registers the ScanIndexForward value with this object. |
96
|
|
|
* |
97
|
|
|
* @param boolean $scanIndexForward Whether or not to scan forward. |
98
|
|
|
* @return QueryOperation This object. |
99
|
|
|
*/ |
100
|
1 |
|
public function setScanIndexForward(bool $scanIndexForward): QueryOperation |
101
|
|
|
{ |
102
|
1 |
|
$this->scanIndexForward = $scanIndexForward; |
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
2 |
|
public function execute(): CollectionInterface |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
2 |
|
$results = $this->client->query($this->toArray()); |
113
|
1 |
|
$rows = []; |
114
|
1 |
|
foreach ($results['Items'] as $item) { |
115
|
1 |
|
$rows[] = $this->marshaler->unmarshalItem($item); |
116
|
|
|
} |
117
|
1 |
|
return Collection::make($rows); |
118
|
1 |
|
} catch (DynamoDbException $ex) { |
119
|
1 |
|
throw ExceptionFactory::factory($ex); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
16 |
|
public function toArray(): array |
127
|
|
|
{ |
128
|
16 |
|
$operation = parent::toArray(); |
129
|
16 |
|
$operation['ScanIndexForward'] = $this->scanIndexForward; |
130
|
16 |
|
if (!empty($this->keyConditionExpression)) { |
131
|
3 |
|
$operation['KeyConditionExpression'] = $this->keyConditionExpression; |
132
|
|
|
} |
133
|
16 |
|
return $operation; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|