IdoQueryExtensionHook: "just a new hook"

This commit is contained in:
Thomas Gelf 2015-11-13 16:48:06 +01:00
parent 6394cec933
commit b7352105d2
2 changed files with 72 additions and 1 deletions

View File

@ -5,6 +5,7 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Zend_Db_Expr;
use Icinga\Application\Icinga;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Data\Db\DbQuery;
use Icinga\Data\Filter\Filter;
@ -119,6 +120,15 @@ abstract class IdoQuery extends DbQuery
*/
protected $joinedVirtualTables = array();
/**
* A map of virtual table names and corresponding hook instances
*
* Joins for those tables will be delegated to them
*
* @var array
*/
protected $hookedVirtualTables = array();
/**
* List of column aliases used for sorting the result
*
@ -593,6 +603,18 @@ abstract class IdoQuery extends DbQuery
return isset($this->caseInsensitiveColumns[$table][$alias]);
}
/**
* Return our column map
*
* Might be useful for hooks
*
* @return array
*/
public function getColumnMap()
{
return $this->columnMap;
}
/**
* Apply oracle specific query initialization
*/
@ -651,6 +673,23 @@ abstract class IdoQuery extends DbQuery
{
parent::init();
$this->prefix = $this->ds->getTablePrefix();
foreach (Hook::all('monitoring/idoQueryExtension') as $hook) {
$extensions = $hook->extendColumnMap($this);
if (! is_array($extensions)) continue;
foreach ($extensions as $vTable => $cols) {
if (! array_key_exists($vTable, $this->columnMap)) {
$this->hookedVirtualTables[$vTable] = $hook;
$this->columMap[$vTable] = array();
}
foreach ($cols as $k => $v) {
$this->columnMap[$vTable][$k] = $v;
}
}
}
$dbType = $this->ds->getDbType();
if ($dbType === 'oracle') {
$this->initializeForOracle();
@ -787,7 +826,24 @@ abstract class IdoQuery extends DbQuery
if ($this->hasJoinedVirtualTable($name)) {
return $this;
}
return $this->joinVirtualTable($name);
if ($this->virtualTableIsHooked($name)) {
return $this->joinHookedVirtualTable($name);
} else {
return $this->joinVirtualTable($name);
}
}
/**
* Whether a given virtual table name has been provided by a hook
*
* @param string $name Virtual table name
*
* @return boolean
*/
protected function virtualTableIsHooked($name)
{
return array_key_exists($name, $this->hookedVirtualTables);
}
protected function conflictsWithVirtualTable($name)

View File

@ -0,0 +1,15 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Hook;
use Icinga\Module\Monitoring\Backend\Ido\Query\IdoQuery;
abstract class IdoQueryExtensionHook
{
abstract public function extendColumnMap(IdoQuery $query);
public function joinVirtualTable(IdoQuery $query, $virtualTable)
{
}
}