/usr/share/php/Icinga/Data/Db/DbQuery.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Data\Db;
use Exception;
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;
/**
* Database query class
*/
class DbQuery extends SimpleQuery
{
/**
* @var Zend_Db_Adapter_Abstract
*/
protected $db;
/**
* Whether or not the query is a sub query
*
* Sub queries are automatically wrapped in parentheses
*
* @var bool
*/
protected $isSubQuery = false;
/**
* Select query
*
* @var Zend_Db_Select
*/
protected $select;
/**
* Whether to use a subquery for counting
*
* When the query is distinct or has a HAVING or GROUP BY clause this must be set to true
*
* @var bool
*/
protected $useSubqueryCount = false;
/**
* Count query result
*
* Count queries are only executed once
*
* @var int
*/
protected $count;
/**
* GROUP BY clauses
*
* @var string|array
*/
protected $group;
protected function init()
{
$this->db = $this->ds->getDbAdapter();
$this->select = $this->db->select();
parent::init();
}
/**
* Get whether or not the query is a sub query
*/
public function getIsSubQuery()
{
return $this->isSubQuery;
}
/**
* Set whether or not the query is a sub query
*
* @param bool $isSubQuery
*
* @return $this
*/
public function setIsSubQuery($isSubQuery = true)
{
$this->isSubQuery = (bool) $isSubQuery;
return $this;
}
public function setUseSubqueryCount($useSubqueryCount = true)
{
$this->useSubqueryCount = $useSubqueryCount;
return $this;
}
public function from($target, array $fields = null)
{
parent::from($target, $fields);
$this->select->from($this->target, array());
return $this;
}
public function where($condition, $value = null)
{
// $this->count = $this->select = null;
return parent::where($condition, $value);
}
protected function dbSelect()
{
return clone $this->select;
}
/**
* Return the underlying select
*
* @return Zend_Db_Select
*/
public function select()
{
return $this->select;
}
/**
* Get the select query
*
* Applies order and limit if any
*
* @return Zend_Db_Select
*/
public function getSelectQuery()
{
$select = $this->dbSelect();
// Add order fields to select for postgres distinct queries (#6351)
if ($this->hasOrder()
&& $this->getDatasource()->getDbType() === 'pgsql'
&& $select->getPart(Zend_Db_Select::DISTINCT) === true) {
foreach ($this->getOrder() as $fieldAndDirection) {
if (array_search($fieldAndDirection[0], $this->columns, true) === false) {
$this->columns[] = $fieldAndDirection[0];
}
}
}
$group = $this->getGroup();
if ($group) {
$select->group($group);
}
$select->columns($this->columns);
$this->applyFilterSql($select);
if ($this->hasLimit() || $this->hasOffset()) {
$select->limit($this->getLimit(), $this->getOffset());
}
if ($this->hasOrder()) {
foreach ($this->getOrder() as $fieldAndDirection) {
$select->order(
$fieldAndDirection[0] . ' ' . $fieldAndDirection[1]
);
}
}
return $select;
}
protected function applyFilterSql($select)
{
$where = $this->renderFilter($this->filter);
if ($where !== '') {
$select->where($where);
}
}
protected function renderFilter($filter, $level = 0)
{
$str = '';
if ($filter instanceof FilterChain) {
if ($filter instanceof FilterAnd) {
$op = ' AND ';
} elseif ($filter instanceof FilterOr) {
$op = ' OR ';
} elseif ($filter instanceof FilterNot) {
$op = ' AND ';
$str .= ' NOT ';
} else {
throw new QueryException(
'Cannot render filter: %s',
$filter
);
}
$parts = array();
if (! $filter->isEmpty()) {
foreach ($filter->filters() as $f) {
$filterPart = $this->renderFilter($f, $level + 1);
if ($filterPart !== '') {
$parts[] = $filterPart;
}
}
if (! empty($parts)) {
if ($level > 0) {
$str .= ' (' . implode($op, $parts) . ') ';
} else {
$str .= implode($op, $parts);
}
}
}
} else {
$str .= $this->whereToSql($filter->getColumn(), $filter->getSign(), $filter->getExpression());
}
return $str;
}
protected function escapeForSql($value)
{
// bindParam? bindValue?
if (is_array($value)) {
$ret = array();
foreach ($value as $val) {
$ret[] = $this->escapeForSql($val);
}
return implode(', ', $ret);
} else {
//if (preg_match('/^\d+$/', $value)) {
// return $value;
//} else {
return $this->db->quote($value);
//}
}
}
protected function escapeWildcards($value)
{
return preg_replace('/\*/', '%', $value);
}
protected function valueToTimestamp($value)
{
// We consider integers as valid timestamps. Does not work for URL params
if (ctype_digit($value)) {
return $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
));
*/
}
return $value;
}
protected function timestampForSql($value)
{
// TODO: do this db-aware
return $this->escapeForSql(date('Y-m-d H:i:s', $value));
}
/**
* Check for timestamp fields
*
* TODO: This is not here to do automagic timestamp stuff. One may
* override this function for custom voodoo, IdoQuery right now
* does. IMO we need to split whereToSql functionality, however
* I'd prefer to wait with this unless we understood how other
* backends will work. We probably should also rename this
* function to isTimestampColumn().
*
* @param string $field Field Field name to checked
* @return bool Whether this field expects timestamps
*/
public function isTimestamp($field)
{
return false;
}
public function whereToSql($col, $sign, $expression)
{
if ($this->isTimestamp($col)) {
$expression = $this->valueToTimestamp($expression);
}
if (is_array($expression)) {
if ($sign === '=') {
return $col . ' IN (' . $this->escapeForSql($expression) . ')';
} elseif ($sign === '!=') {
return $col . ' NOT IN (' . $this->escapeForSql($expression) . ')';
}
throw new QueryException('Unable to render array expressions with operators other than equal or not equal');
} elseif ($sign === '=' && strpos($expression, '*') !== false) {
if ($expression === '*') {
// We'll ignore such filters as it prevents index usage and because "*" means anything, anything means
// all whereas all means that whether we use a filter to match anything or no filter at all makes no
// difference, except for performance reasons...
return '';
}
return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression));
} elseif ($sign === '!=' && strpos($expression, '*') !== false) {
if ($expression === '*') {
// We'll ignore such filters as it prevents index usage and because "*" means nothing, so whether we're
// using a real column with a valid comparison here or just an expression which cannot be evaluated to
// true makes no difference, except for performance reasons...
return $this->escapeForSql(0);
}
return $col . ' NOT LIKE ' . $this->escapeForSql($this->escapeWildcards($expression));
} else {
return $col . ' ' . $sign . ' ' . $this->escapeForSql($expression);
}
}
/**
* Get the count query
*
* @return Zend_Db_Select
*/
public function getCountQuery()
{
// TODO: there may be situations where we should clone the "select"
$count = $this->dbSelect();
$this->applyFilterSql($count);
$group = $this->getGroup();
if ($this->useSubqueryCount || $group) {
$count->columns($this->columns);
if ($group) {
$count->group($group);
}
$columns = array('cnt' => 'COUNT(*)');
return $this->db->select()->from($count, $columns);
}
$count->columns(array('cnt' => 'COUNT(*)'));
return $count;
}
/**
* Count all rows of the result set
*
* @return int
*/
public function count()
{
if ($this->count === null) {
$this->count = parent::count();
}
return $this->count;
}
/**
* Return the select and count query as a textual representation
*
* @return string A string containing the select and count query, using unix style newlines as linebreaks
*/
public function dump()
{
return "QUERY\n=====\n"
. $this->getSelectQuery()
. "\n\nCOUNT\n=====\n"
. $this->getCountQuery()
. "\n\n";
}
public function __clone()
{
parent::__clone();
$this->select = clone $this->select;
}
/**
* @return string
*/
public function __toString()
{
try {
$select = (string) $this->getSelectQuery();
return $this->getIsSubQuery() ? ('(' . $select . ')') : $select;
} catch (Exception $e) {
Logger::debug('Failed to render DbQuery. An error occured: %s', $e);
return '';
}
}
/**
* Add a GROUP BY clause
*
* @param string|array $group
*
* @return $this
*/
public function group($group)
{
$this->group = $group;
return $this;
}
/**
* Return the GROUP BY clause
*
* @return string|array
*/
public function getGroup()
{
return $this->group;
}
/**
* Return whether the given table has been joined
*
* @param string $table
*
* @return bool
*/
public function hasJoinedTable($table)
{
$fromPart = $this->select->getPart(Zend_Db_Select::FROM);
if (isset($fromPart[$table])) {
return true;
}
foreach ($fromPart as $options) {
if ($options['tableName'] === $table && $options['joinType'] !== Zend_Db_Select::FROM) {
return true;
}
}
return false;
}
/**
* Return the alias used for joining the given table
*
* @param string $table
*
* @return string|null null in case no alias is being used
*
* @throws ProgrammingError In case the given table has not been joined
*/
public function getJoinedTableAlias($table)
{
$fromPart = $this->select->getPart(Zend_Db_Select::FROM);
if (isset($fromPart[$table])) {
if ($fromPart[$table]['joinType'] === Zend_Db_Select::FROM) {
throw new ProgrammingError('Table "%s" has not been joined', $table);
}
return; // No alias in use
}
foreach ($fromPart as $alias => $options) {
if ($options['tableName'] === $table && $options['joinType'] !== Zend_Db_Select::FROM) {
return $alias;
}
}
throw new ProgrammingError('Table "%s" has not been joined', $table);
}
/**
* Add an INNER JOIN table and colums to the query
*
* @param array|string|Zend_Db_Expr $name The table name
* @param string $cond Join on this condition
* @param array|string $cols The columns to select from the joined table
* @param string $schema The database name to specify, if any
*
* @return $this
*/
public function join($name, $cond, $cols = Zend_Db_Select::SQL_WILDCARD, $schema = null)
{
$this->select->joinInner($name, $cond, $cols, $schema);
return $this;
}
/**
* Add an INNER JOIN table and colums to the query
*
* @param array|string|Zend_Db_Expr $name The table name
* @param string $cond Join on this condition
* @param array|string $cols The columns to select from the joined table
* @param string $schema The database name to specify, if any
*
* @return $this
*/
public function joinInner($name, $cond, $cols = Zend_Db_Select::SQL_WILDCARD, $schema = null)
{
$this->select->joinInner($name, $cond, $cols, $schema);
return $this;
}
/**
* Add a LEFT OUTER JOIN table and colums to the query
*
* @param array|string|Zend_Db_Expr $name The table name
* @param string $cond Join on this condition
* @param array|string $cols The columns to select from the joined table
* @param string $schema The database name to specify, if any
*
* @return $this
*/
public function joinLeft($name, $cond, $cols = Zend_Db_Select::SQL_WILDCARD, $schema = null)
{
$this->select->joinLeft($name, $cond, $cols, $schema);
return $this;
}
/**
* Add a RIGHT OUTER JOIN table and colums to the query
*
* @param array|string|Zend_Db_Expr $name The table name
* @param string $cond Join on this condition
* @param array|string $cols The columns to select from the joined table
* @param string $schema The database name to specify, if any
*
* @return $this
*/
public function joinRight($name, $cond, $cols = Zend_Db_Select::SQL_WILDCARD, $schema = null)
{
$this->select->joinRight($name, $cond, $cols, $schema);
return $this;
}
/**
* Add a FULL OUTER JOIN table and colums to the query
*
* @param array|string|Zend_Db_Expr $name The table name
* @param string $cond Join on this condition
* @param array|string $cols The columns to select from the joined table
* @param string $schema The database name to specify, if any
*
* @return $this
*/
public function joinFull($name, $cond, $cols = Zend_Db_Select::SQL_WILDCARD, $schema = null)
{
$this->select->joinFull($name, $cond, $cols, $schema);
return $this;
}
/**
* Add a CROSS JOIN table and colums to the query
*
* @param array|string|Zend_Db_Expr $name The table name
* @param array|string $cols The columns to select from the joined table
* @param string $schema The database name to specify, if any
*
* @return $this
*/
public function joinCross($name, $cols = Zend_Db_Select::SQL_WILDCARD, $schema = null)
{
$this->select->joinCross($name, $cols, $schema);
return $this;
}
/**
* Add a NATURAL JOIN table and colums to the query
*
* @param array|string|Zend_Db_Expr $name The table name
* @param array|string $cols The columns to select from the joined table
* @param string $schema The database name to specify, if any
*
* @return $this
*/
public function joinNatural($name, $cols = Zend_Db_Select::SQL_WILDCARD, $schema = null)
{
$this->select->joinNatural($name, $cols, $schema);
return $this;
}
}
|