Add support for distinct database queries

refs #4180
This commit is contained in:
Johannes Meyer 2014-03-13 15:15:56 +01:00
parent 13b509bf4b
commit eae4cd3b2a
2 changed files with 42 additions and 2 deletions

View File

@ -59,6 +59,13 @@ abstract class BaseQuery implements Filterable
*/ */
private $limitOffset; private $limitOffset;
/**
* Whether its a distinct query or not
*
* @var bool
*/
private $distinct = false;
/** /**
* The backend independent filter to use for this query * The backend independent filter to use for this query
* *
@ -294,6 +301,30 @@ abstract class BaseQuery implements Filterable
return $this; return $this;
} }
/**
* Return only distinct results
*
* @param bool $distinct Whether the query should be distinct or not
*
* @return BaseQuery
*/
public function distinct($distinct = true)
{
$this->distinct = $distinct;
return $this;
}
/**
* Determine whether this query returns only distinct results
*
* @return bool True in case its a distinct query otherwise false
*/
public function isDistinct()
{
return $this->distinct;
}
/** /**
* Determine whether this query will be ordered explicitly * Determine whether this query will be ordered explicitly
* *

View File

@ -85,6 +85,14 @@ class Query extends BaseQuery
if ($this->baseQuery !== null) { if ($this->baseQuery !== null) {
$this->baseQuery = clone $this->baseQuery; $this->baseQuery = clone $this->baseQuery;
} }
if ($this->selectQuery !== null) {
$this->selectQuery = clone $this->selectQuery;
}
if ($this->countQuery !== null) {
$this->countQuery = clone $this->countQuery;
}
} }
/** /**
@ -148,6 +156,7 @@ class Query extends BaseQuery
{ {
$this->selectQuery = clone($this->baseQuery); $this->selectQuery = clone($this->baseQuery);
$this->selectQuery->columns($this->getColumns()); $this->selectQuery->columns($this->getColumns());
$this->selectQuery->distinct($this->isDistinct());
if ($this->hasOrder()) { if ($this->hasOrder()) {
foreach ($this->getOrderColumns() as $col) { foreach ($this->getOrderColumns() as $col) {
$this->selectQuery->order( $this->selectQuery->order(
@ -200,8 +209,8 @@ class Query extends BaseQuery
*/ */
private function createCountQuery() private function createCountQuery()
{ {
if ($this->useSubqueryCount) { if ($this->isDistinct() || $this->useSubqueryCount) {
$this->countQuery = $this->createCountAsSubquery(); $this->countQuery = $this->createCountAsSubQuery();
} else { } else {
$this->countQuery = $this->createCustomCountQuery(); $this->countQuery = $this->createCustomCountQuery();
} }