1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BaoPham\DynamoDb\DynamoDb; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use BaoPham\DynamoDb\DynamoDbClientInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class QueryBuilder |
11
|
|
|
* |
12
|
|
|
* @package BaoPham\DynamoDb\DynamoDb |
13
|
|
|
* |
14
|
|
|
* Methods are in the form of `set<key_name>`, where `<key_name>` |
15
|
|
|
* is the key name of the query body to be sent. |
16
|
|
|
* |
17
|
|
|
* For example, to build a query: |
18
|
|
|
* [ |
19
|
|
|
* 'AttributeDefinitions' => ..., |
20
|
|
|
* 'GlobalSecondaryIndexUpdates' => ... |
21
|
|
|
* 'TableName' => ... |
22
|
|
|
* ] |
23
|
|
|
* |
24
|
|
|
* Do: |
25
|
|
|
* |
26
|
|
|
* $query = $query->setAttributeDefinitions(...)->setGlobalSecondaryIndexUpdates(...)->setTableName(...); |
27
|
|
|
* |
28
|
|
|
* When ready: |
29
|
|
|
* |
30
|
|
|
* $query->prepare()->updateTable(); |
31
|
|
|
* |
32
|
|
|
* Common methods: |
33
|
|
|
* |
34
|
|
|
* @method QueryBuilder setExpressionAttributeNames(array $mapping) |
35
|
|
|
* @method QueryBuilder setExpressionAttributeValues(array $mapping) |
36
|
|
|
* @method QueryBuilder setFilterExpression(string $expression) |
37
|
|
|
* @method QueryBuilder setKeyConditionExpression(string $expression) |
38
|
|
|
* @method QueryBuilder setProjectionExpression(string $expression) |
39
|
|
|
* @method QueryBuilder setUpdateExpression(string $expression) |
40
|
|
|
* @method QueryBuilder setAttributeUpdates(array $updates) |
41
|
|
|
* @method QueryBuilder setScanIndexForward(bool $forward) |
42
|
|
|
* @method QueryBuilder setExclusiveStartKey(mixed $key) |
43
|
|
|
* @method QueryBuilder setReturnValues(string $type) |
44
|
|
|
* @method QueryBuilder setTableName(string $table) |
45
|
|
|
* @method QueryBuilder setIndexName(string $index) |
46
|
|
|
* @method QueryBuilder setSelect(string $select) |
47
|
|
|
* @method QueryBuilder setItem(array $item) |
48
|
|
|
* @method QueryBuilder setLimit(int $limit) |
49
|
|
|
* @method QueryBuilder setKey(array $item) |
50
|
|
|
*/ |
51
|
|
|
class QueryBuilder |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* @var DynamoDbClientInterface |
55
|
|
|
*/ |
56
|
|
|
private $service; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Query body to be sent to AWS |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
public $query = []; |
64
|
|
|
|
65
|
|
|
public function __construct(DynamoDbClientInterface $service) |
66
|
|
|
{ |
67
|
|
|
$this->service = $service; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function hydrate(array $query) |
71
|
|
|
{ |
72
|
|
|
$this->query = $query; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setExpressionAttributeName($placeholder, $name) |
78
|
|
|
{ |
79
|
|
|
$this->query['ExpressionAttributeNames'][$placeholder] = $name; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setExpressionAttributeValue($placeholder, $value) |
85
|
|
|
{ |
86
|
|
|
$this->query['ExpressionAttributeValues'][$placeholder] = $value; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param DynamoDbClient|null $client |
93
|
|
|
* @return ExecutableQuery |
94
|
|
|
*/ |
95
|
|
|
public function prepare(DynamoDbClient $client = null) |
96
|
|
|
{ |
97
|
|
|
return new ExecutableQuery($client ?: $this->service->getClient(), $this->query); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $method |
102
|
|
|
* @param array $parameters |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function __call($method, $parameters) |
106
|
|
|
{ |
107
|
|
|
if (starts_with($method, 'set')) { |
108
|
|
|
$this->query[str_after($method, 'set')] = current($parameters); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
throw new BadMethodCallException(sprintf( |
114
|
|
|
'Method %s::%s does not exist.', |
115
|
|
|
static::class, |
116
|
|
|
$method |
117
|
|
|
)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|