Skip to content
Merged
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
50 changes: 31 additions & 19 deletions src/ComparisonOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@
*/
class ComparisonOperator
{
const EQ = 'EQ';
const GT = 'GT';
const GE = 'GE';
const LT = 'LT';
const LE = 'LE';
const IN = 'IN';
const NE = 'NE';
const BEGINS_WITH = 'BEGINS_WITH';
const BETWEEN = 'BETWEEN';
const NOT_CONTAINS = 'NOT_CONTAINS';
const CONTAINS = 'CONTAINS';

public static function getOperatorMapping()
{
return [
'=' => 'EQ',
'>' => 'GT',
'>=' => 'GE',
'<' => 'LT',
'<=' => 'LE',
'in' => 'IN',
'!=' => 'NE',
'begins_with' => 'BEGINS_WITH',
'between' => 'BETWEEN',
'not_contains' => 'NOT_CONTAINS',
'contains' => 'CONTAINS',
'=' => static::EQ,
'>' => static::GT,
'>=' => static::GE,
'<' => static::LT,
'<=' => static::LE,
'in' => static::IN,
'!=' => static::NE,
'begins_with' => static::BEGINS_WITH,
'between' => static::BETWEEN,
'not_contains' => static::NOT_CONTAINS,
'contains' => static::CONTAINS,
];
}

Expand Down Expand Up @@ -51,17 +63,17 @@ public static function getQuerySupportedOperators($isRangeKey = false)
{
if ($isRangeKey) {
return [
'EQ',
'LE',
'LT',
'GE',
'GT',
'BEGINS_WITH',
'BETWEEN',
static::EQ,
static::LE,
static::LT,
static::GE,
static::GT,
static::BEGINS_WITH,
static::BETWEEN,
];
}

return ['EQ'];
return [static::EQ];
}

public static function isValidQueryOperator($operator, $isRangeKey = false)
Expand Down
32 changes: 29 additions & 3 deletions src/DynamoDbQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ public function count()
protected function getAll($columns = [], $limit = -1, $use_iterator = true)
{
if ($conditionValue = $this->conditionsContainKey()) {
$item = $this->find($conditionValue, $columns);
if ($this->conditionsAreExactSearch()) {
$item = $this->find($conditionValue, $columns);

return new Collection([$item]);
return new Collection([$item]);
}
}

$query = [
Expand Down Expand Up @@ -260,6 +262,9 @@ protected function getAll($columns = [], $limit = -1, $use_iterator = true)
$query['QueryFilter'] = $nonKeyConditions;
}
}
} else if ($this->conditionsContainKey()) {
$op = 'Query';
$query['KeyConditions'] = $this->where;
}

if ($op === 'Scan') {
Expand All @@ -270,7 +275,7 @@ protected function getAll($columns = [], $limit = -1, $use_iterator = true)
if ($use_iterator) {
$iterator = $this->client->getIterator($op, $query);
} else {
if ($op == 'Scan') {
if ($op === 'Scan') {
$res = $this->client->scan($query);
} else {
$res = $this->client->query($query);
Expand All @@ -293,10 +298,31 @@ protected function getAll($columns = [], $limit = -1, $use_iterator = true)
return new Collection($results);
}

protected function conditionsAreExactSearch()
{
if (empty($this->where)) {
return false;
}

foreach ($this->where as $condition) {
if (array_get($condition, 'ComparisonOperator') !== ComparisonOperator::EQ) {
return false;
}
}

return true;
}

/**
* Check if conditions "where" contain primary key or composite key.
* For composite key, it will return false if the conditions don't have all composite key.
*
* For example:
* Consider a composite key condition:
* $model->where('partition_key', 'foo')->where('sort_key', 'bar')
* We return ['partition_key' => 'foo', 'sort_key' => 'bar'] since the conditions
* contain all the composite key.
*
* @return array|bool the condition value
*/
protected function conditionsContainKey()
Expand Down
28 changes: 27 additions & 1 deletion tests/DynamoDbCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testDeleteRecord()
$this->assertArrayNotHasKey('Item', $record);
}

public function testLookingUpByKey()
public function testLookUpByKey()
{
$this->seed();

Expand All @@ -139,6 +139,32 @@ public function testLookingUpByKey()
$this->assertEquals($this->testModel->unmarshalItem($item), $foundItems->first()->toArray());
}

public function testSearchByHashAndSortKey()
{
$partitionKey = 'foo';
$item1 = $this->seed([
'id' => ['S' => $partitionKey],
'id2' => ['S' => 'bar_1']
]);
$item2 = $this->seed([
'id' => ['S' => $partitionKey],
'id2' => ['S' => 'bar_2']
]);
$this->seed([
'id' => ['S' => 'other'],
'id2' => ['S' => 'foo_1']
]);

$foundItems = $this->testModel
->where('id', $partitionKey)
->where('id2', 'begins_with', 'bar')
->get();

$this->assertEquals(2, $foundItems->count());
$this->assertEquals($this->testModel->unmarshalItem($item1), $foundItems->first()->toArray());
$this->assertEquals($this->testModel->unmarshalItem($item2), $foundItems->last()->toArray());
}

public function testStaticMethods()
{
$item = $this->seed(['name' => ['S' => 'Foo'], 'description' => ['S' => 'Bar']]);
Expand Down
2 changes: 1 addition & 1 deletion tests/DynamoDbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function testChainedMethods()
$this->assertEquals($this->testModel->unmarshalItem($secondItem), $foundItems->first()->toArray());
}

public function testLookingUpByKey()
public function testLookUpByKey()
{
$this->seed();

Expand Down