From f468ba089e342b1d474914d559d9acc523d90ab3 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Fri, 10 May 2024 08:16:59 +0200 Subject: [PATCH] IBX-8019: Replaced multiple `JOIN` statements with `SELECT ... IN` query in ObjectStateId Criterion --- .../Limitation/ObjectStateLimitationType.php | 41 +------------------ .../Content/ObjectStateIdQueryBuilder.php | 24 ++++++----- 2 files changed, 14 insertions(+), 51 deletions(-) diff --git a/eZ/Publish/Core/Limitation/ObjectStateLimitationType.php b/eZ/Publish/Core/Limitation/ObjectStateLimitationType.php index 28aa2fa218..c13ac659d9 100644 --- a/eZ/Publish/Core/Limitation/ObjectStateLimitationType.php +++ b/eZ/Publish/Core/Limitation/ObjectStateLimitationType.php @@ -218,46 +218,7 @@ public function getCriterion(APILimitationValue $value, APIUserReference $curren return new Criterion\ObjectStateId($value->limitationValues[0]); } - $groupedLimitationValues = $this->groupLimitationValues($value->limitationValues); - - if (count($groupedLimitationValues) === 1) { - // one group, several limitation values: IN operation - return new Criterion\ObjectStateId($groupedLimitationValues[0]); - } - - // limitations from different groups require logical AND between them - $criterions = []; - foreach ($groupedLimitationValues as $limitationGroup) { - $criterions[] = new Criterion\ObjectStateId($limitationGroup); - } - - return new Criterion\LogicalAnd($criterions); - } - - /** - * Groups limitation values by the State Group. - * - * @param string[] $limitationValues - * - * @return int[][] - */ - private function groupLimitationValues(array $limitationValues) - { - $objectStateHandler = $this->persistence->objectStateHandler(); - $stateGroups = $objectStateHandler->loadAllGroups(); - $groupedLimitationValues = []; - foreach ($stateGroups as $stateGroup) { - $states = $objectStateHandler->loadObjectStates($stateGroup->id); - $stateIds = array_map(static function ($state) { - return $state->id; - }, $states); - $limitationValuesGroup = array_intersect($stateIds, $limitationValues); - if (!empty($limitationValuesGroup)) { - $groupedLimitationValues[] = array_values($limitationValuesGroup); - } - } - - return $groupedLimitationValues; + return new Criterion\ObjectStateId($value->limitationValues); } /** diff --git a/eZ/Publish/Core/Persistence/Legacy/Filter/CriterionQueryBuilder/Content/ObjectStateIdQueryBuilder.php b/eZ/Publish/Core/Persistence/Legacy/Filter/CriterionQueryBuilder/Content/ObjectStateIdQueryBuilder.php index a189f42847..60466a30bd 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Filter/CriterionQueryBuilder/Content/ObjectStateIdQueryBuilder.php +++ b/eZ/Publish/Core/Persistence/Legacy/Filter/CriterionQueryBuilder/Content/ObjectStateIdQueryBuilder.php @@ -29,22 +29,24 @@ public function buildQueryConstraint( FilteringQueryBuilder $queryBuilder, FilteringCriterion $criterion ): ?string { - $tableAlias = uniqid('osl_'); + $value = (array)$criterion->value; - /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion\ObjectStateId $criterion */ - $queryBuilder - ->join( - 'content', + $subSelect = $queryBuilder->getConnection()->createQueryBuilder() + ->select( + 'osl.contentobject_id' + )->from( Gateway::OBJECT_STATE_LINK_TABLE, - $tableAlias, - 'content.id = ' . $tableAlias . '.contentobject_id', + 'osl' + )->andWhere( + $queryBuilder->expr()->in( + 'osl.contentobject_state_id', + $queryBuilder->createNamedParameter($value, Connection::PARAM_INT_ARRAY) + ) ); - $value = (array)$criterion->value; - return $queryBuilder->expr()->in( - $tableAlias . '.contentobject_state_id', - $queryBuilder->createNamedParameter($value, Connection::PARAM_INT_ARRAY) + 'content.id', + $subSelect->getSQL() ); } }