Skip to content

Commit 6783271

Browse files
hschimpfkitar
authored andcommitted
Added count() support
1 parent fa90ff9 commit 6783271

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ public function __call($method, $parameters)
343343
{
344344
$allowedBuilderMethods = [
345345
"select",
346+
"count",
346347
"take",
347348
"limit",
348349
"index",

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ class Builder extends BaseBuilder
120120
*/
121121
protected $key_condition_query;
122122

123+
/**
124+
* The attributes to be returned in the result
125+
* @var string
126+
*/
127+
protected $selectAttributes = 'ALL_ATTRIBUTES';
128+
123129
/**
124130
* Create a new query builder instance.
125131
*
@@ -260,6 +266,27 @@ public function getWhereAs()
260266
return $this->where_as;
261267
}
262268

269+
/**
270+
* Set select attributes
271+
*
272+
* @param string $selectAttributes
273+
* @return $this
274+
*/
275+
protected function selectAttributes(string $selectAttributes) {
276+
$this->selectAttributes = $selectAttributes;
277+
278+
return $this;
279+
}
280+
281+
/**
282+
* Get the selectAttributes attribute.
283+
*
284+
* @return string
285+
*/
286+
public function getSelectAttributes() {
287+
return $this->selectAttributes;
288+
}
289+
263290
/**
264291
* Get item.
265292
*
@@ -368,6 +395,13 @@ public function batchWriteItem($request_items = [])
368395
return $this->process('batchWriteItem', null);
369396
}
370397

398+
public function count($columns = '*') {
399+
// reset columns selection
400+
$this->select([])->selectAttributes('COUNT');
401+
402+
return (int) $this->process('scan', 'processCount');
403+
}
404+
371405
/**
372406
* @inheritdoc
373407
*/
@@ -604,14 +638,15 @@ public function newQuery()
604638
* @param string $processor_method
605639
* @return array|\Illuminate\Support\Collection|\Aws\Result
606640
*/
607-
protected function process($query_method, $processor_method)
641+
protected function process($query_method, $processor_method = null)
608642
{
609643
$table_name = $this->connection->getTablePrefix() . $this->from;
610644

611645
// Compile columns and wheres attributes.
612646
// These attributes needs to interact with ExpressionAttributes during compile,
613647
// so it need to run before compileExpressionAttributes.
614648
$params = array_merge(
649+
$this->grammar->compileSelectAttributes($this->selectAttributes),
615650
$this->grammar->compileProjectionExpression($this->columns, $this->expression_attributes),
616651
$this->grammar->compileConditions($this->filter_query),
617652
$this->grammar->compileConditions($this->condition_query),

src/Kitar/Dynamodb/Query/Grammar.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ public function __construct()
4646
$this->marshaler = new Marshaler;
4747
}
4848

49+
/**
50+
* Compile the Select attribute.
51+
*
52+
* @param $select_attributes
53+
* @return array
54+
*/
55+
public function compileSelectAttributes($select_attributes) {
56+
if ($select_attributes === 'ALL_ATTRIBUTES') {
57+
return [];
58+
}
59+
60+
return [
61+
'Select' => $select_attributes,
62+
];
63+
}
64+
4965
/**
5066
* Compile the TableName attribute.
5167
*

src/Kitar/Dynamodb/Query/Processor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ protected function unmarshal(Result $res)
4545
return $responseArray;
4646
}
4747

48+
public function processCount(Result $awsResponse, $modelClass = null) {
49+
$response = $this->unmarshal($awsResponse);
50+
51+
if (empty($modelClass)) {
52+
return $response;
53+
}
54+
55+
if (! empty($response['Count'])) {
56+
return $response['Count'];
57+
}
58+
}
59+
4860
public function processSingleItem(Result $awsResponse, $modelClass = null)
4961
{
5062
$response = $this->unmarshal($awsResponse);

0 commit comments

Comments
 (0)