Skip to content

Commit

Permalink
Short-circuit when there are no additional conditions added.
Browse files Browse the repository at this point in the history
No need to add in the existence checks where they do not need to exist.
  • Loading branch information
adam-vessey committed Mar 9, 2024
1 parent 9e33238 commit da3ad07
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Access/QueryTagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function tagQuery(SelectInterface $query) : void {
$null_query->addExpression(1, 'lut_null_existance');
$null_query->condition($null_condition = $null_query->orConditionGroup());
$query->addMetaData('islandora_hierarchical_access_tagged_null_alias', $lut_null_alias);
$query->addMetaData('islandora_hierarchical_access_tagged_null_query', $null_query);

// Test that where we _are_ making assertions, things are left in the LUT.
$existence = $this->database->select(LUTGeneratorInterface::TABLE_NAME, $lut_exist_alias);
Expand All @@ -95,14 +96,9 @@ public function tagQuery(SelectInterface $query) : void {
$query
->addMetaData('islandora_hierarchical_access_tagged_null_condition', $null_condition)
->addMetaData('islandora_hierarchical_access_tagged_existence_condition', $existence_condition);

$query->condition(
$query->orConditionGroup()
->notExists($null_query)
->exists($existence)
);
}
else {
$null_query = $query->getMetaData('islandora_hierarchical_access_tagged_null_query');
$null_condition = $query->getMetaData('islandora_hierarchical_access_tagged_null_condition');
$existence_condition = $query->getMetaData('islandora_hierarchical_access_tagged_existence_condition');
}
Expand All @@ -129,6 +125,8 @@ public function tagQuery(SelectInterface $query) : void {
'!field' => "{$lut_exist_alias}.{$lut_column}",
]));

$before_tagging = clone $existence;

$this->eventDispatcher->dispatch(new Event($type, $query));

// We need to allow arbitrary altered queries for parent entities to affect
Expand Down Expand Up @@ -156,16 +154,26 @@ public function tagQuery(SelectInterface $query) : void {
->addMetaData('base_table', $parent)
->where("{$lut_exist_alias}.{$parent_lut_column} = {$parent_alias}.{$parent_key}");

$before = "{$entity_select}";
$before_subquery = clone $entity_select;
$this->moduleHandler->alter("query_{$parent}_access", $entity_select);
if ($before !== "{$entity_select}") {
if ("{$before_subquery}" !== "{$entity_select}") {
// If the query was altered, let us apply its effects;
// otherwise, the base query makes no assertion for which we have not
// already accounted above.
$existence_condition->exists($entity_select);
}
$this->eventDispatcher->dispatch(new Event($parent, $query));
}

// If there is no change, then we do not need to add any additional
// conditions.
if ("{$before_tagging}" !== "{$existence}") {
$query->condition(
$query->orConditionGroup()
->notExists($null_query)
->exists($existence)
);
}
}

}

0 comments on commit da3ad07

Please sign in to comment.