Skip to content

Commit

Permalink
Transform custom variable filters as late as possible
Browse files Browse the repository at this point in the history
fixes #865
  • Loading branch information
nilmerg committed Mar 11, 2024
1 parent 274b903 commit 976a53e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
33 changes: 28 additions & 5 deletions library/Icingadb/Model/Behavior/FlattenedObjectVars.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ipl\Orm\Contract\QueryAwareBehavior;
use ipl\Orm\Contract\RewriteColumnBehavior;
use ipl\Orm\Query;
use ipl\Stdlib\Data;
use ipl\Stdlib\Filter;

class FlattenedObjectVars implements RewriteColumnBehavior, QueryAwareBehavior
Expand All @@ -32,11 +33,33 @@ public function rewriteCondition(Filter\Condition $condition, $relation = null)
$column = $condition->metaData()->get('columnName');
if ($column !== null) {
$relation = substr($relation, 0, -5) . 'customvar_flat.';
$nameFilter = Filter::like($relation . 'flatname', $column);
$class = get_class($condition);
$valueFilter = new $class($relation . 'flatvalue', $condition->getValue());

return Filter::all($nameFilter, $valueFilter);
$condition
->setColumn($relation . $column)
->metaData()
->set('requiresTransformation', true)
->set('columnPath', $relation . $column)
->set('relationPath', substr($relation, 0, -1));

// The ORM's FilterProcessor only optimizes filter conditions that are in the same level (chain).
// Previously, this behavior transformed a single condition to an ALL chain and hence the semantics
// of the level changed, since the FilterProcessor interpreted the conditions separately from there on.
// To not change the semantics of the condition it is required to delay the transformation of the condition
// until the subquery is created. Though, since the FilterProcessor only applies behaviors once, this
// hack is required. (The entire filter, metadata and optimization is total garbage.)
$oldMetaData = $condition->metaData();
$reflection = new \ReflectionClass($condition);
$reflection->getProperty('metaData')->setAccessible(true);
$reflection->getProperty('metaData')->setValue($condition, new class () extends Data {
public function set($name, $value)
{
if ($name === 'behaviorsApplied') {
return $this;
}

return parent::set($name, $value);
}
});
$condition->metaData()->merge($oldMetaData);
}
}

Expand Down
15 changes: 15 additions & 0 deletions library/Icingadb/Model/CustomvarFlat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Contract\RewriteFilterBehavior;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;
use ipl\Stdlib\Filter;
use ipl\Stdlib\Filter\Condition;
use Traversable;

class CustomvarFlat extends Model
Expand Down Expand Up @@ -42,6 +45,18 @@ public function createBehaviors(Behaviors $behaviors)
'customvar_id',
'flatname_checksum'
]));
$behaviors->add(new class implements RewriteFilterBehavior {
public function rewriteCondition(Condition $condition, $relation = null)
{
if ($condition->metaData()->has('requiresTransformation')) {
$nameFilter = Filter::like($relation . 'flatname', $condition->metaData()->get('columnName'));

Check failure on line 52 in library/Icingadb/Model/CustomvarFlat.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.3 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::like() expects array<string>|string, mixed given.

Check failure on line 52 in library/Icingadb/Model/CustomvarFlat.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::like() expects array<string>|string, mixed given.

Check failure on line 52 in library/Icingadb/Model/CustomvarFlat.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::like() expects array<string>|string, mixed given.

Check failure on line 52 in library/Icingadb/Model/CustomvarFlat.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::like() expects array<string>|string, mixed given.

Check failure on line 52 in library/Icingadb/Model/CustomvarFlat.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::like() expects array<string>|string, mixed given.

Check failure on line 52 in library/Icingadb/Model/CustomvarFlat.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::like() expects array<string>|string, mixed given.

Check failure on line 52 in library/Icingadb/Model/CustomvarFlat.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::like() expects array<string>|string, mixed given.
$class = get_class($condition);
$valueFilter = new $class($relation . 'flatvalue', $condition->getValue());

return Filter::all($nameFilter, $valueFilter);
}
}
});
}

public function createRelations(Relations $relations)
Expand Down

0 comments on commit 976a53e

Please sign in to comment.