Skip to content

Commit 7ec0d1c

Browse files
committed
Refactoring bindings in PostgresEngine::performSearch() method
To preserve order of binding insertion And to do it in a more Laravel way
1 parent 4c79e5c commit 7ec0d1c

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

src/PostgresEngine.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
219219
// does not receive a model instance
220220
$this->preserveModel($builder->model);
221221

222-
$bindings = collect([]);
223-
224222
$indexColumn = $this->getIndexColumn($builder->model);
225223

226224
// Build the SQL query
@@ -234,7 +232,6 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
234232
// Apply where clauses that were set on the builder instance if any
235233
foreach ($builder->wheres as $key => $value) {
236234
$query->where($key, $value);
237-
$bindings->push($value);
238235
}
239236

240237
// If parsed documents are being stored in the model's table
@@ -270,14 +267,10 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
270267
: $this->defaultQueryMethod($builder->query, $this->searchConfig($builder->model));
271268

272269
$query->crossJoin($this->database->raw($tsQuery->sql().' AS "tsquery"'));
273-
274-
// Transfer bindings
275-
foreach ($tsQuery->bindings() as $binding) {
276-
$bindings->prepend($binding);
277-
}
270+
$query->addBinding($tsQuery->bindings(), 'join');
278271

279272
return $this->database
280-
->select($query->toSql(), $bindings->all());
273+
->select($query->toSql(), $query->getBindings());
281274
}
282275

283276
/**

src/TsQuery/BaseTsQueryable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ public function sql()
5454
*/
5555
public function bindings()
5656
{
57-
return [$this->query, $this->config];
57+
return [$this->config, $this->query];
5858
}
5959
}

tests/PostgresEngineTest.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ public function test_search()
9696
$table->shouldReceive('skip')->with($skip)->andReturnSelf()
9797
->shouldReceive('limit')->with($limit)->andReturnSelf()
9898
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
99-
->shouldReceive('where')->with('baz', 'qux');
99+
->shouldReceive('where')->with('baz', 'qux')
100+
->shouldReceive('getBindings')->andReturn([null, 'foo', 1, 'qux']);
100101

101102
$db->shouldReceive('select')
102-
->with(null, [null, 'foo', 1, 'qux']);
103+
->with(null, $table->getBindings());
103104

104105
$builder = new Builder(new TestModel(), 'foo');
105106
$builder->where('bar', 1)
@@ -116,10 +117,11 @@ public function test_search_with_order_by()
116117
$table = $this->setDbExpectations($db, false);
117118

118119
$table->shouldReceive('orderBy')->with('bar', 'desc')->andReturnSelf()
119-
->shouldReceive('orderBy')->with('baz', 'asc')->andReturnSelf();
120+
->shouldReceive('orderBy')->with('baz', 'asc')->andReturnSelf()
121+
->shouldReceive('getBindings')->andReturn([null, 'foo']);
120122

121123
$db->shouldReceive('select')
122-
->with(null, [null, 'foo']);
124+
->with(null, $table->getBindings());
123125

124126
$builder = new Builder(new TestModel(), 'foo');
125127
$builder->orderBy('bar', 'desc')
@@ -138,9 +140,10 @@ public function test_search_with_global_config()
138140

139141
$table->shouldReceive('skip')->with($skip)->andReturnSelf()
140142
->shouldReceive('limit')->with($limit)->andReturnSelf()
141-
->shouldReceive('where')->with('bar', 1);
143+
->shouldReceive('where')->with('bar', 1)
144+
->shouldReceive('getBindings')->andReturn(['simple', 'foo', 1]);
142145

143-
$db->shouldReceive('select')->with(null, ['simple', 'foo', 1]);
146+
$db->shouldReceive('select')->with(null, $table->getBindings());
144147

145148
$builder = new Builder(new TestModel(), 'foo');
146149
$builder->where('bar', 1)->take(5);
@@ -158,9 +161,10 @@ public function test_search_with_model_config()
158161

159162
$table->shouldReceive('skip')->with($skip)->andReturnSelf()
160163
->shouldReceive('limit')->with($limit)->andReturnSelf()
161-
->shouldReceive('where')->with('bar', 1);
164+
->shouldReceive('where')->with('bar', 1)
165+
->shouldReceive('getBindings')->andReturn(['english', 'foo', 1]);
162166

163-
$db->shouldReceive('select')->with(null, ['english', 'foo', 1]);
167+
$db->shouldReceive('select')->with(null, $table->getBindings());
164168

165169
$model = new TestModel();
166170
$model->searchableOptions['config'] = 'english';
@@ -180,9 +184,10 @@ public function test_search_with_soft_deletes()
180184
$table->shouldReceive('skip')->with(0)->andReturnSelf()
181185
->shouldReceive('limit')->with(5)->andReturnSelf()
182186
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
183-
->shouldReceive('whereNull')->with('deleted_at');
187+
->shouldReceive('whereNull')->with('deleted_at')
188+
->shouldReceive('getBindings')->andReturn([null, 'foo', 1]);
184189

185-
$db->shouldReceive('select')->with(null, [null, 'foo', 1]);
190+
$db->shouldReceive('select')->with(null, $table->getBindings());
186191

187192
$builder = new Builder(new SoftDeletableTestModel(), 'foo');
188193
$builder->where('bar', 1)->take(5);
@@ -240,7 +245,8 @@ public function test_map_ids_returns_right_key()
240245
{
241246
list($engine, $db) = $this->getEngine();
242247

243-
$this->setDbExpectations($db);
248+
$table = $this->setDbExpectations($db);
249+
$table->shouldReceive('getBindings')->andReturn([null, 'foo']);
244250

245251
$db->shouldReceive('select')
246252
->andReturn(json_decode('[{"id": 1}, {"id": 2}]'));
@@ -275,6 +281,9 @@ protected function setDbExpectations($db, $withDefaultOrderBy = true)
275281
$table->shouldReceive('crossJoin')
276282
->with('plainto_tsquery(COALESCE(?, get_current_ts_config()), ?) AS "tsquery"')
277283
->andReturnSelf()
284+
->shouldReceive('addBinding')
285+
->with(Mockery::type('array'), 'join')
286+
->andReturnSelf()
278287
->shouldReceive('select')
279288
->with('id')
280289
->andReturnSelf()

0 commit comments

Comments
 (0)