Skip to content

Commit 4167754

Browse files
committed
Share only expression_attributes to the nested queries
1 parent 0421f49 commit 4167754

File tree

3 files changed

+70
-83
lines changed

3 files changed

+70
-83
lines changed

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 65 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,78 +14,65 @@ class Builder extends BaseBuilder
1414
{
1515

1616
/**
17-
* DynamoDB params which expected to share across all subqueries.
17+
* Name of the index.
18+
* @var string|null
1819
*/
19-
public $dynamo_params = [
20-
21-
/**
22-
* Name of the index.
23-
* @var string|null
24-
*/
25-
'index' => null,
26-
27-
/**
28-
* The key.
29-
* @var array
30-
*/
31-
'key' => [],
32-
33-
/**
34-
* The item.
35-
* @var array
36-
*/
37-
'item' => [],
38-
39-
/**
40-
* The source of ProjectionExpression.
41-
* @var array
42-
*/
43-
'projections' => [],
44-
45-
/**
46-
* ConsistentRead option.
47-
* @var boolean
48-
*/
49-
'consistent_read' => null,
50-
51-
/**
52-
* dry run option.
53-
* @var boolean
54-
*/
55-
'dry_run' => false,
56-
57-
/**
58-
* The attribute name to place compiled wheres.
59-
* @var string
60-
*/
61-
'bind_wheres_to' => 'FilterExpression',
62-
63-
/**
64-
* The ExpressionAttributes object.
65-
* @var Kitar\Dynamodb\Query\ExpressionAttributes
66-
*/
67-
'expression_attributes' => null,
68-
];
20+
public $index = null;
21+
22+
/**
23+
* The key.
24+
* @var array
25+
*/
26+
public $key = [];
27+
28+
/**
29+
* The item.
30+
* @var array
31+
*/
32+
public $item = [];
33+
34+
/**
35+
* The source of ProjectionExpression.
36+
* @var array
37+
*/
38+
public $projections = [];
39+
40+
/**
41+
* ConsistentRead option.
42+
* @var boolean|null
43+
*/
44+
public $consistent_read = null;
45+
46+
/**
47+
* dry run option.
48+
* @var boolean
49+
*/
50+
public $dry_run = false;
51+
52+
/**
53+
* The attribute name to place compiled wheres.
54+
* @var string
55+
*/
56+
public $bind_wheres_to = 'FilterExpression';
57+
58+
/**
59+
* The ExpressionAttributes object.
60+
* @var Kitar\Dynamodb\Query\ExpressionAttributes
61+
*/
62+
public $expression_attributes = null;
6963

7064
/**
7165
* @inheritdoc
7266
*/
73-
public function __construct(Connection $connection, Grammar $grammar, Processor $processor, array $dynamo_params = [])
67+
public function __construct(Connection $connection, Grammar $grammar, Processor $processor, $expression_attributes = null)
7468
{
7569
$this->connection = $connection;
7670

7771
$this->grammar = $grammar;
7872

7973
$this->processor = $processor;
8074

81-
if (empty($dynamo_params['expression_attributes'])) {
82-
$dynamo_params['expression_attributes'] = new ExpressionAttributes;
83-
}
84-
85-
$this->dynamo_params = array_merge(
86-
$this->dynamo_params,
87-
$dynamo_params
88-
);
75+
$this->expression_attributes = $expression_attributes ?? new ExpressionAttributes;
8976
}
9077

9178
/**
@@ -95,7 +82,7 @@ public function __construct(Connection $connection, Grammar $grammar, Processor
9582
*/
9683
public function index(string $index)
9784
{
98-
$this->dynamo_params['index'] = $index;
85+
$this->index = $index;
9986

10087
return $this;
10188
}
@@ -107,7 +94,7 @@ public function index(string $index)
10794
*/
10895
public function key(array $key)
10996
{
110-
$this->dynamo_params['key'] = $key;
97+
$this->key = $key;
11198

11299
return $this;
113100
}
@@ -119,7 +106,7 @@ public function key(array $key)
119106
*/
120107
public function consistentRead($active = true)
121108
{
122-
$this->dynamo_params['consistent_read'] = $active;
109+
$this->consistent_read = $active;
123110

124111
return $this;
125112
}
@@ -131,7 +118,7 @@ public function consistentRead($active = true)
131118
*/
132119
public function dryRun($active = true)
133120
{
134-
$this->dynamo_params['dry_run'] = $active;
121+
$this->dry_run = $active;
135122

136123
return $this;
137124
}
@@ -142,7 +129,7 @@ public function dryRun($active = true)
142129
*/
143130
public function whereAsFilter()
144131
{
145-
$this->dynamo_params['bind_wheres_to'] = 'FilterExpression';
132+
$this->bind_wheres_to = 'FilterExpression';
146133

147134
return $this;
148135
}
@@ -153,7 +140,7 @@ public function whereAsFilter()
153140
*/
154141
public function whereAsCondition()
155142
{
156-
$this->dynamo_params['bind_wheres_to'] = 'ConditionExpression';
143+
$this->bind_wheres_to = 'ConditionExpression';
157144

158145
return $this;
159146
}
@@ -164,7 +151,7 @@ public function whereAsCondition()
164151
*/
165152
public function whereAsKeyCondition()
166153
{
167-
$this->dynamo_params['bind_wheres_to'] = 'KeyConditionExpression';
154+
$this->bind_wheres_to = 'KeyConditionExpression';
168155

169156
return $this;
170157
}
@@ -188,7 +175,7 @@ public function getItem($key = null)
188175
*/
189176
public function putItem($item)
190177
{
191-
$this->dynamo_params['item'] = $item;
178+
$this->item = $item;
192179

193180
return $this->process('putItem', null);
194181
}
@@ -231,9 +218,9 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
231218
{
232219
// Convert column and value to ExpressionAttributes.
233220
if (! $column instanceof Closure) {
234-
$column = $this->dynamo_params['expression_attributes']->addName($column);
221+
$column = $this->expression_attributes->addName($column);
235222
if (! empty($value)) {
236-
$value = $this->dynamo_params['expression_attributes']->addValue($value);
223+
$value = $this->expression_attributes->addValue($value);
237224
}
238225
}
239226

@@ -276,7 +263,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
276263
*/
277264
public function newQuery()
278265
{
279-
return new static($this->connection, $this->grammar, $this->processor, $this->dynamo_params);
266+
return new static($this->connection, $this->grammar, $this->processor, $this->expression_attributes);
280267
}
281268

282269
/**
@@ -292,23 +279,23 @@ protected function process($query_method, $processor_method)
292279
// These attributes needs to intaract with ExpressionAttributes during compile,
293280
// so it need to run before compileExpressionAttributes.
294281
$params = array_merge(
295-
$this->grammar->compileProjectionExpression($this->columns, $this->dynamo_params['expression_attributes']),
282+
$this->grammar->compileProjectionExpression($this->columns, $this->expression_attributes),
296283
$this->grammar->compileConditions($this),
297284
);
298285

299286
// Compile rest of attributes.
300287
$params = array_merge(
301288
$params,
302289
$this->grammar->compileTableName($this->from),
303-
$this->grammar->compileIndexName($this->dynamo_params['index']),
304-
$this->grammar->compileKey($this->dynamo_params['key']),
305-
$this->grammar->compileItem($this->dynamo_params['item']),
306-
$this->grammar->compileConsistentRead($this->dynamo_params['consistent_read']),
307-
$this->grammar->compileExpressionAttributes($this->dynamo_params['expression_attributes']),
290+
$this->grammar->compileIndexName($this->index),
291+
$this->grammar->compileKey($this->key),
292+
$this->grammar->compileItem($this->item),
293+
$this->grammar->compileConsistentRead($this->consistent_read),
294+
$this->grammar->compileExpressionAttributes($this->expression_attributes),
308295
);
309296

310297
// Dry run.
311-
if ($this->dynamo_params['dry_run']) {
298+
if ($this->dry_run) {
312299
return [
313300
'method' => $query_method,
314301
'params' => $params,

src/Kitar/Dynamodb/Query/Grammar.php

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

173-
$param_key = $query->dynamo_params['bind_wheres_to'];
173+
$param_key = $query->bind_wheres_to;
174174

175175
return [
176176
$param_key => preg_replace('/^where\s/', '', $this->compileWheres($query))
@@ -259,11 +259,11 @@ protected function compileSizeCondition($where)
259259
*/
260260
protected function whereBetween($query, $where)
261261
{
262-
$column = $query->dynamo_params['expression_attributes']->addName($where['column']);
262+
$column = $query->expression_attributes->addName($where['column']);
263263

264-
$min = $query->dynamo_params['expression_attributes']->addValue(reset($where['values']));
264+
$min = $query->expression_attributes->addValue(reset($where['values']));
265265

266-
$max = $query->dynamo_params['expression_attributes']->addValue(end($where['values']));
266+
$max = $query->expression_attributes->addValue(end($where['values']));
267267

268268
return "({$column} between {$min} and {$max})";
269269
}

tests/Query/BuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function setUp() :void
1818
public function dry_run_is_disabled_by_default()
1919
{
2020
$builder = (new Connection([]))->table('test');
21-
$this->assertFalse($builder->dynamo_params['dry_run']);
21+
$this->assertFalse($builder->dry_run);
2222
}
2323

2424
/** @test */

0 commit comments

Comments
 (0)