Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Postgres ilike method for case-insensitive like comparison #404

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ doctrine:
greatest: DoctrineExtensions\Query\Postgresql\Greatest
least: DoctrineExtensions\Query\Postgresql\Least
regexp_replace: DoctrineExtensions\Query\Postgresql\RegexpReplace
ilike: DoctrineExtensions\Query\Postgresql\Ilike
45 changes: 45 additions & 0 deletions src/Query/Postgresql/Ilike.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace DoctrineExtensions\Query\Postgresql;

use Doctrine\ORM\Query\AST\ASTException;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;

class Ilike extends FunctionNode
{
/** @var Node */
protected $field;
/** @var Node */
protected $query;

/**
* @param Parser $parser
*
* @throws QueryException
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->field = $parser->StringExpression();
$parser->match(Lexer::T_COMMA);
$this->query = $parser->StringExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

/**
* @param SqlWalker $sqlWalker
*
* @return string
* @throws ASTException
*/
public function getSql(SqlWalker $sqlWalker)
{
return '(' . $this->field->dispatch($sqlWalker) . ' ILIKE ' . $this->query->dispatch($sqlWalker) . ')';
}
}
16 changes: 16 additions & 0 deletions tests/Query/Postgresql/IlikeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace DoctrineExtensions\Tests\Query\Postgresql;

class IlikeTest extends \DoctrineExtensions\Tests\Query\PostgresqlTestCase
{
public function testILike()
{
$dql = "SELECT p FROM DoctrineExtensions\Tests\Entities\Product p WHERE ILIKE(p.name, :searchName) = true";
$q = $this->entityManager->createQuery($dql);
$q->setParameter('searchName', '%search term%');
$sql = 'SELECT p0_.id AS id_0, p0_.name AS name_1, p0_.created AS created_2, p0_.price AS price_3, p0_.weight AS weight_4 FROM Product p0_ WHERE (p0_.name ILIKE ?) = 1';

$this->assertEquals($sql, $q->getSql());
}
}