Skip to content

Commit

Permalink
IBX-8019: Replaced multiple JOIN statements with SELECT ... IN qu…
Browse files Browse the repository at this point in the history
…ery in ObjectStateId Criterion
  • Loading branch information
webhdx committed May 10, 2024
1 parent b5fe9ad commit f468ba0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 51 deletions.
41 changes: 1 addition & 40 deletions eZ/Publish/Core/Limitation/ObjectStateLimitationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}

0 comments on commit f468ba0

Please sign in to comment.