mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-03 20:49:41 +00:00
Add JsonbContains custom DQL function and corresponding test
- Introduced `JsonbContains` class to enable the usage of PostgreSQL's JSONB containment operator in DQL queries. - Created `JsonbContainsTest` to validate the functionality of the custom DQL function.
This commit is contained in:
@@ -38,6 +38,7 @@ use Chill\MainBundle\Doctrine\DQL\GetJsonFieldByKey;
|
||||
use Chill\MainBundle\Doctrine\DQL\Greatest;
|
||||
use Chill\MainBundle\Doctrine\DQL\JsonAggregate;
|
||||
use Chill\MainBundle\Doctrine\DQL\JsonbArrayLength;
|
||||
use Chill\MainBundle\Doctrine\DQL\JsonbContains;
|
||||
use Chill\MainBundle\Doctrine\DQL\JsonbExistsInArray;
|
||||
use Chill\MainBundle\Doctrine\DQL\JsonBuildObject;
|
||||
use Chill\MainBundle\Doctrine\DQL\JsonExtract;
|
||||
@@ -283,6 +284,7 @@ class ChillMainExtension extends Extension implements
|
||||
'STRICT_WORD_SIMILARITY_OPS' => StrictWordSimilarityOPS::class,
|
||||
'ST_CONTAINS' => STContains::class,
|
||||
'JSONB_ARRAY_LENGTH' => JsonbArrayLength::class,
|
||||
'JSONB_CONTAINS' => JsonbContains::class,
|
||||
'ST_X' => STX::class,
|
||||
'ST_Y' => STY::class,
|
||||
'GREATEST' => Greatest::class,
|
||||
|
||||
42
src/Bundle/ChillMainBundle/Doctrine/DQL/JsonbContains.php
Normal file
42
src/Bundle/ChillMainBundle/Doctrine/DQL/JsonbContains.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Doctrine\DQL;
|
||||
|
||||
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
|
||||
use Doctrine\ORM\Query\Parser;
|
||||
use Doctrine\ORM\Query\SqlWalker;
|
||||
|
||||
class JsonbContains extends FunctionNode
|
||||
{
|
||||
private ?\Doctrine\ORM\Query\AST\Node $expr1 = null;
|
||||
|
||||
private ?\Doctrine\ORM\Query\AST\InputParameter $expr2 = null;
|
||||
|
||||
public function getSql(SqlWalker $sqlWalker): string
|
||||
{
|
||||
return sprintf(
|
||||
'%s @> %s',
|
||||
$this->expr1->dispatch($sqlWalker),
|
||||
$this->expr2->dispatch($sqlWalker)
|
||||
);
|
||||
}
|
||||
|
||||
public function parse(Parser $parser): void
|
||||
{
|
||||
$parser->match(\Doctrine\ORM\Query\TokenType::T_IDENTIFIER);
|
||||
$parser->match(\Doctrine\ORM\Query\TokenType::T_OPEN_PARENTHESIS);
|
||||
$this->expr1 = $parser->StringPrimary();
|
||||
$parser->match(\Doctrine\ORM\Query\TokenType::T_COMMA);
|
||||
$this->expr2 = $parser->InputParameter();
|
||||
$parser->match(\Doctrine\ORM\Query\TokenType::T_CLOSE_PARENTHESIS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Tests\Doctrine\DQL;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class JsonbContainsTest extends KernelTestCase
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
$this->em = self::getContainer()->get(EntityManagerInterface::class);
|
||||
}
|
||||
|
||||
public function testDQLFunctionWorks()
|
||||
{
|
||||
$result = $this->em
|
||||
->createQuery('SELECT JSONB_CONTAINS(u.attributes, :param) FROM '.User::class.' u')
|
||||
->setParameter('param', ['fr'], Types::JSON)
|
||||
->getResult();
|
||||
|
||||
$this->assertIsArray($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user