Sync: implement purge with nested keys

This commit is contained in:
Thomas Gelf 2016-07-14 12:11:27 +02:00
parent 354392d216
commit 56257c1c0f
3 changed files with 24 additions and 2 deletions

View File

@ -68,7 +68,9 @@ class ImportRunBasedPurgeStrategy extends PurgeStrategy
if ($rule->object_type === 'service') {
$pattern = $rule->getSourceKeyPattern();
$columns = SyncUtils::extractVariableNames($pattern);
$columns = SyncUtils::getRootVariables(
SyncUtils::extractVariableNames($pattern)
);
$rows = $runA->fetchRows($columns, null, $result);
$result = array();
foreach ($rows as $row) {

View File

@ -246,7 +246,9 @@ class Sync
$key = $source->key_column;
$this->sourceColumns[$sourceId][$key] = $key;
$run = $source->fetchLastRun(true);
$rows = $run->fetchRows($this->sourceColumns[$sourceId]);
$rows = $run->fetchRows(
SyncUtils::getRootVariables($this->sourceColumns[$sourceId])
);
$this->imported[$sourceId] = array();
foreach ($rows as $row) {

View File

@ -75,6 +75,10 @@ class SyncUtils
} else {
$parts = explode('.', $var);
$main = array_shift($parts);
if (! property_exists($row, $main)) {
return null;
}
if (! is_object($row->$main)) {
throw new IcingaException('Data is not nested, cannot access %s: %s', $var, var_export($row, 1));
}
@ -108,4 +112,18 @@ class SyncUtils
return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);
}
public static function getRootVariables($vars)
{
$res = array();
foreach ($vars as $p) {
if (false === ($pos = strpos($p, '.'))) {
$res[] = $p;
} else {
$res[] = substr($p, 0, $pos);
}
}
return array_combine($res, $res);
}
}