mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Create a custom dql function to build json object (JSON_BUILD_OBJECT)
This commit is contained in:
parent
978db5a5c5
commit
21524f052e
@ -33,6 +33,7 @@ use Chill\MainBundle\Doctrine\DQL\Greatest;
|
|||||||
use Chill\MainBundle\Doctrine\DQL\JsonAggregate;
|
use Chill\MainBundle\Doctrine\DQL\JsonAggregate;
|
||||||
use Chill\MainBundle\Doctrine\DQL\JsonbArrayLength;
|
use Chill\MainBundle\Doctrine\DQL\JsonbArrayLength;
|
||||||
use Chill\MainBundle\Doctrine\DQL\JsonbExistsInArray;
|
use Chill\MainBundle\Doctrine\DQL\JsonbExistsInArray;
|
||||||
|
use Chill\MainBundle\Doctrine\DQL\JsonBuildObject;
|
||||||
use Chill\MainBundle\Doctrine\DQL\JsonExtract;
|
use Chill\MainBundle\Doctrine\DQL\JsonExtract;
|
||||||
use Chill\MainBundle\Doctrine\DQL\Least;
|
use Chill\MainBundle\Doctrine\DQL\Least;
|
||||||
use Chill\MainBundle\Doctrine\DQL\OverlapsI;
|
use Chill\MainBundle\Doctrine\DQL\OverlapsI;
|
||||||
@ -255,6 +256,7 @@ class ChillMainExtension extends Extension implements
|
|||||||
'AGGREGATE' => JsonAggregate::class,
|
'AGGREGATE' => JsonAggregate::class,
|
||||||
'REPLACE' => Replace::class,
|
'REPLACE' => Replace::class,
|
||||||
'JSON_EXTRACT' => JsonExtract::class,
|
'JSON_EXTRACT' => JsonExtract::class,
|
||||||
|
'JSON_BUILD_OBJECT' => JsonBuildObject::class,
|
||||||
],
|
],
|
||||||
'numeric_functions' => [
|
'numeric_functions' => [
|
||||||
'JSONB_EXISTS_IN_ARRAY' => JsonbExistsInArray::class,
|
'JSONB_EXISTS_IN_ARRAY' => JsonbExistsInArray::class,
|
||||||
|
53
src/Bundle/ChillMainBundle/Doctrine/DQL/JsonBuildObject.php
Normal file
53
src/Bundle/ChillMainBundle/Doctrine/DQL/JsonBuildObject.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?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\AST\Node;
|
||||||
|
use Doctrine\ORM\Query\Lexer;
|
||||||
|
use Doctrine\ORM\Query\Parser;
|
||||||
|
use Doctrine\ORM\Query\SqlWalker;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an aggregation of values in a json representation, as a string.
|
||||||
|
*
|
||||||
|
* Internally, this function use the postgresql `jsonb_agg` function. Using
|
||||||
|
* json allow to aggregate data from different types without have to cast them.
|
||||||
|
*/
|
||||||
|
class JsonBuildObject extends FunctionNode
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array|Node[]
|
||||||
|
*/
|
||||||
|
private array $exprs = [];
|
||||||
|
|
||||||
|
public function getSql(SqlWalker $sqlWalker)
|
||||||
|
{
|
||||||
|
return 'JSONB_BUILD_OBJECT(' . implode(', ', array_map(static fn (Node $expr) => $expr->dispatch($sqlWalker), $this->exprs)) . ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parse(Parser $parser)
|
||||||
|
{
|
||||||
|
$lexer = $parser->getLexer();
|
||||||
|
$parser->match(Lexer::T_IDENTIFIER);
|
||||||
|
$parser->match(Lexer::T_OPEN_PARENTHESIS);
|
||||||
|
|
||||||
|
$this->exprs[] = $parser->ArithmeticPrimary();
|
||||||
|
|
||||||
|
while (Lexer::T_COMMA === $lexer->lookahead['type']) {
|
||||||
|
$parser->match(Lexer::T_COMMA);
|
||||||
|
$this->exprs[] = $parser->ArithmeticPrimary();
|
||||||
|
}
|
||||||
|
|
||||||
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
<?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 Doctrine\DBAL\Types\Types;
|
||||||
|
use Doctrine\ORM\AbstractQuery;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
|
class JsonBuildObjectTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
private EntityManagerInterface $entityManager;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
self::bootKernel();
|
||||||
|
$this->entityManager = self::$container->get(EntityManagerInterface::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideQueries
|
||||||
|
*/
|
||||||
|
public function testQuery(string $sql, array $params, array $paramType): void
|
||||||
|
{
|
||||||
|
$query = $this->entityManager->createQuery($sql);
|
||||||
|
|
||||||
|
foreach ($params as $k => $v) {
|
||||||
|
$query->setParameter($k, $v, $paramType[$k]);
|
||||||
|
}
|
||||||
|
$query->setMaxResults(1);
|
||||||
|
|
||||||
|
$result = $query->getResult(AbstractQuery::HYDRATE_ARRAY);
|
||||||
|
|
||||||
|
self::assertIsArray($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideQueries(): iterable
|
||||||
|
{
|
||||||
|
yield ["SELECT JSON_BUILD_OBJECT(1, 2, 3, 4) FROM " . Address::class . " a", [], []];
|
||||||
|
yield ["SELECT JSON_BUILD_OBJECT('st', a.street, 'sn', a.streetNumber) FROM " . Address::class . ' a', [], []];
|
||||||
|
// next query make the test fails. But we do not need it for now.
|
||||||
|
//yield ["SELECT JSON_BUILD_OBJECT(a.street, :param), LOWER(:param) FROM " . Address::class . " a", ['param' => 1], ['param' => Types::INTEGER]];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -24,6 +24,7 @@ use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
|
|||||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
|
||||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkGoal;
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkGoal;
|
||||||
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkReferrerHistory;
|
||||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\UserHistory;
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\UserHistory;
|
||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\PersonBundle\Entity\Person;
|
||||||
use Chill\PersonBundle\Entity\Person\PersonCenterHistory;
|
use Chill\PersonBundle\Entity\Person\PersonCenterHistory;
|
||||||
@ -69,6 +70,7 @@ class ListAccompanyingPeriodWork implements ListInterface, GroupedExportInterfac
|
|||||||
'personsName',
|
'personsName',
|
||||||
'thirdParties',
|
'thirdParties',
|
||||||
'handlingThierParty',
|
'handlingThierParty',
|
||||||
|
//'acpwReferrers',
|
||||||
'referrers',
|
'referrers',
|
||||||
'createdAt',
|
'createdAt',
|
||||||
'createdBy',
|
'createdBy',
|
||||||
@ -76,7 +78,20 @@ class ListAccompanyingPeriodWork implements ListInterface, GroupedExportInterfac
|
|||||||
'updatedBy',
|
'updatedBy',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(private readonly EntityManagerInterface $entityManager, private readonly DateTimeHelper $dateTimeHelper, private readonly UserHelper $userHelper, private readonly LabelPersonHelper $personHelper, private readonly LabelThirdPartyHelper $thirdPartyHelper, private readonly TranslatableStringExportLabelHelper $translatableStringExportLabelHelper, private readonly SocialIssueRender $socialIssueRender, private readonly SocialIssueRepository $socialIssueRepository, private readonly SocialActionRender $socialActionRender, private readonly RollingDateConverterInterface $rollingDateConverter, private readonly AggregateStringHelper $aggregateStringHelper, private readonly SocialActionRepository $socialActionRepository) {}
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $entityManager,
|
||||||
|
private readonly DateTimeHelper $dateTimeHelper,
|
||||||
|
private readonly UserHelper $userHelper,
|
||||||
|
private readonly LabelPersonHelper $personHelper,
|
||||||
|
private readonly LabelThirdPartyHelper $thirdPartyHelper,
|
||||||
|
private readonly TranslatableStringExportLabelHelper $translatableStringExportLabelHelper,
|
||||||
|
private readonly SocialIssueRender $socialIssueRender,
|
||||||
|
private readonly SocialIssueRepository $socialIssueRepository,
|
||||||
|
private readonly SocialActionRender $socialActionRender,
|
||||||
|
private readonly RollingDateConverterInterface $rollingDateConverter,
|
||||||
|
private readonly AggregateStringHelper $aggregateStringHelper,
|
||||||
|
private readonly SocialActionRepository $socialActionRepository
|
||||||
|
) {}
|
||||||
|
|
||||||
public function buildForm(FormBuilderInterface $builder)
|
public function buildForm(FormBuilderInterface $builder)
|
||||||
{
|
{
|
||||||
@ -141,6 +156,7 @@ class ListAccompanyingPeriodWork implements ListInterface, GroupedExportInterfac
|
|||||||
},
|
},
|
||||||
'createdBy', 'updatedBy', 'acp_user' => $this->userHelper->getLabel($key, $values, 'export.list.acpw.' . $key),
|
'createdBy', 'updatedBy', 'acp_user' => $this->userHelper->getLabel($key, $values, 'export.list.acpw.' . $key),
|
||||||
'referrers' => $this->userHelper->getLabel($key, $values, 'export.list.acpw.' . $key),
|
'referrers' => $this->userHelper->getLabel($key, $values, 'export.list.acpw.' . $key),
|
||||||
|
//'acpwReferrers' => $this->userHelper->getLabelMulti($key, $values, 'export.list.acpw.' . $key),
|
||||||
'personsName' => $this->personHelper->getLabelMulti($key, $values, 'export.list.acpw.' . $key),
|
'personsName' => $this->personHelper->getLabelMulti($key, $values, 'export.list.acpw.' . $key),
|
||||||
'handlingThierParty' => $this->thirdPartyHelper->getLabel($key, $values, 'export.list.acpw.' . $key),
|
'handlingThierParty' => $this->thirdPartyHelper->getLabel($key, $values, 'export.list.acpw.' . $key),
|
||||||
'thirdParties' => $this->thirdPartyHelper->getLabelMulti($key, $values, 'export.list.acpw.' . $key),
|
'thirdParties' => $this->thirdPartyHelper->getLabelMulti($key, $values, 'export.list.acpw.' . $key),
|
||||||
@ -167,7 +183,7 @@ class ListAccompanyingPeriodWork implements ListInterface, GroupedExportInterfac
|
|||||||
|
|
||||||
public function getResult($query, $data)
|
public function getResult($query, $data)
|
||||||
{
|
{
|
||||||
return $query->getQuery()->getResult(AbstractQuery::HYDRATE_SCALAR);
|
return dump($query->getQuery()->getResult(AbstractQuery::HYDRATE_SCALAR));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTitle(): string
|
public function getTitle(): string
|
||||||
@ -269,9 +285,20 @@ class ListAccompanyingPeriodWork implements ListInterface, GroupedExportInterfac
|
|||||||
|
|
||||||
// referrers => at date XXXX
|
// referrers => at date XXXX
|
||||||
$qb
|
$qb
|
||||||
->addSelect('(SELECT IDENTITY(history.user) FROM ' . UserHistory::class . ' history ' .
|
->addSelect('(SELECT JSON_BUILD_OBJECT(\'uid\', IDENTITY(history.user), \'d\', history.startDate) FROM ' . UserHistory::class . ' history ' .
|
||||||
'WHERE history.accompanyingPeriod = acp AND history.startDate <= :calcDate AND (history.endDate IS NULL OR history.endDate > :calcDate)) AS referrers');
|
'WHERE history.accompanyingPeriod = acp AND history.startDate <= :calcDate AND (history.endDate IS NULL OR history.endDate > :calcDate)) AS referrers');
|
||||||
|
|
||||||
|
/*
|
||||||
|
// acpwReferrers at date XXX
|
||||||
|
$qb
|
||||||
|
->addSelect('(
|
||||||
|
SELECT IDENTITY(acpw_ref_history.accompanyingPeriodWork) AS acpw_ref_history_id,
|
||||||
|
JSON_BUILD_OBJECT(\'uid\', IDENTITY(acpw_ref_history.user), \'d\', acpw_ref_history.startDate)
|
||||||
|
FROM ' . AccompanyingPeriodWorkReferrerHistory::class . ' acpw_ref_history ' .
|
||||||
|
'WHERE acpw_ref_history.accompanyingPeriodWork = acpw AND acpw_ref_history.startDate <= :calcDate AND (acpw_ref_history.endDate IS NULL or acpw_ref_history.endDate > :calcDate) GROUP BY acpw_ref_history_id) AS acpwReferrers'
|
||||||
|
);
|
||||||
|
*/
|
||||||
|
|
||||||
// thirdparties
|
// thirdparties
|
||||||
$qb
|
$qb
|
||||||
->addSelect('(SELECT AGGREGATE(tp.id) FROM ' . ThirdParty::class . ' tp '
|
->addSelect('(SELECT AGGREGATE(tp.id) FROM ' . ThirdParty::class . ' tp '
|
||||||
|
Loading…
x
Reference in New Issue
Block a user