Commit bd776a68 authored by zYne's avatar zYne

DQL: support for DISTINCT keyword in aggregate functions, fixes #220

parent ba4c83ef
......@@ -147,10 +147,20 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
case 'COUNT':
case 'AVG':
$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);
$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;
default:
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];
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;
}
......
......@@ -104,17 +104,14 @@ class Doctrine_Schema_Relation extends Doctrine_Schema_Object {
$this->referencedColumn = $referencedColumn;
}
/**
*
* @return
* @access public
* @return string
*/
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
* @access public
*/
public function isValid( ) {
......
<?php
class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase {
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() {
$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)');
}
public function testMultipleAggregateFunctions() {
$q = new Doctrine_Query();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment