DX: Add a DQL AGE function to calculate age between two dates

This commit is contained in:
Julien Fastré 2022-10-12 11:03:05 +02:00
parent 6f1ec00d3a
commit 3a8640db43
3 changed files with 135 additions and 0 deletions

View File

@ -22,6 +22,7 @@ use Chill\MainBundle\Controller\UserController;
use Chill\MainBundle\Controller\UserJobApiController; use Chill\MainBundle\Controller\UserJobApiController;
use Chill\MainBundle\Controller\UserJobController; use Chill\MainBundle\Controller\UserJobController;
use Chill\MainBundle\DependencyInjection\Widget\Factory\WidgetFactoryInterface; use Chill\MainBundle\DependencyInjection\Widget\Factory\WidgetFactoryInterface;
use Chill\MainBundle\Doctrine\DQL\Age;
use Chill\MainBundle\Doctrine\DQL\Extract; use Chill\MainBundle\Doctrine\DQL\Extract;
use Chill\MainBundle\Doctrine\DQL\GetJsonFieldByKey; use Chill\MainBundle\Doctrine\DQL\GetJsonFieldByKey;
use Chill\MainBundle\Doctrine\DQL\JsonAggregate; use Chill\MainBundle\Doctrine\DQL\JsonAggregate;
@ -243,6 +244,7 @@ class ChillMainExtension extends Extension implements
'datetime_functions' => [ 'datetime_functions' => [
'EXTRACT' => Extract::class, 'EXTRACT' => Extract::class,
'TO_CHAR' => ToChar::class, 'TO_CHAR' => ToChar::class,
'AGE' => Age::class,
], ],
], ],
'hydrators' => [ 'hydrators' => [

View File

@ -0,0 +1,54 @@
<?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\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class Age extends FunctionNode
{
private $value1;
private $value2;
public function getSql(SqlWalker $sqlWalker)
{
if (null !== $this->value2) {
return sprintf(
'AGE(%s, %s)',
$this->value1->dispatch($sqlWalker),
$this->value2->dispatch($sqlWalker)
);
}
return sprintf(
'AGE(%s)',
$this->value1->dispatch($sqlWalker),
);
}
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->value1 = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->value2 = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}

View File

@ -0,0 +1,79 @@
<?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\Address;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
* @coversNothing
*/
final class AgeTest extends KernelTestCase
{
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::$container->get(EntityManagerInterface::class);
}
public function generateQueries(): iterable
{
yield [
'SELECT AGE(a.validFrom, a.validTo) FROM ' . Address::class . ' a',
[],
];
yield [
'SELECT AGE(:date0, :date1) FROM ' . Address::class . ' a',
[
'date0' => new DateTimeImmutable('now'),
'date1' => new DateTimeImmutable('2020-01-01'),
],
];
yield [
'SELECT AGE(a.validFrom, :date1) FROM ' . Address::class . ' a',
[
'date1' => new DateTimeImmutable('now'),
],
];
yield [
'SELECT AGE(:date0, a.validFrom) FROM ' . Address::class . ' a',
[
'date0' => new DateTimeImmutable('now'),
],
];
}
/**
* @dataProvider generateQueries
*/
public function testWorking(string $dql, array $args)
{
$dql = $this->entityManager->createQuery($dql)->setMaxResults(3);
foreach ($args as $key => $value) {
$dql->setParameter($key, $value);
}
$results = $dql->getResult();
$this->assertIsArray($results);
}
}