From d0c615317aafba5385ae28d14e021eed14d7ef0e Mon Sep 17 00:00:00 2001 From: Michael Rutz Date: Wed, 19 Sep 2018 10:12:31 +0200 Subject: [PATCH] make any column searchable in postgres --- .../Query/Grammars/PostgresGrammar.php | 19 ++++++--------- tests/Database/DatabaseQueryBuilderTest.php | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php index 62e93df45b4e..9aa6483e8226 100755 --- a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php @@ -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) @@ -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; } /** diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index d1ee2db3184e..a2685d691387 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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();