Drop method `DbQuery::whereToSql()`

It's been used by `DbQuery::renderFilter()` rather
exclusively. So if removing one, both need to go.
This commit is contained in:
Johannes Meyer 2022-05-13 14:13:57 +02:00
parent e179f9cf3f
commit 46a21d9709
16 changed files with 138 additions and 222 deletions

View File

@ -39,6 +39,7 @@ v2.6 to v2.8 requires to follow the instructions for v2.7 too.
+ `MonitoringBackend::createBackend()`: Use `MonitoringBackend::instance()` instead.
+ `DbConnection::getConnection()`: Use `Connection::getDbAdapter()` instead.
+ `DbQuery::renderFilter()`: Use `DbConnection::renderFilter()` instead.
+ `DbQuery::whereToSql()`: Use `DbConnection::renderFilter()` instead.
**Classes:**
+ `Icinga\Util\String`: Use `Icinga\Util\StringHelper` instead.

View File

@ -7,13 +7,10 @@ use DateInterval;
use DateTime;
use DateTimeZone;
use Exception;
use Icinga\Data\Filter\Filter;
use Zend_Db_Expr;
use Zend_Db_Select;
use Icinga\Application\Logger;
use Icinga\Data\Filter\FilterAnd;
use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterNot;
use Icinga\Data\Filter\FilterOr;
use Icinga\Data\SimpleQuery;
use Icinga\Exception\ProgrammingError;
use Icinga\Exception\QueryException;
@ -116,6 +113,23 @@ class DbQuery extends SimpleQuery
return parent::where($condition, $value);
}
public function addFilter(Filter $filter)
{
$this->expressionsToTimestamp($filter);
return parent::addFilter($filter);
}
private function expressionsToTimestamp(Filter $filter)
{
if ($filter->isChain()) {
foreach ($filter->filters() as $child) {
$this->expressionsToTimestamp($child);
}
} elseif ($this->isTimestamp($filter->getColumn())) {
$filter->setExpression($this->valueToTimestamp($filter->getExpression()));
}
}
protected function dbSelect()
{
return clone $this->select;
@ -210,20 +224,16 @@ class DbQuery extends SimpleQuery
protected function valueToTimestamp($value)
{
// We consider integers as valid timestamps. Does not work for URL params
if (! is_string($value) || ctype_digit($value)) {
return $value;
if (ctype_digit($value)) {
$value = (int) $value;
} elseif (is_string($value)) {
$value = strtotime($value);
}
$value = strtotime($value);
if (! $value) {
/*
NOTE: It's too late to throw exceptions, we might finish in __toString
throw new QueryException(sprintf(
'"%s" is not a valid time expression',
$value
));
*/
if (is_int($value)) {
$value = $this->timestampForSql($value);
}
return $value;
}
@ -258,7 +268,7 @@ class DbQuery extends SimpleQuery
}
}
return $this->escapeForSql($dateTime->format('Y-m-d H:i:s'));
return $dateTime->format('Y-m-d H:i:s');
}
/**
@ -279,63 +289,6 @@ class DbQuery extends SimpleQuery
return false;
}
public function whereToSql($col, $sign, $expression)
{
if ($this->isTimestamp($col)) {
$expression = $this->valueToTimestamp($expression);
}
if (is_array($expression)) {
$comp = [];
$pattern = [];
foreach ($expression as $value) {
if (strpos($value, '*') === false) {
$comp[] = $value;
} else {
$pattern[] = $this->whereToSql($col, $sign, $value);
}
}
$sql = $pattern;
if ($sign === '=') {
if (! empty($comp)) {
$sql[] = $col . ' IN (' . $this->escapeForSql($comp) . ')';
}
$operator = 'OR';
} elseif ($sign === '!=') {
if (! empty($comp)) {
$sql[] = sprintf('(%1$s NOT IN (%2$s) OR %1$s IS NULL)', $col, $this->escapeForSql($comp));
}
$operator = 'AND';
} else {
throw new QueryException(
'Unable to render array expressions with operators other than equal or not equal'
);
}
return '(' . implode(" $operator ", $sql) . ')';
} elseif ($sign === '=' && $expression !== null && strpos($expression, '*') !== false) {
if ($expression === '*') {
return $col . ' IS NOT NULL';
}
return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression));
} elseif ($sign === '!=' && $expression !== null && strpos($expression, '*') !== false) {
if ($expression === '*') {
return $col . ' IS NULL';
}
return sprintf(
'(%1$s NOT LIKE %2$s OR %1$s IS NULL)',
$col,
$this->escapeForSql($this->escapeWildcards($expression))
);
} elseif ($sign === '!=') {
return sprintf('(%1$s %2$s %3$s OR %1$s IS NULL)', $col, $sign, $this->escapeForSql($expression));
} else {
return sprintf('%s %s %s', $col, $sign, $this->escapeForSql($expression));
}
}
/**
* Get the count query
*

View File

@ -8,16 +8,13 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query;
*/
class HostcommentdeletionhistoryQuery extends HostcommenthistoryQuery
{
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(hch.deletion_time)') {
return 'hch.deletion_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'hch.deletion_time';
}
return true;
}
/**

View File

@ -40,7 +40,7 @@ class HostcommenthistoryQuery extends IdoQuery
'object_type' => '(\'host\')',
'output' => "('[' || hch.author_name || '] ' || hch.comment_data)",
'state' => '(-1)',
'timestamp' => 'UNIX_TIMESTAMP(hch.comment_time)',
'timestamp' => 'hch.comment_time',
'type' => "(CASE hch.entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'dt_comment' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END)"
),
'hostgroups' => array(
@ -68,16 +68,13 @@ class HostcommenthistoryQuery extends IdoQuery
)
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(hch.comment_time)') {
return 'hch.comment_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'hch.comment_time';
}
return true;
}
/**

View File

@ -8,18 +8,13 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query;
*/
class HostdowntimeendhistoryQuery extends HostdowntimestarthistoryQuery
{
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(hdh.actual_end_time)') {
return 'hdh.actual_end_time ' . $sign . ' ' . $this->timestampForSql(
$this->valueToTimestamp($expression)
);
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'hdh.actual_end_time';
}
return true;
}
/**

View File

@ -40,7 +40,7 @@ class HostdowntimestarthistoryQuery extends IdoQuery
'object_type' => '(\'host\')',
'output' => "('[' || hdh.author_name || '] ' || hdh.comment_data)",
'state' => '(-1)',
'timestamp' => 'UNIX_TIMESTAMP(hdh.actual_start_time)',
'timestamp' => 'hdh.actual_start_time',
'type' => "('dt_start')"
),
'hostgroups' => array(
@ -68,18 +68,13 @@ class HostdowntimestarthistoryQuery extends IdoQuery
)
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(hdh.actual_start_time)') {
return 'hdh.actual_start_time ' . $sign . ' ' . $this->timestampForSql(
$this->valueToTimestamp($expression)
);
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'hdh.actual_start_time';
}
return true;
}
/**

View File

@ -40,7 +40,7 @@ class HostflappingstarthistoryQuery extends IdoQuery
'object_type' => '(\'host\')',
'output' => '(hfh.percent_state_change || \'\')',
'state' => '(-1)',
'timestamp' => 'UNIX_TIMESTAMP(hfh.event_time)',
'timestamp' => 'hfh.event_time',
'type' => '(\'flapping\')'
),
'hostgroups' => array(
@ -68,16 +68,13 @@ class HostflappingstarthistoryQuery extends IdoQuery
)
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(hfh.event_time)') {
return 'hfh.event_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'hfh.event_time';
}
return true;
}
/**

View File

@ -3,6 +3,9 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterExpression;
/**
* Query for host notifications
*/
@ -37,7 +40,7 @@ class HostnotificationQuery extends IdoQuery
'history' => array(
'output' => null,
'state' => 'hn.state',
'timestamp' => 'UNIX_TIMESTAMP(hn.start_time)',
'timestamp' => 'hn.start_time',
'type' => '
CASE hn.notification_reason
WHEN 1 THEN \'notification_ack\'
@ -60,7 +63,7 @@ class HostnotificationQuery extends IdoQuery
'notification_output' => 'hn.output',
'notification_reason' => 'hn.notification_reason',
'notification_state' => 'hn.state',
'notification_timestamp' => 'UNIX_TIMESTAMP(hn.start_time)',
'notification_timestamp' => 'hn.start_time',
'object_type' => '(\'host\')'
),
'servicegroups' => array(
@ -76,18 +79,24 @@ class HostnotificationQuery extends IdoQuery
)
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
protected function requireFilterColumns(Filter $filter)
{
if ($col === 'UNIX_TIMESTAMP(hn.start_time)') {
return 'hn.start_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} elseif ($col === $this->columnMap['history']['output']) {
return parent::whereToSql('hn.output', $sign, $expression);
} else {
return parent::whereToSql($col, $sign, $expression);
if ($filter instanceof FilterExpression && $filter->getColumn() === 'output') {
$this->requireColumn($filter->getColumn());
$filter->setColumn('hn.output');
return null;
}
return parent::requireFilterColumns($filter);
}
public function isTimestamp($field)
{
if (! parent::isTimestamp($field)) {
return $field === 'hn.start_time';
}
return true;
}
/**

View File

@ -73,26 +73,18 @@ class HoststatehistoryQuery extends IdoQuery
'object_type' => '(\'host\')',
'output' => '(CASE WHEN hh.state_type = 1 THEN hh.output ELSE \'[ \' || hh.current_check_attempt || \'/\' || hh.max_check_attempts || \' ] \' || hh.output END)',
'state' => 'hh.state',
'timestamp' => 'UNIX_TIMESTAMP(hh.state_time)',
'timestamp' => 'hh.state_time',
'type' => "(CASE WHEN hh.state_type = 1 THEN 'hard_state' ELSE 'soft_state' END)"
),
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(hh.state_time)') {
return 'hh.state_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} elseif ($col === $this->columnMap['statehistory']['type']
&& ! is_array($expression)
&& array_key_exists($expression, $this->types)
) {
return 'hh.state_type ' . $sign . ' ' . $this->types[$expression];
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'hh.state_time';
}
return true;
}
/**

View File

@ -8,16 +8,13 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query;
*/
class ServicecommentdeletionhistoryQuery extends ServicecommenthistoryQuery
{
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(sch.deletion_time)') {
return 'sch.deletion_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'sch.deletion_time';
}
return true;
}
/**

View File

@ -44,7 +44,7 @@ class ServicecommenthistoryQuery extends IdoQuery
'service_host' => 'so.name1 COLLATE latin1_general_ci',
'service_host_name' => 'so.name1',
'state' => '(-1)',
'timestamp' => 'UNIX_TIMESTAMP(sch.comment_time)',
'timestamp' => 'sch.comment_time',
'type' => "(CASE sch.entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'dt_comment' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END)"
),
'hostgroups' => array(
@ -69,16 +69,13 @@ class ServicecommenthistoryQuery extends IdoQuery
)
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(sch.comment_time)') {
return 'sch.comment_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'sch.comment_time';
}
return true;
}
/**

View File

@ -8,18 +8,13 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query;
*/
class ServicedowntimeendhistoryQuery extends ServicedowntimestarthistoryQuery
{
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(sdh.actual_end_time)') {
return 'sdh.actual_end_time ' . $sign . ' ' . $this->timestampForSql(
$this->valueToTimestamp($expression)
);
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'sdh.actual_end_time';
}
return true;
}
/**

View File

@ -44,7 +44,7 @@ class ServicedowntimestarthistoryQuery extends IdoQuery
'service_host' => 'so.name1 COLLATE latin1_general_ci',
'service_host_name' => 'so.name1',
'state' => '(-1)',
'timestamp' => 'UNIX_TIMESTAMP(sdh.actual_start_time)',
'timestamp' => 'sdh.actual_start_time',
'type' => "('dt_start')"
),
'hostgroups' => array(
@ -69,18 +69,13 @@ class ServicedowntimestarthistoryQuery extends IdoQuery
)
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(sdh.actual_start_time)') {
return 'sdh.actual_start_time ' . $sign . ' ' . $this->timestampForSql(
$this->valueToTimestamp($expression)
);
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'sdh.actual_start_time';
}
return true;
}
/**

View File

@ -43,7 +43,7 @@ class ServiceflappingstarthistoryQuery extends IdoQuery
'service_description' => 'so.name2',
'service_host_name' => 'so.name1',
'state' => '(-1)',
'timestamp' => 'UNIX_TIMESTAMP(sfh.event_time)',
'timestamp' => 'sfh.event_time',
'type' => "('flapping')"
),
'hostgroups' => array(
@ -68,18 +68,13 @@ class ServiceflappingstarthistoryQuery extends IdoQuery
)
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(sfh.event_time)') {
return 'sfh.event_time ' . $sign . ' ' . $this->timestampForSql(
$this->valueToTimestamp($expression)
);
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'sfh.event_time';
}
return true;
}
/**

View File

@ -3,6 +3,9 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterExpression;
/**
* Query for service notifications
*/
@ -28,7 +31,7 @@ class ServicenotificationQuery extends IdoQuery
'history' => array(
'output' => null,
'state' => 'sn.state',
'timestamp' => 'UNIX_TIMESTAMP(sn.start_time)',
'timestamp' => 'sn.start_time',
'type' => '
CASE sn.notification_reason
WHEN 1 THEN \'notification_ack\'
@ -60,7 +63,7 @@ class ServicenotificationQuery extends IdoQuery
'notification_output' => 'sn.output',
'notification_reason' => 'sn.notification_reason',
'notification_state' => 'sn.state',
'notification_timestamp' => 'UNIX_TIMESTAMP(sn.start_time)',
'notification_timestamp' => 'sn.start_time',
'object_type' => '(\'service\')',
'service' => 'so.name2 COLLATE latin1_general_ci',
'service_description' => 'so.name2',
@ -76,18 +79,24 @@ class ServicenotificationQuery extends IdoQuery
)
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
protected function requireFilterColumns(Filter $filter)
{
if ($col === 'UNIX_TIMESTAMP(sn.start_time)') {
return 'sn.start_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} elseif ($col === $this->columnMap['history']['output']) {
return parent::whereToSql('sn.output', $sign, $expression);
} else {
return parent::whereToSql($col, $sign, $expression);
if ($filter instanceof FilterExpression && $filter->getColumn() === 'output') {
$this->requireColumn($filter->getColumn());
$filter->setColumn('sn.output');
return null;
}
return parent::requireFilterColumns($filter);
}
public function isTimestamp($field)
{
if (! parent::isTimestamp($field)) {
return $field === 'sn.start_time';
}
return true;
}
/**

View File

@ -74,26 +74,18 @@ class ServicestatehistoryQuery extends IdoQuery
'service_host' => 'so.name1 COLLATE latin1_general_ci',
'service_host_name' => 'so.name1',
'state' => 'sh.state',
'timestamp' => 'UNIX_TIMESTAMP(sh.state_time)',
'timestamp' => 'sh.state_time',
'type' => "(CASE WHEN sh.state_type = 1 THEN 'hard_state' ELSE 'soft_state' END)"
),
);
/**
* {@inheritdoc}
*/
public function whereToSql($col, $sign, $expression)
public function isTimestamp($field)
{
if ($col === 'UNIX_TIMESTAMP(sh.state_time)') {
return 'sh.state_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} elseif ($col === $this->columnMap['statehistory']['type']
&& ! is_array($expression)
&& array_key_exists($expression, $this->types)
) {
return 'sh.state_type ' . $sign . ' ' . $this->types[$expression];
} else {
return parent::whereToSql($col, $sign, $expression);
if (! parent::isTimestamp($field)) {
return $field === 'sh.state_time';
}
return true;
}
/**