Skip to content

Commit 2afa7cd

Browse files
authored
Merge pull request #77 from baopham/whereIn
Add whereIn support
2 parents 797f5a9 + d2df23f commit 2afa7cd

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

src/ComparisonOperator.php

+6
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,10 @@ public static function isValidQueryDynamoDbOperator($dynamoDbOperator, $isRangeK
8787
{
8888
return in_array($dynamoDbOperator, static::getQuerySupportedOperators($isRangeKey));
8989
}
90+
91+
public static function is($op, $opToCompare)
92+
{
93+
$mapping = static::getOperatorMapping();
94+
return $mapping[strtolower($op)] === $opToCompare;
95+
}
9096
}

src/DynamoDbModel.php

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ abstract class DynamoDbModel extends Model
4949
*/
5050
protected $dynamoDbIndexKeys = [];
5151

52-
5352
/**
5453
* Array of your composite key.
5554
* ['hash', 'range']

src/DynamoDbQueryBuilder.php

+43-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace BaoPham\DynamoDb;
44

55
use Aws\DynamoDb\DynamoDbClient;
6+
use Closure;
67
use Exception;
8+
use Illuminate\Contracts\Support\Arrayable;
79
use Illuminate\Database\Eloquent\Collection;
810
use \Illuminate\Database\Eloquent\ModelNotFoundException;
911
use Illuminate\Support\Facades\Log;
@@ -116,7 +118,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
116118

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

119-
if (strtolower($operator) === 'between') {
121+
if (
122+
ComparisonOperator::is($operator, ComparisonOperator::BETWEEN) ||
123+
ComparisonOperator::is($operator, ComparisonOperator::IN)
124+
) {
120125
$valueList = head($valueList)['L'];
121126
}
122127

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

136+
/**
137+
* Add a "where in" clause to the query.
138+
*
139+
* @param string $column
140+
* @param mixed $values
141+
* @param string $boolean
142+
* @param bool $not
143+
* @return $this
144+
*/
145+
public function whereIn($column, $values, $boolean = 'and', $not = false)
146+
{
147+
if ($boolean != 'and') {
148+
throw new NotSupportedException('Only support "and" in whereIn clause');
149+
}
150+
151+
if ($not) {
152+
throw new NotSupportedException('"not in" is not a valid DynamoDB comparison operator');
153+
}
154+
155+
// If the value is a query builder instance, not supported
156+
if ($values instanceof static) {
157+
throw new NotSupportedException('Value is a query builder instance');
158+
}
159+
160+
// If the value of the where in clause is actually a Closure, not supported
161+
if ($values instanceof Closure) {
162+
throw new NotSupportedException('Value is a Closure');
163+
}
164+
165+
// Next, if the value is Arrayable we need to cast it to its raw array form
166+
if ($values instanceof Arrayable) {
167+
$values = $values->toArray();
168+
}
169+
170+
return $this->where($column, ComparisonOperator::IN, $values);
171+
}
172+
131173
/**
132174
* Implements the Query Chunk method
133175
*

tests/DynamoDbModelTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,22 @@ public function testDifferentQueries()
380380
$this->assertEquals($this->testModel->unmarshalItem($expectedBar), $barQuery->first()->toArray());
381381
}
382382

383+
public function testWhereIn()
384+
{
385+
$this->seed(['name' => ['S' => 'foo']]);
386+
$this->seed(['name' => ['S' => 'foo']]);
387+
$this->seed(['name' => ['S' => 'bar']]);
388+
$this->seed(['name' => ['S' => 'foobar']]);
389+
390+
$items = $this->testModel->whereIn('name', ['foo', 'bar'])->get();
391+
392+
$this->assertCount(3, $items->toArray());
393+
394+
foreach ($items as $item) {
395+
$this->assertContains($item->name, ['foo', 'bar']);
396+
}
397+
}
398+
383399
public function testChunkScan()
384400
{
385401
$this->seed(['name' => ['S' => 'Foo']]);

0 commit comments

Comments
 (0)