Skip to content

Commit 608db9d

Browse files
author
Peter Matseykanets
committed
Apply ORDER BY clauses set on the builder instance. Resolves #16
1 parent 85de4f2 commit 608db9d

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# Changelog
22

3+
## [2.1.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v2.1.0) - 2018-02-23
4+
5+
### Added
6+
7+
- Added support for applying `ORDER BY` clauses set on the builder instance.
8+
39
## [2.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v2.0.0) - 2018-02-09
410

511
### Changed
612

7-
- Switched to Scout 4 (Laravel 5.6) and PHPUnit 7
13+
- Switched to Scout 4 (Laravel 5.6) and PHPUnit 7.
814

915
## [1.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v1.0.0) - 2017-09-03
1016

src/PostgresEngine.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,9 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
235235
->select($builder->model->getKeyName())
236236
->selectRaw("{$this->rankingExpression($builder->model, $indexColumn)} AS rank")
237237
->selectRaw('COUNT(*) OVER () AS total_count')
238-
->whereRaw("$indexColumn @@ query")
239-
->orderBy('rank', 'desc')
240-
->orderBy($builder->model->getKeyName());
238+
->whereRaw("$indexColumn @@ query");
241239

242-
// Transfer the where clauses that were set on the builder instance if any
240+
// Apply where clauses that were set on the builder instance if any
243241
foreach ($builder->wheres as $key => $value) {
244242
$query->where($key, $value);
245243
$bindings->push($value);
@@ -253,6 +251,17 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
253251
}
254252
}
255253

254+
// Apply order by clauses that were set on the builder instance if any
255+
foreach ($builder->orders as $order) {
256+
$query->orderBy($order['column'], $order['direction']);
257+
}
258+
259+
// Apply default order by clauses (rank and id)
260+
if (empty($builder->orders)) {
261+
$query->orderBy('rank', 'desc')
262+
->orderBy($builder->model->getKeyName());
263+
}
264+
256265
if ($perPage > 0) {
257266
$query->skip(($page - 1) * $perPage)
258267
->limit($perPage);

tests/PostgresEngineTest.php

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,39 @@ public function test_search()
9191

9292
$skip = 0;
9393
$limit = 5;
94-
$table = $this->setDbExpectations($db, $skip, $limit);
94+
$table = $this->setDbExpectations($db);
9595

96-
$table->shouldReceive('skip')
97-
->with($skip)
98-
->andReturnSelf()
99-
->shouldReceive('limit')
100-
->with($limit)
101-
->andReturnSelf()
102-
->shouldReceive('where')
103-
->with('bar', 1);
96+
$table->shouldReceive('skip')->with($skip)->andReturnSelf()
97+
->shouldReceive('limit')->with($limit)->andReturnSelf()
98+
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
99+
->shouldReceive('where')->with('baz', 'qux');
104100

105101
$db->shouldReceive('select')
106-
->with(null, [null, 'foo', 1]);
102+
->with(null, [null, 'foo', 1, 'qux']);
107103

108104
$builder = new Builder(new TestModel(), 'foo');
109-
$builder->where('bar', 1)->take(5);
105+
$builder->where('bar', 1)
106+
->where('baz', 'qux')
107+
->take(5);
108+
109+
$engine->search($builder);
110+
}
111+
112+
public function test_search_with_order_by()
113+
{
114+
list($engine, $db) = $this->getEngine();
115+
116+
$table = $this->setDbExpectations($db, false);
117+
118+
$table->shouldReceive('orderBy')->with('bar', 'desc')->andReturnSelf()
119+
->shouldReceive('orderBy')->with('baz', 'asc')->andReturnSelf();
120+
121+
$db->shouldReceive('select')
122+
->with(null, [null, 'foo']);
123+
124+
$builder = new Builder(new TestModel(), 'foo');
125+
$builder->orderBy('bar', 'desc')
126+
->orderBy('baz', 'asc');
110127

111128
$engine->search($builder);
112129
}
@@ -247,7 +264,7 @@ protected function getEngine($config = [])
247264
return [new PostgresEngine($resolver, $config), $db];
248265
}
249266

250-
protected function setDbExpectations($db)
267+
protected function setDbExpectations($db, $withDefaultOrderBy = true)
251268
{
252269
$db->shouldReceive('table')
253270
->andReturn($table = Mockery::mock('stdClass'));
@@ -268,14 +285,18 @@ protected function setDbExpectations($db)
268285
->with('COUNT(*) OVER () AS total_count')
269286
->andReturnSelf()
270287
->shouldReceive('whereRaw')
271-
->andReturnSelf()
272-
->shouldReceive('orderBy')
273-
->with('rank', 'desc')
274-
->andReturnSelf()
275-
->shouldReceive('orderBy')
276-
->with('id')
277-
->andReturnSelf()
278-
->shouldReceive('toSql');
288+
->andReturnSelf();
289+
290+
if ($withDefaultOrderBy) {
291+
$table->shouldReceive('orderBy')
292+
->with('rank', 'desc')
293+
->andReturnSelf()
294+
->shouldReceive('orderBy')
295+
->with('id')
296+
->andReturnSelf();
297+
}
298+
299+
$table->shouldReceive('toSql');
279300

280301
return $table;
281302
}

0 commit comments

Comments
 (0)