Skip to content

Commit

Permalink
fix: Resolve HasRootProblem behavior review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Oct 8, 2024
1 parent faeb8b4 commit 37daae8
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions library/Icingadb/Model/Behavior/HasRootProblem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,35 @@ public function setQuery(Query $query): self
return $this;
}

public function rewriteColumn($column, ?string $relation = null)
public function rewriteColumn($column, ?string $relation = null): ?AliasedExpression
{
if ($this->isSelectableColumn($column)) {
$path = 'from.dependency_node';
$subQueryRelation = $relation !== null ? $relation . $path : $path;
$subQuery = $this->query->createSubQuery(new DependencyEdge(), $subQueryRelation)
->limit(1)
->columns([new Expression('1')]);
if (! $this->isSelectableColumn($column)) {
return null;
}

$subQuery->getSelectBase()->join(
['root_dependency' => 'dependency'],
[$subQuery->getResolver()->getAlias($subQuery->getModel()) . '.dependency_id = root_dependency.id']
)->join(
['root_dependency_state' => 'dependency_state'],
['root_dependency.id = root_dependency_state.dependency_id']
)->where(new Expression("root_dependency_state.failed = 'y'"));
$path = 'from.dependency_node';
$subQueryRelation = $relation !== null ? $relation . $path : $path;
$subQuery = $this->query->createSubQuery(new DependencyEdge(), $subQueryRelation)
->limit(1)
->columns([new Expression('1')]);

$column = $relation !== null ? str_replace('.', '_', $relation) . "_$column" : $column;
$subQuery->getSelectBase()->join(
['root_dependency' => 'dependency'],
[$subQuery->getResolver()->getAlias($subQuery->getModel()) . '.dependency_id = root_dependency.id']
)->join(
['root_dependency_state' => 'dependency_state'],
['root_dependency.id = root_dependency_state.dependency_id']
)->where(new Expression("root_dependency_state.failed = 'y'"));

$alias = $this->query->getDb()->quoteIdentifier([$column]);
$column = $relation !== null ? str_replace('.', '_', $relation) . "_$column" : $column;

list($select, $values) = $this->query->getDb()
->getQueryBuilder()
->assembleSelect($subQuery->assembleSelect());
$alias = $this->query->getDb()->quoteIdentifier([$column]);

return new AliasedExpression($alias, "($select)", null, ...$values);
}
list($select, $values) = $this->query->getDb()
->getQueryBuilder()
->assembleSelect($subQuery->assembleSelect());

return new AliasedExpression($alias, "($select)", null, ...$values);
}

public function isSelectableColumn(string $name): bool
Expand Down

0 comments on commit 37daae8

Please sign in to comment.