Skip to content

Commit

Permalink
Merge pull request #77 from baopham/whereIn
Browse files Browse the repository at this point in the history
Add whereIn support
  • Loading branch information
baopham authored Aug 12, 2017
2 parents 797f5a9 + d2df23f commit 2afa7cd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/ComparisonOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,10 @@ public static function isValidQueryDynamoDbOperator($dynamoDbOperator, $isRangeK
{
return in_array($dynamoDbOperator, static::getQuerySupportedOperators($isRangeKey));
}

public static function is($op, $opToCompare)
{
$mapping = static::getOperatorMapping();
return $mapping[strtolower($op)] === $opToCompare;
}
}
1 change: 0 additions & 1 deletion src/DynamoDbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ abstract class DynamoDbModel extends Model
*/
protected $dynamoDbIndexKeys = [];


/**
* Array of your composite key.
* ['hash', 'range']
Expand Down
44 changes: 43 additions & 1 deletion src/DynamoDbQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace BaoPham\DynamoDb;

use Aws\DynamoDb\DynamoDbClient;
use Closure;
use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Collection;
use \Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -116,7 +118,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'

$valueList = [$attributeValueList['AttributeValueList']];

if (strtolower($operator) === 'between') {
if (
ComparisonOperator::is($operator, ComparisonOperator::BETWEEN) ||
ComparisonOperator::is($operator, ComparisonOperator::IN)
) {
$valueList = head($valueList)['L'];
}

Expand All @@ -128,6 +133,43 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
return $this;
}

/**
* Add a "where in" clause to the query.
*
* @param string $column
* @param mixed $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereIn($column, $values, $boolean = 'and', $not = false)
{
if ($boolean != 'and') {
throw new NotSupportedException('Only support "and" in whereIn clause');
}

if ($not) {
throw new NotSupportedException('"not in" is not a valid DynamoDB comparison operator');
}

// If the value is a query builder instance, not supported
if ($values instanceof static) {
throw new NotSupportedException('Value is a query builder instance');
}

// If the value of the where in clause is actually a Closure, not supported
if ($values instanceof Closure) {
throw new NotSupportedException('Value is a Closure');
}

// Next, if the value is Arrayable we need to cast it to its raw array form
if ($values instanceof Arrayable) {
$values = $values->toArray();
}

return $this->where($column, ComparisonOperator::IN, $values);
}

/**
* Implements the Query Chunk method
*
Expand Down
16 changes: 16 additions & 0 deletions tests/DynamoDbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,22 @@ public function testDifferentQueries()
$this->assertEquals($this->testModel->unmarshalItem($expectedBar), $barQuery->first()->toArray());
}

public function testWhereIn()
{
$this->seed(['name' => ['S' => 'foo']]);
$this->seed(['name' => ['S' => 'foo']]);
$this->seed(['name' => ['S' => 'bar']]);
$this->seed(['name' => ['S' => 'foobar']]);

$items = $this->testModel->whereIn('name', ['foo', 'bar'])->get();

$this->assertCount(3, $items->toArray());

foreach ($items as $item) {
$this->assertContains($item->name, ['foo', 'bar']);
}
}

public function testChunkScan()
{
$this->seed(['name' => ['S' => 'Foo']]);
Expand Down

0 comments on commit 2afa7cd

Please sign in to comment.