Skip to content

Commit

Permalink
make any column searchable in postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
m-bymike committed Sep 19, 2018
1 parent caa7d53 commit 9c44092
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,33 @@ class PostgresGrammar extends Grammar
/**
* {@inheritdoc}
*
* @param Builder $query
* @param array $where
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBasic(Builder $query, $where)
{
// Special treatment for like clauses. In order to make
// any column searchable, we need to convert it to text.
if (Str::contains($where['operator'], 'like')) {
if (Str::contains(strtolower($where['operator']), 'like')) {
return $this->whereBasicLike($query, $where);
}

$value = $this->parameter($where['value']);

return $this->wrap($where['column']).' '.$where['operator'].' '.$value;
return parent::whereBasic($query, $where);
}

/**
* Compile a "where like" clause. Suffix column with text conversion
* to make numeric columns searchable.
*
* @param Builder $query
* @param array $where
* Compile "where like", "where ilike", "where not like" and "where not ilike" clauses.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBasicLike(Builder $query, $where)
{
$value = $this->parameter($where['value']);

return $this->wrap($where['column']).' :: text '.$where['operator'].' '.$value;
return $this->wrap($where['column']).'::text '.$where['operator'].' '.$value;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,34 @@ public function testWhereTimePostgres()
$this->assertEquals([0 => '22:00'], $builder->getBindings());
}

public function testWhereLikePostgres()
{
$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'like', '1');
$this->assertEquals('select * from "users" where "id"::text like ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'LIKE', '1');
$this->assertEquals('select * from "users" where "id"::text LIKE ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'ilike', '1');
$this->assertEquals('select * from "users" where "id"::text ilike ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'not like', '1');
$this->assertEquals('select * from "users" where "id"::text not like ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'not ilike', '1');
$this->assertEquals('select * from "users" where "id"::text not ilike ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());
}

public function testWhereDateSqlite()
{
$builder = $this->getSQLiteBuilder();
Expand Down

0 comments on commit 9c44092

Please sign in to comment.