Data\DataArray: use SimpleQuery and new interfaces

Removed ArrayQuery implementation as SimpleQuery is enough here.
Renamed the Datasource class to ArrayDatasource.

refs #6418
This commit is contained in:
Thomas Gelf 2014-06-06 06:21:35 +00:00
parent 31047e8082
commit 1fbca25d99
5 changed files with 52 additions and 118 deletions

View File

@ -156,6 +156,7 @@ class ConfigController extends BaseConfigController
$this->view->modules = Icinga::app()->getModuleManager()->select() $this->view->modules = Icinga::app()->getModuleManager()->select()
->from('modules') ->from('modules')
->order('enabled', 'desc')
->order('name'); ->order('name');
$this->render('module/overview'); $this->render('module/overview');
} }

View File

@ -32,8 +32,8 @@ namespace Icinga\Application\Modules;
use Icinga\Application\ApplicationBootstrap; use Icinga\Application\ApplicationBootstrap;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Logger\Logger; use Icinga\Logger\Logger;
use Icinga\Data\DataArray\Datasource as ArrayDatasource; use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Data\DataArray\Query as ArrayQuery; use Icinga\Data\SimpleQuery;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Exception\SystemPermissionException; use Icinga\Exception\SystemPermissionException;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
@ -110,7 +110,7 @@ class Manager
/** /**
* Query interface for the module manager * Query interface for the module manager
* *
* @return ArrayQuery * @return SimpleQuery
*/ */
public function select() public function select()
{ {

View File

@ -2,12 +2,15 @@
namespace Icinga\Data\DataArray; namespace Icinga\Data\DataArray;
use Icinga\Data\DatasourceInterface; use Icinga\Data\Selectable;
use Icinga\Data\SimpleQuery;
class Datasource implements DatasourceInterface class ArrayDatasource implements Selectable
{ {
protected $data; protected $data;
protected $result;
/** /**
* Constructor, create a new Datasource for the given Array * Constructor, create a new Datasource for the given Array
* *
@ -21,14 +24,14 @@ class Datasource implements DatasourceInterface
/** /**
* Instantiate a Query object * Instantiate a Query object
* *
* @return Query * @return SimpleQuery
*/ */
public function select() public function select()
{ {
return new Query($this); return new SimpleQuery($this);
} }
public function fetchColumn(Query $query) public function fetchColumn(SimpleQuery $query)
{ {
$result = array(); $result = array();
foreach ($this->getResult($query) as $row) { foreach ($this->getResult($query) as $row) {
@ -38,7 +41,7 @@ class Datasource implements DatasourceInterface
return $result; return $result;
} }
public function fetchPairs(Query $query) public function fetchPairs(SimpleQuery $query)
{ {
$result = array(); $result = array();
$keys = null; $keys = null;
@ -54,27 +57,31 @@ class Datasource implements DatasourceInterface
return $result; return $result;
} }
public function fetchAll(Query $query) public function fetchAll(SimpleQuery $query)
{ {
return $this->getResult($query); return $this->getResult($query);
} }
public function count(Query $query) public function count(SimpleQuery $query)
{ {
$this->createResult($query); $this->createResult($query);
return $query->getCount(); return count($this->result);
} }
protected function createResult(Query $query) protected function createResult(SimpleQuery $query)
{ {
if ($query->hasResult()) { if ($this->hasResult()) {
return $this; return $this;
} }
$result = array(); $result = array();
$columns = $query->getColumns(); $columns = $query->getColumns();
$filter = $query->getFilter();
foreach ($this->data as & $row) { foreach ($this->data as & $row) {
if (! $filter->matches($row)) {
continue;
}
// Get only desired columns if asked so // Get only desired columns if asked so
if (empty($columns)) { if (empty($columns)) {
@ -96,19 +103,44 @@ class Datasource implements DatasourceInterface
} }
// Sort the result // Sort the result
if ($query->hasOrder()) { if ($query->hasOrder()) {
usort($result, array($query, 'compare')); usort($result, array($query, 'compare'));
} }
$query->setResult($result); $this->setResult($result);
return $this; return $this;
} }
protected function getResult(Query $query) protected function getLimitedResult($query)
{ {
if (! $query->hasResult()) { if ($query->hasLimit()) {
if ($query->hasOffset()) {
$offset = $query->getOffset();
} else {
$offset = 0;
}
return array_slice($this->result, $offset, $query->getLimit());
} else {
return $this->result;
}
}
protected function hasResult()
{
return $this->result !== null;
}
protected function setResult($result)
{
return $this->result = $result;
}
protected function getResult(SimpleQuery $query)
{
if (! $this->hasResult()) {
$this->createResult($query); $this->createResult($query);
} }
return $query->getLimitedResult(); return $this->getLimitedResult($query);
} }
} }

View File

@ -1,99 +0,0 @@
<?php
namespace Icinga\Data\DataArray;
use Icinga\Data\SimpleQuery;
class Query extends SimpleQuery
{
/**
* Remember the last count
*/
protected $count;
/**
* Remember the last result without applied limits
*/
protected $result;
public function getCount()
{
return $this->count;
}
public function hasResult()
{
return $this->result !== null;
}
public function getFullResult()
{
return $this->result;
}
public function getLimitedResult()
{
if ($this->hasLimit()) {
if ($this->hasOffset()) {
$offset = $this->getOffset();
} else {
$offset = 0;
}
return array_slice($this->result, $offset, $this->getLimit());
} else {
return $this->result;
}
}
public function setResult($result)
{
$this->result = $result;
$this->count = count($result);
return $this;
}
/**
* ArrayDatasource will apply this function to sort the array
*
* @param mixed $a Left side comparsion value
* @param mixed $b Right side comparsion value
* @param int $col_num Current position in order_columns array
*
* @return int
*/
public function compare(& $a, & $b, $col_num = 0)
{
$orderColumns = $this->getOrderColumns();
if (! array_key_exists($col_num, $orderColumns)) {
return 0;
}
$col = $orderColumns[$col_num][0];
$dir = $orderColumns[$col_num][1];
//$res = strnatcmp(strtolower($a->$col), strtolower($b->$col));
$res = strcmp(strtolower($a->$col), strtolower($b->$col));
if ($res === 0) {
if (array_key_exists(++$col_num, $orderColumns)) {
return $this->compare($a, $b, $col_num);
} else {
return 0;
}
}
if ($dir === self::SORT_ASC) {
return $res;
} else {
return $res * -1;
}
}
public function parseFilterExpression($expression, $parameters = null)
{
return null;
}
public function applyFilter()
{
return null;
}
}

View File

@ -13,7 +13,7 @@ interface Filterable
public function setFilter(Filter $filter); public function setFilter(Filter $filter);
public function getFilter(Filter $filter); public function getFilter();
public function addFilter(Filter $filter); public function addFilter(Filter $filter);