DQL: support for DISTINCT keyword in aggregate functions, fixes #220
This commit is contained in:
parent
ba4c83ef3a
commit
bd776a680a
@ -147,10 +147,20 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
case 'COUNT':
|
case 'COUNT':
|
||||||
case 'AVG':
|
case 'AVG':
|
||||||
$reference = substr($func, ($pos + 1), -1);
|
$reference = substr($func, ($pos + 1), -1);
|
||||||
|
$e2 = explode(' ', $reference);
|
||||||
|
|
||||||
|
$distinct = '';
|
||||||
|
if(count($e2) > 1) {
|
||||||
|
if(strtoupper($e2[0]) == 'DISTINCT')
|
||||||
|
$distinct = 'DISTINCT ';
|
||||||
|
|
||||||
|
$reference = $e2[1];
|
||||||
|
}
|
||||||
|
|
||||||
$parts = explode('.', $reference);
|
$parts = explode('.', $reference);
|
||||||
|
|
||||||
$alias = (isset($e[1])) ? $e[1] : $name;
|
$alias = (isset($e[1])) ? $e[1] : $name;
|
||||||
$this->pendingAggregates[$parts[0]][] = array($alias, $parts[1]);
|
$this->pendingAggregates[$parts[0]][] = array($alias, $parts[1], $distinct);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Doctrine_Query_Exception('Unknown aggregate function '.$name);
|
throw new Doctrine_Query_Exception('Unknown aggregate function '.$name);
|
||||||
@ -167,9 +177,9 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
$table = $this->components[$componentPath];
|
$table = $this->components[$componentPath];
|
||||||
|
|
||||||
foreach($this->pendingAggregates[$componentAlias] as $args) {
|
foreach($this->pendingAggregates[$componentAlias] as $args) {
|
||||||
list($name, $arg) = $args;
|
list($name, $arg, $distinct) = $args;
|
||||||
|
|
||||||
$this->parts["select"][] = $name . '(' . $tableAlias . '.' . $arg . ') AS ' . $tableAlias . '__' . count($this->aggregateMap);
|
$this->parts["select"][] = $name . '(' . $distinct . $tableAlias . '.' . $arg . ') AS ' . $tableAlias . '__' . count($this->aggregateMap);
|
||||||
|
|
||||||
$this->aggregateMap[] = $table;
|
$this->aggregateMap[] = $table;
|
||||||
}
|
}
|
||||||
|
@ -104,17 +104,14 @@ class Doctrine_Schema_Relation extends Doctrine_Schema_Object {
|
|||||||
$this->referencedColumn = $referencedColumn;
|
$this->referencedColumn = $referencedColumn;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
* @return string
|
||||||
* @return
|
|
||||||
* @access public
|
|
||||||
*/
|
*/
|
||||||
public function __toString( ) {
|
public function __toString( ) {
|
||||||
return "Relation between '".$this->referencingColumn."' and '".$this->referencedTable."'.'".$this->referencingColumn."'";
|
return "Relation between '".$this->referencingColumn."' and '".$this->referencedTable."'.'".$this->referencingColumn."'";
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @access public
|
|
||||||
*/
|
*/
|
||||||
public function isValid( ) {
|
public function isValid( ) {
|
||||||
|
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase {
|
class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase {
|
||||||
public function prepareTables() { }
|
public function prepareTables() { }
|
||||||
|
public function testAggregateFunctionWithDistinctKeyword() {
|
||||||
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
|
$q->parseQuery('SELECT COUNT(DISTINCT u.name) FROM User u');
|
||||||
|
|
||||||
|
$this->assertEqual($q->getQuery(), 'SELECT COUNT(DISTINCT e.name) AS e__0 FROM entity e WHERE (e.type = 0)');
|
||||||
|
}
|
||||||
|
|
||||||
public function testAggregateFunction() {
|
public function testAggregateFunction() {
|
||||||
$q = new Doctrine_Query();
|
$q = new Doctrine_Query();
|
||||||
@ -9,6 +16,7 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase {
|
|||||||
|
|
||||||
$this->assertEqual($q->getQuery(), 'SELECT COUNT(e.id) AS e__0 FROM entity e WHERE (e.type = 0)');
|
$this->assertEqual($q->getQuery(), 'SELECT COUNT(e.id) AS e__0 FROM entity e WHERE (e.type = 0)');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMultipleAggregateFunctions() {
|
public function testMultipleAggregateFunctions() {
|
||||||
$q = new Doctrine_Query();
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user