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 d0c6153
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ 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)
Expand All @@ -37,25 +36,21 @@ protected function whereBasic(Builder $query, $where)
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
23 changes: 23 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,29 @@ 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', '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 d0c6153

Please sign in to comment.