Data\SimpleQuery: concrete implementation

SimpleQuery is no longer abstract, implements the Filterable interface
and should now be usable as a basic Query implemenation as is.
This commit is contained in:
Thomas Gelf 2014-06-06 06:12:17 +00:00
parent 85605597cc
commit 31047e8082

View File

@ -2,11 +2,13 @@
namespace Icinga\Data; namespace Icinga\Data;
use Zend_Controller_Front; use Icinga\Application\Icinga;
use Zend_Paginator; use Icinga\Data\Filter\Filter;
use Icinga\Web\Paginator\Adapter\QueryAdapter; use Icinga\Web\Paginator\Adapter\QueryAdapter;
use Zend_Paginator;
use Exception;
abstract class SimpleQuery implements QueryInterface, Queryable class SimpleQuery implements QueryInterface, Queryable
{ {
/** /**
* Query data source * Query data source
@ -15,6 +17,29 @@ abstract class SimpleQuery implements QueryInterface, Queryable
*/ */
protected $ds; protected $ds;
/**
* The table you are going to query
*/
protected $table;
/**
* 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();
/** /**
* The columns you're using to sort the query result * The columns you're using to sort the query result
* *
@ -36,15 +61,24 @@ abstract class SimpleQuery implements QueryInterface, Queryable
*/ */
protected $limitOffset; protected $limitOffset;
protected $filter;
/** /**
* Constructor * Constructor
* *
* @param mixed $ds * @param mixed $ds
*/ */
public function __construct($ds) public function __construct($ds, $columns = null)
{ {
$this->ds = $ds; $this->ds = $ds;
$this->filter = Filter::matchAll();
if ($columns !== null) {
$this->desiredColumns = $columns;
}
$this->init(); $this->init();
if ($this->desiredColumns !== null) {
$this->columns($this->desiredColumns);
}
} }
/** /**
@ -53,9 +87,7 @@ abstract class SimpleQuery implements QueryInterface, Queryable
* Overwrite this instead of __construct (it's called at the end of the construct) to * Overwrite this instead of __construct (it's called at the end of the construct) to
* implement custom initialization logic on construction time * implement custom initialization logic on construction time
*/ */
protected function init() protected function init() {}
{
}
/** /**
* Get the data source * Get the data source
@ -67,6 +99,22 @@ abstract class SimpleQuery implements QueryInterface, Queryable
return $this->ds; return $this->ds;
} }
/**
* Choose a table and the colums you are interested in
*
* Query will return all available columns if none are given here
*
* @return self
*/
public function from($target, array $fields = null)
{
$this->target = $target;
if ($fields !== null) {
$this->columns($fields);
}
return $this;
}
/** /**
* Add a where condition to the query by and * Add a where condition to the query by and
* *
@ -77,23 +125,37 @@ abstract class SimpleQuery implements QueryInterface, Queryable
* *
* @return self * @return self
*/ */
abstract public function where($condition, $value = null); public function where($condition, $value = null)
{
$this->filter->addFilter(Filter::where($condition, $value));
return $this;
}
/** public function getFilter()
* Add a where condition to the query by or {
* return $this->filter;
* The syntax of the condition and valid values are defined by the concrete backend-specific query implementation. }
*
* @param string $condition public function applyFilter(Filter $filter)
* @param mixed $value {
* return $this->addFilter($filter);
* @return self }
*/
abstract public function orWhere($condition, $value = null); public function addFilter(Filter $filter)
{
$this->filter->addFilter($filter);
return $this;
}
public function setFilter(Filter $filter)
{
$this->filter = $filter;
return $this;
}
public function setOrderColumns(array $orderColumns) public function setOrderColumns(array $orderColumns)
{ {
throw new \Exception('This function does nothing and will be removed'); throw new Exception('This function does nothing and will be removed');
} }
/** /**
@ -134,6 +196,35 @@ abstract class SimpleQuery implements QueryInterface, Queryable
return $this; return $this;
} }
public function compare($a, $b, $col_num = 0)
{
// Last column to sort reached, rows are considered being equal
if (! array_key_exists($col_num, $this->order)) {
return 0;
}
$col = $this->order[$col_num][0];
$dir = $this->order[$col_num][1];
// TODO: throw Exception if column is missing
//$res = strnatcmp(strtolower($a->$col), strtolower($b->$col));
$res = strcmp(strtolower($a->$col), strtolower($b->$col));
if ($res === 0) {
// return $this->compare($a, $b, $col_num++);
if (array_key_exists(++$col_num, $this->order)) {
return $this->compare($a, $b, $col_num);
} else {
return 0;
}
}
if ($dir === self::SORT_ASC) {
return $res;
} else {
return $res * -1;
}
}
/** /**
* Whether an order is set * Whether an order is set
* *