2013-10-15 19:56:33 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-10-15 19:56:33 +02:00
|
|
|
|
|
|
|
namespace Icinga\Data;
|
|
|
|
|
2015-05-19 09:41:55 +02:00
|
|
|
use Iterator;
|
2015-05-15 14:26:00 +02:00
|
|
|
use IteratorAggregate;
|
2015-06-02 14:47:29 +02:00
|
|
|
use Zend_Paginator;
|
|
|
|
use Icinga\Application\Icinga;
|
2015-05-19 09:41:18 +02:00
|
|
|
use Icinga\Application\Benchmark;
|
2014-06-06 08:12:17 +02:00
|
|
|
use Icinga\Data\Filter\Filter;
|
2015-05-18 11:25:02 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2015-07-31 13:51:39 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2015-06-02 14:47:29 +02:00
|
|
|
use Icinga\Web\Paginator\Adapter\QueryAdapter;
|
2013-10-15 19:56:33 +02:00
|
|
|
|
2015-05-19 09:41:55 +02:00
|
|
|
class SimpleQuery implements QueryInterface, Queryable, Iterator
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Query data source
|
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @var mixed
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
|
|
|
protected $ds;
|
|
|
|
|
2015-05-19 09:41:55 +02:00
|
|
|
/**
|
|
|
|
* This query's iterator
|
|
|
|
*
|
|
|
|
* @var Iterator
|
|
|
|
*/
|
|
|
|
protected $iterator;
|
|
|
|
|
2015-07-31 13:51:39 +02:00
|
|
|
/**
|
|
|
|
* The current position of this query's iterator
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $iteratorPosition;
|
|
|
|
|
2014-06-06 08:12:17 +02:00
|
|
|
/**
|
2015-05-04 11:12:43 +02:00
|
|
|
* The target you are going to query
|
|
|
|
*
|
|
|
|
* @var mixed
|
2014-06-06 08:12:17 +02:00
|
|
|
*/
|
2015-05-04 11:12:43 +02:00
|
|
|
protected $target;
|
2014-06-06 08:12:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The columns you asked for
|
|
|
|
*
|
|
|
|
* All columns if null, no column if empty??? Alias handling goes here!
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $desiredColumns = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The columns you are interested in
|
|
|
|
*
|
|
|
|
* All columns if null, no column if empty??? Alias handling goes here!
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $columns = array();
|
|
|
|
|
2015-05-05 07:31:50 +02:00
|
|
|
/**
|
|
|
|
* The columns and their aliases flipped in order to handle aliased sort columns
|
|
|
|
*
|
|
|
|
* Supposed to be used and populated by $this->compare *only*.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $flippedColumns;
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
/**
|
|
|
|
* The columns you're using to sort the query result
|
2014-04-15 16:40:25 +02:00
|
|
|
*
|
2013-10-15 19:56:33 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
protected $order = array();
|
2013-10-15 19:56:33 +02:00
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Number of rows to return
|
|
|
|
*
|
2013-10-15 19:56:33 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
protected $limitCount;
|
2013-10-15 19:56:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Result starts with this row
|
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @var int
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
protected $limitOffset;
|
2013-10-15 19:56:33 +02:00
|
|
|
|
2015-07-31 13:51:39 +02:00
|
|
|
/**
|
|
|
|
* Whether to peek ahead for more results
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $peekAhead;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the query did not yield all available results
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $hasMore;
|
|
|
|
|
2014-06-06 08:12:17 +02:00
|
|
|
protected $filter;
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @param mixed $ds
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-06-06 08:12:17 +02:00
|
|
|
public function __construct($ds, $columns = null)
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
|
|
|
$this->ds = $ds;
|
2014-06-06 08:12:17 +02:00
|
|
|
$this->filter = Filter::matchAll();
|
|
|
|
if ($columns !== null) {
|
|
|
|
$this->desiredColumns = $columns;
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
$this->init();
|
2014-06-06 08:12:17 +02:00
|
|
|
if ($this->desiredColumns !== null) {
|
|
|
|
$this->columns($this->desiredColumns);
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Initialize query
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* Overwrite this instead of __construct (it's called at the end of the construct) to
|
|
|
|
* implement custom initialization logic on construction time
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-06-06 08:12:17 +02:00
|
|
|
protected function init() {}
|
2013-10-15 19:56:33 +02:00
|
|
|
|
2015-05-15 14:26:00 +02:00
|
|
|
/**
|
2015-05-19 09:41:55 +02:00
|
|
|
* Get the data source
|
2015-05-15 14:26:00 +02:00
|
|
|
*
|
2015-05-19 09:41:55 +02:00
|
|
|
* @return mixed
|
2015-05-15 14:26:00 +02:00
|
|
|
*/
|
2015-05-19 09:41:55 +02:00
|
|
|
public function getDatasource()
|
2015-05-15 14:26:00 +02:00
|
|
|
{
|
2015-05-19 09:41:55 +02:00
|
|
|
return $this->ds;
|
2015-05-15 14:26:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-06 09:39:23 +02:00
|
|
|
/**
|
|
|
|
* Return the current position of this query's iterator
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getIteratorPosition()
|
|
|
|
{
|
|
|
|
return $this->iteratorPosition;
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
/**
|
2015-05-19 09:41:55 +02:00
|
|
|
* Start or rewind the iteration
|
|
|
|
*/
|
|
|
|
public function rewind()
|
|
|
|
{
|
|
|
|
if ($this->iterator === null) {
|
|
|
|
$iterator = $this->ds->query($this);
|
|
|
|
if ($iterator instanceof IteratorAggregate) {
|
|
|
|
$this->iterator = $iterator->getIterator();
|
|
|
|
} else {
|
|
|
|
$this->iterator = $iterator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->iterator->rewind();
|
2015-08-03 14:09:55 +02:00
|
|
|
$this->iteratorPosition = null;
|
2015-05-19 09:41:55 +02:00
|
|
|
Benchmark::measure('Query result iteration started');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and return the current row of this query's result
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2015-05-19 09:41:55 +02:00
|
|
|
* @return object
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2015-05-19 09:41:55 +02:00
|
|
|
public function current()
|
2013-10-17 21:40:02 +02:00
|
|
|
{
|
2015-05-19 09:41:55 +02:00
|
|
|
return $this->iterator->current();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the current row of this query's result is valid
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function valid()
|
|
|
|
{
|
2015-07-31 13:51:39 +02:00
|
|
|
$valid = $this->iterator->valid();
|
|
|
|
if ($valid && $this->peekAhead && $this->hasLimit() && $this->iteratorPosition + 1 === $this->getLimit()) {
|
|
|
|
$this->hasMore = true;
|
|
|
|
$valid = false; // We arrived at the last result, which is the requested extra row, so stop the iteration
|
|
|
|
} elseif (! $valid) {
|
|
|
|
$this->hasMore = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $valid) {
|
2015-05-19 09:41:55 +02:00
|
|
|
Benchmark::measure('Query result iteration finished');
|
|
|
|
return false;
|
2015-08-03 14:09:55 +02:00
|
|
|
} elseif ($this->iteratorPosition === null) {
|
|
|
|
$this->iteratorPosition = 0;
|
2015-05-19 09:41:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the key for the current row of this query's result
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return $this->iterator->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance to the next row of this query's result
|
|
|
|
*/
|
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
$this->iterator->next();
|
2015-07-31 13:51:39 +02:00
|
|
|
$this->iteratorPosition += 1;
|
2013-10-17 21:40:02 +02:00
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-04 11:12:43 +02:00
|
|
|
* Choose a table and the columns you are interested in
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2015-05-04 11:12:43 +02:00
|
|
|
* Query will return all available columns if none are given here.
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2015-05-04 11:12:43 +02:00
|
|
|
* @param mixed $target
|
|
|
|
* @param array $fields
|
|
|
|
*
|
|
|
|
* @return $this
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-06-06 08:12:17 +02:00
|
|
|
public function from($target, array $fields = null)
|
|
|
|
{
|
|
|
|
$this->target = $target;
|
|
|
|
if ($fields !== null) {
|
|
|
|
$this->columns($fields);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
|
|
|
|
/**
|
2014-06-06 08:12:17 +02:00
|
|
|
* Add a where condition to the query by and
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* The syntax of the condition and valid values are defined by the concrete backend-specific query implementation.
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @param string $condition
|
|
|
|
* @param mixed $value
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-06-06 08:12:17 +02:00
|
|
|
public function where($condition, $value = null)
|
|
|
|
{
|
2014-06-17 14:34:02 +02:00
|
|
|
// TODO: more intelligence please
|
|
|
|
$this->filter->addFilter(Filter::expression($condition, '=', $value));
|
2014-06-06 08:12:17 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilter()
|
|
|
|
{
|
|
|
|
return $this->filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function applyFilter(Filter $filter)
|
|
|
|
{
|
|
|
|
return $this->addFilter($filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addFilter(Filter $filter)
|
|
|
|
{
|
|
|
|
$this->filter->addFilter($filter);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFilter(Filter $filter)
|
|
|
|
{
|
|
|
|
$this->filter = $filter;
|
|
|
|
return $this;
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
|
2014-04-15 16:40:25 +02:00
|
|
|
public function setOrderColumns(array $orderColumns)
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException('This function does nothing and will be removed');
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
2015-03-13 17:09:32 +01:00
|
|
|
/**
|
|
|
|
* Split order field into its field and sort direction
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function splitOrder($field)
|
|
|
|
{
|
|
|
|
$fieldAndDirection = explode(' ', $field, 2);
|
|
|
|
if (count($fieldAndDirection) === 1) {
|
|
|
|
$direction = null;
|
|
|
|
} else {
|
|
|
|
$field = $fieldAndDirection[0];
|
|
|
|
$direction = (strtoupper(trim($fieldAndDirection[1])) === 'DESC') ?
|
|
|
|
Sortable::SORT_DESC : Sortable::SORT_ASC;
|
|
|
|
}
|
|
|
|
return array($field, $direction);
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Sort result set by the given field (and direction)
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
|
|
|
* Preferred usage:
|
|
|
|
* <code>
|
2014-04-15 16:40:25 +02:00
|
|
|
* $query->order('field, 'ASC')
|
2013-10-15 19:56:33 +02:00
|
|
|
* </code>
|
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @param string $field
|
2014-05-07 10:00:41 +02:00
|
|
|
* @param string $direction
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function order($field, $direction = null)
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2014-04-15 16:40:25 +02:00
|
|
|
if ($direction === null) {
|
2015-03-13 17:09:32 +01:00
|
|
|
list($field, $direction) = $this->splitOrder($field);
|
|
|
|
if ($direction === null) {
|
|
|
|
$direction = Sortable::SORT_ASC;
|
2014-04-15 16:40:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (($direction = strtoupper($direction))) {
|
|
|
|
case Sortable::SORT_ASC:
|
|
|
|
case Sortable::SORT_DESC:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$direction = Sortable::SORT_ASC;
|
|
|
|
break;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-15 16:40:25 +02:00
|
|
|
$this->order[] = array($field, $direction);
|
2013-10-15 19:56:33 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-05-05 07:31:50 +02:00
|
|
|
/**
|
|
|
|
* Compare $a with $b based on this query's sort rules and column aliases
|
|
|
|
*
|
|
|
|
* @param object $a
|
|
|
|
* @param object $b
|
|
|
|
* @param int $orderIndex
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function compare($a, $b, $orderIndex = 0)
|
2014-06-06 08:12:17 +02:00
|
|
|
{
|
2015-05-05 07:31:50 +02:00
|
|
|
if (! array_key_exists($orderIndex, $this->order)) {
|
|
|
|
return 0; // Last column to sort reached, rows are considered being equal
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->flippedColumns === null) {
|
|
|
|
$this->flippedColumns = array_flip($this->columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
$column = $this->order[$orderIndex][0];
|
|
|
|
if (array_key_exists($column, $this->flippedColumns)) {
|
|
|
|
$column = $this->flippedColumns[$column];
|
2014-06-06 08:12:17 +02:00
|
|
|
}
|
|
|
|
|
2015-05-05 07:31:50 +02:00
|
|
|
// TODO: throw Exception if column is missing
|
|
|
|
//$res = strnatcmp(strtolower($a->$column), strtolower($b->$column));
|
|
|
|
$result = @strcmp(strtolower($a->$column), strtolower($b->$column));
|
|
|
|
if ($result === 0) {
|
|
|
|
return $this->compare($a, $b, ++$orderIndex);
|
2014-06-06 08:12:17 +02:00
|
|
|
}
|
|
|
|
|
2015-05-05 07:31:50 +02:00
|
|
|
$direction = $this->order[$orderIndex][1];
|
|
|
|
if ($direction === self::SORT_ASC) {
|
|
|
|
return $result;
|
2014-06-06 08:12:17 +02:00
|
|
|
} else {
|
2015-05-05 07:31:50 +02:00
|
|
|
return $result * -1;
|
2014-06-06 08:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 13:21:18 +01:00
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Whether an order is set
|
2014-03-20 13:21:18 +01:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return bool
|
2014-03-20 13:21:18 +01:00
|
|
|
*/
|
2015-06-26 15:13:46 +02:00
|
|
|
public function hasOrder()
|
2014-03-20 13:21:18 +01:00
|
|
|
{
|
2015-06-26 15:13:46 +02:00
|
|
|
return !empty($this->order);
|
2014-03-20 13:21:18 +01:00
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Get the order if any
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return array|null
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function getOrder()
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2014-04-15 16:40:25 +02:00
|
|
|
return $this->order;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 13:51:39 +02:00
|
|
|
/**
|
|
|
|
* Set whether this query should peek ahead for more results
|
|
|
|
*
|
|
|
|
* Enabling this causes the current query limit to be increased by one. The potential extra row being yielded will
|
|
|
|
* be removed from the result set. Note that this only applies when fetching multiple results of limited queries.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function peekAhead($state = true)
|
|
|
|
{
|
|
|
|
$this->peekAhead = (bool) $state;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether this query did not yield all available results
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws ProgrammingError In case the query did not run yet
|
|
|
|
*/
|
|
|
|
public function hasMore()
|
|
|
|
{
|
|
|
|
if ($this->hasMore === null) {
|
|
|
|
throw new ProgrammingError('Query did not run. Cannot determine whether there are more results.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->hasMore;
|
|
|
|
}
|
|
|
|
|
2015-08-03 14:49:33 +02:00
|
|
|
/**
|
|
|
|
* Return whether this query will or has yielded any result
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasResult()
|
|
|
|
{
|
|
|
|
return $this->iteratorPosition !== null || $this->fetchRow() !== false;
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Set a limit count and offset to the query
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @param int $count Number of rows to return
|
|
|
|
* @param int $offset Start returning after this many rows
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
|
|
|
public function limit($count = null, $offset = null)
|
|
|
|
{
|
2014-04-15 16:40:25 +02:00
|
|
|
$this->limitCount = $count !== null ? (int) $count : null;
|
|
|
|
$this->limitOffset = (int) $offset;
|
2014-03-13 15:15:56 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Whether a limit is set
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return bool
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
|
|
|
public function hasLimit()
|
|
|
|
{
|
2015-06-26 14:21:09 +02:00
|
|
|
return $this->limitCount !== null && $this->limitCount > 0;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Get the limit if any
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return int|null
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
|
|
|
public function getLimit()
|
|
|
|
{
|
2015-07-31 13:51:39 +02:00
|
|
|
return $this->peekAhead && $this->hasLimit() ? $this->limitCount + 1 : $this->limitCount;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Whether an offset is set
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return bool
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function hasOffset()
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2014-04-15 16:40:25 +02:00
|
|
|
return $this->limitOffset > 0;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Get the offset if any
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return int|null
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function getOffset()
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2014-04-15 16:40:25 +02:00
|
|
|
return $this->limitOffset;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
2015-06-02 14:47:29 +02:00
|
|
|
/**
|
|
|
|
* Paginate data
|
|
|
|
*
|
|
|
|
* Auto-detects pagination parameters from request when unset
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage Number of items per page
|
|
|
|
* @param int $pageNumber Current page number
|
|
|
|
*
|
|
|
|
* @return Zend_Paginator
|
|
|
|
*
|
|
|
|
* @deprecated Use Icinga\Web\Controller::setupPaginationControl() and/or Icinga\Web\Widget\Paginator instead
|
|
|
|
*/
|
|
|
|
public function paginate($itemsPerPage = null, $pageNumber = null)
|
|
|
|
{
|
|
|
|
trigger_error(
|
|
|
|
'SimpleQuery::paginate() is deprecated. Use Icinga\Web\Controller::setupPaginationControl()'
|
|
|
|
. ' and/or Icinga\Web\Widget\Paginator instead',
|
|
|
|
E_USER_DEPRECATED
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($itemsPerPage === null || $pageNumber === null) {
|
|
|
|
// Detect parameters from request
|
2015-07-30 14:02:44 +02:00
|
|
|
$request = Icinga::app()->getRequest();
|
2015-06-02 14:47:29 +02:00
|
|
|
if ($itemsPerPage === null) {
|
|
|
|
$itemsPerPage = $request->getParam('limit', 25);
|
|
|
|
}
|
|
|
|
if ($pageNumber === null) {
|
|
|
|
$pageNumber = $request->getParam('page', 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->limit($itemsPerPage, $pageNumber * $itemsPerPage);
|
|
|
|
$paginator = new Zend_Paginator(new QueryAdapter($this));
|
|
|
|
$paginator->setItemCountPerPage($itemsPerPage);
|
|
|
|
$paginator->setCurrentPageNumber($pageNumber);
|
|
|
|
return $paginator;
|
|
|
|
}
|
|
|
|
|
2013-10-19 20:09:17 +02:00
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Retrieve an array containing all rows of the result set
|
2013-10-19 20:09:17 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return array
|
2013-10-19 20:09:17 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function fetchAll()
|
2013-10-19 20:09:17 +02:00
|
|
|
{
|
2015-05-19 09:41:18 +02:00
|
|
|
Benchmark::measure('Fetching all results started');
|
|
|
|
$results = $this->ds->fetchAll($this);
|
|
|
|
Benchmark::measure('Fetching all results finished');
|
2015-07-31 13:51:39 +02:00
|
|
|
|
|
|
|
if ($this->peekAhead && $this->hasLimit() && count($results) === $this->getLimit()) {
|
|
|
|
$this->hasMore = true;
|
|
|
|
array_pop($results);
|
|
|
|
} else {
|
|
|
|
$this->hasMore = false;
|
|
|
|
}
|
|
|
|
|
2015-05-19 09:41:18 +02:00
|
|
|
return $results;
|
2013-10-19 20:09:17 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Fetch the first row of the result set
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return mixed
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function fetchRow()
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2015-05-19 09:41:18 +02:00
|
|
|
Benchmark::measure('Fetching one row started');
|
|
|
|
$row = $this->ds->fetchRow($this);
|
|
|
|
Benchmark::measure('Fetching one row finished');
|
|
|
|
return $row;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 09:48:20 +02:00
|
|
|
* Fetch the first column of all rows of the result set as an array
|
2014-04-15 16:40:25 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2015-05-19 09:48:20 +02:00
|
|
|
public function fetchColumn()
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2015-05-19 09:41:18 +02:00
|
|
|
Benchmark::measure('Fetching one column started');
|
2015-05-19 09:48:20 +02:00
|
|
|
$values = $this->ds->fetchColumn($this);
|
2015-05-19 09:41:18 +02:00
|
|
|
Benchmark::measure('Fetching one column finished');
|
2015-07-31 13:51:39 +02:00
|
|
|
|
|
|
|
if ($this->peekAhead && $this->hasLimit() && count($values) === $this->getLimit()) {
|
|
|
|
$this->hasMore = true;
|
|
|
|
array_pop($values);
|
|
|
|
} else {
|
|
|
|
$this->hasMore = false;
|
|
|
|
}
|
|
|
|
|
2015-05-19 09:41:18 +02:00
|
|
|
return $values;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Fetch the first column of the first row of the result set
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @return string
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function fetchOne()
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2015-05-19 09:41:18 +02:00
|
|
|
Benchmark::measure('Fetching one value started');
|
|
|
|
$value = $this->ds->fetchOne($this);
|
|
|
|
Benchmark::measure('Fetching one value finished');
|
|
|
|
return $value;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Fetch all rows of the result set as an array of key-value pairs
|
|
|
|
*
|
|
|
|
* The first column is the key, the second column is the value.
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function fetchPairs()
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2015-05-19 09:41:18 +02:00
|
|
|
Benchmark::measure('Fetching pairs started');
|
|
|
|
$pairs = $this->ds->fetchPairs($this);
|
|
|
|
Benchmark::measure('Fetching pairs finished');
|
2015-07-31 13:51:39 +02:00
|
|
|
|
|
|
|
if ($this->peekAhead && $this->hasLimit() && count($pairs) === $this->getLimit()) {
|
|
|
|
$this->hasMore = true;
|
|
|
|
array_pop($pairs);
|
|
|
|
} else {
|
|
|
|
$this->hasMore = false;
|
|
|
|
}
|
|
|
|
|
2015-05-19 09:41:18 +02:00
|
|
|
return $pairs;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-06 10:41:39 +02:00
|
|
|
* Count all rows of the result set, ignoring limit and offset
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2015-05-06 10:41:39 +02:00
|
|
|
* @return int
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-04-15 16:40:25 +02:00
|
|
|
public function count()
|
2013-10-15 19:56:33 +02:00
|
|
|
{
|
2015-05-06 10:41:39 +02:00
|
|
|
$query = clone $this;
|
|
|
|
$query->limit(0, 0);
|
2015-05-19 09:41:18 +02:00
|
|
|
Benchmark::measure('Counting all results started');
|
|
|
|
$count = $this->ds->count($query);
|
|
|
|
Benchmark::measure('Counting all results finished');
|
|
|
|
return $count;
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:40:25 +02:00
|
|
|
* Set columns
|
2013-10-15 19:56:33 +02:00
|
|
|
*
|
2014-04-15 16:40:25 +02:00
|
|
|
* @param array $columns
|
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2013-10-15 19:56:33 +02:00
|
|
|
*/
|
2014-06-06 07:49:39 +02:00
|
|
|
public function columns(array $columns)
|
|
|
|
{
|
|
|
|
$this->columns = $columns;
|
2015-05-05 07:31:50 +02:00
|
|
|
$this->flippedColumns = null; // Reset, due to updated columns
|
2014-06-06 07:49:39 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
return $this->columns;
|
|
|
|
}
|
2015-06-24 14:33:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deep clone self::$filter
|
|
|
|
*/
|
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
$this->filter = clone $this->filter;
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|