From 7cd696b31bd838afa299e08ced3241309f00be67 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 17 Jun 2014 12:34:02 +0000 Subject: [PATCH] Data/queries: adjust to fit new filters Slight changes to get our queries working with new filter implementation. --- library/Icinga/Data/Db/DbQuery.php | 18 +++++++++++------- library/Icinga/Data/SimpleQuery.php | 3 ++- .../Monitoring/Backend/Ido/Query/IdoQuery.php | 5 +++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/library/Icinga/Data/Db/DbQuery.php b/library/Icinga/Data/Db/DbQuery.php index e7d92b34d..6ed877a82 100644 --- a/library/Icinga/Data/Db/DbQuery.php +++ b/library/Icinga/Data/Db/DbQuery.php @@ -4,7 +4,8 @@ namespace Icinga\Data\Db; use Icinga\Data\SimpleQuery; use Icinga\Application\Benchmark; -use Icinga\Data\Filter\FilterOperator; +use Icinga\Data\Filter\FilterChain; +use Icinga\Data\Filter\FilterExpression; use Icinga\Data\Filter\FilterOr; use Icinga\Data\Filter\FilterAnd; use Icinga\Data\Filter\FilterNot; @@ -70,7 +71,7 @@ class DbQuery extends SimpleQuery protected function dbSelect() { if ($this->select === null) { - $this->select = $this->db->select()->from($this->table, array()); + $this->select = $this->db->select()->from($this->target, array()); } return clone $this->select; } @@ -112,7 +113,7 @@ class DbQuery extends SimpleQuery protected function renderFilter($filter, $level = 0) { $str = ''; - if ($filter instanceof FilterOperator) { + if ($filter instanceof FilterChain) { if ($filter instanceof FilterAnd) { $op = ' AND '; } elseif ($filter instanceof FilterOr) { @@ -136,7 +137,7 @@ class DbQuery extends SimpleQuery } } else { // TODO: render Filter (Where/like/time...) - $str .= $this->whereToSql($filter->getColumn(), $filter->getExpression()); + $str .= $this->whereToSql($filter->getColumn(), $filter->getSign(), $filter->getExpression()); } return $str; } @@ -164,13 +165,16 @@ class DbQuery extends SimpleQuery return preg_replace('/\*/', '%', $value); } - public function whereToSql($col, $expression) + public function whereToSql($col, $sign, $expression) { - if (is_array($expression)) { + if ($this->isTimestamp($col)) { + $expression = strtotime($expression); + } + if (is_array($expression) && $sign === '=') { // TODO: Should we support this? Doesn't work for blub* return $col . ' IN (' . $this->escapeForSql($expression) . ')'; } elseif (strpos($expression, '*') === false) { - return $col . ' = ' . $this->escapeForSql($expression); + return $col . ' ' . $sign . ' ' . $this->escapeForSql($expression); } else { return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression)); } diff --git a/library/Icinga/Data/SimpleQuery.php b/library/Icinga/Data/SimpleQuery.php index c33ec1e2f..e7392307a 100644 --- a/library/Icinga/Data/SimpleQuery.php +++ b/library/Icinga/Data/SimpleQuery.php @@ -127,7 +127,8 @@ class SimpleQuery implements QueryInterface, Queryable */ public function where($condition, $value = null) { - $this->filter->addFilter(Filter::where($condition, $value)); + // TODO: more intelligence please + $this->filter->addFilter(Filter::expression($condition, '=', $value)); return $this; } diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php index d3f720614..4d4bf82dc 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php @@ -8,7 +8,7 @@ use Icinga\Exception\ProgrammingError; use Icinga\Application\Icinga; use Icinga\Web\Session; use Icinga\Data\Filter\Filter; -use Icinga\Data\Filter\FilterWhere; +use Icinga\Data\Filter\FilterExpression; /** * Base class for Ido Queries @@ -241,7 +241,7 @@ abstract class IdoQuery extends DbQuery protected function requireFilterColumns(Filter $filter) { - if ($filter instanceof FilterWhere) { + if ($filter instanceof FilterExpression) { $col = $filter->getColumn(); $this->requireColumn($col); @@ -285,6 +285,7 @@ abstract class IdoQuery extends DbQuery { $mapped = $this->getMappedField($field); if ($mapped === null) { + return stripos($field, 'UNIX_TIMESTAMP') !== false; return false; } return stripos($mapped, 'UNIX_TIMESTAMP') !== false;