Data/queries: adjust to fit new filters

Slight changes to get our queries working with new filter implementation.
This commit is contained in:
Thomas Gelf 2014-06-17 12:34:02 +00:00
parent d1b2d47fed
commit 7cd696b31b
3 changed files with 16 additions and 10 deletions

View File

@ -4,7 +4,8 @@ namespace Icinga\Data\Db;
use Icinga\Data\SimpleQuery; use Icinga\Data\SimpleQuery;
use Icinga\Application\Benchmark; 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\FilterOr;
use Icinga\Data\Filter\FilterAnd; use Icinga\Data\Filter\FilterAnd;
use Icinga\Data\Filter\FilterNot; use Icinga\Data\Filter\FilterNot;
@ -70,7 +71,7 @@ class DbQuery extends SimpleQuery
protected function dbSelect() protected function dbSelect()
{ {
if ($this->select === null) { 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; return clone $this->select;
} }
@ -112,7 +113,7 @@ class DbQuery extends SimpleQuery
protected function renderFilter($filter, $level = 0) protected function renderFilter($filter, $level = 0)
{ {
$str = ''; $str = '';
if ($filter instanceof FilterOperator) { if ($filter instanceof FilterChain) {
if ($filter instanceof FilterAnd) { if ($filter instanceof FilterAnd) {
$op = ' AND '; $op = ' AND ';
} elseif ($filter instanceof FilterOr) { } elseif ($filter instanceof FilterOr) {
@ -136,7 +137,7 @@ class DbQuery extends SimpleQuery
} }
} else { } else {
// TODO: render Filter (Where/like/time...) // TODO: render Filter (Where/like/time...)
$str .= $this->whereToSql($filter->getColumn(), $filter->getExpression()); $str .= $this->whereToSql($filter->getColumn(), $filter->getSign(), $filter->getExpression());
} }
return $str; return $str;
} }
@ -164,13 +165,16 @@ class DbQuery extends SimpleQuery
return preg_replace('/\*/', '%', $value); 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* // TODO: Should we support this? Doesn't work for blub*
return $col . ' IN (' . $this->escapeForSql($expression) . ')'; return $col . ' IN (' . $this->escapeForSql($expression) . ')';
} elseif (strpos($expression, '*') === false) { } elseif (strpos($expression, '*') === false) {
return $col . ' = ' . $this->escapeForSql($expression); return $col . ' ' . $sign . ' ' . $this->escapeForSql($expression);
} else { } else {
return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression)); return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression));
} }

View File

@ -127,7 +127,8 @@ class SimpleQuery implements QueryInterface, Queryable
*/ */
public function where($condition, $value = null) 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; return $this;
} }

View File

@ -8,7 +8,7 @@ use Icinga\Exception\ProgrammingError;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Web\Session; use Icinga\Web\Session;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterWhere; use Icinga\Data\Filter\FilterExpression;
/** /**
* Base class for Ido Queries * Base class for Ido Queries
@ -241,7 +241,7 @@ abstract class IdoQuery extends DbQuery
protected function requireFilterColumns(Filter $filter) protected function requireFilterColumns(Filter $filter)
{ {
if ($filter instanceof FilterWhere) { if ($filter instanceof FilterExpression) {
$col = $filter->getColumn(); $col = $filter->getColumn();
$this->requireColumn($col); $this->requireColumn($col);
@ -285,6 +285,7 @@ abstract class IdoQuery extends DbQuery
{ {
$mapped = $this->getMappedField($field); $mapped = $this->getMappedField($field);
if ($mapped === null) { if ($mapped === null) {
return stripos($field, 'UNIX_TIMESTAMP') !== false;
return false; return false;
} }
return stripos($mapped, 'UNIX_TIMESTAMP') !== false; return stripos($mapped, 'UNIX_TIMESTAMP') !== false;