diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php index eb4634ce7..d3f720614 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php @@ -1,31 +1,4 @@ - * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 - * @author Icinga Development Team - * - */ -// {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Module\Monitoring\Backend\Ido\Query; @@ -34,6 +7,8 @@ use Icinga\Data\Db\DbQuery; use Icinga\Exception\ProgrammingError; use Icinga\Application\Icinga; use Icinga\Web\Session; +use Icinga\Data\Filter\Filter; +use Icinga\Data\Filter\FilterWhere; /** * Base class for Ido Queries @@ -59,7 +34,7 @@ use Icinga\Web\Session; * * This allows you to select e.g. fieldalias1, which automatically calls the query code for joining 'virtualTable'. If * you afterwards select 'host', 'virtualTable2' will be joined. The joining logic is up to you, in order to make the - * above example work you need to implement the joinVirtualTable() and joinVirtualTable2() method which contain your + * above example work you need to implement the joinVirtualTable() method which contain your * custom (Zend_Db) logic for joining, filtering and querying the data you want. * */ @@ -252,9 +227,54 @@ abstract class IdoQuery extends DbQuery return $columnSet[$field]; } } + if ($this->isCustomVar($field)) { + return $this->getCustomvarColumnName($field); + } return null; } + public function distinct() + { + $this->select->distinct(); + return $this; + } + + protected function requireFilterColumns(Filter $filter) + { + if ($filter instanceof FilterWhere) { + $col = $filter->getColumn(); + $this->requireColumn($col); + + if ($this->isCustomvar($col)) { + $col = $this->getCustomvarColumnName($col); + } else { + $col = $this->aliasToColumnName($col); + } + + $filter->setColumn($col); + } else { + foreach ($filter->filters() as $filter) { + $this->requireFilterColumns($filter); + } + } + } + + public function addFilter(Filter $filter) + { + $this->requireFilterColumns($filter); + parent::addFilter($filter); + } + + public function where($condition, $value = null) + { + $this->requireColumn($condition); + $col = $this->getMappedField($condition); + if ($col === null) { + throw new \Exception("No such field: $condition"); + } + return parent::where($col, $value); + } + /** * Return true if an field contains an explicit timestamp * @@ -316,10 +336,22 @@ abstract class IdoQuery extends DbQuery } elseif ($this->ds->getDbType() === 'pgsql') { $this->initializeForPostgres(); } - $this->joinBaseTables(); + $this->dbSelect(); + + $this->select->columns($this->columns); + //$this->joinBaseTables(); $this->prepareAliasIndexes(); } + protected function dbSelect() + { + if ($this->select === null) { + $this->select = $this->db->select(); + $this->joinBaseTables(); + } + return clone $this->select; + } + /** * Join the base tables for this query */ @@ -524,16 +556,15 @@ abstract class IdoQuery extends DbQuery } else { $leftcol = 'h.' . $type . '_object_id'; } - $joinOn = $leftcol - . ' = ' - . $alias - . '.object_id' - . ' AND ' - . $alias - . '.varname = ' - . $this->db->quote(strtoupper($name)); + $joinOn = sprintf( + '%s = %s.object_id AND %s.varname = %s', + $leftcol, + $alias, + $alias, + $this->db->quote(strtoupper($name)) + ); - $this->baseQuery->joinLeft( + $this->select->joinLeft( array($alias => $this->prefix . 'customvariablestatus'), $joinOn, array() @@ -590,6 +621,8 @@ abstract class IdoQuery extends DbQuery public function columns(array $columns) { $this->columns = $this->resolveColumns($columns); + // TODO: we need to refresh our select! + // $this->select->columns($columns); return $this; }