Skip to content

Commit 4a3cf53

Browse files
committed
Add queryCallback, whereIn and whereNotIn support.
1 parent 6f0a634 commit 4a3cf53

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/PostgresEngine.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,26 @@ protected function performSearch(Builder $builder, ?int $perPage = 0, int $page
365365
->selectRaw('COUNT(*) OVER () AS total_count')
366366
->whereRaw("{$indexColumn} @@ \"tsquery\"");
367367

368+
// Apply query callback if set
369+
if ($builder->queryCallback) {
370+
call_user_func($builder->queryCallback, $builder);
371+
}
372+
368373
// Apply where clauses that were set on the builder instance if any
369374
foreach ($builder->wheres as $key => $value) {
370375
$query->where($key, $value);
371376
}
372377

378+
// Apply whereIn clauses that were set on the builder instance if any
379+
foreach ($builder->whereIns as $key => $value) {
380+
$query->whereIn($key, $value);
381+
}
382+
383+
// Apply whereNoIn clauses that were set on the builder instance if any
384+
foreach ($builder->whereNotIns as $key => $value) {
385+
$query->whereNotIn($key, $value);
386+
}
387+
373388
// If parsed documents are being stored in the model's table
374389
if (! $this->isExternalIndex($builder->model)) {
375390
// and the model uses soft deletes we need to exclude trashed rows

tests/PostgresEngineTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,85 @@ public function search_with_order_by()
180180
$engine->search($builder);
181181
}
182182

183+
/**
184+
* @test
185+
*/
186+
public function search_with_queryCallback()
187+
{
188+
[$engine, $db] = $this->getEngine();
189+
190+
$skip = 0;
191+
$limit = 5;
192+
$table = $this->setDbExpectations($db);
193+
194+
$table->shouldReceive('skip')->with($skip)->andReturnSelf()
195+
->shouldReceive('limit')->with($limit)->andReturnSelf()
196+
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
197+
->shouldReceive('where')->with('baz', 'qux')
198+
->shouldReceive('getBindings')->andReturn([null, 'foo', 1, 'qux']);
199+
200+
$db->shouldReceive('select')
201+
->with(null, $table->getBindings())
202+
->once();
203+
204+
$builder = new Builder(new TestModel, 'foo');
205+
$builder->query(function ($q) {
206+
$q->where('bar', 1)
207+
->where('baz', 'qux')
208+
->take(5);
209+
});
210+
211+
$engine->search($builder);
212+
}
213+
214+
/**
215+
* @test
216+
*/
217+
public function search_with_whereIn()
218+
{
219+
[$engine, $db] = $this->getEngine();
220+
221+
$skip = 0;
222+
$limit = 5;
223+
$table = $this->setDbExpectations($db);
224+
225+
$table->shouldReceive('whereIn')->with('bar', [1])->andReturnSelf()
226+
->shouldReceive('getBindings')->andReturn([null, 'foo', [1]]);
227+
228+
$db->shouldReceive('select')
229+
->with(null, $table->getBindings())
230+
->once();
231+
232+
$builder = new Builder(new TestModel, 'foo');
233+
$builder->whereIn('bar', [1]);
234+
235+
$engine->search($builder);
236+
}
237+
238+
/**
239+
* @test
240+
*/
241+
public function search_with_whereNotIn()
242+
{
243+
[$engine, $db] = $this->getEngine();
244+
245+
$skip = 0;
246+
$limit = 5;
247+
$table = $this->setDbExpectations($db);
248+
249+
$table->shouldReceive('whereNotIn')->with('bar', [1])->andReturnSelf()
250+
->shouldReceive('getBindings')->andReturn([null, 'foo', [1]]);
251+
252+
$db->shouldReceive('select')
253+
->with(null, $table->getBindings())
254+
->once();
255+
256+
$builder = new Builder(new TestModel, 'foo');
257+
$builder->whereNotIn('bar', [1]);
258+
259+
$engine->search($builder);
260+
}
261+
183262
/**
184263
* @test
185264
*/

0 commit comments

Comments
 (0)