Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model count() support #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Kitar/Dynamodb/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ public function __call($method, $parameters)
{
$allowedBuilderMethods = [
"select",
"count",
"take",
"limit",
"index",
Expand Down
37 changes: 36 additions & 1 deletion src/Kitar/Dynamodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class Builder extends BaseBuilder
*/
protected $key_condition_query;

/**
* The attributes to be returned in the result
* @var string
*/
protected $selectAttributes = 'ALL_ATTRIBUTES';

/**
* Create a new query builder instance.
*
Expand Down Expand Up @@ -260,6 +266,27 @@ public function getWhereAs()
return $this->where_as;
}

/**
* Set select attributes
*
* @param string $selectAttributes
* @return $this
*/
protected function selectAttributes(string $selectAttributes) {
$this->selectAttributes = $selectAttributes;

return $this;
}

/**
* Get the selectAttributes attribute.
*
* @return string
*/
public function getSelectAttributes() {
return $this->selectAttributes;
}

/**
* Get item.
*
Expand Down Expand Up @@ -368,6 +395,13 @@ public function batchWriteItem($request_items = [])
return $this->process('batchWriteItem', null);
}

public function count($columns = '*') {
// reset columns selection
$this->select([])->selectAttributes('COUNT');

return (int) $this->process('clientQuery', 'processCount');
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -575,12 +609,13 @@ public function newQuery()
* @param string $processor_method
* @return array|\Illuminate\Support\Collection|\Aws\Result
*/
protected function process($query_method, $processor_method)
protected function process($query_method, $processor_method = null)
{
// Compile columns and wheres attributes.
// These attributes needs to interact with ExpressionAttributes during compile,
// so it need to run before compileExpressionAttributes.
$params = array_merge(
$this->grammar->compileSelectAttributes($this->selectAttributes),
$this->grammar->compileProjectionExpression($this->columns, $this->expression_attributes),
$this->grammar->compileConditions($this->filter_query),
$this->grammar->compileConditions($this->condition_query),
Expand Down
16 changes: 16 additions & 0 deletions src/Kitar/Dynamodb/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public function __construct()
$this->marshaler = new Marshaler;
}

/**
* Compile the Select attribute.
*
* @param $select_attributes
* @return array
*/
public function compileSelectAttributes($select_attributes) {
if ($select_attributes === 'ALL_ATTRIBUTES') {
return [];
}

return [
'Select' => $select_attributes,
];
}

/**
* Compile the TableName attribute.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Kitar/Dynamodb/Query/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ protected function unmarshal(Result $res)
return $responseArray;
}

public function processCount(Result $awsResponse, $modelClass = null) {
$response = $this->unmarshal($awsResponse);

if (empty($modelClass)) {
return $response;
}

if (! empty($response['Count'])) {
return $response['Count'];
}
}

public function processSingleItem(Result $awsResponse, $modelClass = null)
{
$response = $this->unmarshal($awsResponse);
Expand Down