Skip to content

Commit db78702

Browse files
committed
Add OR conditions support
1 parent e2a9f97 commit db78702

File tree

3 files changed

+184
-28
lines changed

3 files changed

+184
-28
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,15 @@ $model->delete();
9090
$model->where('key', 'key value')->get();
9191
9292
$model->where(['key' => 'key value']);
93-
// Chainable for 'AND'. 'OR' is not tested.
93+
// Chainable for 'AND'.
9494
$model->where('foo', 'bar')
9595
->where('foo2', '!=' 'bar2')
9696
->get();
97+
98+
// Chainable for 'OR'.
99+
$model->where('foo', 'bar')
100+
->orWhere('foo2', '!=' 'bar2')
101+
->get();
97102
$model->where('count', 'between', [0, 100])->get();
98103
$model->where('count', '>', 0)->get();
99104
$model->where('description', 'begins_with', 'foo')->get();
@@ -264,7 +269,6 @@ Laravel ^5.1
264269
TODO
265270
----
266271
- [ ] Nested conditions
267-
- [ ] Verify OR conditions
268272
269273
FAQ
270274
---

src/DynamoDbQueryBuilder.php

+47
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,19 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
192192
return $this;
193193
}
194194

195+
/**
196+
* Add an "or where" clause to the query.
197+
*
198+
* @param string $column
199+
* @param string $operator
200+
* @param mixed $value
201+
* @return $this
202+
*/
203+
public function orWhere($column, $operator = null, $value = null)
204+
{
205+
return $this->where($column, $operator, $value, 'or');
206+
}
207+
195208
/**
196209
* Add a "where in" clause to the query.
197210
*
@@ -226,6 +239,18 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
226239
return $this->where($column, ComparisonOperator::IN, $values, $boolean);
227240
}
228241

242+
/**
243+
* Add an "or where in" clause to the query.
244+
*
245+
* @param string $column
246+
* @param mixed $values
247+
* @return $this
248+
*/
249+
public function orWhereIn($column, $values)
250+
{
251+
return $this->whereIn($column, $values, 'or');
252+
}
253+
229254
/**
230255
* Add a "where null" clause to the query.
231256
*
@@ -243,6 +268,28 @@ public function whereNull($column, $boolean = 'and', $not = false)
243268
return $this;
244269
}
245270

271+
/**
272+
* Add an "or where null" clause to the query.
273+
*
274+
* @param string $column
275+
* @return $this
276+
*/
277+
public function orWhereNull($column)
278+
{
279+
return $this->whereNull($column, 'or');
280+
}
281+
282+
/**
283+
* Add an "or where not null" clause to the query.
284+
*
285+
* @param string $column
286+
* @return $this
287+
*/
288+
public function orWhereNotNull($column)
289+
{
290+
return $this->whereNotNull($column, 'or');
291+
}
292+
246293
/**
247294
* Add a "where not null" clause to the query.
248295
*

tests/DynamoDbModelTest.php

+131-26
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,19 @@ public function testGetAllRecordsWithEqualCondition()
184184
$this->seed(['count' => ['N' => 10]]);
185185
$this->seed(['count' => ['N' => 10]]);
186186
$this->seed(['count' => ['N' => 10]]);
187+
$this->seed(['count' => ['N' => 9]]);
188+
189+
$items = $this->testModel->where('count', 10)->get();
190+
191+
$this->assertEquals(3, $items->count());
187192

188-
$items = $this->testModel->where('count', 10)->get()->toArray();
193+
// OR condition
194+
$items = $this->testModel
195+
->where('count', 10)
196+
->orWhere('count', 9)
197+
->get();
189198

190-
$this->assertEquals(3, count($items));
199+
$this->assertEquals(4, $items->count());
191200
}
192201

193202
public function testGetAllRecordsWithNonEqualCondition()
@@ -196,9 +205,17 @@ public function testGetAllRecordsWithNonEqualCondition()
196205
$this->seed(['count' => ['N' => 11]]);
197206
$this->seed(['count' => ['N' => 11]]);
198207

199-
$items = $this->testModel->where('count', '!=', 10)->get()->toArray();
208+
$items = $this->testModel->where('count', '!=', 10)->get();
200209

201-
$this->assertEquals(2, count($items));
210+
$this->assertEquals(2, $items->count());
211+
212+
// OR condition
213+
$items = $this->testModel
214+
->where('count', '!=', 10)
215+
->orWhere('count', '!=', 11)
216+
->get();
217+
218+
$this->assertEquals(3, $items->count());
202219
}
203220

204221
public function testGetAllRecordsWithGTCondition()
@@ -207,9 +224,17 @@ public function testGetAllRecordsWithGTCondition()
207224
$this->seed(['count' => ['N' => 11]]);
208225
$this->seed(['count' => ['N' => 11]]);
209226

210-
$items = $this->testModel->where('count', '>', 10)->get()->toArray();
227+
$items = $this->testModel->where('count', '>', 10)->get();
211228

212-
$this->assertEquals(2, count($items));
229+
$this->assertEquals(2, $items->count());
230+
231+
// OR condition
232+
$items = $this->testModel
233+
->where('count', '>', 100)
234+
->orWhere('count', '>', 10)
235+
->get();
236+
237+
$this->assertEquals(2, $items->count());
213238
}
214239

215240
public function testGetAllRecordsWithLTCondition()
@@ -218,9 +243,17 @@ public function testGetAllRecordsWithLTCondition()
218243
$this->seed(['count' => ['N' => 9]]);
219244
$this->seed(['count' => ['N' => 11]]);
220245

221-
$items = $this->testModel->where('count', '<', 10)->get()->toArray();
246+
$items = $this->testModel->where('count', '<', 10)->get();
222247

223-
$this->assertEquals(1, count($items));
248+
$this->assertEquals(1, $items->count());
249+
250+
// OR condition
251+
$items = $this->testModel
252+
->where('count', '<', 1)
253+
->orWhere('count', '<', 10)
254+
->get();
255+
256+
$this->assertEquals(1, $items->count());
224257
}
225258

226259
public function testGetAllRecordsWithGECondition()
@@ -229,9 +262,17 @@ public function testGetAllRecordsWithGECondition()
229262
$this->seed(['count' => ['N' => 9]]);
230263
$this->seed(['count' => ['N' => 11]]);
231264

232-
$items = $this->testModel->where('count', '>=', 10)->get()->toArray();
265+
$items = $this->testModel->where('count', '>=', 10)->get();
233266

234-
$this->assertEquals(2, count($items));
267+
$this->assertEquals(2, $items->count());
268+
269+
// OR condition
270+
$items = $this->testModel
271+
->where('count', '>=', 10)
272+
->orWhere('count', '>=', 100)
273+
->get();
274+
275+
$this->assertEquals(2, $items->count());
235276
}
236277

237278
public function testGetAllRecordsWithLECondition()
@@ -240,9 +281,17 @@ public function testGetAllRecordsWithLECondition()
240281
$this->seed(['count' => ['N' => 9]]);
241282
$this->seed(['count' => ['N' => 11]]);
242283

243-
$items = $this->testModel->where('count', '<=', 10)->get()->toArray();
284+
$items = $this->testModel->where('count', '<=', 10)->get();
244285

245-
$this->assertEquals(2, count($items));
286+
$this->assertEquals(2, $items->count());
287+
288+
// OR condition
289+
$items = $this->testModel
290+
->where('count', '<=', 10)
291+
->orWhere('count', '<=', 1)
292+
->get();
293+
294+
$this->assertEquals(2, $items->count());
246295
}
247296

248297
public function testGetAllRecordsWithBeginsWithOperator()
@@ -251,51 +300,83 @@ public function testGetAllRecordsWithBeginsWithOperator()
251300
$this->seed(['description' => ['S' => 'Foo_2']]);
252301
$this->seed(['description' => ['S' => 'Bar_Foo']]);
253302

254-
$items = $this->testModel->where('description', 'BEGINS_WITH', 'Foo')->get()->toArray();
303+
$items = $this->testModel->where('description', 'BEGINS_WITH', 'Foo')->get();
255304

256-
$this->assertEquals(2, count($items));
305+
$this->assertEquals(2, $items->count());
306+
307+
// OR condition
308+
$items = $this->testModel
309+
->where('description', 'BEGINS_WITH', 'Foo')
310+
->orWhere('description', 'BEGINS_WITH', 'Bar')
311+
->get();
312+
313+
$this->assertEquals(3, $items->count());
257314
}
258315

259316
public function testGetAllRecordsWithContainsOperator()
260317
{
261318
$this->seed(['description' => ['L' => [['S' => 'foo'], ['S' => 'bar']]]]);
262319
$this->seed(['description' => ['L' => [['S' => 'foo'], ['S' => 'bar2']]]]);
263320

264-
$items = $this->testModel->where('description', 'CONTAINS', 'foo')->get()->toArray();
321+
$items = $this->testModel->where('description', 'CONTAINS', 'foo')->get();
265322

266-
$this->assertEquals(2, count($items));
323+
$this->assertEquals(2, $items->count());
267324

268-
$items = $this->testModel->where('description', 'CONTAINS', 'bar2')->get()->toArray();
325+
$items = $this->testModel->where('description', 'CONTAINS', 'bar2')->get();
326+
327+
$this->assertEquals(1, $items->count());
328+
329+
// OR condition
330+
$items = $this->testModel
331+
->where('description', 'CONTAINS', 'bar2')
332+
->orWhere('description', 'CONTAINS', 'foo')
333+
->get();
269334

270-
$this->assertEquals(1, count($items));
335+
$this->assertEquals(2, $items->count());
271336
}
272337

273338
public function testGetAllRecordsWithNotContainsOperator()
274339
{
275340
$this->seed(['description' => ['L' => [['S' => 'foo'], ['S' => 'bar']]]]);
276341
$this->seed(['description' => ['L' => [['S' => 'foo'], ['S' => 'bar2']]]]);
277342

278-
$items = $this->testModel->where('description', 'NOT_CONTAINS', 'foo')->get()->toArray();
343+
$items = $this->testModel->where('description', 'NOT_CONTAINS', 'foo')->get();
279344

280-
$this->assertEquals(0, count($items));
345+
$this->assertEquals(0, $items->count());
281346

282-
$items = $this->testModel->where('description', 'NOT_CONTAINS', 'foobar')->get()->toArray();
347+
$items = $this->testModel->where('description', 'NOT_CONTAINS', 'foobar')->get();
283348

284-
$this->assertEquals(2, count($items));
349+
$this->assertEquals(2, $items->count());
350+
351+
// OR condition
352+
$items = $this->testModel
353+
->where('description', 'NOT_CONTAINS', 'oo')
354+
->orWhere('description', 'NOT_CONTAINS', 'aa')
355+
->get();
356+
357+
$this->assertEquals(2, $items->count());
285358
}
286359

287360
public function testGetAllRecordsWithBetweenOperator()
288361
{
289362
$this->seed(['description' => ['N' => 10]]);
290363
$this->seed(['description' => ['N' => 11]]);
291364

292-
$items = $this->testModel->where('description', 'BETWEEN', [1, 11])->get()->toArray();
365+
$items = $this->testModel->where('description', 'BETWEEN', [1, 11])->get();
293366

294-
$this->assertEquals(2, count($items));
367+
$this->assertEquals(2, $items->count());
368+
369+
$items = $this->testModel->where('description', 'BETWEEN', [100, 110])->get();
370+
371+
$this->assertEquals(0, $items->count());
295372

296-
$items = $this->testModel->where('description', 'BETWEEN', [100, 110])->get()->toArray();
373+
// OR condition
374+
$items = $this->testModel
375+
->where('description', 'BETWEEN', [100, 110])
376+
->orWhere('description', 'BETWEEN', [1, 11])
377+
->get();
297378

298-
$this->assertEquals(0, count($items));
379+
$this->assertEquals(2, $items->count());
299380
}
300381

301382
public function testGetFirstRecord()
@@ -394,6 +475,18 @@ public function testWhereIn()
394475
foreach ($items as $item) {
395476
$this->assertContains($item->name, ['foo', 'bar']);
396477
}
478+
479+
// OR condition
480+
$items = $this->testModel
481+
->whereIn('name', ['foo', 'bar'])
482+
->orWhereIn('name', ['foobar'])
483+
->get();
484+
485+
$this->assertEquals(4, $items->count());
486+
487+
foreach ($items as $item) {
488+
$this->assertContains($item->name, ['foo', 'bar', 'foobar']);
489+
}
397490
}
398491

399492
public function testWhereNull()
@@ -406,6 +499,13 @@ public function testWhereNull()
406499
$this->assertEquals(1, $items->count());
407500

408501
$this->assertNull($items->first()->name);
502+
503+
// OR condition
504+
$items = $this->testModel->whereNull('name')->orWhereNull('description')->get();
505+
506+
$this->assertEquals(1, $items->count());
507+
508+
$this->assertNull($items->first()->name);
409509
}
410510

411511
public function testWhereNotNull()
@@ -418,6 +518,11 @@ public function testWhereNotNull()
418518
$this->assertEquals(1, $items->count());
419519

420520
$this->assertNotNull($items->first()->name);
521+
522+
// OR condition
523+
$items = $this->testModel->whereNotNull('name')->orWhereNotNull('description')->get();
524+
525+
$this->assertEquals(2, $items->count());
421526
}
422527

423528
public function testChunkScan()

0 commit comments

Comments
 (0)