Skip to content

Commit 83af168

Browse files
committed
FlattenedObjectVars: Force processing to finish early
fixes #1001
1 parent 06da58c commit 83af168

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

library/Icingadb/Model/Behavior/FlattenedObjectVars.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use ipl\Orm\Contract\QueryAwareBehavior;
1212
use ipl\Orm\Contract\RewriteColumnBehavior;
1313
use ipl\Orm\Query;
14-
use ipl\Stdlib\Data;
1514
use ipl\Stdlib\Filter;
1615

1716
class FlattenedObjectVars implements RewriteColumnBehavior, QueryAwareBehavior
@@ -42,22 +41,11 @@ public function rewriteCondition(Filter\Condition $condition, $relation = null)
4241
// Previously, this behavior transformed a single condition to an ALL chain and hence the semantics
4342
// of the level changed, since the FilterProcessor interpreted the conditions separately from there on.
4443
// To not change the semantics of the condition it is required to delay the transformation of the condition
45-
// until the subquery is created. Though, since the FilterProcessor only applies behaviors once, this
46-
// hack is required. (The entire filter, metadata and optimization is total garbage.)
47-
$oldMetaData = $condition->metaData();
48-
$metaDataProperty = (new \ReflectionClass($condition))->getProperty('metaData');
49-
$metaDataProperty->setAccessible(true);
50-
$metaDataProperty->setValue($condition, new class () extends Data {
51-
public function set($name, $value)
52-
{
53-
if ($name === 'behaviorsApplied') {
54-
return $this;
55-
}
56-
57-
return parent::set($name, $value);
58-
}
59-
});
60-
$condition->metaData()->merge($oldMetaData);
44+
// until the subquery is created. Though, since this is about custom variables, and such can contain dots,
45+
// the FilterProcessor then continues traversing the parts of the column's path, which then would include
46+
// the dot-separated parts of the custom variable name. To prevent this, we have to signal that what we
47+
// return a replacement here, that should be used as-is and not processed further.
48+
$condition->metaData()->set('forceResolved', true);
6149

6250
// But to make it even worse: If we do that, (not transforming the condition) the FilterProcessor sees
6351
// multiple conditions as targeting different columns, as it doesn't know that the *columns* are in fact
@@ -66,6 +54,8 @@ public function set($name, $value)
6654
// the condition refer to a different column, which is totally irrelevant, but since it's always the same
6755
// column, the FilterProcessor won't attempt to combine the conditions. The literal icing on the cake.
6856
$condition->setColumn('always_the_same_but_totally_irrelevant');
57+
58+
return $condition;
6959
}
7060
}
7161

test/php/library/Icingadb/Model/Behavior/FlattenedObjectVarsTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,23 @@ public function testSingleUnequalCondition()
109109
$query = Host::on($this->connection)
110110
->columns('host.id')
111111
->orderBy('host.id')
112-
->filter(Filter::unequal('host.vars.invalid', 'foo'));
112+
->filter(Filter::unequal('host.vars.in.valid', 'foo'));
113113

114-
$this->assertSql(self::SINGLE_UNEQUAL_RESULT, $query->assembleSelect(), ['invalid', 'foo', 1]);
114+
$this->assertSql(self::SINGLE_UNEQUAL_RESULT, $query->assembleSelect(), ['in.valid', 'foo', 1]);
115115
}
116116

117117
public function testDoubleUnequalCondition()
118118
{
119119
$query = Host::on($this->connection)
120120
->columns('host.id')
121121
->orderBy('host.id')
122-
->filter(Filter::unequal('host.vars.invalid', 'foo'))
122+
->filter(Filter::unequal('host.vars.in.valid', 'foo'))
123123
->filter(Filter::unequal('host.vars.missing', 'bar'));
124124

125125
$this->assertSql(
126126
self::DOUBLE_UNEQUAL_RESULT,
127127
$query->assembleSelect(),
128-
['invalid', 'foo', 'missing', 'bar', 1]
128+
['in.valid', 'foo', 'missing', 'bar', 1]
129129
);
130130
}
131131

@@ -134,13 +134,13 @@ public function testEqualAndUnequalCondition()
134134
$query = Host::on($this->connection)
135135
->columns('host.id')
136136
->orderBy('host.id')
137-
->filter(Filter::unequal('host.vars.invalid', 'bar'))
137+
->filter(Filter::unequal('host.vars.in.valid', 'bar'))
138138
->filter(Filter::equal('host.vars.env', 'foo'));
139139

140140
$this->assertSql(
141141
self::EQUAL_UNEQUAL_RESULT,
142142
$query->assembleSelect(),
143-
['invalid', 'bar', 1, 'env', 'foo', 1]
143+
['in.valid', 'bar', 1, 'env', 'foo', 1]
144144
);
145145
}
146146

0 commit comments

Comments
 (0)