Skip to content

Commit a665da6

Browse files
committed
Fix #3
Use dedicated queries for each types of conditions.
1 parent 4167754 commit a665da6

File tree

4 files changed

+85
-60
lines changed

4 files changed

+85
-60
lines changed

README.md

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,11 @@ DB::table('ProductCatalog')
168168

169169
#### Preventing Overwrites of an Existing Item
170170

171-
Use `where` clause to build Condition Expressions.
172-
173-
We also need to call `whereAsCondition()` to let QueryBuilder know that `wheres` array should be compiled to `ConditionExpression`.
174-
175-
> `where` clause also be used for `KeyConditionExpression` and `FilterExpression`.
171+
Use `condition` clause to build Condition Expressions. This works basically same as original `where` clause, but it's for the ConditionExpression.
176172

177173
```php
178174
DB::table('ProductCatalog')
179-
->where('Id', 'attribute_not_exists')
180-
->whereAsCondition()
175+
->condition('Id', 'attribute_not_exists')
181176
->putItem([
182177
'Id' => 456,
183178
'ProductCategory' => 'Can I overwrite?'
@@ -188,49 +183,55 @@ DB::table('ProductCatalog')
188183

189184
```php
190185
DB::table('ProductCatalog')
191-
->where('Price', 'attribute_not_exists')
192-
->whereAsCondition()
186+
->condition('Price', 'attribute_not_exists')
193187
->deleteItem([
194188
'Id' => 456
195189
]);
196190
```
197191

198-
> We can also specify functions instead of operators in `where` clause. For this time, `attriute_not_exists`.
192+
> We can also specify functions instead of operators in `where` clause. In the case above, `attriute_not_exists`.
199193
200194
### Working with Queries in DynamoDB
201195

202196
[corresponding document](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html)
203197

204198
#### Key Condition Expression
205199

200+
Use `keyCondition` clause to build Key Conditions.
201+
206202
```php
207203
$items = DB::table('Thread')
208-
->where('ForumName', '=', 'Amazon DynamoDB')
209-
->whereAsKeyCondition()
204+
->keyCondition('ForumName', '=', 'Amazon DynamoDB')
210205
->query();
211206
```
212207

213208
```php
214209
$items = DB::table('Thread')
215-
->where('ForumName', '=', 'Amazon DynamoDB')
216-
->where('Subject', '=', 'DynamoDB Thread 1')
217-
->whereAsKeyCondition()
210+
->keyCondition('ForumName', '=', 'Amazon DynamoDB')
211+
->keyCondition('Subject', '=', 'DynamoDB Thread 1')
218212
->query();
219213
```
220214

221215
```php
222216
$items = DB::table('Reply')
223-
->where('Id', '=', 'Amazon DynamoDB#DynamoDB Thread 1')
224-
->where('ReplyDateTime', 'begins_with', '2015-09')
225-
->whereAsKeyCondition()
217+
->keyCondition('Id', '=', 'Amazon DynamoDB#DynamoDB Thread 1')
218+
->keyCondition('ReplyDateTime', 'begins_with', '2015-09')
226219
->query();
227220
```
228221

229-
> We called `whereAsKeyCondition()` for this case.
230-
231222
#### Filter Expressions for Query
232223

233-
Currently, we cant handle Filter Expressions with Query. If you want to use Filter Expressions, use it with Scan at this time.
224+
Use `filter` clause to build Filter Conditions.
225+
226+
For `query`, KeyConditionExprssion is required, so we specify both KeyConditionExpression and FilterExpression.
227+
228+
```php
229+
$itmes = DB::table('Thread')
230+
->keyCondition('ForumName', '=', 'Amazon DynamoDB')
231+
->keyCondition('Subject', '=', 'DynamoDB Thread 1')
232+
->filter('Views', '>', 3)
233+
->query();
234+
```
234235

235236
### Working with Scans in DynamoDB
236237

@@ -240,13 +241,10 @@ Currently, we cant handle Filter Expressions with Query. If you want to use Filt
240241

241242
```php
242243
$items = DB::table('Thread')
243-
->where('LastPostedBy', '=', 'User A')
244-
->whereAsFilter()
244+
->filter('LastPostedBy', '=', 'User A')
245245
->scan();
246246
```
247247

248-
> We called `whereAsFilter()` for this case.
249-
250248
### Using Global Secondary Indexes in DynamoDB
251249

252250
[corresponding document](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html)
@@ -258,9 +256,8 @@ Use `index` clause to specify IndexName.
258256
```php
259257
$items = DB::table('Reply')
260258
->index('PostedBy-Message-index')
261-
->where('PostedBy', '=', 'User A')
262-
->where('Message', '=', 'DynamoDB Thread 2 Reply 1 text')
263-
->whereAsKeyCondition()
259+
->keyCondition('PostedBy', '=', 'User A')
260+
->keyCondition('Message', '=', 'DynamoDB Thread 2 Reply 1 text')
264261
->query();
265262
```
266263

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Builder extends BaseBuilder
1717
* Name of the index.
1818
* @var string|null
1919
*/
20-
public $index = null;
20+
public $index;
2121

2222
/**
2323
* The key.
@@ -31,17 +31,11 @@ class Builder extends BaseBuilder
3131
*/
3232
public $item = [];
3333

34-
/**
35-
* The source of ProjectionExpression.
36-
* @var array
37-
*/
38-
public $projections = [];
39-
4034
/**
4135
* ConsistentRead option.
4236
* @var boolean|null
4337
*/
44-
public $consistent_read = null;
38+
public $consistent_read;
4539

4640
/**
4741
* dry run option.
@@ -53,18 +47,36 @@ class Builder extends BaseBuilder
5347
* The attribute name to place compiled wheres.
5448
* @var string
5549
*/
56-
public $bind_wheres_to = 'FilterExpression';
50+
public $where_as;
5751

5852
/**
5953
* The ExpressionAttributes object.
6054
* @var Kitar\Dynamodb\Query\ExpressionAttributes
6155
*/
62-
public $expression_attributes = null;
56+
public $expression_attributes;
57+
58+
/**
59+
* Query for building FilterExpression.
60+
* @var Kitar\Dynamodb\Query\Builder
61+
*/
62+
public $filter_query;
63+
64+
/**
65+
* Query for building ConditionExpression.
66+
* @var Kitar\Dynamodb\Query\Builder
67+
*/
68+
public $condition_query;
69+
70+
/**
71+
* Query for building KeyConditionExpression.
72+
* @var Kitar\Dynamodb\Query\Builder
73+
*/
74+
public $key_condition_query;
6375

6476
/**
6577
* @inheritdoc
6678
*/
67-
public function __construct(Connection $connection, Grammar $grammar, Processor $processor, $expression_attributes = null)
79+
public function __construct(Connection $connection, Grammar $grammar, Processor $processor, $expression_attributes = null, $is_nested_query = false)
6880
{
6981
$this->connection = $connection;
7082

@@ -73,6 +85,12 @@ public function __construct(Connection $connection, Grammar $grammar, Processor
7385
$this->processor = $processor;
7486

7587
$this->expression_attributes = $expression_attributes ?? new ExpressionAttributes;
88+
89+
if (! $is_nested_query) {
90+
$this->filter_query = $this->newQuery()->whereAs('FilterExpression');
91+
$this->condition_query = $this->newQuery()->whereAs('ConditionExpression');
92+
$this->key_condition_query = $this->newQuery()->whereAs('KeyConditionExpression');
93+
}
7694
}
7795

7896
/**
@@ -124,34 +142,46 @@ public function dryRun($active = true)
124142
}
125143

126144
/**
127-
* If called, compiled wheres will be placed to FilterExpression.
145+
* where for FilterExpression.
146+
* @return $this
147+
*/
148+
public function filter($column, $operator = null, $value = null, $boolean = 'and')
149+
{
150+
$this->filter_query = $this->filter_query->where($column, $operator, $value, $boolean);
151+
152+
return $this;
153+
}
154+
155+
/**
156+
* where for ConditionExpression.
128157
* @return $this
129158
*/
130-
public function whereAsFilter()
159+
public function condition($column, $operator = null, $value = null, $boolean = 'and')
131160
{
132-
$this->bind_wheres_to = 'FilterExpression';
161+
$this->condition_query = $this->condition_query->where($column, $operator, $value, $boolean);
133162

134163
return $this;
135164
}
136165

137166
/**
138-
* If called, compiled wheres will be placed to ConditionExpression.
167+
* where for KeyConditionExpression.
139168
* @return $this
140169
*/
141-
public function whereAsCondition()
170+
public function keyCondition($column, $operator = null, $value = null, $boolean = 'and')
142171
{
143-
$this->bind_wheres_to = 'ConditionExpression';
172+
$this->key_condition_query = $this->key_condition_query->where($column, $operator, $value, $boolean);
144173

145174
return $this;
146175
}
147176

148177
/**
149-
* If called, compiled wheres will be placed to KeyConditionExpression.
178+
* Set key name of wheres. eg. FilterExpression
179+
* @param string $condition_key_name
150180
* @return $this
151181
*/
152-
public function whereAsKeyCondition()
182+
public function whereAs($condition_key_name)
153183
{
154-
$this->bind_wheres_to = 'KeyConditionExpression';
184+
$this->where_as = $condition_key_name;
155185

156186
return $this;
157187
}
@@ -263,7 +293,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
263293
*/
264294
public function newQuery()
265295
{
266-
return new static($this->connection, $this->grammar, $this->processor, $this->expression_attributes);
296+
return new static($this->connection, $this->grammar, $this->processor, $this->expression_attributes, true);
267297
}
268298

269299
/**
@@ -280,7 +310,9 @@ protected function process($query_method, $processor_method)
280310
// so it need to run before compileExpressionAttributes.
281311
$params = array_merge(
282312
$this->grammar->compileProjectionExpression($this->columns, $this->expression_attributes),
283-
$this->grammar->compileConditions($this),
313+
$this->grammar->compileConditions($this->filter_query),
314+
$this->grammar->compileConditions($this->condition_query),
315+
$this->grammar->compileConditions($this->key_condition_query),
284316
);
285317

286318
// Compile rest of attributes.

src/Kitar/Dynamodb/Query/Grammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ public function compileConditions(Builder $query)
170170
return [];
171171
}
172172

173-
$param_key = $query->bind_wheres_to;
173+
$key = $query->where_as;
174174

175175
return [
176-
$param_key => preg_replace('/^where\s/', '', $this->compileWheres($query))
176+
$key => preg_replace('/^where\s/', '', $this->compileWheres($query))
177177
];
178178
}
179179

tests/Query/BuilderTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public function it_can_compile_wheres_to_filter_expression()
7777
]
7878
];
7979
$query = $this->builder
80-
->where('foo', '=', 'bar')
81-
->whereAsFilter()
80+
->filter('foo', '=', 'bar')
8281
->scan();
8382

8483
$this->assertEquals($params, $query['params']);
@@ -100,8 +99,7 @@ public function it_can_compile_wheres_to_condition_expression()
10099
]
101100
];
102101
$query = $this->builder
103-
->where('foo', '=', 'bar')
104-
->whereAsCondition()
102+
->condition('foo', '=', 'bar')
105103
->scan();
106104

107105
$this->assertEquals($params, $query['params']);
@@ -123,8 +121,7 @@ public function it_can_compile_wheres_to_key_condition_expression()
123121
]
124122
];
125123
$query = $this->builder
126-
->where('foo', '=', 'bar')
127-
->whereAsKeyCondition()
124+
->keyCondition('foo', '=', 'bar')
128125
->scan();
129126

130127
$this->assertEquals($params, $query['params']);
@@ -249,8 +246,7 @@ public function it_can_process_query()
249246
$processor = 'processMultipleItems';
250247

251248
$query = $this->builder
252-
->where('foo', '=', 'bar')
253-
->whereAsKeyCondition()
249+
->keyCondition('foo', '=', 'bar')
254250
->query();
255251

256252
$this->assertEquals($method, $query['method']);

0 commit comments

Comments
 (0)